public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* calls to socket() fail when calling getaddrinfo() with IPPROTO_TCP
@ 2021-07-29 22:41 John Scott
  2021-07-30  9:34 ` Corinna Vinschen
  2021-07-30 16:44 ` Brian Inglis
  0 siblings, 2 replies; 5+ messages in thread
From: John Scott @ 2021-07-29 22:41 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 1307 bytes --]

Hi,

I was wondering why my daytime server doesn't work when built for
Cygwin, and I have been able to narrow it down to this reproducible
test case:

#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

int main(void) {
	struct addrinfo *res;
	int s = getaddrinfo(NULL, "daytime", &(const struct addrinfo){.ai_flags = AI_PASSIVE, .ai_protocol = IPPROTO_TCP}, &res);
	if(s) {
		fprintf(stderr, "Failed to get address info: %s\n", (s != EAI_SYSTEM) ? gai_strerror(s) : strerror(errno));
		exit(EXIT_FAILURE);
	}

	for(const struct addrinfo *tmp = res; tmp; tmp = tmp->ai_next) {
		int fd = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol);
		if(fd == -1) {
			perror("Failed to create socket");
		} else if(close(fd) == -1) {
			perror("Failed to close socket");
		}
	}
	freeaddrinfo(res);
}

This code fails with "Failed to create socket: Invalid argument". Does
anyone have an idea why this happens, given that the arguments to
socket() come directly from the call to getaddrinfo()? Remarkably,
changing the service from "daytime" to "http" seems to fix it, which
seems quite strange.

I'm not subscribed, so please CC me on replies.

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-07-30 16:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-29 22:41 calls to socket() fail when calling getaddrinfo() with IPPROTO_TCP John Scott
2021-07-30  9:34 ` Corinna Vinschen
2021-07-30 10:47   ` Corinna Vinschen
2021-07-30 11:02     ` John Scott
2021-07-30 16:44 ` Brian Inglis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).