28 #include <sys/socket.h>
29 #include <arpa/inet.h>
31 #include <sys/ioctl.h>
37 #define TRACE(type,fmt,args...)
55 #ifndef LINUX_LINK_STATIC
57 struct in_addr host_ip;
59 if (host[0] !=
'\0' && !inet_aton(host, &host_ip)) {
62 hp = gethostbyname(host);
64 fprintf(stderr,
"could not obtain address of %s\n", host);
68 if (hp->h_addrtype == AF_INET && hp->h_length == 4) {
70 memcpy(&host_ip.s_addr, hp->h_addr_list[0], hp->h_length);
78 sock->
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
81 setsockopt(sock->
sockfd, SOL_SOCKET, SO_REUSEADDR, &one,
sizeof(one));
84 setsockopt(sock->
sockfd, SOL_SOCKET, SO_REUSEPORT, &one,
sizeof(one));
89 setsockopt(sock->
sockfd, SOL_SOCKET, SO_BROADCAST, &one,
sizeof(one));
95 sock->
addr_in.sin_family = PF_INET;
96 sock->
addr_in.sin_port = htons(port_in);
97 sock->
addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
103 sock->
addr_out.sin_family = PF_INET;
104 sock->
addr_out.sin_port = htons(port_out);
105 #ifndef LINUX_LINK_STATIC
106 sock->
addr_out.sin_addr.s_addr = host_ip.s_addr;
108 sock->
addr_out.sin_addr.s_addr = inet_addr(host);
126 ssize_t bytes_sent = sendto(sock->
sockfd, buffer, len, 0,
128 if (bytes_sent != ((ssize_t)len)) {
129 TRACE(
TRACE_ERROR,
"error sending to sock %d (%d)\n", (
int)bytes_sent, strerror(errno));
147 ssize_t bytes_sent = sendto(sock->
sockfd, buffer, len, MSG_DONTWAIT,
162 socklen_t slen =
sizeof(
struct sockaddr_in);
163 ssize_t bytes_read = recvfrom(sock->
sockfd, buffer, len, MSG_DONTWAIT,
164 (
struct sockaddr *)&sock->
addr_in, &slen);
166 if (bytes_read == -1) {
168 if (errno == EWOULDBLOCK) {
187 socklen_t slen =
sizeof(
struct sockaddr_in);
188 ssize_t bytes_read = recvfrom(sock->
sockfd, buffer, len, 0,
189 (
struct sockaddr *)&sock->
addr_in, &slen);
191 return (
int)bytes_read;
198 mreq.imr_multiaddr.s_addr = inet_addr(multicast_addr);
199 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
202 return setsockopt(sock->
sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
char *)&mreq,
sizeof(mreq));
208 unsigned int optval_size = 4;
210 setsockopt(sock->
sockfd, SOL_SOCKET, SO_RCVBUF, (
char *)&buf_size, optval_size);
211 getsockopt(sock->
sockfd, SOL_SOCKET, SO_RCVBUF, (
char *)&buf_ret, &optval_size);
213 if (buf_size != buf_ret) {
223 unsigned int optval_size = 4;
225 setsockopt(sock->
sockfd, SOL_SOCKET, SO_SNDBUF, (
char *)&buf_size, optval_size);
226 getsockopt(sock->
sockfd, SOL_SOCKET, SO_SNDBUF, (
char *)&buf_ret, &optval_size);
228 if (buf_size != buf_ret) {