calls to socket() fail when calling getaddrinfo() with IPPROTO_TCP

John Scott jscott@posteo.net
Thu Jul 29 22:41:30 GMT 2021


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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: This is a digitally signed message part
URL: <https://cygwin.com/pipermail/cygwin/attachments/20210729/fd13d47f/attachment.sig>


More information about the Cygwin mailing list