Note that there are some explanatory texts on larger screens.

plurals
  1. POtrouble accepting ipv6 requests on c web server
    primarykey
    data
    text
    <p>The following c function I wrote returns a file descriptor that accepts IPv4 connections but not IPv6. Could someone help me figure out what went wrong? I suspect I did not use getaddrinfo() correctly.</p> <p>Open file descriptor that listens to connections.</p> <pre><code>/* * open_listenfd - open and return a listening socket on port * Returns -1 and sets errno on Unix error. */ int open_listenfd(int port) { const char* hostname=0; // Converts port to string char* pName = malloc(numPlaces(port) + 1); sprintf(pName, "%d", port); const char* portname= pName; struct addrinfo hints; memset(&amp;hints,0,sizeof(hints)); hints.ai_family=AF_UNSPEC; hints.ai_socktype=SOCK_STREAM; hints.ai_protocol= 0; hints.ai_flags=AI_PASSIVE|AI_ADDRCONFIG; struct addrinfo* res=0; int err=getaddrinfo(hostname,portname,&amp;hints,&amp;res); free(pName); if (err!=0) { return -1; } int listenfd, optval=1; struct sockaddr_in serveraddr; /* Create a socket descriptor */ if ((listenfd = socket(res-&gt;ai_family,res-&gt;ai_socktype, res-&gt;ai_protocol)) &lt; 0) return -1; /* Eliminates "Address already in use" error from bind. */ if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&amp;optval , sizeof(int)) &lt; 0) return -1; /* Listenfd will be an endpoint for all requests to port on any IP address for this host */ bzero((char *) &amp;serveraddr, sizeof(serveraddr)); serveraddr.sin_family = /*AF_INET;*/ AF_UNSPEC; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons((unsigned short)port); if (bind(listenfd, res-&gt;ai_addr, res-&gt;ai_addrlen/*(SA *)&amp;serveraddr, sizeof(serveraddr)*/) &lt; 0) return -1; freeaddrinfo(res); /* Make it a listening socket ready to accept connection requests */ if (listen(listenfd, 1024) &lt; 0) return -1; return listenfd; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload