5 #define IP_LISTENER_BACKLOG 511
13 sa.sa_handler = SIG_IGN;
15 sigemptyset(&sa.sa_mask);
16 if(sigaction(SIGPIPE, &sa, NULL) != 0)
23 struct sockaddr_in in_addr;
28 if((fd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
31 if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (
char *)(&reuseVal),
32 sizeof(reuseVal)) != 0)
35 in_addr.sin_family = AF_INET;
36 memcpy(&in_addr.sin_addr.s_addr, ip, 4);
37 in_addr.sin_port = htons(port);
39 if(bind(fd, (
struct sockaddr *)&in_addr,
sizeof(
struct sockaddr_in)) != 0)
53 struct sockaddr_in in_addr;
57 if((fd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
60 if(((flags = fcntl(fd, F_GETFL, 0)) < 0) ||
61 (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0))
64 in_addr.sin_family = AF_INET;
65 memcpy(&in_addr.sin_addr.s_addr, ip, 4);
66 in_addr.sin_port = htons(port);
68 if((connect(fd, (
struct sockaddr *)&in_addr,
69 sizeof(
struct sockaddr_in)) != 0) &&
70 (
errno != EINPROGRESS))
79 static char all_local_ip[] = {0x00,0x00,0x00,0x00};
82 unsigned short *parsed_port,
int accept_all_ip)
85 struct hostent *lookup;
87 const char *
ptr = strstr(address,
":");
88 const char *ip = all_local_ip;
98 if((ptr - address) > 255)
101 memcpy(buf, address, ptr - address);
103 if((lookup = gethostbyname(buf)) == NULL) {
106 fprintf(stderr,
"hostname lookup for '%s' failed\n", buf);
109 ip = lookup->h_addr_list[0];
116 *parsed_port = (
unsigned short)port;
142 return accept(listen_fd, NULL, NULL);