-
Notifications
You must be signed in to change notification settings - Fork 6
/
udp_util.c
executable file
·118 lines (93 loc) · 3.09 KB
/
udp_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <poll.h>
#include <netdb.h>
#include <unistd.h>
/** make and bind a udp socket to a specified port. Returns the fd. **/
int udp_socket_listen(int port)
{
int sock = socket(PF_INET, SOCK_DGRAM, 0);
if (sock < 0)
return -1;
struct sockaddr_in listen_addr;
memset(&listen_addr, 0, sizeof(struct sockaddr_in));
listen_addr.sin_family = AF_INET;
listen_addr.sin_port = htons(port);
listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);
int res = bind(sock, (struct sockaddr*) &listen_addr, sizeof(listen_addr));
if (res < 0)
return -2;
return sock;
}
/** make and bind a udp socket to an ephemeral port. Returns the fd. **/
int udp_socket_create(void)
{
int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0)
return -1;
struct sockaddr_in listen_addr;
memset(&listen_addr, 0, sizeof(struct sockaddr_in));
listen_addr.sin_family = AF_INET;
listen_addr.sin_port = INADDR_ANY; // ephemeral port, please
listen_addr.sin_addr.s_addr = INADDR_ANY;
int res = bind(sock, (struct sockaddr*) &listen_addr, sizeof(struct sockaddr_in));
if (res < 0)
return -2;
return sock;
}
/** return the local port number for a socket. **/
int udp_socket_get_port(int sock)
{
struct sockaddr_in addr;
socklen_t addr_len = sizeof(addr);
int res = getsockname(sock, (struct sockaddr*) &addr, &addr_len);
if (res < 0)
return -1;
return ntohs(addr.sin_port);
}
// return 0 on success
int udp_send(const char *ipaddr, int port, void *data, int datalen)
{
// fill in address structure
struct sockaddr_in remote_addr;
memset(&remote_addr, 0, sizeof(struct sockaddr_in));
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(port);
struct hostent *host = gethostbyname(ipaddr);
if (host == NULL) {
printf("Couldn't resolve host %s\n", ipaddr);
return -1;
}
memcpy(&remote_addr.sin_addr.s_addr, host->h_addr, host->h_length);
// now actually send.
int sock = udp_socket_create();
ssize_t res = sendto(sock, data, datalen, 0, (struct sockaddr*) &remote_addr, sizeof(remote_addr));
// cleanup
close(sock);
return res != datalen;
}
// return 0 on success
int udp_send_fd(int sock, const char *ipaddr, int port, void *data, int datalen)
{
// fill in address structure
struct sockaddr_in remote_addr;
memset(&remote_addr, 0, sizeof(struct sockaddr_in));
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(port);
struct hostent *host = gethostbyname(ipaddr);
if (host == NULL) {
printf("Couldn't resolve host %s\n", ipaddr);
return -1;
}
memcpy(&remote_addr.sin_addr.s_addr, host->h_addr, host->h_length);
// now actually send.
ssize_t res = sendto(sock, data, datalen, 0, (struct sockaddr*) &remote_addr, sizeof(remote_addr));
// cleanup
return res != datalen;
}