32 ret = WSAStartup(MAKEWORD(2,0), &
WsaData);
33 if (ret!=0) WSACleanup();
72 int sofd, err, nullflg =
OFF;
73 struct addrinfo hints;
74 struct addrinfo* address;
81 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
88 memset(&hints, 0,
sizeof(
struct addrinfo));
89 hints.ai_family = family;
90 hints.ai_flags = AI_PASSIVE;
91 hints.ai_socktype = SOCK_DGRAM;
94 err = getaddrinfo(NULL, str, &hints, sv_addr);
101 sofd = (int)socket((*sv_addr)->ai_family, (*sv_addr)->ai_socktype, (*sv_addr)->ai_protocol);
103 freeaddrinfo(*sv_addr);
112 err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, (
const char*)&lg,
sizeof(lg));
114 err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, &lg,
sizeof(lg));
117 freeaddrinfo(*sv_addr);
122 err = bind(sofd, (*sv_addr)->ai_addr, (
int)(*sv_addr)->ai_addrlen);
124 freeaddrinfo(*sv_addr);
130 if (nullflg) freeaddrinfo(*sv_addr);
158 int sofd, err, nullflg =
OFF;
159 struct addrinfo hints;
160 struct addrinfo* address;
161 int nonblock =
FALSE;
167 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
174 memset(&hints, 0,
sizeof(
struct addrinfo));
175 hints.ai_family = family;
176 hints.ai_flags = AI_PASSIVE;
177 hints.ai_socktype = SOCK_DGRAM;
180 err = getaddrinfo(NULL, str, &hints, sv_addr);
184 sofd = (int)socket((*sv_addr)->ai_family, (*sv_addr)->ai_socktype, (*sv_addr)->ai_protocol);
186 freeaddrinfo(*sv_addr);
194 err = setsockopt(sofd, SOL_SOCKET, opt, (
const char*)optval, optlen);
196 err = setsockopt(sofd, SOL_SOCKET, opt, optval, optlen);
199 freeaddrinfo(*sv_addr);
206 err = bind(sofd, (*sv_addr)->ai_addr, (
int)(*sv_addr)->ai_addrlen);
208 freeaddrinfo(*sv_addr);
214 if (nullflg) freeaddrinfo(*sv_addr);
240 struct addrinfo hints;
243 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
245 memset(&hints, 0,
sizeof(
struct addrinfo));
246 hints.ai_family = family;
247 hints.ai_flags = AI_PASSIVE;
248 hints.ai_socktype = SOCK_DGRAM;
251 err = getaddrinfo(hostname, str, &hints, sv_addr);
258 sofd = (int)socket((*sv_addr)->ai_family, (*sv_addr)->ai_socktype, (*sv_addr)->ai_protocol);
260 freeaddrinfo(*sv_addr);
286 struct addrinfo hints;
287 struct addrinfo* address;
289 if (sofd<=0)
return sofd;
290 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
292 memset(&hints, 0,
sizeof(
struct addrinfo));
293 hints.ai_family = family;
294 hints.ai_flags = AI_PASSIVE;
295 hints.ai_socktype = SOCK_DGRAM;
298 err = getaddrinfo(NULL, str, &hints, &address);
302 err = bind(sofd, address->ai_addr, (
int)address->ai_addrlen);
304 freeaddrinfo(address);
309 freeaddrinfo(address);
332int _udp_bind_setopt(
int sofd,
int port,
int opt,
const void* optval,
int optlen,
int family)
335 struct addrinfo hints;
336 struct addrinfo* address;
338 if (sofd<=0)
return sofd;
339 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
343 err = setsockopt(sofd, SOL_SOCKET, opt, (
const char*)optval, optlen);
345 err = setsockopt(sofd, SOL_SOCKET, opt, optval, optlen);
353 memset(&hints, 0,
sizeof(
struct addrinfo));
354 hints.ai_family = family;
355 hints.ai_flags = AI_PASSIVE;
356 hints.ai_socktype = SOCK_DGRAM;
359 err = getaddrinfo(NULL, str, &hints, &address);
366 err = bind(sofd, address->ai_addr, (
int)address->ai_addrlen);
368 freeaddrinfo(address);
373 freeaddrinfo(address);
399 struct addrinfo hints;
400 struct addrinfo* address;
401 int nonblock =
FALSE;
403 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
405 memset(&hints, 0,
sizeof(
struct addrinfo));
406 hints.ai_family = family;
407 hints.ai_flags = AI_PASSIVE;
408 hints.ai_socktype = SOCK_STREAM;
416 err = getaddrinfo(NULL, str, &hints, &address);
420 sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
422 freeaddrinfo(address);
430 err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, (
const char*)&lg,
sizeof(lg));
432 err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, &lg,
sizeof(lg));
435 freeaddrinfo(address);
440 err = bind(sofd, address->ai_addr, (
int)address->ai_addrlen);
442 freeaddrinfo(address);
447 err = listen(sofd, 10);
449 freeaddrinfo(address);
454 freeaddrinfo(address);
485 struct addrinfo hints;
486 struct addrinfo* address;
487 int nonblock =
FALSE;
489 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
491 memset(&hints, 0,
sizeof(
struct addrinfo));
492 hints.ai_family = family;
493 hints.ai_flags = AI_PASSIVE;
494 hints.ai_socktype = SOCK_STREAM;
502 err = getaddrinfo(NULL, str, &hints, &address);
506 sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
508 freeaddrinfo(address);
515 err = setsockopt(sofd, SOL_SOCKET, opt, (
const char*)optval, optlen);
517 err = setsockopt(sofd, SOL_SOCKET, opt, optval, optlen);
520 freeaddrinfo(address);
526 err = bind(sofd, address->ai_addr, (
int)address->ai_addrlen);
528 freeaddrinfo(address);
533 err = listen(sofd, 10);
535 freeaddrinfo(address);
540 freeaddrinfo(address);
565 struct addrinfo hints;
566 struct addrinfo* address;
567 int nonblock =
FALSE;
569 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
571 memset(&hints, 0,
sizeof(
struct addrinfo));
572 hints.ai_family = family;
573 hints.ai_flags = AI_PASSIVE;
574 hints.ai_socktype = SOCK_STREAM;
582 err = getaddrinfo(NULL, str, &hints, &address);
586 sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
588 freeaddrinfo(address);
596 err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, (
const char*)&lg,
sizeof(lg));
598 err = setsockopt(sofd, SOL_SOCKET, SO_REUSEADDR, &lg,
sizeof(lg));
601 freeaddrinfo(address);
606 err = bind(sofd, address->ai_addr, (
int)address->ai_addrlen);
608 freeaddrinfo(address);
613 freeaddrinfo(address);
642 struct addrinfo hints;
643 struct addrinfo* address;
644 int nonblock =
FALSE;
646 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
648 memset(&hints, 0,
sizeof(
struct addrinfo));
649 hints.ai_family = family;
650 hints.ai_flags = AI_PASSIVE;
651 hints.ai_socktype = SOCK_STREAM;
659 err = getaddrinfo(NULL, str, &hints, &address);
663 sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
665 freeaddrinfo(address);
672 err = setsockopt(sofd, SOL_SOCKET, opt, (
const char*)optval, optlen);
674 err = setsockopt(sofd, SOL_SOCKET, opt, optval, optlen);
677 freeaddrinfo(address);
683 err = bind(sofd, address->ai_addr, (
int)address->ai_addrlen);
685 freeaddrinfo(address);
690 freeaddrinfo(address);
715 struct addrinfo hints;
716 struct addrinfo* address;
718 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
720 memset(&hints, 0,
sizeof(
struct addrinfo));
721 hints.ai_family = family;
722 hints.ai_flags = AI_PASSIVE;
723 hints.ai_socktype = SOCK_STREAM;
726 err = getaddrinfo(hostname, str, &hints, &address);
730 sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
732 freeaddrinfo(address);
736 err = connect(sofd, address->ai_addr, (
int)address->ai_addrlen);
738 freeaddrinfo(address);
743 freeaddrinfo(address);
770 struct addrinfo hints;
771 struct addrinfo* address;
773 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
775 memset(&hints, 0,
sizeof(
struct addrinfo));
776 hints.ai_family = family;
777 hints.ai_flags = AI_PASSIVE;
778 hints.ai_socktype = SOCK_STREAM;
781 err = getaddrinfo(NULL, str, &hints, &address);
785 sofd = (int)socket(address->ai_family, address->ai_socktype, address->ai_protocol);
787 freeaddrinfo(address);
791 err = bind(sofd, address->ai_addr, (
int)address->ai_addrlen);
793 freeaddrinfo(address);
797 freeaddrinfo(address);
801 err = getaddrinfo(hostname, str, &hints, &address);
808 err = connect(sofd, address->ai_addr, (
int)address->ai_addrlen);
810 freeaddrinfo(address);
815 freeaddrinfo(address);
837 struct addrinfo hints;
838 struct addrinfo* address;
840 if (sofd<=0)
return sofd;
841 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
843 memset(&hints, 0,
sizeof(
struct addrinfo));
844 hints.ai_family = family;
845 hints.ai_flags = AI_PASSIVE;
846 hints.ai_socktype = SOCK_STREAM;
849 err = getaddrinfo(NULL, str, &hints, &address);
856 err = bind(sofd, address->ai_addr, (
int)address->ai_addrlen);
858 freeaddrinfo(address);
863 freeaddrinfo(address);
886int _tcp_bind_setopt(
int sofd,
int port,
int opt,
const void* optval,
int optlen,
int family)
889 struct addrinfo hints;
890 struct addrinfo* address;
892 if (sofd<=0)
return sofd;
893 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
897 err = setsockopt(sofd, SOL_SOCKET, opt, (
const char*)optval, optlen);
899 err = setsockopt(sofd, SOL_SOCKET, opt, optval, optlen);
907 memset(&hints, 0,
sizeof(
struct addrinfo));
908 hints.ai_family = family;
909 hints.ai_flags = AI_PASSIVE;
910 hints.ai_socktype = SOCK_STREAM;
913 err = getaddrinfo(NULL, str, &hints, &address);
920 err = bind(sofd, address->ai_addr, (
int)address->ai_addrlen);
922 freeaddrinfo(address);
927 freeaddrinfo(address);
952 struct addrinfo hints;
953 struct addrinfo* address;
955 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
957 memset(&hints, 0,
sizeof(
struct addrinfo));
958 hints.ai_family = family;
959 hints.ai_flags = AI_PASSIVE;
960 hints.ai_socktype = SOCK_STREAM;
963 err = getaddrinfo(hostname, str, &hints, &address);
970 err = connect(sofd, address->ai_addr, (
int)address->ai_addrlen);
972 freeaddrinfo(address);
977 freeaddrinfo(address);
1000 nsofd = (int)accept(sock, cl_addr, cdlen);
1001 }
while (nsofd==-1 && errno==EINTR);
1028 err = shutdown(sofd, 2);
1031 err = shutdown(sofd, SHUT_RDWR);
1047 ioctlsocket(sock, FIONBIO, &val);
1049 int val = fcntl(sock, F_GETFL, 0);
1050 if (val>=0) fcntl(sock, F_SETFL, val | O_NONBLOCK);
1064 ioctlsocket(sock, FIONBIO, &val);
1066 int val = fcntl(sock, F_GETFL, 0);
1067 if (val>=0) fcntl(sock, F_SETFL, val & ~O_NONBLOCK);
1081 struct addrinfo* address;
1083 range = max - min + 1;
1084 *port = rand()%range + min;
1088 while(sock<=0 && i<range) {
1090 if (*port>max) *port = ((int)*port)%max + min - 1;
1095 if (sock<=0) *port = 0;
1097 freeaddrinfo(address);
1106 range = max - min + 1;
1107 *port = rand()%range + min;
1111 while(sock<=0 && i<range) {
1113 if (*port>max) *port = ((int)*port)%max + min - 1;
1118 if (sock<=0) *port = 0;
1128 range = max - min + 1;
1129 *cport = rand()%range + min;
1133 while(sock<0 && i<range) {
1135 if (*cport>max) *cport = ((int)*cport)%max + min - 1;
1140 if (sock<=0) *cport = 0;
1166int udp_recv(
int sock,
char* rmsg,
int size,
struct addrinfo* sv_addr)
1173 cadlen = (int)sv_addr->ai_addrlen;
1174 memset(rmsg, 0, size);
1175 cc = recvfrom(sock, rmsg, size, 0, sv_addr->ai_addr, &cadlen);
1199int udp_send(
int sock,
char* smsg,
int size,
struct addrinfo* sv_addr)
1205 if (size<=0) size = (int)strlen(smsg);
1206 cc = sendto(sock, smsg, size, 0, sv_addr->ai_addr, (
int)sv_addr->ai_addrlen);
1230 memset(rmsg, 0, size);
1231 cc = recv(sock, rmsg, size, 0);
1256 if (size<=0) size = (int)strlen(smsg);
1257 cc = send(sock, smsg, size, 0);
1283int udp_recv_wait(
int sock,
char* rmsg,
int size,
struct addrinfo* sv_addr,
int tm)
1288 cc =
udp_recv(sock, rmsg, size, sv_addr);
1321 memset(mesg, 0, sz);
1323 cc = recv(sock, mesg, sz, 0);
1350 sz = (int)strlen(mesg) + 3;
1351 buf = (
char*)malloc(sz);
1354 strncpy(buf, mesg, sz);
1355 strncat(buf,
"\r\n", 2);
1356 cc = send(sock, buf, (
int)strlen(buf), 0);
1403 if (sb->buf==NULL) {
1408 while (sb->datano==0) {
1420 if (strlen((
const char*)pp)>=(
unsigned int)sz) {
1421 memcpy(mesg, pp, sz-1);
1425 memcpy(mesg, pp, strlen((
const char*)pp));
1428 return (
int)strlen(mesg);
1447 struct timeval timeout;
1452 timeout.tv_sec = tm;
1453 timeout.tv_usec = 0;
1455 FD_SET(sock, &mask);
1458 nd = select(sock+1, &mask, NULL, NULL, &timeout);
1460 }
while ((nd<0 || !FD_ISSET(sock, &mask)) && (
int)(ntm-otm)<=tm);
1462 return FD_ISSET(sock, &mask);
1485 struct timeval timeout;
1488 nm =
Max(sock1, sock2);
1491 timeout.tv_sec = tm;
1492 timeout.tv_usec = 0;
1494 FD_SET(sock1, &mask);
1495 FD_SET(sock2, &mask);
1496 nd = select(nm+1, &mask, NULL, NULL, &timeout);
1498 }
while ((nd<0 || (!FD_ISSET(sock1, &mask) && !FD_ISSET(sock2, &mask))) && (
int)(ntm-otm)<=tm);
1500 if (FD_ISSET(sock1, &mask)) ret += 1;
1501 if (FD_ISSET(sock2, &mask)) ret += 2;
1522 struct timeval timeout;
1527 timeout.tv_sec = tm;
1528 timeout.tv_usec = 0;
1530 FD_SET(sock, &mask);
1533 nd = select(sock+1, NULL, &mask, NULL, &timeout);
1535 }
while ((nd<0 || !FD_ISSET(sock, &mask)) && (
int)(ntm-otm)<=tm);
1537 return FD_ISSET(sock, &mask);
1558 struct sockaddr* sa = NULL;
1561 len =
sizeof(
struct sockaddr);
1562 sa = (
struct sockaddr*)malloc(len);
1563 if (sa==NULL)
return NULL;
1566 if (family==AF_INET6) {
1567 struct sockaddr_in6* sa6;
1568 len =
sizeof(
struct sockaddr_in6);
1569 sa6 = (
struct sockaddr_in6*)malloc(len);
1570 memset(sa6, 0, len);
1572 sa6->sin6_family = AF_INET6;
1573 sa6->sin6_port = htons(port);
1575 memcpy(&(sa6->sin6_addr), addr, 16);
1576 sa = (
struct sockaddr*)sa6;
1580 struct sockaddr_in* sa4;
1581 len =
sizeof(
struct sockaddr_in);
1582 sa4 = (
struct sockaddr_in*)malloc(len);
1583 memset(sa4, 0, len);
1585 sa4->sin_family = AF_INET;
1586 sa4->sin_port = htons(port);
1588 memcpy(&(sa4->sin_addr), addr, 4);
1589 sa = (
struct sockaddr*)sa4;
1607 int err, family = -1;
1609 unsigned char num[16];
1610 struct sockaddr* sa;
1612 pp = strstr((
char*)addr,
".");
1617 pp = strstr((
char*)addr,
":");
1622 if (family<0)
return NULL;
1625 err = inet_pton(family, addr, num);
1626 if (err!=1)
return NULL;
1647 struct sockaddr* sa;
1649 if (num==NULL)
return NULL;
1650 if (family!=AF_INET6) family = AF_INET6;
1653 if (sa==NULL)
return NULL;
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);
1663 len = (int)strlen(htemp);
1664 hname = (
char*)malloc(len+1);
1671 memcpy(hname, htemp, len+1);
1689 struct sockaddr* sa;
1691 if (addr==NULL)
return NULL;
1694 if (sa==NULL)
return NULL;
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);
1704 len = (int)strlen(htemp);
1705 hname = (
char*)malloc(len+1);
1712 memcpy(hname, htemp, len+1);
1735 struct addrinfo hints;
1736 struct addrinfo* address;
1737 struct sockaddr* sa;
1739 if (addr==NULL)
return NULL;
1740 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
1742 memset(&hints, 0,
sizeof(
struct addrinfo));
1743 hints.ai_family = family;
1744 hints.ai_flags = AI_PASSIVE;
1745 hints.ai_socktype = SOCK_DGRAM;
1747 err = getaddrinfo(addr, 0, &hints, &address);
1748 if (err!=0 || address==NULL)
return NULL;
1749 sa = (
struct sockaddr*)address->ai_addr;
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);
1755 freeaddrinfo(address);
1759 len = (int)strlen(htemp);
1760 hname = (
char*)malloc(len+1);
1762 freeaddrinfo(address);
1766 freeaddrinfo(address);
1767 memcpy(hname, htemp, len+1);
1785 unsigned char* ipnum;
1787 if (family!=AF_INET6) family = AF_INET;
1790 if (ipnum==NULL)
return NULL;
1794 ip = (
char*)malloc(len);
1795 if (ip==NULL)
return NULL;
1798 inet_ntop(family, ipnum, ip, len);
1816 struct addrinfo hints;
1817 struct addrinfo* address;
1818 unsigned char* ip = NULL;
1819 unsigned char* pp = NULL;
1821 if (family!=AF_INET6) family = AF_INET;
1823 memset(&hints, 0,
sizeof(
struct addrinfo));
1824 hints.ai_socktype = SOCK_STREAM;
1825 hints.ai_family = family;
1827 err = getaddrinfo(hostname, NULL, &hints, &address);
1828 if (err!=0 || address==NULL)
return NULL;
1830 if (family==AF_INET6) {
1831 ip = (
unsigned char*)malloc(16);
1834 pp = (
unsigned char*)&(((
struct sockaddr_in6*)(address->ai_addr))->sin6_addr);
1840 ip = (
unsigned char*)malloc(4);
1843 pp = (
unsigned char*)&(((
struct sockaddr_in*)(address->ai_addr))->sin_addr);
1848 freeaddrinfo(address);
1855#include <iphlpapi.h>
1856#pragma comment( lib, "iphlpapi.lib" )
1860 int len, prefix, fndflg;
1862 char address[
LNAME];
1863 IP_ADAPTER_ADDRESSES* pp, * pl;
1864 IP_ADAPTER_UNICAST_ADDRESS* pu;
1867 if (family != AF_INET6) {
1872 ULONG flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
1873 DWORD err, size = 0;
1875 err = GetAdaptersAddresses(family, flags, NULL, NULL, &size);
1876 if (err != ERROR_BUFFER_OVERFLOW)
return NULL;
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) {
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)) {
1900 inet_ntop(AF_INET6, ch, address,
LNAME);
1905 unsigned char* ch = (
unsigned char*)&((
struct sockaddr_in*)sa)->sin_addr;
1906 if (!(ch[0] == 169 && ch[1] == 254)) {
1908 inet_ntop(AF_INET, ch, address,
LNAME);
1915 if (fndflg ==
ON)
break;
1920 int addrlen = (int)strlen(address);
1921 snprintf(address + addrlen,
LNAME - addrlen - 1,
"/%d\0", prefix);
1923 ipa = (
char*)malloc(len);
1924 if (ipa == NULL)
return NULL;
1925 memset(ipa, 0, len);
1926 memcpy(ipa, address, strlen(address)+1);
1937 int err, flg, len, lst;
1938 struct ifaddrs *if_addr;
1939 struct ifaddrs *ifap;
1944 if (family!=AF_INET6) {
1946 len =
sizeof(
struct sockaddr_in);
1949 len =
sizeof(
struct sockaddr_in6);
1952 err = getifaddrs(&if_addr);
1953 if (err<0)
return 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);
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;
1973 if (strncmp(htemp,
"::1", 3) &&
strncasecmp(htemp,
"fe80:", 5)) {
1980 ifap=ifap->ifa_next;
1983 freeifaddrs(if_addr);
1987 freeifaddrs(if_addr);
1989 lst = strlen(htemp);
1990 if (
LNAME-lst-1<=0)
return NULL;
1993 err = getnameinfo(ifap->ifa_netmask, len, &(htemp[lst]),
LNAME-lst, NULL, 0, NI_NUMERICHOST);
1994 if (err!=0)
return NULL;
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);
2013 unsigned char* haddr = NULL;
2015 if (family!=AF_INET6) family = AF_INET;
2019 if (htemp==NULL)
return NULL;
2021 pos = strstr(htemp,
"/");
2028 if (family==AF_INET6) len = 16;
2030 haddr = (
unsigned char*)malloc(len*2);
2035 memset(haddr, 0, len*2);
2037 err = inet_pton(family, htemp, haddr);
2043 err = inet_pton(family, pos+1, haddr+len);
2069 struct addrinfo hints;
2070 struct addrinfo* sv_addr;
2071 struct sockaddr* sa;
2073 if (family!=AF_INET && family!=AF_INET6 && family!=AF_UNSPEC) family = AF_UNSPEC;
2075 memset(&hints, 0,
sizeof(
struct addrinfo));
2076 hints.ai_family = family;
2077 hints.ai_flags = AI_PASSIVE;
2078 hints.ai_socktype = SOCK_DGRAM;
2080 err = getaddrinfo(dest,
"9999", &hints, &sv_addr);
2086 sofd = (int)socket(sv_addr->ai_family, sv_addr->ai_socktype, sv_addr->ai_protocol);
2088 freeaddrinfo(sv_addr);
2092 err = connect(sofd, sv_addr->ai_addr, (
int)sv_addr->ai_addrlen);
2098 if (sv_addr->ai_family==AF_INET6) {
2099 len =
sizeof(
struct sockaddr_in6);
2100 sa = (
struct sockaddr*)malloc(len);
2102 err = getsockname(sofd, (
struct sockaddr *)sa, (
socklen_t*)&len);
2104 pp = (
unsigned char*)&(((
struct sockaddr_in6*)sa)->sin6_addr);
2108 len =
sizeof(
struct sockaddr_in);
2109 sa = (
struct sockaddr*)malloc(len);
2111 err = getsockname(sofd, (
struct sockaddr *)sa, (
socklen_t*)&len);
2113 pp = (
unsigned char*)&(((
struct sockaddr_in*)sa)->sin_addr);
2117 freeaddrinfo(sv_addr);
2120 addr = (
char*)malloc(len);
2125 memset(addr, 0, len);
2127 inet_ntop(family, pp, addr, len);
2149 if (family!=AF_INET6) {
2155 if (pp==NULL)
return NULL;
2157 net = (
char*)malloc(len);
2162 memset(net, 0, len);
2164 inet_ntop(family, pp, net, len);
2185 if (family!=AF_INET6) len = 4;
2188 if (ip==NULL)
return NULL;
2191 net = (
unsigned char*)malloc(len);
2196 for (i=0; i<len; i++) net[i] = ip[i] & mk[i];
2222 unsigned char *p1, *p2;
2223 struct sockaddr_in *sa1, *sa2;
2224 struct sockaddr_in6 *sa61, *sa62;
2226 if (addr1==NULL || addr2==NULL)
return FALSE;
2227 if (addr1->sa_family!=addr2->sa_family)
return FALSE;
2229 if (addr1->sa_family==AF_INET) {
2230 sa1 = (
struct sockaddr_in*)addr1;
2231 sa2 = (
struct sockaddr_in*)addr2;
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;
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;
2248 sa61 = (
struct sockaddr_in6*)addr1;
2249 sa62 = (
struct sockaddr_in6*)addr2;
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;
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;
2290 if (addr1==NULL || addr2==NULL || mask==NULL)
return FALSE;
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;
2302 if (a1==NULL || a2==NULL || mk==NULL) {
2335 if (addr1==NULL || addr2==NULL)
return FALSE;
2338 if (family!=AF_INET6) len = 4;
2341 for (i=0; i<len; i++) {
2342 if (addr1[i] != addr2[i])
return FALSE;
2346 for (i=0; i<len; i++) {
2347 if ((addr1[i] & mask[i]) != (addr2[i] & mask[i]))
return FALSE;
2382 if (addr==NULL)
return NULL;
2386 if (family!=AF_INET6) {
2394 i = (int)strlen(addr) - 1;
2395 while (i>0 && addr[i]==deli) i--;
2397 if (isalpha(addr[i]))
return NULL;
2401 num = (
unsigned char*)malloc(len*2);
2402 if (num==NULL)
return NULL;
2403 memset(num, 0, len*2);
2406 ps =
awk(addr,
'/', 1);
2414 if (uc==NULL)
return NULL;
2415 memcpy(num, uc, len);
2418 if (mask==0)
return num;
2421 ps =
awk(addr,
'/', 2);
2426 for (i=len-1; i>=0; i--) {
2427 if (num[i]!=0 || f==
ON) {
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);
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);
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;
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;
2498 int mlen, plen, slen;
2500 if (addr==NULL)
return NULL;
2504 if (family!=AF_INET6) {
2510 str = (
char*)malloc(mlen);
2511 if (str==NULL)
return NULL;
2512 memset(str, 0, mlen);
2514 inet_ntop(family, addr, str, mlen);
2515 if (mask==0)
return str;
2517 slen = (int)strlen(str);
2519 inet_ntop(family, addr+plen, str+slen, mlen-slen);
2541 memset(data, 0, nm);
2556 struct hostent* shost = NULL;
2560 sofd = (int)socket(AF_INET, SOCK_DGRAM, 0);
2563 shost = gethostbyname(hostname);
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);
2583 if (sv_addr==NULL)
return 0;
2585 cadlen =
sizeof(*sv_addr);
2586 memset(rmsg, 0, size);
2587 cc = recvfrom(sock, rmsg, size, 0, (
struct sockaddr*)sv_addr, &cadlen);
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));
2621 struct sockaddr_in ss_addr;
2622 struct hostent *shost;
2625 shost = gethostbyname(hostname);
2627 ss_addr.sin_family = 0;
2628 ss_addr.sin_port = 0;
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);
2643 struct sockaddr_in ss_addr;
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);
2656 struct sockaddr_in ss_addr;
2657 struct hostent *shost;
2660 shost = gethostbyname(
"127.0.0.1");
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);
2690 pp = (
unsigned char*)&(sin_addr);
2691 if (pp[0]==0)
return NULL;
2693 ip = (
char*)malloc(16);
2694 if (ip==NULL)
return NULL;
2697 snprintf(ip, 15,
"%d.%d.%d.%d", pp[0], pp[1], pp[2], pp[3]);
2722 ip = (
unsigned char*)malloc(4);
2723 if (ip==NULL)
return NULL;
2725 pp = (
unsigned char*)&(sin_addr);
2748 memset(data, 0, nm);
#define LEN_IPADDR
strlen("AAA.BBB.CCC.DDD") + '\0' + 1(予備)
#define LEN_IPADDR6
strlen("1111:2222:333:4444:5555:6666:7777:8888") + '\0' + 1(予備)
#define JBXL_NET_INFO_ERROR
ホスト情報の取得に失敗
#define JBXL_NET_SOCKET_ERROR
ソケットの作成に失敗
#define JBXL_NET_LISTEN_ERROR
リッスンに失敗
#define JBXL_ARGS_ERROR
不正な引数(NULLなど)
#define JBXL_NET_CONNECT_ERROR
接続に失敗
#define JBXL_NET_SEND_ERROR
データの送信エラー
#define JBXL_NET_OPTION_ERROR
オプションの設定に失敗
#define JBXL_NET_BIND_ERROR
バインドに失敗
#define JBXL_NET_BUFSZ_ERROR
受信バッファの長さが足りない.はみ出したデータは捨てられた
#define JBXL_NET_RECV_ERROR
データの受信エラー
#define JBXL_MALLOC_ERROR
メモリ確保エラー
#define JBXL_NET_BUF_ERROR
受信バッファにデータは存在するはずだが,原因不明の理由により獲得に失敗した
#define JBXL_NET_RECV_TIMEOUT
受信タイムアウト
int udp_recv_wait_sockaddr_in(int sock, char *rmsg, int size, struct sockaddr_in *sv_addr, int tm)
use udp_recv_wait()
int tcp_recv_wait(int sock, char *mesg, int sz, int tm)
int tcp_send(int sock, char *smsg, int size)
char * _get_hostname(const char *addr, int family)
IPv4/IPv6 アドレス(文字列), FQDN → ホスト名(文字列)
char * get_ipaddr_byname(const char *hostname, int family)
ホスト名 → IPv4/IPv6 アドレス(文字列)
struct sockaddr_in get_local_sockaddr_in(unsigned short cport)
int udp_send_sockaddr_in(int sock, char *smsg, int size, struct sockaddr_in *sv_addr)
use udp_send()
int send_wait(int sock, int tm)
int tcp_recv_mstream(int sock, char *mesg, int sz, mstream *sb, int tm)
char * _get_localip_bydest(const char *dest, int family)
自分の IPv4/IPv6 アドレス(文字列)
int _udp_server_socket(int port, struct addrinfo **sv_addr, int family)
call socket(), bind()
int get_valid_tcp_client_socket(int min, int max, char *hname, unsigned short sport, unsigned short *cport)
int is_same_network_num(unsigned char *addr1, unsigned char *addr2, unsigned char *mask, int family)
int _udp_server_socket_setopt(int port, int opt, const void *optval, int optlen, struct addrinfo **sv_addr, int family)
call socket(), setsockopt(), bind()
int socket_close(int sofd)
call shutdown(), close()
int _udp_bind_setopt(int sofd, int port, int opt, const void *optval, int optlen, int family)
call setsockopt(), bind()
int tcp_send_mesgln(int sock, char *mesg)
int udp_send(int sock, char *smsg, int size, struct addrinfo *sv_addr)
char * get_mynetaddr(int family)
自分の IPv4/IPv6 ネットワークアドレス(文字列)
int get_valid_tcp_server_socket(int min, int max, unsigned short *port)
int _tcp_client_socket(char *hostname, int port, int family)
call socket(), connect()
int udp_client_socket_sockaddr_in(char *hostname, int port, struct sockaddr_in *sv_addr)
use udp_client_socket()
void udp_hole_punching(int sock, struct addrinfo *addr, int nm)
void udp_hole_punching_sockaddr_in(int sock, struct sockaddr_in addr, int nm)
use udp_hole_punching()
int set_nonblock_socket(int sock)
int _udp_bind(int sofd, int port, int family)
call bind()
char * get_hostname_bynum(unsigned char *num, int family)
IPv4/IPv6 アドレス(バイナリ)→ ホスト名(文字列)
char * to_address_char(unsigned char *addr, int mask, int family)
[IPv4/IPv6 アドレス],[ネットマスク](数字8/32Byte)→ IPv4/IPv6 アドレス(文字列)
int accept_intr(int sock, struct sockaddr *cl_addr, socklen_t *cdlen)
call accept()
int udp_recv_sockaddr_in(int sock, char *rmsg, int size, struct sockaddr_in *sv_addr)
use udp_recv()
int tcp_recv(int sock, char *rmsg, int size)
unsigned char * get_ipaddr_num_ipv4(struct in_addr sin_addr)
構造体 → IPv4 アドレス(バイナリ) use get_ipaddr_byname_num()
int _tcp_server_bind_setopt(int port, int opt, const void *optval, int optlen, int family)
call socket(), setsockopt(), bind()
struct sockaddr_in get_sockaddr_in(char *hostname, unsigned short cport)
struct sockaddr * make_sockaddr_bystr(const char *addr, int port)
IPv4/IPv6 対応
int _tcp_bind_setopt(int sofd, int port, int opt, const void *optval, int optlen, int family)
call setsockopt(), bind()
int _udp_client_socket(char *hostname, int port, struct addrinfo **sv_addr, int family)
call socket()
int is_same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
int _tcp_client_bind_socket(char *hostname, int sport, int cport, int family)
call socket(), bind(), connect()
int _tcp_server_socket(int port, int family)
call socket(), bind(), listen()
char * get_ipaddr_ipv4(struct in_addr sin_addr)
構造体 → IPv4 アドレス(文字列) use get_ipaddr_byname()
struct sockaddr * make_sockaddr_bynum(unsigned char *addr, int port, int family)
IPv4/IPv6 対応
unsigned char * to_address_num(char *addr, int mode, int mask, int family)
IPv4/IPv6 アドレス(文字列)→ [IPv4/IPv6 アドレス],[ネットマスク](数字8/32Byte)
int recv_wait(int sock, int tm)
int _tcp_connect(int sofd, char *hostname, int port, int family)
call connect()
struct sockaddr_in get_sockaddr_in_bynum(char *ipnum, unsigned short cport)
unsigned char * get_mynetaddr_num(int family)
自分の IPv4/IPv6 ネットワークアドレス(バイナリ)
int udp_recv(int sock, char *rmsg, int size, struct addrinfo *sv_addr)
int get_valid_udp_socket(int min, int max, unsigned short *port)
int _tcp_bind(int sofd, int port, int family)
call bind()
char * get_myipaddr(int family)
自分の [IPv4/IPv6 ネットワークアドレス]/[ネットマスク](文字列)
int udp_recv_wait(int sock, char *rmsg, int size, struct addrinfo *sv_addr, int tm)
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_byname_num(const char *hostname, int family)
ホスト名 → IPv4/IPv6 アドレス(バイナリ)
int recv_wait_twin(int sock1, int sock2, int tm)
int set_block_socket(int sock)
int _tcp_server_socket_setopt(int port, int opt, const void *optval, int optlen, int family)
call socket(), setsockopt(), bind(), listen()
void cleanup_network(void)
char * get_hostname_bystr(const char *addr)
IPv4/IPv6 アドレス(文字列) → ホスト名(文字列)
ネットワーク用ライブラリヘッダ for IPv4/IPv6
#define tcp_client_bind_socket(h, s, c)
#define udp_server_socket(p, a)
#define tcp_server_socket(p)