Added comments, fixed formatting for IPv6 with port number.

FossilOrigin-Name: 3811a844e550a8d4095298c5bab3b32012f60cadcdbbc21d2153dbedf8056b96
This commit is contained in:
7u83@mail.ru 2016-03-06 09:30:04 +00:00
parent 370476b921
commit 2e4eafc44a
1 changed files with 43 additions and 27 deletions

View File

@ -1,7 +1,7 @@
/*
This file is part of libcapwap.
This file is part of actube.
libcapwap is free software: you can redistribute it and/or modify
actube is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
@ -15,6 +15,17 @@
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief implements sock_addrtostr
*/
/**
* @addtogroup sock
* @{
*/
#include <string.h>
#include <stdio.h>
@ -32,40 +43,40 @@
#include "sock.h"
/*
void sock_hwaddrtostr(const uint8_t *haddr,int len,char *dst)
/**
* Convert a struct sockaddr int to a human readable string.
* @param sa sockaddr to convert
* @param s Destination buffer
* @param maxlen Size of destination buffer
* @param addport If true then the port number wil be added to the output
* Works only for sockaddrs of type AF_INET, AF_INET6, AF_PACKET, AF_LINK.
*/
char *sock_addrtostr(const struct sockaddr *sa, char *s, size_t maxlen,int addport)
{
int i;
for (i=0; i<len-1; i++){
sprintf(dst,"%02X:",haddr[i]);
dst+=3;
}
sprintf(dst,"%02X",haddr[i]);
}
*/
char *sock_strsockaddr(const struct sockaddr *sa, char *s, size_t maxlen,int addport)
{
char d[maxlen];
int port;
switch(sa->sa_family) {
case AF_INET:
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), s, maxlen);
port = ((struct sockaddr_in *)sa)->sin_port;
if (addport)
sprintf(s,"%s:%i",s,ntohs(port));
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), d, maxlen);
if (addport){
port = ((struct sockaddr_in *)sa)->sin_port;
sprintf(s,"%s:%i",d,ntohs(port));
}
else
sprintf(s,"%s",s);
sprintf(s,"%s",d);
break;
case AF_INET6:
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), s, maxlen);
break;
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), d, maxlen);
if (addport){
port = ((struct sockaddr_in6 *)sa)->sin6_port;
sprintf(s,"[%s]:%i",d,ntohs(port));
}
else
sprintf(s,"%s",d);
break;
#ifdef AF_LINK
case AF_LINK:
{
@ -99,3 +110,8 @@ char *sock_strsockaddr(const struct sockaddr *sa, char *s, size_t maxlen,int add
}
return s;
}
/**
* @}
*/