JunkBox_Lib  1.10.2
network.c
Go to the documentation of this file.
1 //
8 #ifdef CPLUSPLUS
9  #undef CPLUSPLUS
10 #endif
11 
12 
13 #include "network.h"
14 #include <time.h>
15 
16 
18 // for WinSock
19 //
20 
21 #ifdef WIN32
22 WSADATA WsaData;
23 #endif
24 
25 
26 int init_network(void)
27 {
28  int ret = 0;
29 
30 #ifdef WIN32
31  // for 10093 エラー
32  ret = WSAStartup(MAKEWORD(2,0), &WsaData);
33  if (ret!=0) WSACleanup();
34 #endif
35 
36  return ret;
37 }
38 
39 
40 void cleanup_network(void)
41 {
42 #ifdef WIN32
43  WSACleanup();
44 #endif
45 }
46 
47 
48 
50 // Berkeley Socket
51 //
52 
70 int _udp_server_socket(int port, struct addrinfo** sv_addr, int family)
71 {
72  int sofd, err, nullflg = OFF;
73  struct addrinfo hints;
74  struct addrinfo* address;
75  int nonblock = FALSE;
76 
77  if (sv_addr==NULL) {
78  nullflg = ON;
79  sv_addr = &address;
80  }
81  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
82  //
83  if (port<0) {
84  nonblock = TRUE;
85  port = -port;
86  }
87 
88  memset(&hints, 0, sizeof(struct addrinfo));
89  hints.ai_family = family;
90  hints.ai_flags = AI_PASSIVE; // 任意のIPアドレスの接続を許す設定.サーバ用.
91  hints.ai_socktype = SOCK_DGRAM;
92 
93  char* str = itostr_ts(port);
94  err = getaddrinfo(NULL, str, &hints, sv_addr);
95  freeNull(str);
96  if (err!=0) {
97  *sv_addr = NULL;
98  return JBXL_NET_INFO_ERROR;
99  }
100 
101  sofd = (int)socket((*sv_addr)->ai_family, (*sv_addr)->ai_socktype, (*sv_addr)->ai_protocol);
102  if (sofd<0) {
103  freeaddrinfo(*sv_addr);
104  *sv_addr = NULL;
105  return JBXL_NET_SOCKET_ERROR;
106  }
107  if (nonblock) sofd = set_nonblock_socket(sofd);
108 
109  // TIME_WAIT しない設定.
110  int lg = 1;
111  #ifdef WIN32
112  err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, (const char*)&lg, sizeof(lg));
113  #else
114  err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, &lg, sizeof(lg));
115  #endif
116  if (err<0) {
117  freeaddrinfo(*sv_addr);
118  socket_close(sofd);
119  return JBXL_NET_OPTION_ERROR;
120  }
121 
122  err = bind(sofd, (*sv_addr)->ai_addr, (int)(*sv_addr)->ai_addrlen);
123  if (err<0) {
124  freeaddrinfo(*sv_addr);
125  *sv_addr = NULL;
126  socket_close(sofd);
127  return JBXL_NET_BIND_ERROR;
128  }
129 
130  if (nullflg) freeaddrinfo(*sv_addr);
131  return sofd;
132 }
133 
134 
156 int _udp_server_socket_setopt(int port, int opt, const void* optval, int optlen, struct addrinfo** sv_addr, int family)
157 {
158  int sofd, err, nullflg = OFF;
159  struct addrinfo hints;
160  struct addrinfo* address;
161  int nonblock = FALSE;
162 
163  if (sv_addr==NULL) {
164  nullflg = ON;
165  sv_addr = &address;
166  }
167  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
168  //
169  if (port<0) {
170  nonblock = TRUE;
171  port = -port;
172  }
173 
174  memset(&hints, 0, sizeof(struct addrinfo));
175  hints.ai_family = family;
176  hints.ai_flags = AI_PASSIVE;
177  hints.ai_socktype = SOCK_DGRAM;
178 
179  char* str = itostr_ts(port);
180  err = getaddrinfo(NULL, str, &hints, sv_addr);
181  freeNull(str);
182  if (err!=0) return JBXL_NET_INFO_ERROR;
183 
184  sofd = (int)socket((*sv_addr)->ai_family, (*sv_addr)->ai_socktype, (*sv_addr)->ai_protocol);
185  if (sofd<0) {
186  freeaddrinfo(*sv_addr);
187  *sv_addr = NULL;
188  return JBXL_NET_SOCKET_ERROR;
189  }
190  if (nonblock) sofd = set_nonblock_socket(sofd);
191 
192  if (opt>0) {
193  #ifdef WIN32
194  err = setsockopt(sofd, SOL_SOCKET, opt, (const char*)optval, optlen);
195  #else
196  err = setsockopt(sofd, SOL_SOCKET, opt, optval, optlen);
197  #endif
198  if (err<0) {
199  freeaddrinfo(*sv_addr);
200  *sv_addr = NULL;
201  socket_close(sofd);
202  return JBXL_NET_OPTION_ERROR;
203  }
204  }
205 
206  err = bind(sofd, (*sv_addr)->ai_addr, (int)(*sv_addr)->ai_addrlen);
207  if (err<0) {
208  freeaddrinfo(*sv_addr);
209  *sv_addr = NULL;
210  socket_close(sofd);
211  return JBXL_NET_BIND_ERROR;
212  }
213 
214  if (nullflg) freeaddrinfo(*sv_addr);
215  return sofd;
216 }
217 
218 
237 int _udp_client_socket(char* hostname, int port, struct addrinfo** sv_addr, int family)
238 {
239  int sofd, err;
240  struct addrinfo hints;
241 
242  if (sv_addr==NULL) return JBXL_NET_INFO_ERROR;
243  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
244 
245  memset(&hints, 0, sizeof(struct addrinfo));
246  hints.ai_family = family;
247  hints.ai_flags = AI_PASSIVE; // 任意のIPアドレスの接続を許す設定.本来はサーバ用.多分不必要(無視される).
248  hints.ai_socktype = SOCK_DGRAM;
249 
250  char* str = itostr_ts(port);
251  err = getaddrinfo(hostname, str, &hints, sv_addr);
252  freeNull(str);
253  if (err!=0) {
254  *sv_addr = NULL;
255  return JBXL_NET_INFO_ERROR;
256  }
257 
258  sofd = (int)socket((*sv_addr)->ai_family, (*sv_addr)->ai_socktype, (*sv_addr)->ai_protocol);
259  if (sofd<0) {
260  freeaddrinfo(*sv_addr);
261  *sv_addr = NULL;
262  return JBXL_NET_SOCKET_ERROR;
263  }
264 
265  return sofd;
266 }
267 
268 
283 int _udp_bind(int sofd, int port, int family)
284 {
285  int err;
286  struct addrinfo hints;
287  struct addrinfo* address;
288 
289  if (sofd<=0) return sofd;
290  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
291 
292  memset(&hints, 0, sizeof(struct addrinfo));
293  hints.ai_family = family;
294  hints.ai_flags = AI_PASSIVE;
295  hints.ai_socktype = SOCK_DGRAM;
296 
297  char* str = itostr_ts(port);
298  err = getaddrinfo(NULL, str, &hints, &address);
299  freeNull(str);
300  if (err!=0) return JBXL_NET_INFO_ERROR;
301 
302  err = bind(sofd, address->ai_addr, (int)address->ai_addrlen);
303  if (err<0) {
304  freeaddrinfo(address);
305  socket_close(sofd);
306  return JBXL_NET_BIND_ERROR;
307  }
308 
309  freeaddrinfo(address);
310  return sofd;
311 }
312 
313 
332 int _udp_bind_setopt(int sofd, int port, int opt, const void* optval, int optlen, int family)
333 {
334  int err;
335  struct addrinfo hints;
336  struct addrinfo* address;
337 
338  if (sofd<=0) return sofd;
339  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
340 
341  if (opt>0) {
342  #ifdef WIN32
343  err = setsockopt(sofd, SOL_SOCKET, opt, (const char*)optval, optlen);
344  #else
345  err = setsockopt(sofd, SOL_SOCKET, opt, optval, optlen);
346  #endif
347  if (err<0) {
348  socket_close(sofd);
349  return JBXL_NET_OPTION_ERROR;
350  }
351  }
352 
353  memset(&hints, 0, sizeof(struct addrinfo));
354  hints.ai_family = family;
355  hints.ai_flags = AI_PASSIVE;
356  hints.ai_socktype = SOCK_DGRAM;
357 
358  char* str = itostr_ts(port);
359  err = getaddrinfo(NULL, str, &hints, &address);
360  freeNull(str);
361  if (err!=0) {
362  socket_close(sofd);
363  return JBXL_NET_INFO_ERROR;
364  }
365 
366  err = bind(sofd, address->ai_addr, (int)address->ai_addrlen);
367  if (err<0) {
368  freeaddrinfo(address);
369  socket_close(sofd);
370  return JBXL_NET_BIND_ERROR;
371  }
372 
373  freeaddrinfo(address);
374  return sofd;
375 }
376 
377 
396 int _tcp_server_socket(int port, int family)
397 {
398  int sofd, err;
399  struct addrinfo hints;
400  struct addrinfo* address;
401  int nonblock = FALSE;
402 
403  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
404 
405  memset(&hints, 0, sizeof(struct addrinfo));
406  hints.ai_family = family;
407  hints.ai_flags = AI_PASSIVE;
408  hints.ai_socktype = SOCK_STREAM;
409 
410  if (port<0) {
411  nonblock = TRUE;
412  port = -port;
413  }
414 
415  char* str = itostr_ts(port);
416  err = getaddrinfo(NULL, str, &hints, &address);
417  freeNull(str);
418  if (err!=0) return JBXL_NET_INFO_ERROR;
419 
420  sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
421  if (sofd<0) {
422  freeaddrinfo(address);
423  return JBXL_NET_SOCKET_ERROR;
424  }
425  if (nonblock) sofd = set_nonblock_socket(sofd);
426 
427  // TIME_WAIT しない設定.
428  int lg = 1;
429  #ifdef WIN32
430  err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, (const char*)&lg, sizeof(lg));
431  #else
432  err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, &lg, sizeof(lg));
433  #endif
434  if (err<0) {
435  freeaddrinfo(address);
436  socket_close(sofd);
437  return JBXL_NET_OPTION_ERROR;
438  }
439 
440  err = bind(sofd, address->ai_addr, (int)address->ai_addrlen);
441  if (err<0) {
442  freeaddrinfo(address);
443  socket_close(sofd);
444  return JBXL_NET_BIND_ERROR;
445  }
446 
447  err = listen(sofd, 10);
448  if (err==-1) {
449  freeaddrinfo(address);
450  socket_close(sofd);
451  return JBXL_NET_LISTEN_ERROR;
452  }
453 
454  freeaddrinfo(address);
455  return sofd;
456 }
457 
458 
482 int _tcp_server_socket_setopt(int port, int opt, const void* optval, int optlen, int family)
483 {
484  int sofd, err;
485  struct addrinfo hints;
486  struct addrinfo* address;
487  int nonblock = FALSE;
488 
489  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
490 
491  memset(&hints, 0, sizeof(struct addrinfo));
492  hints.ai_family = family;
493  hints.ai_flags = AI_PASSIVE;
494  hints.ai_socktype = SOCK_STREAM;
495 
496  if (port<0) {
497  nonblock = TRUE;
498  port = -port;
499  }
500 
501  char* str = itostr_ts(port);
502  err = getaddrinfo(NULL, str, &hints, &address);
503  freeNull(str);
504  if (err!=0) return JBXL_NET_INFO_ERROR;
505 
506  sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
507  if (sofd<0) {
508  freeaddrinfo(address);
509  return JBXL_NET_SOCKET_ERROR;
510  }
511  if (nonblock) sofd = set_nonblock_socket(sofd);
512 
513  if (opt>0) {
514  #ifdef WIN32
515  err = setsockopt(sofd, SOL_SOCKET, opt, (const char*)optval, optlen);
516  #else
517  err = setsockopt(sofd, SOL_SOCKET, opt, optval, optlen);
518  #endif
519  if (err<0) {
520  freeaddrinfo(address);
521  socket_close(sofd);
522  return JBXL_NET_OPTION_ERROR;
523  }
524  }
525 
526  err = bind(sofd, address->ai_addr, (int)address->ai_addrlen);
527  if (err<0) {
528  freeaddrinfo(address);
529  socket_close(sofd);
530  return JBXL_NET_BIND_ERROR;
531  }
532 
533  err = listen(sofd, 10);
534  if (err==-1) {
535  freeaddrinfo(address);
536  socket_close(sofd);
537  return JBXL_NET_LISTEN_ERROR;
538  }
539 
540  freeaddrinfo(address);
541  return sofd;
542 }
543 
544 
562 int _tcp_server_bind(int port, int family)
563 {
564  int sofd, err;
565  struct addrinfo hints;
566  struct addrinfo* address;
567  int nonblock = FALSE;
568 
569  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
570 
571  memset(&hints, 0, sizeof(struct addrinfo));
572  hints.ai_family = family;
573  hints.ai_flags = AI_PASSIVE;
574  hints.ai_socktype = SOCK_STREAM;
575 
576  if (port<0) {
577  nonblock = TRUE;
578  port = -port;
579  }
580 
581  char* str = itostr_ts(port);
582  err = getaddrinfo(NULL, str, &hints, &address);
583  freeNull(str);
584  if (err!=0) return JBXL_NET_INFO_ERROR;
585 
586  sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
587  if (sofd<0) {
588  freeaddrinfo(address);
589  return JBXL_NET_SOCKET_ERROR;
590  }
591  if (nonblock) sofd = set_nonblock_socket(sofd);
592 
593  // TIME_WAIT しない設定.
594  int lg = 1;
595  #ifdef WIN32
596  err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, (const char*)&lg, sizeof(lg));
597  #else
598  err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, &lg, sizeof(lg));
599  #endif
600  if (err<0) {
601  freeaddrinfo(address);
602  socket_close(sofd);
603  return JBXL_NET_OPTION_ERROR;
604  }
605 
606  err = bind(sofd, address->ai_addr, (int)address->ai_addrlen);
607  if (err<0) {
608  freeaddrinfo(address);
609  socket_close(sofd);
610  return JBXL_NET_BIND_ERROR;
611  }
612 
613  freeaddrinfo(address);
614  return sofd;
615 }
616 
617 
639 int _tcp_server_bind_setopt(int port, int opt, const void* optval, int optlen, int family)
640 {
641  int sofd, err;
642  struct addrinfo hints;
643  struct addrinfo* address;
644  int nonblock = FALSE;
645 
646  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
647 
648  memset(&hints, 0, sizeof(struct addrinfo));
649  hints.ai_family = family;
650  hints.ai_flags = AI_PASSIVE;
651  hints.ai_socktype = SOCK_STREAM;
652 
653  if (port<0) {
654  nonblock = TRUE;
655  port = -port;
656  }
657 
658  char* str = itostr_ts(port);
659  err = getaddrinfo(NULL, str, &hints, &address);
660  freeNull(str);
661  if (err!=0) return JBXL_NET_INFO_ERROR;
662 
663  sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
664  if (sofd<0) {
665  freeaddrinfo(address);
666  return JBXL_NET_SOCKET_ERROR;
667  }
668  if (nonblock) sofd = set_nonblock_socket(sofd);
669 
670  if (opt>0) {
671  #ifdef WIN32
672  err = setsockopt(sofd, SOL_SOCKET, opt, (const char*)optval, optlen);
673  #else
674  err = setsockopt(sofd, SOL_SOCKET, opt, optval, optlen);
675  #endif
676  if (err<0) {
677  freeaddrinfo(address);
678  socket_close(sofd);
679  return JBXL_NET_OPTION_ERROR;
680  }
681  }
682 
683  err = bind(sofd, address->ai_addr, (int)address->ai_addrlen);
684  if (err<0) {
685  freeaddrinfo(address);
686  socket_close(sofd);
687  return JBXL_NET_BIND_ERROR;
688  }
689 
690  freeaddrinfo(address);
691  return sofd;
692 }
693 
694 
712 int _tcp_client_socket(char* hostname, int port, int family)
713 {
714  int sofd, err;
715  struct addrinfo hints;
716  struct addrinfo* address;
717 
718  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
719 
720  memset(&hints, 0, sizeof(struct addrinfo));
721  hints.ai_family = family;
722  hints.ai_flags = AI_PASSIVE;
723  hints.ai_socktype = SOCK_STREAM;
724 
725  char* str = itostr_ts(port);
726  err = getaddrinfo(hostname, str, &hints, &address);
727  freeNull(str);
728  if (err!=0) return JBXL_NET_INFO_ERROR;
729 
730  sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
731  if (sofd<0) {
732  freeaddrinfo(address);
733  return JBXL_NET_SOCKET_ERROR;
734  }
735 
736  err = connect(sofd, address->ai_addr, (int)address->ai_addrlen);
737  if (err<0) {
738  freeaddrinfo(address);
739  socket_close(sofd);
740  return JBXL_NET_CONNECT_ERROR;
741  }
742 
743  freeaddrinfo(address);
744  return sofd;
745 }
746 
747 
767 int _tcp_client_bind_socket(char* hostname, int sport, int cport, int family)
768 {
769  int sofd, err;
770  struct addrinfo hints;
771  struct addrinfo* address;
772 
773  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
774 
775  memset(&hints, 0, sizeof(struct addrinfo));
776  hints.ai_family = family;
777  hints.ai_flags = AI_PASSIVE;
778  hints.ai_socktype = SOCK_STREAM;
779 
780  char* str = itostr_ts(cport);
781  err = getaddrinfo(NULL, str, &hints, &address);
782  freeNull(str);
783  if (err!=0) return JBXL_NET_INFO_ERROR;
784 
785  sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
786  if (sofd<0) {
787  freeaddrinfo(address);
788  return JBXL_NET_SOCKET_ERROR;
789  }
790 
791  err = bind(sofd, address->ai_addr, (int)address->ai_addrlen);
792  if (err<0) {
793  freeaddrinfo(address);
794  socket_close(sofd);
795  return JBXL_NET_BIND_ERROR;
796  }
797  freeaddrinfo(address);
798 
799  //
800  str = itostr_ts(sport);
801  err = getaddrinfo(hostname, str, &hints, &address);
802  freeNull(str);
803  if (err!=0) {
804  socket_close(sofd);
805  return JBXL_NET_INFO_ERROR;
806  }
807 
808  err = connect(sofd, address->ai_addr, (int)address->ai_addrlen);
809  if (err<0) {
810  freeaddrinfo(address);
811  socket_close(sofd);
812  return JBXL_NET_CONNECT_ERROR;
813  }
814 
815  freeaddrinfo(address);
816  return sofd;
817 }
818 
819 
834 int _tcp_bind(int sofd, int port, int family)
835 {
836  int err;
837  struct addrinfo hints;
838  struct addrinfo* address;
839 
840  if (sofd<=0) return sofd;
841  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
842 
843  memset(&hints, 0, sizeof(struct addrinfo));
844  hints.ai_family = family;
845  hints.ai_flags = AI_PASSIVE;
846  hints.ai_socktype = SOCK_STREAM;
847 
848  char* str = itostr_ts(port);
849  err = getaddrinfo(NULL, str, &hints, &address);
850  freeNull(str);
851  if (err!=0) {
852  socket_close(sofd);
853  return JBXL_NET_INFO_ERROR;
854  }
855 
856  err = bind(sofd, address->ai_addr, (int)address->ai_addrlen);
857  if (err<0) {
858  freeaddrinfo(address);
859  socket_close(sofd);
860  return JBXL_NET_BIND_ERROR;
861  }
862 
863  freeaddrinfo(address);
864  return sofd;
865 }
866 
867 
886 int _tcp_bind_setopt(int sofd, int port, int opt, const void* optval, int optlen, int family)
887 {
888  int err;
889  struct addrinfo hints;
890  struct addrinfo* address;
891 
892  if (sofd<=0) return sofd;
893  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
894 
895  if (opt>0) {
896  #ifdef WIN32
897  err = setsockopt(sofd, SOL_SOCKET, opt, (const char*)optval, optlen);
898  #else
899  err = setsockopt(sofd, SOL_SOCKET, opt, optval, optlen);
900  #endif
901  if (err<0) {
902  socket_close(sofd);
903  return JBXL_NET_OPTION_ERROR;
904  }
905  }
906 
907  memset(&hints, 0, sizeof(struct addrinfo));
908  hints.ai_family = family;
909  hints.ai_flags = AI_PASSIVE;
910  hints.ai_socktype = SOCK_STREAM;
911 
912  char* str = itostr_ts(port);
913  err = getaddrinfo(NULL, str, &hints, &address);
914  freeNull(str);
915  if (err!=0) {
916  socket_close(sofd);
917  return JBXL_NET_INFO_ERROR;
918  }
919 
920  err = bind(sofd, address->ai_addr, (int)address->ai_addrlen);
921  if (err<0) {
922  freeaddrinfo(address);
923  socket_close(sofd);
924  return JBXL_NET_BIND_ERROR;
925  }
926 
927  freeaddrinfo(address);
928  return sofd;
929 }
930 
931 
932 /*
933 int _tcp_connect(int sofd, char* hostname, int port, int family)
934 
935 IPv4/IPv6 のTCPのクライアントソケットを通して,サーバに接続する.
936 
937 この関数内で呼び出される関数: getaddrinfo(), connect()
938 
939 @param sofd ソケット記述子
940 @param hostname サーバ名
941 @param port サーバポート番号
942 @param family プロトコルファミリー(AF_INET/AF_INET6/AF_UNSPEC).
943  AF_UNSPEC の場合は先ずIPv6で接続を試み,不可ならIPv4で接続する.
944 
945 @retval 0以上 作成されたソケット記述子.
946 @retval JBXL_NET_INFO_ERROR ホスト情報の取得に失敗.
947 @retval JBXL_NET_CONNECT_ERROR 接続に失敗.
948  */
949 int _tcp_connect(int sofd, char* hostname, int port, int family)
950 {
951  int err;
952  struct addrinfo hints;
953  struct addrinfo* address;
954 
955  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
956 
957  memset(&hints, 0, sizeof(struct addrinfo));
958  hints.ai_family = family;
959  hints.ai_flags = AI_PASSIVE;
960  hints.ai_socktype = SOCK_STREAM;
961 
962  char* str = itostr_ts(port);
963  err = getaddrinfo(hostname, str, &hints, &address);
964  freeNull(str);
965  if (err!=0) {
966  socket_close(sofd);
967  return JBXL_NET_INFO_ERROR;
968  }
969 
970  err = connect(sofd, address->ai_addr, (int)address->ai_addrlen);
971  if (err<0) {
972  freeaddrinfo(address);
973  socket_close(sofd);
974  return JBXL_NET_CONNECT_ERROR;
975  }
976 
977  freeaddrinfo(address);
978  return 0;
979 }
980 
981 
995 int accept_intr(int sock, struct sockaddr* cl_addr, socklen_t* cdlen)
996 {
997  int nsofd = 0;
998 
999  do {
1000  nsofd = (int)accept(sock, cl_addr, cdlen);
1001  } while (nsofd==-1 && errno==EINTR);
1002 
1003  //if (nsofd<0) Error("accept_intr");
1004 
1005  return nsofd;
1006 }
1007 
1008 
1022 int socket_close(int sofd)
1023 {
1024  int err = -1;
1025 
1026  if (sofd>0) {
1027  #ifdef WIN32
1028  err = shutdown(sofd, 2);
1029  closesocket(sofd);
1030  #else
1031  err = shutdown(sofd, SHUT_RDWR);
1032  close(sofd);
1033  #endif
1034  }
1035  return err;
1036 }
1037 
1038 
1043 int set_nonblock_socket(int sock)
1044 {
1045 #ifdef WIN32
1046  u_long val = 1;
1047  ioctlsocket(sock, FIONBIO, &val);
1048 #else
1049  int val = fcntl(sock, F_GETFL, 0);
1050  if (val>=0) fcntl(sock, F_SETFL, val | O_NONBLOCK);
1051 #endif
1052  return sock;
1053 }
1054 
1055 
1060 int set_block_socket(int sock)
1061 {
1062 #ifdef WIN32
1063  u_long val = 0;
1064  ioctlsocket(sock, FIONBIO, &val);
1065 #else
1066  int val = fcntl(sock, F_GETFL, 0);
1067  if (val>=0) fcntl(sock, F_SETFL, val & ~O_NONBLOCK);
1068 #endif
1069  return sock;
1070 }
1071 
1072 
1073 
1075 // Valid Sockets
1076 //
1077 
1078 int get_valid_udp_socket(int min, int max, unsigned short* port)
1079 {
1080  int i, sock, range;
1081  struct addrinfo* address;
1082 
1083  range = max - min + 1;
1084  *port = rand()%range + min;
1085 
1086  i = 1;
1087  sock = udp_server_socket((int)*port, &address);
1088  while(sock<=0 && i<range) {
1089  (*port)++;
1090  if (*port>max) *port = ((int)*port)%max + min - 1;
1091  sock = udp_server_socket((int)*port, &address);
1092  i++;
1093  }
1094 
1095  if (sock<=0) *port = 0;
1096 
1097  freeaddrinfo(address);
1098  return sock;
1099 }
1100 
1101 
1102 int get_valid_tcp_server_socket(int min, int max, unsigned short* port)
1103 {
1104  int i, sock, range;
1105 
1106  range = max - min + 1;
1107  *port = rand()%range + min;
1108 
1109  i = 1;
1110  sock = tcp_server_socket((int)*port);
1111  while(sock<=0 && i<range) {
1112  (*port)++;
1113  if (*port>max) *port = ((int)*port)%max + min - 1;
1114  sock = tcp_server_socket((int)*port);
1115  i++;
1116  }
1117 
1118  if (sock<=0) *port = 0;
1119 
1120  return sock;
1121 }
1122 
1123 
1124 int get_valid_tcp_client_socket(int min, int max, char* hname, unsigned short sport, unsigned short* cport)
1125 {
1126  int i, sock, range;
1127 
1128  range = max - min + 1;
1129  *cport = rand()%range + min;
1130 
1131  i = 1;
1132  sock = tcp_client_bind_socket(hname, (int)sport, (int)*cport);
1133  while(sock<0 && i<range) {
1134  (*cport)++;
1135  if (*cport>max) *cport = ((int)*cport)%max + min - 1;
1136  sock = tcp_client_bind_socket(hname, (int)sport, (int)*cport);
1137  i++;
1138  }
1139 
1140  if (sock<=0) *cport = 0;
1141 
1142  return sock;
1143 }
1144 
1145 
1146 
1148 // Communication
1149 //
1150 
1166 int udp_recv(int sock, char* rmsg, int size, struct addrinfo* sv_addr)
1167 {
1168  int cc;
1169  socklen_t cadlen;
1170 
1171  if (sv_addr==NULL) return JBXL_ARGS_ERROR;
1172 
1173  cadlen = (int)sv_addr->ai_addrlen;
1174  memset(rmsg, 0, size);
1175  cc = recvfrom(sock, rmsg, size, 0, sv_addr->ai_addr, &cadlen);
1176 
1177  if (cc<0) cc = JBXL_NET_RECV_ERROR;
1178  return cc;
1179 }
1180 
1181 
1199 int udp_send(int sock, char* smsg, int size, struct addrinfo* sv_addr)
1200 {
1201  int cc;
1202 
1203  if (sv_addr==NULL) return JBXL_ARGS_ERROR;
1204 
1205  if (size<=0) size = (int)strlen(smsg);
1206  cc = sendto(sock, smsg, size, 0, sv_addr->ai_addr, (int)sv_addr->ai_addrlen);
1207 
1208  if (cc<0) cc = JBXL_NET_SEND_ERROR;
1209  return cc;
1210 }
1211 
1212 
1226 int tcp_recv(int sock, char* rmsg, int size)
1227 {
1228  int cc;
1229 
1230  memset(rmsg, 0, size);
1231  cc = recv(sock, rmsg, size, 0);
1232 
1233  if (cc<0) cc = JBXL_NET_RECV_ERROR;
1234  return cc;
1235 }
1236 
1237 
1252 int tcp_send(int sock, char* smsg, int size)
1253 {
1254  int cc;
1255 
1256  if (size<=0) size = (int)strlen(smsg);
1257  cc = send(sock, smsg, size, 0);
1258 
1259  if (cc<0) cc = JBXL_NET_SEND_ERROR;
1260  return cc;
1261 }
1262 
1263 
1283 int udp_recv_wait(int sock, char* rmsg, int size, struct addrinfo* sv_addr, int tm)
1284 {
1285  int cc;
1286 
1287  if (recv_wait(sock, tm)) {
1288  cc = udp_recv(sock, rmsg, size, sv_addr);
1289  }
1290  else {
1291  return JBXL_NET_RECV_TIMEOUT;
1292  }
1293 
1294  if (cc<0) cc = JBXL_NET_RECV_ERROR;
1295  return cc;
1296 }
1297 
1298 
1317 int tcp_recv_wait(int sock, char* mesg, int sz, int tm)
1318 {
1319  int cc;
1320 
1321  memset(mesg, 0, sz);
1322  if (recv_wait(sock, tm)) {
1323  cc = recv(sock, mesg, sz, 0);
1324  }
1325  else {
1326  return JBXL_NET_RECV_TIMEOUT;
1327  }
1328 
1329  if (cc<0) cc = JBXL_NET_RECV_ERROR;
1330  return cc;
1331 }
1332 
1333 
1345 int tcp_send_mesgln(int sock, char* mesg)
1346 {
1347  int cc, sz;
1348  char* buf;
1349 
1350  sz = (int)strlen(mesg) + 3; /* CR+LF+0x00 */
1351  buf = (char*)malloc(sz);
1352  if (buf==NULL) return JBXL_NET_SEND_ERROR;
1353 
1354  strncpy(buf, mesg, sz);
1355  strncat(buf, "\r\n", 2);
1356  cc = send(sock, buf, (int)strlen(buf), 0);
1357 
1358  free(buf);
1359  if (cc<0) cc = JBXL_NET_SEND_ERROR;
1360  return cc;
1361 }
1362 
1363 
1395 int tcp_recv_mstream(int sock, char* mesg, int sz, mstream* sb, int tm)
1396 {
1397  int cc;
1398  unsigned char* pp;
1399 
1400  if (mesg==NULL || sb==NULL) return JBXL_ARGS_ERROR;
1401  //memset(mesg, 0, sz);
1402 
1403  if (sb->buf==NULL) {
1404  *sb = make_mstream(RECVBUFSZ);
1405  if (sb->buf==NULL) return JBXL_MALLOC_ERROR;
1406  }
1407 
1408  while (sb->datano==0) {
1409  cc = tcp_recv_wait(sock, mesg, sz, tm);
1410  if (cc<=0) {
1411  if (cc<0) cc = JBXL_NET_RECV_ERROR;
1412  return cc;
1413  }
1414  put_mstream(sb, (unsigned char*)mesg);
1415  //memset(mesg, 0, sz);
1416  }
1417 
1418  pp = get_mstream(sb);
1419  if (pp==NULL) return JBXL_NET_BUF_ERROR;
1420  if (strlen((const char*)pp)>=(unsigned int)sz) {
1421  memcpy(mesg, pp, sz-1);
1422  free(pp);
1423  return JBXL_NET_BUFSZ_ERROR;
1424  }
1425  memcpy(mesg, pp, strlen((const char*)pp));
1426 
1427  free(pp);
1428  return (int)strlen(mesg);
1429 }
1430 
1431 
1443 int recv_wait(int sock, int tm)
1444 {
1445  int nd;
1446  fd_set mask;
1447  struct timeval timeout;
1448  time_t otm, ntm;
1449 
1450  otm = time(NULL);
1451  do {
1452  timeout.tv_sec = tm;
1453  timeout.tv_usec = 0;
1454  FD_ZERO(&mask);
1455  FD_SET(sock, &mask);
1456 
1457  //DEBUG_MESG("Waiting sock = %d for %ds.\n", sock, tm);
1458  nd = select(sock+1, &mask, NULL, NULL, &timeout);
1459  ntm = time(NULL);
1460  } while ((nd<0 || !FD_ISSET(sock, &mask)) && (int)(ntm-otm)<=tm);
1461 
1462  return FD_ISSET(sock, &mask);
1463 }
1464 
1465 
1480 int recv_wait_twin(int sock1, int sock2, int tm)
1481 {
1482  int ret = 0;
1483  int nm, nd;
1484  fd_set mask;
1485  struct timeval timeout;
1486  time_t otm, ntm;
1487 
1488  nm = Max(sock1, sock2);
1489  otm = time(NULL);
1490  do {
1491  timeout.tv_sec = tm;
1492  timeout.tv_usec = 0;
1493  FD_ZERO(&mask);
1494  FD_SET(sock1, &mask);
1495  FD_SET(sock2, &mask);
1496  nd = select(nm+1, &mask, NULL, NULL, &timeout);
1497  ntm = time(NULL);
1498  } while ((nd<0 || (!FD_ISSET(sock1, &mask) && !FD_ISSET(sock2, &mask))) && (int)(ntm-otm)<=tm);
1499 
1500  if (FD_ISSET(sock1, &mask)) ret += 1;
1501  if (FD_ISSET(sock2, &mask)) ret += 2;
1502 
1503  return ret;
1504 }
1505 
1506 
1518 int send_wait(int sock, int tm)
1519 {
1520  int nd;
1521  fd_set mask;
1522  struct timeval timeout;
1523  time_t otm, ntm;
1524 
1525  otm = time(NULL);
1526  do {
1527  timeout.tv_sec = tm;
1528  timeout.tv_usec = 0;
1529  FD_ZERO(&mask);
1530  FD_SET(sock, &mask);
1531 
1532  //DEBUG_MESG("Waiting sock = %d for %ds.\n", sock, tm);
1533  nd = select(sock+1, NULL, &mask, NULL, &timeout);
1534  ntm = time(NULL);
1535  } while ((nd<0 || !FD_ISSET(sock, &mask)) && (int)(ntm-otm)<=tm);
1536 
1537  return FD_ISSET(sock, &mask);
1538 }
1539 
1540 
1541 
1543 // IP address
1544 //
1545 
1556 struct sockaddr* make_sockaddr_bynum(unsigned char* addr, int port, int family)
1557 {
1558  struct sockaddr* sa = NULL;
1559  int len;
1560 
1561  len = sizeof(struct sockaddr);
1562  sa = (struct sockaddr*)malloc(len);
1563  if (sa==NULL) return NULL;
1564  memset(sa, 0, len);
1565 
1566  if (family==AF_INET6) { // IPv6
1567  struct sockaddr_in6* sa6;
1568  len = sizeof(struct sockaddr_in6);
1569  sa6 = (struct sockaddr_in6*)malloc(len);
1570  memset(sa6, 0, len);
1571  //
1572  sa6->sin6_family = AF_INET6;
1573  sa6->sin6_port = htons(port);
1574  //memcpy(sa6->sin6_addr.s6_addr, addr, 16);
1575  memcpy(&(sa6->sin6_addr), addr, 16);
1576  sa = (struct sockaddr*)sa6;
1577  }
1578  //
1579  else { // IPv4
1580  struct sockaddr_in* sa4;
1581  len = sizeof(struct sockaddr_in);
1582  sa4 = (struct sockaddr_in*)malloc(len);
1583  memset(sa4, 0, len);
1584  //
1585  sa4->sin_family = AF_INET;
1586  sa4->sin_port = htons(port);
1587  //memcpy(&(sa4->sin_addr.s_addr), addr, 4);
1588  memcpy(&(sa4->sin_addr), addr, 4);
1589  sa = (struct sockaddr*)sa4;
1590  }
1591 
1592  return sa;
1593 }
1594 
1595 
1605 struct sockaddr* make_sockaddr_bystr(const char* addr, int port)
1606 {
1607  int err, family = -1;
1608  char* pp;
1609  unsigned char num[16];
1610  struct sockaddr* sa;
1611 
1612  pp = strstr((char*)addr, ".");
1613  if (pp!=NULL) {
1614  family = AF_INET;
1615  }
1616  else {
1617  pp = strstr((char*)addr, ":");
1618  if (pp!=NULL) {
1619  family = AF_INET6;
1620  }
1621  }
1622  if (family<0) return NULL;
1623 
1624  memset(num, 0, 16);
1625  err = inet_pton(family, addr, num);
1626  if (err!=1) return NULL;
1627 
1628  sa = make_sockaddr_bynum(num, port, family);
1629  return sa;
1630 }
1631 
1632 
1642 char* get_hostname_bynum(unsigned char* num, int family)
1643 {
1644  int len, err;
1645  char htemp[LNAME];
1646  char* hname = NULL;
1647  struct sockaddr* sa;
1648 
1649  if (num==NULL) return NULL;
1650  if (family!=AF_INET6) family = AF_INET6;
1651 
1652  sa = make_sockaddr_bynum(num, 0, family);
1653  if (sa==NULL) return NULL;
1654 
1655  if (family==AF_INET6) len = sizeof(struct sockaddr_in6);
1656  else len = sizeof(struct sockaddr_in);
1657  err = getnameinfo(sa, len, htemp, LNAME-1, NULL, 0, 0);
1658  if (err!=0) {
1659  free(sa);
1660  return NULL;
1661  }
1662 
1663  len = (int)strlen(htemp);
1664  hname = (char*)malloc(len+1);
1665  if (hname==NULL) {
1666  free(sa);
1667  return NULL;
1668  }
1669 
1670  free(sa);
1671  memcpy(hname, htemp, len+1);
1672  return hname;
1673 }
1674 
1675 
1684 char* get_hostname_bystr(const char* addr)
1685 {
1686  int len, err;
1687  char htemp[LNAME];
1688  char* hname = NULL;
1689  struct sockaddr* sa;
1690 
1691  if (addr==NULL) return NULL;
1692 
1693  sa = make_sockaddr_bystr(addr, 0);
1694  if (sa==NULL) return NULL;
1695 
1696  if (sa->sa_family==AF_INET6) len = sizeof(struct sockaddr_in6);
1697  else len = sizeof(struct sockaddr_in);
1698  err = getnameinfo(sa, len, htemp, LNAME-1, NULL, 0, 0);
1699  if (err!=0) {
1700  free(sa);
1701  return NULL;
1702  }
1703 
1704  len = (int)strlen(htemp);
1705  hname = (char*)malloc(len+1);
1706  if (hname==NULL) {
1707  free(sa);
1708  return NULL;
1709  }
1710 
1711  free(sa);
1712  memcpy(hname, htemp, len+1);
1713  return hname;
1714 }
1715 
1716 
1729 char* _get_hostname(const char* addr, int family)
1730 {
1731  int len, err;
1732  char htemp[LNAME];
1733  char* hname = NULL;
1734 
1735  struct addrinfo hints;
1736  struct addrinfo* address;
1737  struct sockaddr* sa;
1738 
1739  if (addr==NULL) return NULL;
1740  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
1741 
1742  memset(&hints, 0, sizeof(struct addrinfo));
1743  hints.ai_family = family;
1744  hints.ai_flags = AI_PASSIVE;
1745  hints.ai_socktype = SOCK_DGRAM;
1746 
1747  err = getaddrinfo(addr, 0, &hints, &address);
1748  if (err!=0 || address==NULL) return NULL;
1749  sa = (struct sockaddr*)address->ai_addr;
1750 
1751  if (sa->sa_family==AF_INET6) len = sizeof(struct sockaddr_in6);
1752  else len = sizeof(struct sockaddr_in);
1753  err = getnameinfo(sa, len, htemp, LNAME-1, NULL, 0, 0);
1754  if (err!=0) {
1755  freeaddrinfo(address);
1756  return NULL;
1757  }
1758 
1759  len = (int)strlen(htemp);
1760  hname = (char*)malloc(len+1);
1761  if (hname==NULL) {
1762  freeaddrinfo(address);
1763  return NULL;
1764  }
1765 
1766  freeaddrinfo(address);
1767  memcpy(hname, htemp, len+1);
1768  return hname;
1769 }
1770 
1771 
1781 char* get_ipaddr_byname(const char* hostname, int family)
1782 {
1783  int len;
1784  char* ip;
1785  unsigned char* ipnum;
1786 
1787  if (family!=AF_INET6) family = AF_INET;
1788 
1789  ipnum = get_ipaddr_byname_num(hostname, family); // 4Byte or 16Byte
1790  if (ipnum==NULL) return NULL;
1791 
1792  if (family==AF_INET6) len = LEN_IPADDR6;
1793  else len = LEN_IPADDR;
1794  ip = (char*)malloc(len);
1795  if (ip==NULL) return NULL;
1796  memset(ip, 0, len);
1797 
1798  inet_ntop(family, ipnum, ip, len);
1799  free(ipnum);
1800  return ip;
1801 }
1802 
1803 
1813 unsigned char* get_ipaddr_byname_num(const char* hostname, int family)
1814 {
1815  int err;
1816  struct addrinfo hints;
1817  struct addrinfo* address;
1818  unsigned char* ip = NULL;
1819  unsigned char* pp = NULL;
1820 
1821  if (family!=AF_INET6) family = AF_INET;
1822 
1823  memset(&hints, 0, sizeof(struct addrinfo));
1824  hints.ai_socktype = SOCK_STREAM;
1825  hints.ai_family = family;
1826 
1827  err = getaddrinfo(hostname, NULL, &hints, &address);
1828  if (err!=0 || address==NULL) return NULL;
1829 
1830  if (family==AF_INET6) { // IPv6
1831  ip = (unsigned char*)malloc(16);
1832  if (ip!=NULL) {
1833  //pp = (unsigned char*)(((struct sockaddr_in6*)(address->ai_addr))->sin6_addr.s6_addr); // unsigned char s6_addr[16];
1834  pp = (unsigned char*)&(((struct sockaddr_in6*)(address->ai_addr))->sin6_addr); // unsigned char s6_addr[16];
1835  memcpy(ip, pp, 16);
1836  }
1837  }
1838  //
1839  if (pp==NULL) { // IPv4
1840  ip = (unsigned char*)malloc(4);
1841  if (ip!=NULL) {
1842  //pp = (unsigned char*)(&((struct sockaddr_in*)(address->ai_addr))->sin_addr.s_addr); // ulong s_addr;
1843  pp = (unsigned char*)&(((struct sockaddr_in*)(address->ai_addr))->sin_addr); // ulong s_addr;
1844  memcpy(ip, pp, 4);
1845  }
1846  }
1847 
1848  freeaddrinfo(address);
1849  return ip;
1850 }
1851 
1852 
1853 #ifdef WIN32
1854 
1855 #include <iphlpapi.h>
1856 #pragma comment( lib, "iphlpapi.lib" )
1857 
1858 char* get_myipaddr(int family)
1859 {
1860  int len, prefix, fndflg;
1861  char* ipa;
1862  char address[LNAME];
1863  IP_ADAPTER_ADDRESSES* pp, * pl;
1864  IP_ADAPTER_UNICAST_ADDRESS* pu;
1865 
1866  len = LEN_IPADDR6;
1867  if (family != AF_INET6) {
1868  len = LEN_IPADDR;
1869  family = AF_INET;
1870  }
1871 
1872  ULONG flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
1873  DWORD err, size = 0;
1874 
1875  err = GetAdaptersAddresses(family, flags, NULL, NULL, &size);
1876  if (err != ERROR_BUFFER_OVERFLOW) return NULL;
1877 
1878  pp = (IP_ADAPTER_ADDRESSES*)malloc(size);
1879  if (pp == NULL) return NULL;
1880  memset(pp, 0, size);
1881  err = GetAdaptersAddresses(family, flags, NULL, pp, &size);
1882  if (err != ERROR_SUCCESS) {
1883  free(pp);
1884  return NULL;
1885  }
1886 
1887  prefix = 0;
1888  fndflg = OFF;
1889  pl = pp;
1890  while (pl != NULL) {
1891  pu = pl->FirstUnicastAddress;
1892  while (pu != NULL) {
1893  if ((long)pu->ValidLifetime >= 0) {
1894  prefix = pu->OnLinkPrefixLength;
1895  struct sockaddr* sa = pu->Address.lpSockaddr;
1896  if (family == AF_INET6) {
1897  unsigned char* ch = (unsigned char*)&((struct sockaddr_in6*)sa)->sin6_addr;
1898  if (!(ch[0] == 0xfe && ch[1] == 0x80)) {
1899  fndflg = ON;
1900  inet_ntop(AF_INET6, ch, address, LNAME);
1901  break;
1902  }
1903  }
1904  else {
1905  unsigned char* ch = (unsigned char*)&((struct sockaddr_in*)sa)->sin_addr;
1906  if (!(ch[0] == 169 && ch[1] == 254)) {
1907  fndflg = ON;
1908  inet_ntop(AF_INET, ch, address, LNAME);
1909  break;
1910  }
1911  }
1912  }
1913  pu = pu->Next;
1914  }
1915  if (fndflg == ON) break;
1916  pl = pl->Next;
1917  }
1918  free(pp);
1919 
1920  int addrlen = (int)strlen(address);
1921  snprintf(address + addrlen, LNAME - addrlen - 1, "/%d\0", prefix);
1922 
1923  ipa = (char*)malloc(len);
1924  if (ipa == NULL) return NULL;
1925  memset(ipa, 0, len);
1926  memcpy(ipa, address, strlen(address)+1);
1927 
1928  return ipa;
1929 }
1930 
1931 #else
1932 
1933 #include <ifaddrs.h>
1934 
1935 char* get_myipaddr(int family)
1936 {
1937  int err, flg, len, lst;
1938  struct ifaddrs *if_addr;
1939  struct ifaddrs *ifap;
1940 
1941  char htemp[LNAME];
1942  char* haddr = NULL;
1943 
1944  if (family!=AF_INET6) {
1945  family = AF_INET;
1946  len = sizeof(struct sockaddr_in);
1947  }
1948  else {
1949  len = sizeof(struct sockaddr_in6);
1950  }
1951  //
1952  err = getifaddrs(&if_addr);
1953  if (err<0) return NULL;
1954 
1955  flg = OFF;
1956  ifap = if_addr;
1957  while(ifap!=NULL) {
1958  if (ifap->ifa_addr!=NULL && family==ifap->ifa_addr->sa_family) {
1959  memset(htemp, 0, LNAME);
1960  err = getnameinfo(ifap->ifa_addr, len, htemp, LNAME, NULL, 0, NI_NUMERICHOST);
1961  if (err==0) {
1962  if (family==AF_INET) {
1963  if (strncmp(htemp, "127.0.0.1", 9) && strncmp(htemp, "169.254.", 8)) {
1964  struct sockaddr_in* sa = (struct sockaddr_in*)(ifap->ifa_addr);
1965  unsigned char check = ((unsigned char*)&(sa->sin_addr))[0] & 0xf0;
1966  if (check != 0xe) { // not multi cast
1967  flg = ON;
1968  break;
1969  }
1970  }
1971  }
1972  else {
1973  if (strncmp(htemp, "::1", 3) && strncasecmp(htemp, "fe80:", 5)) {
1974  flg = ON;
1975  break;
1976  }
1977  }
1978  }
1979  }
1980  ifap=ifap->ifa_next;
1981  }
1982  if (flg==OFF) {
1983  freeifaddrs(if_addr);
1984  return NULL;
1985  }
1986 
1987  freeifaddrs(if_addr);
1988 
1989  lst = strlen(htemp);
1990  if (LNAME-lst-1<=0) return NULL;
1991  htemp[lst++] = '/';
1992 
1993  err = getnameinfo(ifap->ifa_netmask, len, &(htemp[lst]), LNAME-lst, NULL, 0, NI_NUMERICHOST);
1994  if (err!=0) return NULL;
1995 
1996  len = strlen(htemp) + 1;
1997  haddr = (char*)malloc(len);
1998  if (haddr==NULL) return NULL;
1999  memset(haddr, 0, len);
2000  memcpy(haddr, htemp, len-1);
2001 
2002  return haddr;
2003 }
2004 
2005 #endif
2006 
2007 
2008 unsigned char* get_myipaddr_num(int family)
2009 {
2010  int len, err;
2011  char* pos;
2012  char* htemp;
2013  unsigned char* haddr = NULL;
2014 
2015  if (family!=AF_INET6) family = AF_INET;
2016 
2017  //
2018  htemp = get_myipaddr(family);
2019  if (htemp==NULL) return NULL;
2020 
2021  pos = strstr(htemp, "/");
2022  if (pos==NULL) {
2023  free(htemp);
2024  return NULL;
2025  }
2026  *pos = '\0';
2027 
2028  if (family==AF_INET6) len = 16;
2029  else len = 4;
2030  haddr = (unsigned char*)malloc(len*2);
2031  if (haddr==NULL) {
2032  free(htemp);
2033  return NULL;
2034  }
2035  memset(haddr, 0, len*2);
2036 
2037  err = inet_pton(family, htemp, haddr);
2038  if (err!=1) {
2039  free(haddr);
2040  return NULL;
2041  }
2042 
2043  err = inet_pton(family, pos+1, haddr+len);
2044  free(htemp);
2045  if (err!=1) {
2046  free(haddr);
2047  return NULL;
2048  }
2049 
2050  return haddr;
2051 }
2052 
2053 
2064 char* _get_localip_bydest(const char* dest, int family)
2065 {
2066  int sofd, err, len;
2067  char* addr;
2068  unsigned char* pp;
2069  struct addrinfo hints;
2070  struct addrinfo* sv_addr;
2071  struct sockaddr* sa;
2072 
2073  if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
2074 
2075  memset(&hints, 0, sizeof(struct addrinfo));
2076  hints.ai_family = family;
2077  hints.ai_flags = AI_PASSIVE;
2078  hints.ai_socktype = SOCK_DGRAM;
2079 
2080  err = getaddrinfo(dest, "9999", &hints, &sv_addr);
2081  if (err != 0) {
2082  // if 10093 error is occured, execute init_network()
2083  return NULL;
2084  }
2085 
2086  sofd = (int)socket(sv_addr->ai_family, sv_addr->ai_socktype, sv_addr->ai_protocol);
2087  if (sofd<0) {
2088  freeaddrinfo(sv_addr);
2089  return NULL;
2090  }
2091 
2092  err = connect(sofd, sv_addr->ai_addr, (int)sv_addr->ai_addrlen);
2093  if (err<0) {
2094  socket_close(sofd);
2095  return NULL;
2096  }
2097 
2098  if (sv_addr->ai_family==AF_INET6) {
2099  len = sizeof(struct sockaddr_in6);
2100  sa = (struct sockaddr*)malloc(len);
2101  memset(sa, 0, len);
2102  err = getsockname(sofd, (struct sockaddr *)sa, (socklen_t*)&len);
2103  //pp = (unsigned char*)(((struct sockaddr_in6*)sa)->sin6_addr.s6_addr);
2104  pp = (unsigned char*)&(((struct sockaddr_in6*)sa)->sin6_addr);
2105  len = LEN_IPADDR6;
2106  }
2107  else {
2108  len = sizeof(struct sockaddr_in);
2109  sa = (struct sockaddr*)malloc(len);
2110  memset(sa, 0, len);
2111  err = getsockname(sofd, (struct sockaddr *)sa, (socklen_t*)&len);
2112  //pp = (unsigned char*)(&((struct sockaddr_in*)sa)->sin_addr.s_addr);
2113  pp = (unsigned char*)&(((struct sockaddr_in*)sa)->sin_addr);
2114  len = LEN_IPADDR;
2115  }
2116 
2117  freeaddrinfo(sv_addr);
2118  socket_close(sofd);
2119 
2120  addr = (char*)malloc(len);
2121  if (addr==NULL) {
2122  free(sa);
2123  return NULL;
2124  }
2125  memset(addr, 0, len);
2126 
2127  inet_ntop(family, pp, addr, len);
2128  free(sa);
2129  return addr;
2130 }
2131 
2132 
2142 char* get_mynetaddr(int family)
2143 {
2144  int len;
2145  char* net;
2146  unsigned char* pp;
2147 
2148  len = LEN_IPADDR6;
2149  if (family!=AF_INET6) {
2150  len = LEN_IPADDR;
2151  family = AF_INET;
2152  }
2153 
2154  pp = get_mynetaddr_num(family);
2155  if (pp==NULL) return NULL;
2156 
2157  net = (char*)malloc(len);
2158  if (net==NULL) {
2159  free(pp);
2160  return NULL;
2161  }
2162  memset(net, 0, len);
2163 
2164  inet_ntop(family, pp, net, len);
2165  free(pp);
2166  return net;
2167 }
2168 
2169 
2177 unsigned char* get_mynetaddr_num(int family)
2178 {
2179  int i, len;
2180  unsigned char* net;
2181  unsigned char* ip;
2182  unsigned char* mk;
2183 
2184  len = 16;
2185  if (family!=AF_INET6) len = 4;
2186 
2187  ip = get_myipaddr_num(family);
2188  if (ip==NULL) return NULL;
2189  mk = &(ip[len]);
2190 
2191  net = (unsigned char*)malloc(len);
2192  if (net==NULL) {
2193  free(ip);
2194  return NULL;
2195  }
2196  for (i=0; i<len; i++) net[i] = ip[i] & mk[i];
2197 
2198  free(ip);
2199  return net;
2200 }
2201 
2202 
2203 
2205 // Tools
2206 //
2207 
2219 int is_same_sockaddr(struct sockaddr* addr1, struct sockaddr* addr2)
2220 {
2221  int i, len;
2222  unsigned char *p1, *p2;
2223  struct sockaddr_in *sa1, *sa2;
2224  struct sockaddr_in6 *sa61, *sa62;
2225 
2226  if (addr1==NULL || addr2==NULL) return FALSE;
2227  if (addr1->sa_family!=addr2->sa_family) return FALSE;
2228 
2229  if (addr1->sa_family==AF_INET) {
2230  sa1 = (struct sockaddr_in*)addr1;
2231  sa2 = (struct sockaddr_in*)addr2;
2232 
2233  p1 = (unsigned char*)&(sa1->sin_addr);
2234  p2 = (unsigned char*)&(sa2->sin_addr);
2235  len = sizeof(sa1->sin_addr);
2236  for (i=0; i<len; i++) {
2237  if (p1[i]!=p2[i]) return FALSE;
2238  }
2239 
2240  p1 = (unsigned char*)&(sa1->sin_port);
2241  p2 = (unsigned char*)&(sa2->sin_port);
2242  len = sizeof(sa1->sin_port);
2243  for (i=0; i<len; i++) {
2244  if (p1[i]!=p2[i]) return FALSE;
2245  }
2246  }
2247  else {
2248  sa61 = (struct sockaddr_in6*)addr1;
2249  sa62 = (struct sockaddr_in6*)addr2;
2250 
2251  p1 = (unsigned char*)&(sa61->sin6_addr);
2252  p2 = (unsigned char*)&(sa62->sin6_addr);
2253  len = sizeof(sa61->sin6_addr);
2254  for (i=0; i<len; i++) {
2255  if (p1[i]!=p2[i]) return FALSE;
2256  }
2257 
2258  p1 = (unsigned char*)&(sa61->sin6_port);
2259  p2 = (unsigned char*)&(sa62->sin6_port);
2260  len = sizeof(sa61->sin6_port);
2261  for (i=0; i<len; i++) {
2262  if (p1[i]!=p2[i]) return FALSE;
2263  }
2264  }
2265 
2266  return TRUE;
2267 }
2268 
2269 
2283 int is_same_network(char* addr1, char* addr2, char* mask)
2284 {
2285  int ret, family;
2286  unsigned char* a1;
2287  unsigned char* a2;
2288  unsigned char* mk;
2289 
2290  if (addr1==NULL || addr2==NULL || mask==NULL) return FALSE;
2291 
2292  a1 = (unsigned char*)strstr(addr1, ":");
2293  a2 = (unsigned char*)strstr(addr1, ":");
2294  mk = (unsigned char*)strstr(mask, ":");
2295  if (a1!=NULL && a2!=NULL && mask!=NULL) family = AF_INET6;
2296  else if (a1==NULL && a2==NULL && mask==NULL) family = AF_INET;
2297  else return FALSE;
2298 
2299  a1 = to_address_num(addr1, 0, 0, family);
2300  a2 = to_address_num(addr2, 0, 0, family);
2301  mk = to_address_num(mask, 0, 0, family);
2302  if (a1==NULL || a2==NULL || mk==NULL) {
2303  freeNull(a1);
2304  freeNull(a2);
2305  freeNull(mk);
2306  return FALSE;
2307  }
2308 
2309  ret = is_same_network_num(a1, a2, mk, family);
2310  freeNull(a1);
2311  freeNull(a2);
2312  freeNull(mk);
2313 
2314  return ret;
2315 }
2316 
2317 
2331 int is_same_network_num(unsigned char* addr1, unsigned char* addr2, unsigned char* mask, int family)
2332 {
2333  int i, len;
2334 
2335  if (addr1==NULL || addr2==NULL) return FALSE;
2336 
2337  len = 16;
2338  if (family!=AF_INET6) len = 4;
2339 
2340  if (mask==NULL) {
2341  for (i=0; i<len; i++) {
2342  if (addr1[i] != addr2[i]) return FALSE;
2343  }
2344  }
2345  else {
2346  for (i=0; i<len; i++) {
2347  if ((addr1[i] & mask[i]) != (addr2[i] & mask[i])) return FALSE;
2348  }
2349  }
2350  return TRUE;
2351 }
2352 
2353 
2373 unsigned char* to_address_num(char* addr, int mode, int mask, int family)
2374 {
2375  unsigned char* num;
2376  char deli;
2377  char* ps;
2378  char* pc;
2379  char* uc = NULL;
2380  int i, len;
2381 
2382  if (addr==NULL) return NULL;
2383 
2384  deli = ':';
2385  len = 16;
2386  if (family!=AF_INET6) {
2387  deli = ':';
2388  len = 4;
2389  family = AF_INET;
2390  }
2391 
2392  // mode チェック
2393  if (mode==0) {
2394  i = (int)strlen(addr) - 1;
2395  while (i>0 && addr[i]==deli) i--;
2396  if (i>=0) {
2397  if (isalpha(addr[i])) return NULL;
2398  }
2399  }
2400 
2401  num = (unsigned char*)malloc(len*2);
2402  if (num==NULL) return NULL;
2403  memset(num, 0, len*2);
2404 
2405  // IPアドレス部の変換
2406  ps = awk(addr, '/', 1);
2407  if (ps==NULL) {
2408  free(num);
2409  return NULL;
2410  }
2411 
2412  uc = (char*)get_ipaddr_byname_num(ps, family);
2413  free(ps);
2414  if (uc==NULL) return NULL;
2415  memcpy(num, uc, len);
2416  free(uc);
2417 
2418  if (mask==0) return num;
2419 
2420  // 以下,サブネットマスク処理
2421  ps = awk(addr, '/', 2);
2422 
2423  // サブネットマスク部が省略された場合の処理
2424  if (ps==NULL) {
2425  int f = OFF;
2426  for (i=len-1; i>=0; i--) {
2427  if (num[i]!=0 || f==ON) {
2428  num[i+len] = 0xff;
2429  f = ON;
2430  }
2431  else {
2432  num[i+len] = 0;
2433  }
2434  }
2435  return num;
2436  }
2437 
2438  // サブネットマスク部の処理
2439  if (family==AF_INET) {
2440  for (i=0; i<len; i++) {
2441  pc = awk(ps, deli, i+1);
2442  if (pc==NULL) break;
2443  num[i+len] = (unsigned char)atoi(pc);
2444  free(pc);
2445  }
2446  }
2447  else {
2448  for (i=0; i<len/2; i++) {
2449  pc = awk(ps, deli, i+1);
2450  if (pc==NULL) break;
2451  int nn = strtol(pc, NULL, 16);
2452  num[len+i*2] = (unsigned char)(nn/256);
2453  num[len+i*2+1] = (unsigned char)(nn%256);
2454  free(pc);
2455  }
2456  }
2457  free(ps);
2458 
2459  // CIDER形式対応
2460  if (family==AF_INET && num[4]<=32) {
2461  int nn, cl = (int)num[4];
2462  for (i=0; i<4; i++) {
2463  nn = 8 - Max(0, Min(8, cl-8*i));
2464  num[i+4] = 0xff<<nn;
2465  }
2466  }
2467  else if (family==AF_INET6 && num[16]<16) {
2468  int nn, cl = num[16]%16*100 + num[17]/16*10 + num[17]%16;
2469  for (i=0; i<16; i++) {
2470  nn = 8 - Max(0, Min(8, cl-8*i));
2471  num[i+16] = 0xff<<nn;
2472  }
2473  }
2474 
2475  return num;
2476 }
2477 
2478 
2496 char* to_address_char(unsigned char* addr, int mask, int family)
2497 {
2498  int mlen, plen, slen;
2499  char* str;
2500  if (addr==NULL) return NULL;
2501 
2502  plen = 16;
2503  mlen = LEN_IPADDR6*2;
2504  if (family!=AF_INET6) {
2505  family = AF_INET;
2506  plen = 4;
2507  mlen = LEN_IPADDR*2;
2508  }
2509 
2510  str = (char*)malloc(mlen);
2511  if (str==NULL) return NULL;
2512  memset(str, 0, mlen);
2513 
2514  inet_ntop(family, addr, str, mlen);
2515  if (mask==0) return str;
2516 
2517  slen = (int)strlen(str);
2518  str[slen++] = '/';
2519  inet_ntop(family, addr+plen, str+slen, mlen-slen);
2520 
2521  return str;
2522 }
2523 
2524 
2534 void udp_hole_punching(int sock, struct addrinfo* addr, int nm)
2535 {
2536  char data[LBUF];
2537 
2538  if (nm<=0) nm = 4; // for SLVoice
2539  else if (nm>LBUF) nm = LBUF;
2540 
2541  memset(data, 0, nm);
2542  udp_send(sock, data, nm, addr);
2543 
2544  return;
2545 }
2546 
2547 
2548 
2550 // for IPv4 only
2551 //
2552 
2553 int udp_client_socket_sockaddr_in(char* hostname, int port, struct sockaddr_in* sv_addr)
2554 {
2555  int sofd;
2556  struct hostent* shost = NULL;
2557 
2558  if (sv_addr==NULL) return JBXL_NET_INFO_ERROR;
2559 
2560  sofd = (int)socket(AF_INET, SOCK_DGRAM, 0);
2561  if (sofd<0) return JBXL_NET_SOCKET_ERROR;
2562 
2563  shost = gethostbyname(hostname);
2564  if (shost==NULL) {
2565  socket_close(sofd);
2566  return JBXL_NET_INFO_ERROR;
2567  }
2568 
2569  memset(sv_addr, 0, sizeof(*sv_addr));
2570  sv_addr->sin_family = AF_INET;
2571  sv_addr->sin_port = htons(port);
2572  memcpy(&(sv_addr->sin_addr), shost->h_addr, shost->h_length);
2573 
2574  return sofd;
2575 }
2576 
2577 
2578 int udp_recv_sockaddr_in(int sock, char* rmsg, int size, struct sockaddr_in* sv_addr)
2579 {
2580  int cc;
2581  socklen_t cadlen;
2582 
2583  if (sv_addr==NULL) return 0;
2584 
2585  cadlen = sizeof(*sv_addr);
2586  memset(rmsg, 0, size);
2587  cc = recvfrom(sock, rmsg, size, 0, (struct sockaddr*)sv_addr, &cadlen);
2588 
2589  return cc;
2590 }
2591 
2592 
2593 int udp_send_sockaddr_in(int sock, char* smsg, int size, struct sockaddr_in* sv_addr)
2594 {
2595  int cc;
2596 
2597  if (sv_addr==NULL) return 0;
2598  if (size<=0) size = (int)strlen(smsg);
2599  cc = sendto(sock, smsg, size, 0, (struct sockaddr*)sv_addr, sizeof(*sv_addr));
2600 
2601  return cc;
2602 }
2603 
2604 
2605 int udp_recv_wait_sockaddr_in(int sock, char* rmsg, int size, struct sockaddr_in* sv_addr, int tm)
2606 {
2607  int cc;
2608 
2609  if (recv_wait(sock, tm)) {
2610  cc = udp_recv_sockaddr_in(sock, rmsg, size, sv_addr);
2611  }
2612  else {
2613  return JBXL_NET_RECV_TIMEOUT;
2614  }
2615  return cc;
2616 }
2617 
2618 
2619 struct sockaddr_in get_sockaddr_in(char* hostname, unsigned short cport)
2620 {
2621  struct sockaddr_in ss_addr;
2622  struct hostent *shost;
2623 
2624  // Serverの情報を ss_addrに格納
2625  shost = gethostbyname(hostname);
2626  if (shost==NULL) {
2627  ss_addr.sin_family = 0;
2628  ss_addr.sin_port = 0;
2629  return ss_addr;
2630  }
2631 
2632  memset(&ss_addr, 0, sizeof(ss_addr));
2633  ss_addr.sin_family = AF_INET;
2634  ss_addr.sin_port = htons(cport);
2635  memcpy(&(ss_addr.sin_addr), shost->h_addr, shost->h_length);
2636 
2637  return ss_addr;
2638 }
2639 
2640 
2641 struct sockaddr_in get_sockaddr_in_bynum(char* ipnum, unsigned short cport)
2642 {
2643  struct sockaddr_in ss_addr;
2644 
2645  memset(&ss_addr, 0, sizeof(ss_addr));
2646  ss_addr.sin_family = AF_INET;
2647  ss_addr.sin_port = htons(cport);
2648  memcpy(&(ss_addr.sin_addr), ipnum, 4);
2649 
2650  return ss_addr;
2651 }
2652 
2653 
2654 struct sockaddr_in get_local_sockaddr_in(unsigned short cport)
2655 {
2656  struct sockaddr_in ss_addr;
2657  struct hostent *shost;
2658 
2659  // localhost の情報を ss_addrに格納
2660  shost = gethostbyname("127.0.0.1");
2661 
2662  memset(&ss_addr, 0, sizeof(ss_addr));
2663  ss_addr.sin_family = AF_INET;
2664  ss_addr.sin_port = htons(cport);
2665  memcpy(&(ss_addr.sin_addr), shost->h_addr, shost->h_length);
2666 
2667  return ss_addr;
2668 }
2669 
2670 
2685 char* get_ipaddr_ipv4(struct in_addr sin_addr)
2686 {
2687  char* ip;
2688  unsigned char* pp;
2689 
2690  pp = (unsigned char*)&(sin_addr);
2691  if (pp[0]==0) return NULL;
2692 
2693  ip = (char*)malloc(16);
2694  if (ip==NULL) return NULL;
2695  memset(ip, 0, 16);
2696 
2697  snprintf(ip, 15, "%d.%d.%d.%d", pp[0], pp[1], pp[2], pp[3]);
2698 
2699  return ip;
2700 }
2701 
2702 
2717 unsigned char* get_ipaddr_num_ipv4(struct in_addr sin_addr)
2718 {
2719  unsigned char* ip;
2720  unsigned char* pp;
2721 
2722  ip = (unsigned char*)malloc(4);
2723  if (ip==NULL) return NULL;
2724 
2725  pp = (unsigned char*)&(sin_addr);
2726  memcpy(ip, pp, 4);
2727 
2728  return ip;
2729 }
2730 
2731 
2741 void udp_hole_punching_sockaddr_in(int sock, struct sockaddr_in addr, int nm)
2742 {
2743  char data[LBUF];
2744 
2745  if (nm<=0) nm = 4; // for SLVoice
2746  else if (nm>LBUF) nm = LBUF;
2747 
2748  memset(data, 0, nm);
2749  udp_send_sockaddr_in(sock, data, nm, &addr);
2750 
2751  return;
2752 }
2753 
2754 
#define LNAME
Definition: common.h:153
#define LEN_IPADDR
strlen("AAA.BBB.CCC.DDD") + '\0' + 1(予備)
Definition: common.h:172
#define Min(x, y)
Definition: common.h:250
#define OFF
Definition: common.h:231
#define Max(x, y)
Definition: common.h:247
#define RECVBUFSZ
256K
Definition: common.h:134
#define LBUF
Definition: common.h:146
#define TRUE
Definition: common.h:226
#define FALSE
Definition: common.h:223
#define ON
Definition: common.h:230
#define LEN_IPADDR6
strlen("1111:2222:333:4444:5555:6666:7777:8888") + '\0' + 1(予備)
Definition: common.h:173
#define JBXL_NET_INFO_ERROR
ホスト情報の取得に失敗
Definition: jbxl_state.h:67
#define JBXL_NET_SOCKET_ERROR
ソケットの作成に失敗
Definition: jbxl_state.h:64
#define JBXL_NET_LISTEN_ERROR
リッスンに失敗
Definition: jbxl_state.h:69
#define JBXL_ARGS_ERROR
不正な引数(NULLなど)
Definition: jbxl_state.h:42
#define JBXL_NET_CONNECT_ERROR
接続に失敗
Definition: jbxl_state.h:68
#define JBXL_NET_SEND_ERROR
データの送信エラー
Definition: jbxl_state.h:71
#define JBXL_NET_OPTION_ERROR
オプションの設定に失敗
Definition: jbxl_state.h:65
#define JBXL_NET_BIND_ERROR
バインドに失敗
Definition: jbxl_state.h:66
#define JBXL_NET_BUFSZ_ERROR
受信バッファの長さが足りない.はみ出したデータは捨てられた
Definition: jbxl_state.h:73
#define JBXL_NET_RECV_ERROR
データの受信エラー
Definition: jbxl_state.h:70
#define JBXL_MALLOC_ERROR
メモリ確保エラー
Definition: jbxl_state.h:41
#define JBXL_NET_BUF_ERROR
受信バッファにデータは存在するはずだが,原因不明の理由により獲得に失敗した
Definition: jbxl_state.h:72
#define JBXL_NET_RECV_TIMEOUT
受信タイムアウト
Definition: jbxl_state.h:76
unsigned char ** buf
Definition: jpeg_tool.h:96
unsigned char unsigned long * len
Definition: jpeg_tool.h:96
int udp_recv_wait_sockaddr_in(int sock, char *rmsg, int size, struct sockaddr_in *sv_addr, int tm)
use udp_recv_wait()
Definition: network.c:2605
int tcp_recv_wait(int sock, char *mesg, int sz, int tm)
Definition: network.c:1317
char * get_myipaddr(int family)
自分の [IPv4/IPv6 ネットワークアドレス]/[ネットマスク](文字列)
Definition: network.c:1935
int tcp_send(int sock, char *smsg, int size)
Definition: network.c:1252
char * to_address_char(unsigned char *addr, int mask, int family)
[IPv4/IPv6 アドレス],[ネットマスク](数字8/32Byte)→ IPv4/IPv6 アドレス(文字列)
Definition: network.c:2496
char * get_mynetaddr(int family)
自分の IPv4/IPv6 ネットワークアドレス(文字列)
Definition: network.c:2142
struct sockaddr_in get_local_sockaddr_in(unsigned short cport)
Definition: network.c:2654
int udp_send_sockaddr_in(int sock, char *smsg, int size, struct sockaddr_in *sv_addr)
use udp_send()
Definition: network.c:2593
int send_wait(int sock, int tm)
Definition: network.c:1518
int init_network(void)
Definition: network.c:26
int tcp_recv_mstream(int sock, char *mesg, int sz, mstream *sb, int tm)
Definition: network.c:1395
unsigned char * get_ipaddr_num_ipv4(struct in_addr sin_addr)
構造体 → IPv4 アドレス(バイナリ) use get_ipaddr_byname_num()
Definition: network.c:2717
int _udp_server_socket(int port, struct addrinfo **sv_addr, int family)
call socket(), bind()
Definition: network.c:70
int get_valid_tcp_client_socket(int min, int max, char *hname, unsigned short sport, unsigned short *cport)
Definition: network.c:1124
int is_same_network_num(unsigned char *addr1, unsigned char *addr2, unsigned char *mask, int family)
Definition: network.c:2331
int _udp_server_socket_setopt(int port, int opt, const void *optval, int optlen, struct addrinfo **sv_addr, int family)
call socket(), setsockopt(), bind()
Definition: network.c:156
int socket_close(int sofd)
call shutdown(), close()
Definition: network.c:1022
struct sockaddr * make_sockaddr_bystr(const char *addr, int port)
IPv4/IPv6 対応
Definition: network.c:1605
int _udp_bind_setopt(int sofd, int port, int opt, const void *optval, int optlen, int family)
call setsockopt(), bind()
Definition: network.c:332
int tcp_send_mesgln(int sock, char *mesg)
Definition: network.c:1345
int udp_send(int sock, char *smsg, int size, struct addrinfo *sv_addr)
Definition: network.c:1199
unsigned char * get_mynetaddr_num(int family)
自分の IPv4/IPv6 ネットワークアドレス(バイナリ)
Definition: network.c:2177
int get_valid_tcp_server_socket(int min, int max, unsigned short *port)
Definition: network.c:1102
int _tcp_client_socket(char *hostname, int port, int family)
call socket(), connect()
Definition: network.c:712
int udp_client_socket_sockaddr_in(char *hostname, int port, struct sockaddr_in *sv_addr)
use udp_client_socket()
Definition: network.c:2553
unsigned char * get_ipaddr_byname_num(const char *hostname, int family)
ホスト名 → IPv4/IPv6 アドレス(バイナリ)
Definition: network.c:1813
void udp_hole_punching(int sock, struct addrinfo *addr, int nm)
Definition: network.c:2534
void udp_hole_punching_sockaddr_in(int sock, struct sockaddr_in addr, int nm)
use udp_hole_punching()
Definition: network.c:2741
int set_nonblock_socket(int sock)
Definition: network.c:1043
int _udp_bind(int sofd, int port, int family)
call bind()
Definition: network.c:283
int accept_intr(int sock, struct sockaddr *cl_addr, socklen_t *cdlen)
call accept()
Definition: network.c:995
int udp_recv_sockaddr_in(int sock, char *rmsg, int size, struct sockaddr_in *sv_addr)
use udp_recv()
Definition: network.c:2578
int tcp_recv(int sock, char *rmsg, int size)
Definition: network.c:1226
char * get_ipaddr_ipv4(struct in_addr sin_addr)
構造体 → IPv4 アドレス(文字列) use get_ipaddr_byname()
Definition: network.c:2685
char * get_hostname_bynum(unsigned char *num, int family)
IPv4/IPv6 アドレス(バイナリ)→ ホスト名(文字列)
Definition: network.c:1642
int _tcp_server_bind_setopt(int port, int opt, const void *optval, int optlen, int family)
call socket(), setsockopt(), bind()
Definition: network.c:639
struct sockaddr * make_sockaddr_bynum(unsigned char *addr, int port, int family)
IPv4/IPv6 対応
Definition: network.c:1556
struct sockaddr_in get_sockaddr_in(char *hostname, unsigned short cport)
Definition: network.c:2619
char * get_ipaddr_byname(const char *hostname, int family)
ホスト名 → IPv4/IPv6 アドレス(文字列)
Definition: network.c:1781
int _tcp_bind_setopt(int sofd, int port, int opt, const void *optval, int optlen, int family)
call setsockopt(), bind()
Definition: network.c:886
int _udp_client_socket(char *hostname, int port, struct addrinfo **sv_addr, int family)
call socket()
Definition: network.c:237
int is_same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
Definition: network.c:2219
char * _get_hostname(const char *addr, int family)
IPv4/IPv6 アドレス(文字列), FQDN → ホスト名(文字列)
Definition: network.c:1729
int _tcp_client_bind_socket(char *hostname, int sport, int cport, int family)
call socket(), bind(), connect()
Definition: network.c:767
int _tcp_server_socket(int port, int family)
call socket(), bind(), listen()
Definition: network.c:396
int recv_wait(int sock, int tm)
Definition: network.c:1443
int _tcp_connect(int sofd, char *hostname, int port, int family)
call connect()
Definition: network.c:949
struct sockaddr_in get_sockaddr_in_bynum(char *ipnum, unsigned short cport)
Definition: network.c:2641
int udp_recv(int sock, char *rmsg, int size, struct addrinfo *sv_addr)
Definition: network.c:1166
int get_valid_udp_socket(int min, int max, unsigned short *port)
Definition: network.c:1078
int _tcp_bind(int sofd, int port, int family)
call bind()
Definition: network.c:834
unsigned char * get_myipaddr_num(int family)
自分の [IPv4/IPv6 アドレス],[ネットマスク](バイナリ)
Definition: network.c:2008
int udp_recv_wait(int sock, char *rmsg, int size, struct addrinfo *sv_addr, int tm)
Definition: network.c:1283
int is_same_network(char *addr1, char *addr2, char *mask)
Definition: network.c:2283
int _tcp_server_bind(int port, int family)
call socket(), bind()
Definition: network.c:562
int recv_wait_twin(int sock1, int sock2, int tm)
Definition: network.c:1480
int set_block_socket(int sock)
Definition: network.c:1060
char * _get_localip_bydest(const char *dest, int family)
自分の IPv4/IPv6 アドレス(文字列)
Definition: network.c:2064
int _tcp_server_socket_setopt(int port, int opt, const void *optval, int optlen, int family)
call socket(), setsockopt(), bind(), listen()
Definition: network.c:482
char * get_hostname_bystr(const char *addr)
IPv4/IPv6 アドレス(文字列) → ホスト名(文字列)
Definition: network.c:1684
unsigned char * to_address_num(char *addr, int mode, int mask, int family)
IPv4/IPv6 アドレス(文字列)→ [IPv4/IPv6 アドレス],[ネットマスク](数字8/32Byte)
Definition: network.c:2373
void cleanup_network(void)
Definition: network.c:40
ネットワーク用ライブラリヘッダ for IPv4/IPv6
#define tcp_client_bind_socket(h, s, c)
Definition: network.h:134
#define udp_server_socket(p, a)
Definition: network.h:82
#define tcp_server_socket(p)
Definition: network.h:108
int put_mstream(mstream *sb, unsigned char *mesg)
メッセージ(文字列)ストリーム sb へメッセージ(の一部)を格納する
Definition: tools.c:3491
char * awk(char *buf, char cc, int n)
ccを区切り記号として, strのバッファ内の n番目の項目を返す.要 free()
Definition: tools.c:567
unsigned char * get_mstream(mstream *sb)
メッセージ(文字列)ストリーム sb から次のメッセージを取り出す.改行コードは削除される.
Definition: tools.c:3532
char * itostr_ts(int n)
int を文字に変換する.要 free()
Definition: tools.c:1532
#define mstream
バイト型 メッセージストリーム
Definition: tools.h:167
#define freeNull(p)
Definition: tools.h:201
#define make_mstream(s)
make_ringBuffer()
Definition: tools.h:385