68 #include <sys/types.h> 69 #include <sys/socket.h> 70 #include <netinet/in.h> 72 #include <arpa/inet.h> 73 #include <sys/utsname.h> 88 #ifndef UDP_PACKET_MAX_SIZE 89 #define UDP_PACKET_MAX_SIZE (64*1024) 95 struct sockaddr_in addr;
98 struct hostent hostinfo;
101 s = socket(PF_INET, SOCK_STREAM, 0);
103 error(errno,
"socket failed");
108 addr.sin_family = AF_INET;
109 addr.sin_port = htons(
port);
111 addr.sin_addr.s_addr = htonl(INADDR_ANY);
114 error(errno,
"gethostbyname failed");
117 addr.sin_addr = *(
struct in_addr *) hostinfo.h_addr;
121 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (
char *) &reuse,
122 sizeof(reuse)) == -1) {
123 error(errno,
"setsockopt failed for server address");
127 if (bind(s, (
struct sockaddr *) &addr,
sizeof(addr)) == -1) {
128 error(errno,
"bind failed");
132 if (listen(s, 10) == -1) {
133 error(errno,
"listen failed");
158 struct sockaddr_in addr;
159 struct sockaddr_in o_addr;
160 struct hostent hostinfo;
161 struct hostent o_hostinfo;
167 s = socket(PF_INET, SOCK_STREAM, 0);
169 error(errno,
"Couldn't create new socket.");
174 error(errno,
"gethostbyname failed");
178 if (
our_port > 0 || (source_addr != NULL && strcmp(source_addr,
"*") != 0)) {
182 o_addr.sin_family = AF_INET;
184 if (source_addr == NULL || strcmp(source_addr,
"*") == 0)
185 o_addr.sin_addr.s_addr = htonl(INADDR_ANY);
188 error(errno,
"gethostbyname failed");
191 o_addr.sin_addr = *(
struct in_addr *) o_hostinfo.h_addr;
195 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (
char *) &reuse,
sizeof(reuse)) == -1) {
196 error(errno,
"setsockopt failed before bind");
199 if (bind(s, (
struct sockaddr *) &o_addr,
sizeof(o_addr)) == -1) {
210 addr.sin_family = AF_INET;
211 addr.sin_port = htons(
port);
212 addr.sin_addr = *(
struct in_addr *) hostinfo.h_addr_list[i];
218 rc = connect(s, (
struct sockaddr *) &addr,
sizeof(addr));
223 }
while (rc == -1 && hostinfo.h_addr_list[++i] != NULL);
248 struct sockaddr_in addr;
249 struct sockaddr_in o_addr;
250 struct hostent hostinfo;
251 struct hostent o_hostinfo;
252 int s, flags, rc = -1, i;
258 s = socket(PF_INET, SOCK_STREAM, 0);
260 error(errno,
"Couldn't create new socket.");
265 error(errno,
"gethostbyname failed");
269 if (
our_port > 0 || (source_addr != NULL && strcmp(source_addr,
"*") != 0)) {
273 o_addr.sin_family = AF_INET;
275 if (source_addr == NULL || strcmp(source_addr,
"*") == 0)
276 o_addr.sin_addr.s_addr = htonl(INADDR_ANY);
279 error(errno,
"gethostbyname failed");
282 o_addr.sin_addr = *(
struct in_addr *) o_hostinfo.h_addr;
286 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (
char *) &reuse,
sizeof(reuse)) == -1) {
287 error(errno,
"setsockopt failed before bind");
290 if (bind(s, (
struct sockaddr *) &o_addr,
sizeof(o_addr)) == -1) {
296 flags = fcntl(s, F_GETFL, 0);
297 fcntl(s, F_SETFL, flags | O_NONBLOCK);
304 addr.sin_family = AF_INET;
305 addr.sin_port = htons(
port);
306 addr.sin_addr = *(
struct in_addr *) hostinfo.h_addr_list[i];
312 if ((rc = connect(s, (
struct sockaddr *) &addr,
sizeof(addr))) < 0) {
313 if (errno != EINPROGRESS) {
318 }
while (rc == -1 && errno != EINPROGRESS && hostinfo.h_addr_list[++i] != NULL);
320 if (rc == -1 && errno != EINPROGRESS)
352 ret = write(socket, str, len);
354 if (errno == EAGAIN)
continue;
355 if (errno == EINTR)
continue;
356 error(errno,
"Writing to socket failed");
372 flags = fcntl(fd, F_GETFL);
374 error(errno,
"cannot get flags for fd %d", fd);
379 newflags = flags & ~O_NONBLOCK;
381 newflags = flags | O_NONBLOCK;
383 if (newflags != flags) {
384 if (fcntl(fd, F_SETFL, newflags) < 0) {
385 error(errno,
"cannot set flags for fd %d", fd);
398 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on,
sizeof (on));
400 error(errno,
"Unable set TCP_NODELAY(%d)", on);
417 waits = div(wait_usec, 1000000);
418 to.tv_sec = waits.quot;
419 to.tv_usec = waits.rem;
421 ret = select(fd + 1, &rf, NULL, NULL, &to);
422 if (ret > 0 && FD_ISSET(fd, &rf))
438 if (!FD_ISSET(fd, &rf)) {
439 warning(0,
"Tried to select on fd %d, not in the set!\n", fd);
441 warning(0,
"Tried to select on invalid fd %d!\n", fd);
449 if (to.tv_sec > 10000000)
450 warning(0,
"Wait more than three years for a select?\n");
451 if (to.tv_usec > 1000000)
452 warning(0,
"There are only 1000000 usec in a second...\n");
468 s = socket(PF_INET, SOCK_DGRAM, 0);
470 error(errno,
"Couldn't create a UDP socket");
481 struct sockaddr_in sa;
482 struct hostent hostinfo;
485 s = socket(PF_INET, SOCK_DGRAM, 0);
487 error(errno,
"Couldn't create a UDP socket");
492 sa.sin_family = AF_INET;
493 sa.sin_port = htons(
port);
494 if (strcmp(source_addr,
"*") == 0)
495 sa.sin_addr.s_addr = htonl(INADDR_ANY);
498 error(errno,
"gethostbyname failed");
502 sa.sin_addr = *(
struct in_addr *) hostinfo.h_addr;
505 if (bind(s, (
struct sockaddr *) &sa, (
int)
sizeof(sa)) == -1) {
506 error(errno,
"Couldn't bind a UDP socket to port %d",
port);
519 struct sockaddr_in sa;
525 sa.sin_family = AF_INET;
526 sa.sin_port = htons(
port);
529 sa.sin_addr.s_addr = INADDR_ANY;
532 error(0,
"Couldn't find the IP number of `%s'",
537 sa.sin_addr = *(
struct in_addr *) h.h_addr;
549 struct sockaddr_in sa;
553 return ntohs(sa.sin_port);
559 struct sockaddr_in sa;
569 struct sockaddr_in sa;
574 (
struct sockaddr *) &sa, (
int)
sizeof(sa)) == -1) {
575 error(errno,
"Couldn't send UDP packet");
590 struct sockaddr_in sa;
598 bytes = recvfrom(s, buf,
UDP_PACKET_MAX_SIZE, sockrcvflags, (
struct sockaddr *) &sa, &salen);
601 error(errno,
"Couldn't receive UDP packet");
623 return ntohs(addr.sin_port);
649 panic(0,
"uname failed - can't happen, unless " GW_NAME
" is buggy.");
651 error(0,
"Can't find out official hostname for this host, " 652 "using `%s' instead.", u.nodename);
681 char straddr[INET_ADDRSTRLEN];
682 inet_ntop(AF_INET, src, straddr,
sizeof(straddr));
688 char straddr[INET6_ADDRSTRLEN];
689 inet_ntop(AF_INET6, src, straddr,
sizeof(straddr));
702 struct sockaddr_in addr;
707 debug(
"gwlib.socket", 0,
"gwthread_pollfd interrupted or failed");
710 addrlen =
sizeof(addr);
711 new_fd = accept(fd, (
struct sockaddr *) &addr, &addrlen);
713 error(errno,
"accept system call failed.");
717 debug(
"test_smsc", 0,
"accept() succeeded, client from %s",
static const struct sockaddr_in empty_sockaddr_in
void error(int err, const char *fmt,...)
int socket_set_blocking(int fd, int blocking)
gw_assert(wtls_machine->packet_to_send !=NULL)
int tcpip_connect_to_server(char *hostname, int port, const char *source_addr)
int udp_get_port(Octstr *addr)
static Octstr * official_name
int socket_set_nodelay(int fd, int on)
#define octstr_get_cstr(ostr)
void socket_shutdown(void)
int write_to_socket(int socket, char *str)
int udp_bind(int port, const char *source_addr)
int udp_recvfrom_flags(int s, Octstr **datagram, Octstr **addr, int sockrcvflags)
int udp_client_socket(void)
int gw_accept(int fd, Octstr **client_addr)
Octstr * gw_netaddr_to_octstr(int af, void *src)
int make_server_socket(int port, const char *interface_name)
void warning(int err, const char *fmt,...)
int tcpip_connect_to_server_with_port(char *hostname, int port, int our_port, const char *source_addr)
#define UDP_PACKET_MAX_SIZE
void octstr_destroy(Octstr *ostr)
#define octstr_create(cstr)
Octstr * get_official_name(void)
int tcpip_connect_nb_to_server(char *hostname, int port, const char *source_addr, int *done)
int gwthread_pollfd(int fd, int events, double timeout)
int tcpip_connect_nb_to_server_with_port(char *hostname, int port, int our_port, const char *source_addr, int *done)
long octstr_len(const Octstr *ostr)
Octstr * get_official_ip(void)
int read_available(int fd, long wait_usec)
int gw_gethostbyname(struct hostent *ent, const char *name, char **buff)
Octstr * host_ip(struct sockaddr_in addr)
void debug(const char *place, int err, const char *fmt,...)
int host_port(struct sockaddr_in addr)
int udp_sendto(int s, Octstr *datagram, Octstr *addr)
static void setup_official_name(void)
static Octstr * interface_name
#define octstr_create_from_data(data, len)
int udp_recvfrom(int s, Octstr **datagram, Octstr **addr)
Octstr * udp_create_address(Octstr *host_or_ip, int port)
static Octstr * official_ip
Octstr * udp_get_ip(Octstr *addr)