public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: John Scott <jscott@posteo.net>
To: cygwin@cygwin.com
Subject: calls to socket() fail when calling getaddrinfo() with IPPROTO_TCP
Date: Thu, 29 Jul 2021 22:41:30 +0000	[thread overview]
Message-ID: <1f5c0302c156c9391d294ea9df84d93b8742a500.camel@posteo.net> (raw)

[-- 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 --]

             reply	other threads:[~2021-07-29 22:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-29 22:41 John Scott [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1f5c0302c156c9391d294ea9df84d93b8742a500.camel@posteo.net \
    --to=jscott@posteo.net \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).