-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnet_udp_win32.c
129 lines (108 loc) · 2.71 KB
/
net_udp_win32.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
118
119
120
121
122
123
124
125
126
127
128
129
int SOCK_UdpInit(S_Udp *udp, int af)
{
U_bzero(udp, sizeof(*udp));
udp->state = S_UDP_STATE_INIT;
udp->addr.af = af;
#if 0
if (af == S_AF_IPV4) af = AF_INET;
else if (af == S_AF_IPV6) af = AF_INET6;
else return -1;
udp->handle = socket(af, SOCK_DGRAM, 0);
udp->handle = -1;
if (udp->handle == -1)
{
return -1;
}
udp->state = S_UDP_STATE_OPEN;
#endif
return 0;
}
int SOCK_UdpBind(S_Udp *udp, u16 port)
{
#if 0
int ret;
struct sockaddr_in addr;
if (udp->state != S_UDP_STATE_OPEN)
return -1;
if (udp->addr.af == S_AF_IPV4)
{
// allow multiple sockets to use the same PORT number
int yes = 1;
if (setsockopt(udp->handle, SOL_SOCKET, SO_REUSEADDR, (char*) &yes, sizeof(yes) ) < 0 )
{
goto err;
}
U_bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
ret = bind(udp->handle, (struct sockaddr*) &addr, sizeof(addr));
if (ret == -1)
{
goto err;
}
udp->port = port;
return 0;
}
err:
udp->state = S_UDP_STATE_ERROR;
#endif
return -1;
}
int SOCK_UdpJoinMulticast(S_Udp *udp, const char *maddr)
{
if (udp->state != S_UDP_STATE_OPEN)
return -1;
#if 0
if (udp->addr.af == S_AF_IPV4)
{
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(maddr);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(udp->handle, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
{
goto err;
}
return 0;
}
err:
udp->state = S_UDP_STATE_ERROR;
#endif
return -1;
}
void SOCK_UdpRecv(S_Udp *udp)
{
#if 0
ssize_t n;
fd_set readfds;
struct timeval tv;
socklen_t addr_len;
struct sockaddr_in addr;
if (udp->state != S_UDP_STATE_OPEN)
return;
tv.tv_sec = 0;
tv.tv_usec = 5 * 1000; // 5 msec
FD_ZERO(&readfds);
FD_SET(udp->handle, &readfds);
// don't care about writefds and exceptfds:
select(udp->handle+1, &readfds, NULL, NULL, &tv);
if (FD_ISSET(udp->handle, &readfds))
{
addr_len = sizeof(addr);
n = recvfrom(udp->handle, &udp->rx_buf[0], sizeof(udp->rx_buf), 0, (struct sockaddr*)&addr, &addr_len);
if (n > 0 && n < S_UDP_MAX_PKG_SIZE)
{
udp->rx_buf[n] = '\0';
if (udp->rx)
udp->rx(udp->user, udp, &udp->rx_buf[0], (unsigned)n);
}
}
#endif
}
void SOCK_UdpFree(S_Udp *udp)
{
// if (udp->handle)
// close(udp->handle);
U_bzero(udp, sizeof(*udp));
udp->state = S_UDP_STATE_INIT;
}