JunkBox_Lib++ (for Windows) 1.10.1
Loading...
Searching...
No Matches
network.h
Go to the documentation of this file.
1#ifndef __JBXL_NETWORK_H_
2#define __JBXL_NETWORK_H_
3
10#include "tools.h"
11#include "jbxl_state.h"
12
13
14#define _WINSOCK_DEPRECATED_NO_WARNINGS
15#ifdef WIN32
16 //#include <winsock2.h> // included at tools.h
17 #include <ws2tcpip.h>
18 #include <fcntl.h>
19
20 #ifndef socklen_t
21 #define socklen_t int
22 #endif
23 extern WSADATA WsaData;
24
25 #define _WINSOCK_DEPRECATED_NO_WARNINGS
26 #pragma comment( lib, "ws2_32.lib" )
27
28#else
29 // Unix/Linux
30 #include <sys/socket.h>
31
32 #ifdef HAVE_UNISTD_H
33 #ifndef _XOPEN_SOURCE
34 #define _XOPEN_SOURCE
35 #endif
36 #include <unistd.h>
37 #endif
38
39 #ifdef HAVE_SYS_SOCKIO_H
40 #include <sys/sockio.h>
41 #endif
42
43 #ifdef HAVE_NETINET_IN_H
44 #include <netinet/in.h>
45 #endif
46 #ifdef HAVE_ARPA_INET_H
47 #include <arpa/inet.h>
48 #endif
49
50 #include <netdb.h>
51
52 #include <fcntl.h>
53 #include <sys/ioctl.h>
54 #include <net/if.h>
55#endif
56
57
58//#define NET_MAX_IF_NUM 10 ///< 取り扱うネットワークインターフェイスの最大数
59
60
62// for WinSock
63
64int init_network(void);
65void cleanup_network(void);
66
67
69// Berkeley Socket
70
71int _udp_server_socket(int port, struct addrinfo** info, int family);
72int _udp_server_socket_setopt(int, int, const void*, int, struct addrinfo**, int f);
73int _udp_client_socket(char* host, int, struct addrinfo**, int family);
74int _udp_bind(int sock, int port, int family);
75int _udp_bind_setopt(int sock, int port, int opt, const void*, int len, int family);
76
77/*
78int udp_server_socket(int port, struct addrinfo** info); ///< udp のサーバソケットを作成する.IPv6 -> IPv4
79int udp_server_socket_ipv4(int port, struct addrinfo** info); ///< IPv4 用udp のサーバソケットを作成する.
80int udp_server_socket_ipv6(int port, struct addrinfo** info); ///< IPv6 用udp のサーバソケットを作成する.
81*/
82#define udp_server_socket(p, a) _udp_server_socket((p), (a), AF_UNSPEC)
83#define udp_server_socket_ipv4(p) _udp_server_socket((p), NULL, AF_INET)
84#define udp_server_socket_ipv6(p, a) _udp_server_socket((p), (a), AF_INET6)
85
86#define udp_server_socket_setopt(p, o, v, l, a) _udp_server_socket_setopt((p), (o), (v), (l), (a), AF_UNSPEC)
87#define udp_server_socket_setopt_ipv4(p, o, v, l) _udp_server_socket_setopt((p), (o), (v), (l), NULL, AF_INET)
88#define udp_server_socket_setopt_ipv6(p, o, v, l, a) _udp_server_socket_setopt((p), (o), (v), (l), (a), AF_INET6)
89
90#define udp_client_socket(h, p, a) _udp_client_socket((h), (p), (a), AF_UNSPEC)
91#define udp_client_socket_ipv4(h, p, a) _udp_client_socket((h), (p), (a), AF_INET)
92#define udp_client_socket_ipv6(h, p, a) _udp_client_socket((h), (p), (a), AF_INET6)
93
94#define udp_bind(s, p) _udp_bind((s), (p), AF_UNSPEC)
95#define udp_bind_ipv4(s, p) _udp_bind((s), (p), AF_INET)
96#define udp_bind_ipv6(s, p) _udp_bind((s), (p), AF_INET6)
97
98#define udp_bind_setopt(s, p, o, v, l) _udp_bind_setopt((s), (p), (o), (v), (l), AF_UNSPEC)
99#define udp_bind_setopt_ipv4(s, p, o, v, l) _udp_bind_setopt((s), (p), (o), (v), (l), AF_INET)
100#define udp_bind_setopt_ipv6(s, p, o, v, l) _udp_bind_setopt((s), (p), (o), (v), (l), AF_INET6)
101
102//
103int _tcp_server_socket(int port, int family);
104int _tcp_server_socket_setopt(int port, int opt, const void*, int len, int famliy);
105int _tcp_server_bind(int port, int family);
106int _tcp_server_bind_setopt(int port, int opt, const void*, int len, int family);
107
108#define tcp_server_socket(p) _tcp_server_socket((p), AF_UNSPEC)
109#define tcp_server_socket_ipv4(p) _tcp_server_socket((p), AF_INET)
110#define tcp_server_socket_ipv6(p) _tcp_server_socket((p), AF_INET6)
111
112#define tcp_server_socket_setop(p, o, v, l) _tcp_server_socket_setopt((p), (o), (v), (l), AF_UNSPEC)
113#define tcp_server_socket_setop_ipv4(p, o, v, l) _tcp_server_socket_setopt((p), (o), (v), (l), AF_INET)
114#define tcp_server_socket_setop_ipv6(p, o, v, l) _tcp_server_socket_setopt((p), (o), (v), (l), AF_INET6)
115
116#define tcp_server_bind(p) _tcp_server_bind((p), AF_UNSPEC)
117#define tcp_server_bind_ipv4(p) _tcp_server_bind((p), AF_INET)
118#define tcp_server_bind_ipv6(p) _tcp_server_bind((p), AF_INET6)
119
120#define tcp_server_bind_setopt(p, o, v, l) _tcp_server_bind_setopt((p), (o), (v), (l), AF_UNSPEC)
121#define tcp_server_bind_setopt_ipv4(p, o, v, l) _tcp_server_bind_setopt((p), (o), (v), (l), AF_INET)
122#define tcp_server_bind_setopt_ipv6(p, o, v, l) _tcp_server_bind_setopt((p), (o), (v), (l), AF_INET6)
123
124int _tcp_client_socket(char* hname, int port, int family);
125int _tcp_client_bind_socket(char* host, int sport, int cport, int family);
126int _tcp_bind(int sock, int port, int family);
127int _tcp_bind_setopt(int sock, int port, int opt, const void*, int len, int family);
128int _tcp_connect(int sock, char* host, int port, int family);
129
130#define tcp_client_socket(h, p) _tcp_client_socket((h), (p), AF_UNSPEC)
131#define tcp_client_socket_ipv4(h, p) _tcp_client_socket((h), (p), AF_INET)
132#define tcp_client_socket_ipv6(h, p) _tcp_client_socket((h), (p), AF_INET6)
133
134#define tcp_client_bind_socket(h, s, c) _tcp_client_bind_socket((h), (c), (s), AF_UNSPEC)
135#define tcp_client_bind_socket_ipv4(h, s, c) _tcp_client_bind_socket((h), (c), (s), AF_INET)
136#define tcp_client_bind_socket_ipv6(h, s, c) _tcp_client_bind_socket((h), (c), (s), AF_INET6)
137
138#define tcp_bind(s, p) _tcp_bind((s), (p), AD_UNSPEC)
139#define tcp_bind_ipv4(s, p) _tcp_bind((s), (p), AD_INET)
140#define tcp_bind_ipv6(s, p) _tcp_bind((s), (p), AD_INET6)
141
142#define tcp_bind_setopt(s, p, o, v, l) _tcp_bind_setopt((s), (p), (o), (v), (l), AF_UNSPEC)
143#define tcp_bind_setopt_ipv4(s, p, o, v, l) _tcp_bind_setopt((s), (p), (o), (v), (l), AF_INET)
144#define tcp_bind_setopt_ipv6(s, p, o, v, l) _tcp_bind_setopt((s), (p), (o), (v), (l), AF_INET6)
145
146#define tcp_connect(s, h, p) _tcp_connect((s), (h), (p), AF_UNSPEC)
147#define tcp_connect_ipv4(s, h, p) _tcp_connect((s), (h), (p), AF_INET)
148#define tcp_connect_ipv6(s, h, p) _tcp_connect((s), (h), (p), AF_INET6)
149
150//
151int accept_intr(int, struct sockaddr*, socklen_t*);
152int socket_close(int sock);
153int set_nonblock_socket(int sock);
154int set_block_socket(int sock);
155
156
158// Valid Sockets
159
160int get_valid_udp_socket(int min, int max, unsigned short* port);
161int get_valid_tcp_server_socket(int min, int max, unsigned short* port);
162int get_valid_tcp_client_socket(int min, int max, char* hostname, unsigned short sport, unsigned short* cport);
163
164
166// Communication
167
168int udp_recv(int, char*, int, struct addrinfo*);
169int udp_send(int, char*, int, struct addrinfo*);
170int tcp_recv(int, char*, int);
171int tcp_send(int, char*, int);
172
173int udp_recv_wait(int sock, char* data, int size, struct addrinfo* sv_addr, int tm);
174int tcp_recv_wait(int sock, char* data, int sz, int tm);
175
176int tcp_send_mesgln(int, char*);
177int tcp_recv_mstream(int sock, char* mesg, int sz, mstream* sb, int tm);
178
179int recv_wait(int sock, int tm);
180int recv_wait_twin(int sock1, int sock2, int tm);
181int send_wait(int sock, int tm);
182
183#define udp_send_mesg(s, m, a) udp_send((s), (m), 0, (a))
184#define tcp_send_mesg(s, m) tcp_send((s), (m), 0)
185
186
188// IP address
189
190struct sockaddr* make_sockaddr_bynum(unsigned char* addr, int port, int family);
191struct sockaddr* make_sockaddr_bystr(const char* addr, int port);
192
193#define make_sockaddr_bynum_ipv4(a, p) make_sockaddr_bynum((a), (p), AF_INET)
194#define make_sockaddr_bynum_ipv6(a, p) make_sockaddr_bynum((a), (p), AF_INET6)
195
196char* _get_hostname(const char* host, int family);
197
198#define get_hostname(h) _get_hostname((h), AF_UNSPEC)
199#define get_hostname_ipv4(h) _get_hostname((h), AF_INET)
200#define get_hostname_ipv6(h) _get_hostname((h), AF_INET6)
201
202char* get_hostname_bynum(unsigned char* bin, int family);
203char* get_hostname_bystr(const char* str);
204
205#define get_hostname_bynum_ipv4(n) get_hostname_bynum((n), AF_INET)
206#define get_hostname_bynum_ipv6(n) get_hostname_bynum((n), AF_INET6)
207
208char* get_ipaddr_byname(const char* host, int family);
209unsigned char* get_ipaddr_byname_num(const char* h, int family);
210
211#define get_ipaddr_byname_ipv4(h) get_ipaddr_byname((h), AF_INET)
212#define get_ipaddr_byname_ipv6(h) get_ipaddr_byname((h), AF_INET6)
213#define get_ipaddr_byname_num_ipv4(h) get_ipaddr_byname_num((h), AF_INET)
214#define get_ipaddr_byname_num_ipv6(h) get_ipaddr_byname_num((h), AF_INET6)
215
216char* get_myipaddr(int family);
217unsigned char* get_myipaddr_num(int family);
218
219#define get_myipaddr_ipv4() get_myipaddr(AF_INET)
220#define get_myipaddr_ipv6() get_myipaddr(AF_INET6)
221#define get_myipaddr_num_ipv4() get_myipaddr_num(AF_INET)
222#define get_myipaddr_num_ipv6() get_myipaddr_num(AF_INET6)
223
224char* _get_localip_bydest(const char* host, int family);
225
226#define get_localip_bydest(h) _get_localip_bydest((h), AF_UNSPEC)
227#define get_localip_bydest_ipv4(h) _get_localip_bydest((h), AF_INET)
228#define get_localip_bydest_ipv6(h) _get_localip_bydest((h), AF_INET6)
229
230#define get_localip() _get_localip_bydest("www.google.co.jp", AF_UNSPEC)
231#define get_localip_ipv4() _get_localip_bydest("www.google.co.jp", AF_INET)
232#define get_localip_ipv6() _get_localip_bydest("www.google.co.jp", AF_INET6)
233
234unsigned char* get_mynetaddr_num(int family);
235char* get_mynetaddr(int family);
236
237#define get_mynetaddr_num_ipv4() get_mynetaddr_num(AF_INET)
238#define get_mynetaddr_num_ipv6() get_mynetaddr_num(AF_INET6)
239#define get_mynetaddr_ipv4() get_mynetaddr(AF_INET)
240#define get_mynetaddr_ipv6() get_mynetaddr(AF_INET6)
241
242
244// Tools
245
246unsigned char* to_address_num(char*, int md, int mk, int family);
247char* to_address_char(unsigned char* addr, int mask, int family);
248
249#define to_address_num8_ipv4(a, m) to_address_num((a), (m), 1, AF_INET)
250#define to_address_num4_ipv4(a, m) to_address_num((a), (m), 0, AF_INET)
251#define to_address_char8_ipv4(a) to_address_char((a), 1, AF_INET)
252#define to_address_char4_ipv4(a) to_address_char((a), 0, AF_INET)
253
254#define to_address_num32_ipv6(a, m) to_address_num((a), (m), 1, AF_INET6)
255#define to_address_num16_ipv6(a, m) to_address_num((a), (m), 0, AF_INET6)
256#define to_address_char32_ipv6(a) to_address_char((a), 1, AF_INET6)
257#define to_address_char16_ipv6(a) to_address_char((a), 0, AF_INET6)
258
259int is_same_sockaddr(struct sockaddr* addr1, struct sockaddr* addr2);
260int is_same_network(char* addr1, char* addr2, char* mask);
261int is_same_network_num(unsigned char* addr1, unsigned char* addr2, unsigned char* mask, int family);
262
263#define is_same_network_num_ipv4(a1, a2, m) is_same_network_num((a1), (a2), (m), AF_INET)
264#define is_same_network_num_ipv6(a1, a2, m) is_same_network_num((a1), (a2), (m), AF_INET6)
265
266void udp_hole_punching(int sock, struct addrinfo* addr, int num);
267
268
270// for IPv4 only
271
272int udp_client_socket_sockaddr_in(char*, int, struct sockaddr_in*);
273
274int udp_recv_sockaddr_in(int, char*, int, struct sockaddr_in*);
275int udp_send_sockaddr_in(int, char*, int, struct sockaddr_in*);
276int udp_recv_wait_sockaddr_in(int sock, char* data, int size, struct sockaddr_in* sv_addr, int tm);
277
278#define udp_send_mesg_sockaddr_in(s, m, a) udp_send_sockaddr_in((s), (m), 0, (a))
279
280struct sockaddr_in get_sockaddr_in(char* hostname, unsigned short cport);
281struct sockaddr_in get_sockaddr_in_bynum(char* ipnum, unsigned short cport);
282struct sockaddr_in get_local_sockaddr_in(unsigned short cport);
283
284//int is_same_sockaddr_ipv4(struct sockaddr_in addr1, struct sockaddr_in addr2);
285#define is_same_sockaddr_in(a, b) is_same_sockaddr((struct sockaddr*)&(a), (struct sockaddr*)&(b))
286
287unsigned char* get_ipaddr_num_ipv4(struct in_addr);
288char* get_ipaddr_ipv4(struct in_addr);
289
290void udp_hole_punching_sockaddr_in(int sock, struct sockaddr_in addr, int nm);
291
292
293#endif // __JBXL_NETWORK_H_
294
295
298
JunkBox_Lib 状態ヘッダ
int get_valid_tcp_client_socket(int min, int max, char *hostname, unsigned short sport, unsigned short *cport)
Definition network.cpp:1124
int tcp_send(int, char *, int)
Definition network.cpp:1252
struct sockaddr_in get_local_sockaddr_in(unsigned short cport)
Definition network.cpp:2654
int send_wait(int sock, int tm)
Definition network.cpp:1518
int init_network(void)
Definition network.cpp:26
int _udp_server_socket(int port, struct addrinfo **info, int family)
call socket(), bind()
Definition network.cpp:70
int tcp_recv_mstream(int sock, char *mesg, int sz, mstream *sb, int tm)
Definition network.cpp:1395
int _udp_bind_setopt(int sock, int port, int opt, const void *, int len, int family)
call setsockopt(), bind()
Definition network.cpp:332
int udp_send(int, char *, int, struct addrinfo *)
Definition network.cpp:1199
int is_same_network_num(unsigned char *addr1, unsigned char *addr2, unsigned char *mask, int family)
Definition network.cpp:2331
int tcp_send_mesgln(int, char *)
Definition network.cpp:1345
int socket_close(int sock)
call shutdown(), close()
Definition network.cpp:1022
int _udp_server_socket_setopt(int, int, const void *, int, struct addrinfo **, int f)
call socket(), setsockopt(), bind()
Definition network.cpp:156
char * get_ipaddr_byname(const char *host, int family)
ホスト名 → IPv4/IPv6 アドレス(文字列)
Definition network.cpp:1781
char * _get_hostname(const char *host, int family)
IPv4/IPv6 アドレス(文字列), FQDN → ホスト名(文字列)
Definition network.cpp:1729
int accept_intr(int, struct sockaddr *, socklen_t *)
call accept()
Definition network.cpp:995
char * get_mynetaddr(int family)
自分の IPv4/IPv6 ネットワークアドレス(文字列)
Definition network.cpp:2142
int _tcp_bind(int sock, int port, int family)
call bind()
Definition network.cpp:834
int get_valid_tcp_server_socket(int min, int max, unsigned short *port)
Definition network.cpp:1102
int _tcp_connect(int sock, char *host, int port, int family)
call connect()
Definition network.cpp:949
unsigned char * to_address_num(char *, int md, int mk, int family)
IPv4/IPv6 アドレス(文字列)→ [IPv4/IPv6 アドレス],[ネットマスク](数字8/32Byte)
Definition network.cpp:2373
int _udp_bind(int sock, int port, int family)
call bind()
Definition network.cpp:283
void udp_hole_punching_sockaddr_in(int sock, struct sockaddr_in addr, int nm)
use udp_hole_punching()
Definition network.cpp:2741
int _tcp_server_socket_setopt(int port, int opt, const void *, int len, int famliy)
call socket(), setsockopt(), bind(), listen()
Definition network.cpp:482
int set_nonblock_socket(int sock)
Definition network.cpp:1043
int _tcp_client_socket(char *hname, int port, int family)
call socket(), connect()
Definition network.cpp:712
int _tcp_bind_setopt(int sock, int port, int opt, const void *, int len, int family)
call setsockopt(), bind()
Definition network.cpp:886
char * to_address_char(unsigned char *addr, int mask, int family)
[IPv4/IPv6 アドレス],[ネットマスク](数字8/32Byte)→ IPv4/IPv6 アドレス(文字列)
Definition network.cpp:2496
char * get_hostname_bystr(const char *str)
IPv4/IPv6 アドレス(文字列) → ホスト名(文字列)
Definition network.cpp:1684
int _tcp_server_bind_setopt(int port, int opt, const void *, int len, int family)
call socket(), setsockopt(), bind()
Definition network.cpp:639
int tcp_recv_wait(int sock, char *data, int sz, int tm)
Definition network.cpp:1317
struct sockaddr_in get_sockaddr_in(char *hostname, unsigned short cport)
Definition network.cpp:2619
struct sockaddr * make_sockaddr_bystr(const char *addr, int port)
IPv4/IPv6 対応
Definition network.cpp:1605
int udp_recv_sockaddr_in(int, char *, int, struct sockaddr_in *)
use udp_recv()
Definition network.cpp:2578
int _tcp_client_bind_socket(char *host, int sport, int cport, int family)
call socket(), bind(), connect()
Definition network.cpp:767
int udp_recv_wait(int sock, char *data, int size, struct addrinfo *sv_addr, int tm)
Definition network.cpp:1283
int _udp_client_socket(char *host, int, struct addrinfo **, int family)
call socket()
Definition network.cpp:237
int is_same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
Definition network.cpp:2219
int udp_recv_wait_sockaddr_in(int sock, char *data, int size, struct sockaddr_in *sv_addr, int tm)
use udp_recv_wait()
Definition network.cpp:2605
char * get_ipaddr_ipv4(struct in_addr)
構造体 → IPv4 アドレス(文字列) use get_ipaddr_byname()
Definition network.cpp:2685
unsigned char * get_ipaddr_byname_num(const char *h, int family)
ホスト名 → IPv4/IPv6 アドレス(バイナリ)
Definition network.cpp:1813
int udp_recv(int, char *, int, struct addrinfo *)
Definition network.cpp:1166
int _tcp_server_socket(int port, int family)
call socket(), bind(), listen()
Definition network.cpp:396
struct sockaddr * make_sockaddr_bynum(unsigned char *addr, int port, int family)
IPv4/IPv6 対応
Definition network.cpp:1556
char * get_hostname_bynum(unsigned char *bin, int family)
IPv4/IPv6 アドレス(バイナリ)→ ホスト名(文字列)
Definition network.cpp:1642
int recv_wait(int sock, int tm)
Definition network.cpp:1443
struct sockaddr_in get_sockaddr_in_bynum(char *ipnum, unsigned short cport)
Definition network.cpp:2641
unsigned char * get_mynetaddr_num(int family)
自分の IPv4/IPv6 ネットワークアドレス(バイナリ)
Definition network.cpp:2177
int get_valid_udp_socket(int min, int max, unsigned short *port)
Definition network.cpp:1078
#define socklen_t
Definition network.h:21
char * get_myipaddr(int family)
自分の [IPv4/IPv6 ネットワークアドレス]/[ネットマスク](文字列)
Definition network.cpp:1858
WSADATA WsaData
Definition network.cpp:22
int is_same_network(char *addr1, char *addr2, char *mask)
Definition network.cpp:2283
int _tcp_server_bind(int port, int family)
call socket(), bind()
Definition network.cpp:562
unsigned char * get_myipaddr_num(int family)
自分の [IPv4/IPv6 アドレス],[ネットマスク](バイナリ)
Definition network.cpp:2008
unsigned char * get_ipaddr_num_ipv4(struct in_addr)
構造体 → IPv4 アドレス(バイナリ) use get_ipaddr_byname_num()
Definition network.cpp:2717
int recv_wait_twin(int sock1, int sock2, int tm)
Definition network.cpp:1480
int tcp_recv(int, char *, int)
Definition network.cpp:1226
int set_block_socket(int sock)
Definition network.cpp:1060
char * _get_localip_bydest(const char *host, int family)
自分の IPv4/IPv6 アドレス(文字列)
Definition network.cpp:2064
int udp_client_socket_sockaddr_in(char *, int, struct sockaddr_in *)
use udp_client_socket()
Definition network.cpp:2553
int udp_send_sockaddr_in(int, char *, int, struct sockaddr_in *)
use udp_send()
Definition network.cpp:2593
void udp_hole_punching(int sock, struct addrinfo *addr, int num)
Definition network.cpp:2534
void cleanup_network(void)
Definition network.cpp:40
汎用ツールヘッダ
#define mstream
バイト型 メッセージストリーム
Definition tools.h:167