1#ifndef __JBXL_NETWORK_H_
2#define __JBXL_NETWORK_H_
14#define _WINSOCK_DEPRECATED_NO_WARNINGS
25 #define _WINSOCK_DEPRECATED_NO_WARNINGS
26 #pragma comment( lib, "ws2_32.lib" )
30 #include <sys/socket.h>
39 #ifdef HAVE_SYS_SOCKIO_H
40 #include <sys/sockio.h>
43 #ifdef HAVE_NETINET_IN_H
44 #include <netinet/in.h>
46 #ifdef HAVE_ARPA_INET_H
47 #include <arpa/inet.h>
53 #include <sys/ioctl.h>
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);
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)
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)
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)
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)
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)
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)
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)
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)
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)
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);
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)
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)
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)
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)
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)
168int udp_recv(
int,
char*,
int,
struct addrinfo*);
169int udp_send(
int,
char*,
int,
struct addrinfo*);
173int udp_recv_wait(
int sock,
char* data,
int size,
struct addrinfo* sv_addr,
int tm);
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
261int is_same_network_num(
unsigned char* addr1,
unsigned char* addr2,
unsigned char* mask,
int family);
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)
278#define udp_send_mesg_sockaddr_in(s, m, a) udp_send_sockaddr_in((s), (m), 0, (a))
280struct sockaddr_in
get_sockaddr_in(char* hostname,
unsigned short cport);
285#define is_same_sockaddr_in(a, b) is_same_sockaddr((struct sockaddr*)&(a), (struct sockaddr*)&(b))
int get_valid_tcp_client_socket(int min, int max, char *hostname, unsigned short sport, unsigned short *cport)
int tcp_send(int, char *, int)
struct sockaddr_in get_local_sockaddr_in(unsigned short cport)
int send_wait(int sock, int tm)
int _udp_server_socket(int port, struct addrinfo **info, int family)
call socket(), bind()
int tcp_recv_mstream(int sock, char *mesg, int sz, mstream *sb, int tm)
int _udp_bind_setopt(int sock, int port, int opt, const void *, int len, int family)
call setsockopt(), bind()
int udp_send(int, char *, int, struct addrinfo *)
int is_same_network_num(unsigned char *addr1, unsigned char *addr2, unsigned char *mask, int family)
int tcp_send_mesgln(int, char *)
int socket_close(int sock)
call shutdown(), close()
int _udp_server_socket_setopt(int, int, const void *, int, struct addrinfo **, int f)
call socket(), setsockopt(), bind()
char * get_ipaddr_byname(const char *host, int family)
ホスト名 → IPv4/IPv6 アドレス(文字列)
char * _get_hostname(const char *host, int family)
IPv4/IPv6 アドレス(文字列), FQDN → ホスト名(文字列)
int accept_intr(int, struct sockaddr *, socklen_t *)
call accept()
char * get_mynetaddr(int family)
自分の IPv4/IPv6 ネットワークアドレス(文字列)
int _tcp_bind(int sock, int port, int family)
call bind()
int get_valid_tcp_server_socket(int min, int max, unsigned short *port)
int _tcp_connect(int sock, char *host, int port, int family)
call connect()
unsigned char * to_address_num(char *, int md, int mk, int family)
IPv4/IPv6 アドレス(文字列)→ [IPv4/IPv6 アドレス],[ネットマスク](数字8/32Byte)
int _udp_bind(int sock, int port, int family)
call bind()
void udp_hole_punching_sockaddr_in(int sock, struct sockaddr_in addr, int nm)
use udp_hole_punching()
int _tcp_server_socket_setopt(int port, int opt, const void *, int len, int famliy)
call socket(), setsockopt(), bind(), listen()
int set_nonblock_socket(int sock)
int _tcp_client_socket(char *hname, int port, int family)
call socket(), connect()
int _tcp_bind_setopt(int sock, int port, int opt, const void *, int len, int family)
call setsockopt(), bind()
char * to_address_char(unsigned char *addr, int mask, int family)
[IPv4/IPv6 アドレス],[ネットマスク](数字8/32Byte)→ IPv4/IPv6 アドレス(文字列)
char * get_hostname_bystr(const char *str)
IPv4/IPv6 アドレス(文字列) → ホスト名(文字列)
int _tcp_server_bind_setopt(int port, int opt, const void *, int len, int family)
call socket(), setsockopt(), bind()
int tcp_recv_wait(int sock, char *data, int sz, int tm)
struct sockaddr_in get_sockaddr_in(char *hostname, unsigned short cport)
struct sockaddr * make_sockaddr_bystr(const char *addr, int port)
IPv4/IPv6 対応
int udp_recv_sockaddr_in(int, char *, int, struct sockaddr_in *)
use udp_recv()
int _tcp_client_bind_socket(char *host, int sport, int cport, int family)
call socket(), bind(), connect()
int udp_recv_wait(int sock, char *data, int size, struct addrinfo *sv_addr, int tm)
int _udp_client_socket(char *host, int, struct addrinfo **, int family)
call socket()
int is_same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
int udp_recv_wait_sockaddr_in(int sock, char *data, int size, struct sockaddr_in *sv_addr, int tm)
use udp_recv_wait()
char * get_ipaddr_ipv4(struct in_addr)
構造体 → IPv4 アドレス(文字列) use get_ipaddr_byname()
unsigned char * get_ipaddr_byname_num(const char *h, int family)
ホスト名 → IPv4/IPv6 アドレス(バイナリ)
int udp_recv(int, char *, int, struct addrinfo *)
int _tcp_server_socket(int port, int family)
call socket(), bind(), listen()
struct sockaddr * make_sockaddr_bynum(unsigned char *addr, int port, int family)
IPv4/IPv6 対応
char * get_hostname_bynum(unsigned char *bin, int family)
IPv4/IPv6 アドレス(バイナリ)→ ホスト名(文字列)
int recv_wait(int sock, int tm)
struct sockaddr_in get_sockaddr_in_bynum(char *ipnum, unsigned short cport)
unsigned char * get_mynetaddr_num(int family)
自分の IPv4/IPv6 ネットワークアドレス(バイナリ)
int get_valid_udp_socket(int min, int max, unsigned short *port)
char * get_myipaddr(int family)
自分の [IPv4/IPv6 ネットワークアドレス]/[ネットマスク](文字列)
int is_same_network(char *addr1, char *addr2, char *mask)
int _tcp_server_bind(int port, int family)
call socket(), bind()
unsigned char * get_myipaddr_num(int family)
自分の [IPv4/IPv6 アドレス],[ネットマスク](バイナリ)
unsigned char * get_ipaddr_num_ipv4(struct in_addr)
構造体 → IPv4 アドレス(バイナリ) use get_ipaddr_byname_num()
int recv_wait_twin(int sock1, int sock2, int tm)
int tcp_recv(int, char *, int)
int set_block_socket(int sock)
char * _get_localip_bydest(const char *host, int family)
自分の IPv4/IPv6 アドレス(文字列)
int udp_client_socket_sockaddr_in(char *, int, struct sockaddr_in *)
use udp_client_socket()
int udp_send_sockaddr_in(int, char *, int, struct sockaddr_in *)
use udp_send()
void udp_hole_punching(int sock, struct addrinfo *addr, int num)
void cleanup_network(void)