-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathgnb_udp.c
104 lines (70 loc) · 2.46 KB
/
gnb_udp.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
/*
Copyright (C) gnbdev
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__)
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#ifdef _WIN32
#define _POSIX
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include "gnb_udp.h"
int gnb_bind_udp_socket_ipv4(int socketfd,const char *host, int port){
struct sockaddr_in svr_addr;
memset(&svr_addr, 0, sizeof(struct sockaddr_in));
svr_addr.sin_family = AF_INET;
svr_addr.sin_port = htons(port);
if ( NULL != host ) {
svr_addr.sin_addr.s_addr = inet_addr(host);
} else {
svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
}
int on = 1;
setsockopt( socketfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) );
if ( bind(socketfd, (struct sockaddr *)&svr_addr, sizeof(struct sockaddr_in)) < 0 ) {
perror("bind");
return -1;
}
return 0;
}
int gnb_bind_udp_socket_ipv6(int socketfd,const char *host, int port){
struct sockaddr_in6 svr_addr;
memset(&svr_addr,0, sizeof(struct sockaddr_in6));
svr_addr.sin6_family = AF_INET6;
svr_addr.sin6_port = htons(port);
if ( NULL != host ) {
inet_pton( AF_INET6, host, &svr_addr.sin6_addr);
} else {
svr_addr.sin6_addr = in6addr_any;
}
int on;
on = 1;
setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR,(const char *)&on, sizeof(on) );
on = 1;
setsockopt(socketfd, IPPROTO_IPV6, IPV6_V6ONLY,(char *)&on, sizeof(on) );
if ( bind(socketfd, (struct sockaddr *)&svr_addr, sizeof(struct sockaddr_in6))<0 ) {
printf("bind host[%s] port[%d]\n",host, port);
perror("bind");
return -1;
}
return 0;
}