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;
58 if (host[0] !=
'\0' && !inet_aton(host, &host_ip)) {
61 hp = gethostbyname(host);
63 fprintf(stderr,
"could not obtain address of %s\n", host);
67 if (hp->h_addrtype == AF_INET && hp->h_length == 4) {
69 memcpy(&host_ip.s_addr, hp->h_addr_list[0], hp->h_length);
77 sock->
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
80 setsockopt(sock->
sockfd, SOL_SOCKET, SO_REUSEADDR, &one,
sizeof(one));
83 setsockopt(sock->
sockfd, SOL_SOCKET, SO_REUSEPORT, &one,
sizeof(one));
88 setsockopt(sock->
sockfd, SOL_SOCKET, SO_BROADCAST, &one,
sizeof(one));
94 sock->
addr_in.sin_family = PF_INET;
95 sock->
addr_in.sin_port = htons(port_in);
96 sock->
addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
102 sock->
addr_out.sin_family = PF_INET;
103 sock->
addr_out.sin_port = htons(port_out);
104 #ifndef LINUX_LINK_STATIC
105 sock->
addr_out.sin_addr.s_addr = host_ip.s_addr;
107 sock->
addr_out.sin_addr.s_addr = inet_addr(host);
125 ssize_t bytes_sent = sendto(sock->
sockfd, buffer, len, 0,
127 if (bytes_sent != ((ssize_t)len)) {
128 TRACE(
TRACE_ERROR,
"error sending to sock %d (%d)\n", (
int)bytes_sent, strerror(errno));
146 ssize_t bytes_sent = sendto(sock->
sockfd, buffer, len, MSG_DONTWAIT,
161 socklen_t slen =
sizeof(
struct sockaddr_in);
162 ssize_t bytes_read = recvfrom(sock->
sockfd, buffer, len, MSG_DONTWAIT,
163 (
struct sockaddr *)&sock->
addr_in, &slen);
165 if (bytes_read == -1) {
167 if (errno == EWOULDBLOCK) {
186 socklen_t slen =
sizeof(
struct sockaddr_in);
187 ssize_t bytes_read = recvfrom(sock->
sockfd, buffer, len, 0,
188 (
struct sockaddr *)&sock->
addr_in, &slen);
190 return (
int)bytes_read;
196 mreq.imr_multiaddr.s_addr = inet_addr(multicast_addr);
197 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
200 return setsockopt(sock->
sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
char *)&mreq,
sizeof(mreq));
205 unsigned int optval_size = 4;
207 setsockopt(sock->
sockfd, SOL_SOCKET, SO_RCVBUF, (
char *)&buf_size, optval_size);
208 getsockopt(sock->
sockfd, SOL_SOCKET, SO_RCVBUF, (
char *)&buf_ret, &optval_size);
210 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
int udp_socket_create(struct UdpSocket *sock, char *host, int port_out, int port_in, bool_t broadcast)
Create UDP socket and bind it.
#define TRACE(type, fmt, args...)
int udp_socket_set_recvbuf(struct UdpSocket *sock, int buf_size)