33#define _CRT_SECURE_NO_WARNINGS 1
43#include <sys/socket.h>
44#include <netinet/in.h>
53#ifdef HAVE_SOCKADDR_DL_STRUCT
58char *
sock_ntop(
const struct sockaddr *sa, socklen_t salen)
63 switch (sa->sa_family) {
65 const struct sockaddr_in *sin=(
const struct sockaddr_in *)sa;
67 if (
inet_ntop(AF_INET, &sin->sin_addr, str,
sizeof(str)) == NULL)
69 if (ntohs(sin->sin_port) != 0) {
70 snprintf(portstr,
sizeof(portstr) - 1,
":%d",
71 ntohs(sin->sin_port));
72 portstr[
sizeof(portstr) - 1]=
'\0';
81 const struct sockaddr_in6 *sin6=(
const struct sockaddr_in6 *)sa;
84 if (
inet_ntop(AF_INET6, &sin6->sin6_addr, str + 1,
85 sizeof(str) - 1) == NULL)
87 if (ntohs(sin6->sin6_port) != 0) {
88 snprintf(portstr,
sizeof(portstr),
"]:%d",
89 ntohs(sin6->sin6_port));
90 portstr[
sizeof(portstr) - 1]=
'\0';
100 "sock_ntop: unknown AF_xxx: %d, len %d", sa->sa_family,
102 str[
sizeof(str) - 1]=
'\0';
113 if ( (ptr =
sock_ntop(sa, salen)) == NULL)
122 char * host_name = NULL;
123 const char* port=NULL;
129 while ((cp)&&(isspace(*cp))) {
136 while ((*ip_end)&&(*ip_end!=
']')) {
143 host_name=
strndup(cp, ip_end-cp);
151 }
else if (host_name==NULL) {
160 if ((*ip_end==0)&&(port!=NULL)) {
162 host_name=
strndup(cp, port-cp-1);
165 host_name=
strndup(cp, ip_end-cp);
172 struct addrinfo hints, *servinfo, *p;
175 memset(&hints, 0,
sizeof hints);
176 hints.ai_family = AF_UNSPEC;
177 hints.ai_socktype = SOCK_DGRAM;
178 hints.ai_flags = AI_V4MAPPED;
180 if ((rv = getaddrinfo(host_name, port, &hints, &servinfo)) != 0) {
181 fprintf(stderr,
"getaddrinfo: %s\n",
188 for(p = servinfo; p != NULL; p = p->ai_next) {
189 if ((p->ai_family == AF_INET)||(p->ai_family == AF_INET6)) {
190 memcpy(sa, p->ai_addr,
SA_LEN(p->ai_addr));
191 if(host_name==NULL) {
192 switch (p->ai_family) {
194 ((
struct sockaddr_in*)sa)->sin_addr.s_addr = INADDR_ANY;
197 memset(&((
struct sockaddr_in6*)sa)->sin6_addr, 0,
198 sizeof(
struct sockaddr_in6));
209 freeaddrinfo(servinfo);
219 static struct sockaddr_storage sa_s;
220 if (
sock_pton(cp, (
struct sockaddr *)&sa_s)==0) {
221 return (
struct sockaddr *)&sa_s;
230 const char * prefix_begin = NULL;
231 char * prefix = NULL;
234 char * host_name = NULL;
235 const char* port=NULL;
237 if ((!cp)||(!sa)||(!int_prefix)) {
242 while ((cp)&&(isspace(*cp))) {
249 while ((*ip_end)&&(*ip_end!=
']')) {
250 if (*ip_end ==
'/' ){
251 prefix_begin = ip_end+1;
261 host_name=
strndup(cp, prefix_begin-cp-1);
262 prefix =
strndup(prefix_begin, ip_end-prefix_begin);
264 *int_prefix = atoi(prefix);
268 host_name=
strndup(cp, ip_end-cp);
281 }
else if (host_name==NULL) {
290 if ((*ip_end==0)&&(port!=NULL)) {
292 host_name=
strndup(cp, port-cp-1);
295 host_name=
strndup(cp, ip_end-cp);
302 struct addrinfo hints, *servinfo, *p;
305 memset(&hints, 0,
sizeof hints);
306 hints.ai_family = AF_UNSPEC;
307 hints.ai_socktype = SOCK_DGRAM;
308 hints.ai_flags = AI_V4MAPPED;
310 if ((rv = getaddrinfo(host_name, port, &hints, &servinfo)) != 0) {
311 fprintf(stderr,
"getaddrinfo: %s\n",
318 for(p = servinfo; p != NULL; p = p->ai_next) {
319 if ((p->ai_family == AF_INET)||(p->ai_family == AF_INET6)) {
320 memcpy(sa, p->ai_addr,
SA_LEN(p->ai_addr));
321 if(host_name==NULL) {
322 switch (p->ai_family) {
324 memset(&((
struct sockaddr_in6*)sa)->sin6_addr, 0,
325 sizeof(
struct sockaddr_in6));
336 freeaddrinfo(servinfo);
342 if ((sa->sa_family==AF_INET)&&(*int_prefix > 32)) {
347 if ((sa->sa_family==AF_INET6)&&(*int_prefix > 128)) {
struct sockaddr * Sock_pton(const char *cp)
char * sock_ntop(const struct sockaddr *sa, socklen_t salen)
int sock_pton(const char *cp, struct sockaddr *sa)
char * Sock_ntop(const struct sockaddr *sa, socklen_t salen)
int sock_pton_with_prefix(const char *cp, struct sockaddr *sa, int *int_prefix)