35 #include <arpa/inet.h>
36 #include <sys/types.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
71 if (getsockopt(ns->
fd, SOL_SOCKET, SO_SNDBUF, (
char *)&oldbufsize, &buflen) == -1)
73 if (oldbufsize < bufsize) {
75 LOG(
llevDebug,
"Default buffer size was %d bytes, will reset it to %d\n", oldbufsize, bufsize);
77 if (setsockopt(ns->
fd, SOL_SOCKET, SO_SNDBUF, (
char *)&bufsize,
sizeof(bufsize))) {
78 LOG(
llevError,
"init_connection: setsockopt unable to set output buf size to %d\n", bufsize);
81 buflen =
sizeof(oldbufsize);
82 getsockopt(ns->
fd, SOL_SOCKET, SO_SNDBUF, (
char *)&oldbufsize, &buflen);
84 LOG(
llevDebug,
"Socket buffer size now %d bytes\n", oldbufsize);
87 if (setsockopt(ns->
fd, IPPROTO_TCP, TCP_NODELAY, &
tmp,
sizeof(
tmp)))
100 #ifdef CF_MXE_CROSS_COMPILE
106 if (ioctlsocket(ns->
fd, FIONBIO , &
temp) == -1)
107 LOG(
llevError,
"init_connection: Error on ioctlsocket.\n");
109 if (fcntl(ns->
fd, F_SETFL, O_NONBLOCK) == -1) {
193 struct linger linger_opt;
196 if (ns == NULL || ns->
listen == NULL) {
197 LOG(
llevError,
"init_listening_socket: missing listen info in socket_struct?!\n");
205 LOG(
llevError,
"Cannot create socket: %s\n", strerror(errno));
209 linger_opt.l_onoff = 0;
210 linger_opt.l_linger = 0;
211 if (setsockopt(ns->
fd, SOL_SOCKET, SO_LINGER, (
char *)&linger_opt,
sizeof(
struct linger))) {
212 LOG(
llevError,
"Cannot setsockopt(SO_LINGER): %s\n", strerror(errno));
218 #if defined(__osf__) || defined(hpux) || defined(sgi) || defined(NeXT) || \
219 defined(__sun__) || defined(__linux__) || defined(SVR4) || \
220 defined(__FreeBSD__) || defined(__OpenBSD__) || \
230 if (setsockopt(ns->
fd, SOL_SOCKET, SO_REUSEADDR, &
tmp,
sizeof(
tmp))) {
231 LOG(
llevError,
"Cannot setsockopt(SO_REUSEADDR): %s\n", strerror(errno));
233 #ifdef HAVE_GETADDRINFO
234 if ((ns->
listen->
family == AF_INET6) && setsockopt(ns->
fd, IPPROTO_IPV6, IPV6_V6ONLY, &
tmp,
sizeof(
tmp))) {
235 LOG(
llevError,
"Cannot setsockopt(IPV6_V6ONLY): %s\n", strerror(errno));
240 if (setsockopt(ns->
fd, SOL_SOCKET, SO_REUSEADDR, (
char *)NULL, 0)) {
241 LOG(
llevError,
"Cannot setsockopt(SO_REUSEADDR): %s\n", strerror(errno));
246 #ifdef HAVE_GETNAMEINFO
247 getnameinfo(ns->
listen->
addr, ns->
listen->
addrlen, buf1,
sizeof(buf1), buf2,
sizeof(buf2), NI_NUMERICHOST|NI_NUMERICSERV);
252 ip = ntohl(((
struct sockaddr_in *)ns->
listen->
addr)->sin_addr.s_addr);
253 port = ntohs(((
struct sockaddr_in *)ns->
listen->
addr)->sin_port);
254 snprintf(buf1,
sizeof(buf1),
"%ld.%ld.%ld.%ld", (
ip>>24)&255, (
ip>>16)&255, (
ip>>8)&255,
ip&255);
255 snprintf(buf2,
sizeof(buf2),
"%d", port&65535);
257 LOG(
llevError,
"Cannot bind socket to [%s]:%s: %s\n", buf1, buf2, strerror(errno));
259 shutdown(ns->
fd, SD_BOTH);
267 if (listen(ns->
fd, 5) == (-1)) {
268 LOG(
llevError,
"Cannot listen on socket: %s\n", strerror(errno));
270 shutdown(ns->
fd, SD_BOTH);
284 int i, e, listen_socket_count;
285 #ifdef HAVE_GETADDRINFO
286 struct addrinfo *ai, *ai_p;
287 struct addrinfo hints;
290 struct sockaddr_in *insock;
291 struct protoent *protox;
298 WSAStartup(0x0101, &w);
306 # ifdef HAVE_GETDTABLESIZE
309 #error "Unable to find usable function to get max filedescriptors"
325 #ifdef HAVE_GETADDRINFO
326 memset(&hints,
'\0',
sizeof(hints));
327 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
328 hints.ai_socktype = SOCK_STREAM;
330 e = getaddrinfo(NULL,
buf, &hints, &ai);
332 LOG(
llevError,
"init_server: getaddrinfo: %s\n", gai_strerror(e));
336 listen_socket_count = 0;
337 for (ai_p = ai; ai_p != NULL; ai_p = ai_p->ai_next) {
338 listen_socket_count++;
341 assert(listen_socket_count > 0);
343 listen_socket_count = 1;
349 for (i = 0; i < listen_socket_count; i++) {
355 #ifdef HAVE_GETADDRINFO
356 for (i = 0, ai_p = ai; i < listen_socket_count && ai_p != NULL; i++, ai_p = ai_p->ai_next) {
362 memcpy(
init_sockets[i].listen->addr, ai_p->ai_addr, ai_p->ai_addrlen);
366 protox = getprotobyname(
"tcp");
367 if (protox == NULL) {
374 insock =
static_cast<struct sockaddr_in *
>(calloc(
sizeof(
struct sockaddr_in), 1));
375 insock->sin_family = AF_INET;
377 insock->sin_addr.s_addr = htonl(INADDR_ANY);
383 for (i = 0; i < listen_socket_count; i++) {
389 LOG(
llevError,
"init_server: can't open any listening socket\n");
403 LOG(
llevDebug,
"Freeing all new client/server information.\n");
420 shutdown(ns->
fd, SD_BOTH);
421 if (closesocket(ns->
fd)) {