Added comments, fixed formatting for IPv6 with port number.
FossilOrigin-Name: 3811a844e550a8d4095298c5bab3b32012f60cadcdbbc21d2153dbedf8056b96
This commit is contained in:
		@ -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,39 +43,39 @@
 | 
			
		||||
 | 
			
		||||
#include "sock.h"
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
void sock_hwaddrtostr(const uint8_t *haddr,int len,char *dst)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
	for (i=0; i<len-1; i++){
 | 
			
		||||
		sprintf(dst,"%02X:",haddr[i]);
 | 
			
		||||
		dst+=3;
 | 
			
		||||
	}
 | 
			
		||||
	sprintf(dst,"%02X",haddr[i]);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
/**
 | 
			
		||||
 * 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_strsockaddr(const struct sockaddr *sa, char *s, size_t maxlen,int addport)
 | 
			
		||||
char *sock_addrtostr(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);
 | 
			
		||||
			inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), d, maxlen);
 | 
			
		||||
			if (addport){
 | 
			
		||||
				port = ((struct sockaddr_in *)sa)->sin_port;
 | 
			
		||||
			if (addport) 
 | 
			
		||||
				sprintf(s,"%s:%i",s,ntohs(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);
 | 
			
		||||
			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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @}
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user