From 2e4eafc44a3fefc65e9af577ba5f8fa0aacb2ae6 Mon Sep 17 00:00:00 2001 From: "7u83@mail.ru" <7u83@mail.ru@noemail.net> Date: Sun, 6 Mar 2016 09:30:04 +0000 Subject: [PATCH] Added comments, fixed formatting for IPv6 with port number. FossilOrigin-Name: 3811a844e550a8d4095298c5bab3b32012f60cadcdbbc21d2153dbedf8056b96 --- src/cw/sock_addrtostr.c | 70 +++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/src/cw/sock_addrtostr.c b/src/cw/sock_addrtostr.c index f4972c43..5dbe6fda 100644 --- a/src/cw/sock_addrtostr.c +++ b/src/cw/sock_addrtostr.c @@ -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 . */ + +/** + * @file + * @brief implements sock_addrtostr + */ + +/** + * @addtogroup sock + * @{ + */ + #include #include @@ -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; isa_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; } + +/** + * @} + */ +