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;
197 mreq.imr_multiaddr.s_addr = inet_addr(multicast_addr);
198 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
201 return setsockopt(sock->
sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
char *)&mreq,
sizeof(mreq));
206 unsigned int optval_size = 4;
208 setsockopt(sock->
sockfd, SOL_SOCKET, SO_RCVBUF, (
char *)&buf_size, optval_size);
209 getsockopt(sock->
sockfd, SOL_SOCKET, SO_RCVBUF, (
char *)&buf_ret, &optval_size);
211 if(buf_size != buf_ret)
int udp_socket_send_dontwait(struct UdpSocket *sock, uint8_t *buffer, uint32_t len)
Send a packet from buffer, non-blocking.
int udp_socket_recv_dontwait(struct UdpSocket *sock, uint8_t *buffer, uint32_t len)
Receive a UDP packet, dont wait.
int udp_socket_recv(struct UdpSocket *sock, uint8_t *buffer, uint32_t len)
Receive one UDP packet, blocking.
Easily create and use UDP sockets.
int udp_socket_subscribe_multicast(struct UdpSocket *sock, const char *multicast_addr)
int udp_socket_send(struct UdpSocket *sock, uint8_t *buffer, uint32_t len)
Send a packet from buffer, blocking.
struct sockaddr_in addr_in
struct sockaddr_in addr_out
#define TRACE(type, fmt, args...)
int udp_socket_set_recvbuf(struct UdpSocket *sock, int buf_size)
int udp_socket_create(struct UdpSocket *sock, char *host, int port_out, int port_in, bool broadcast)
Create UDP socket and bind it.