35 #include <arpa/inet.h>
36 #include <sys/types.h>
38 #include <netinet/in.h>
63 if (getsockopt(ns->
fd, SOL_SOCKET, SO_SNDBUF, (
char *)&oldbufsize, &buflen) == -1)
65 if (oldbufsize < bufsize) {
67 LOG(
llevDebug,
"Default buffer size was %d bytes, will reset it to %d\n", oldbufsize, bufsize);
69 if (setsockopt(ns->
fd, SOL_SOCKET, SO_SNDBUF, (
char *)&bufsize,
sizeof(bufsize))) {
70 LOG(
llevError,
"init_connection: setsockopt unable to set output buf size to %d\n", bufsize);
73 buflen =
sizeof(oldbufsize);
74 getsockopt(ns->
fd, SOL_SOCKET, SO_SNDBUF, (
char *)&oldbufsize, &buflen);
76 LOG(
llevDebug,
"Socket buffer size now %d bytes\n", oldbufsize);
91 if (ioctlsocket(ns->
fd, FIONBIO , &
temp) == -1)
92 LOG(
llevError,
"init_connection: Error on ioctlsocket.\n");
94 if (fcntl(ns->
fd, F_SETFL, O_NONBLOCK) == -1) {
173 struct linger linger_opt;
176 if (ns == NULL || ns->
listen == NULL) {
177 LOG(
llevError,
"init_listening_socket: missing listen info in socket_struct?!\n");
185 LOG(
llevError,
"Cannot create socket: %s\n", strerror(errno));
189 linger_opt.l_onoff = 0;
190 linger_opt.l_linger = 0;
191 if (setsockopt(ns->
fd, SOL_SOCKET, SO_LINGER, (
char *)&linger_opt,
sizeof(
struct linger))) {
192 LOG(
llevError,
"Cannot setsockopt(SO_LINGER): %s\n", strerror(errno));
198 #if defined(__osf__) || defined(hpux) || defined(sgi) || defined(NeXT) || \
199 defined(__sun__) || defined(__linux__) || defined(SVR4) || \
200 defined(__FreeBSD__) || defined(__OpenBSD__) || \
210 if (setsockopt(ns->
fd, SOL_SOCKET, SO_REUSEADDR, &
tmp,
sizeof(
tmp))) {
211 LOG(
llevError,
"Cannot setsockopt(SO_REUSEADDR): %s\n", strerror(errno));
213 #ifdef HAVE_GETADDRINFO
214 if ((ns->
listen->
family == AF_INET6) && setsockopt(ns->
fd, IPPROTO_IPV6, IPV6_V6ONLY, &
tmp,
sizeof(
tmp))) {
215 LOG(
llevError,
"Cannot setsockopt(IPV6_V6ONLY): %s\n", strerror(errno));
220 if (setsockopt(ns->
fd, SOL_SOCKET, SO_REUSEADDR, (
char *)NULL, 0)) {
221 LOG(
llevError,
"Cannot setsockopt(SO_REUSEADDR): %s\n", strerror(errno));
226 #ifdef HAVE_GETNAMEINFO
227 getnameinfo(ns->
listen->
addr, ns->
listen->
addrlen, buf1,
sizeof(buf1), buf2,
sizeof(buf2), NI_NUMERICHOST|NI_NUMERICSERV);
232 ip = ntohl(((
struct sockaddr_in *)ns->
listen->
addr)->sin_addr.s_addr);
233 port = ntohs(((
struct sockaddr_in *)ns->
listen->
addr)->sin_port);
234 snprintf(buf1,
sizeof(buf1),
"%ld.%ld.%ld.%ld", (
ip>>24)&255, (
ip>>16)&255, (
ip>>8)&255,
ip&255);
235 snprintf(buf2,
sizeof(buf2),
"%d", port&65535);
237 LOG(
llevError,
"Cannot bind socket to [%s]:%s: %s\n", buf1, buf2, strerror(errno));
239 shutdown(ns->
fd, SD_BOTH);
247 if (listen(ns->
fd, 5) == (-1)) {
248 LOG(
llevError,
"Cannot listen on socket: %s\n", strerror(errno));
250 shutdown(ns->
fd, SD_BOTH);
264 int i, e, listen_socket_count;
265 #ifdef HAVE_GETADDRINFO
266 struct addrinfo *ai, *ai_p;
267 struct addrinfo hints;
270 struct sockaddr_in *insock;
271 struct protoent *protox;
278 WSAStartup(0x0101, &w);
286 # ifdef HAVE_GETDTABLESIZE
289 #error "Unable to find usable function to get max filedescriptors"
318 #ifdef HAVE_GETADDRINFO
319 memset(&hints,
'\0',
sizeof(hints));
320 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
321 hints.ai_socktype = SOCK_STREAM;
323 e = getaddrinfo(NULL,
buf, &hints, &ai);
325 LOG(
llevError,
"init_server: getaddrinfo: %s\n", gai_strerror(e));
329 listen_socket_count = 0;
330 for (ai_p = ai; ai_p != NULL; ai_p = ai_p->ai_next) {
331 listen_socket_count++;
334 assert(listen_socket_count > 0);
336 listen_socket_count = 1;
342 for (i = 0; i < listen_socket_count; i++) {
348 #ifdef HAVE_GETADDRINFO
349 for (i = 0, ai_p = ai; i < listen_socket_count && ai_p != NULL; i++, ai_p = ai_p->ai_next) {
355 memcpy(
init_sockets[i].listen->addr, ai_p->ai_addr, ai_p->ai_addrlen);
359 protox = getprotobyname(
"tcp");
360 if (protox == NULL) {
367 insock = calloc(
sizeof(
struct sockaddr_in), 1);
368 insock->sin_family = AF_INET;
370 insock->sin_addr.s_addr = htonl(INADDR_ANY);
376 for (i = 0; i < listen_socket_count; i++) {
382 LOG(
llevError,
"init_server: can't open any listening socket\n");
396 LOG(
llevDebug,
"Freeing all new client/server information.\n");
413 shutdown(ns->
fd, SD_BOTH);
414 if (closesocket(ns->
fd)) {