2014-07-11 22:12:11 +02:00
|
|
|
/*
|
2016-03-06 10:30:04 +01:00
|
|
|
This file is part of actube.
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2016-03-06 10:30:04 +01:00
|
|
|
actube is free software: you can redistribute it and/or modify
|
2014-07-11 22:12:11 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
libcapwap is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
2016-03-06 10:30:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief implements sock_addrtostr
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup sock
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2022-08-23 02:35:54 +02:00
|
|
|
#include <sys/un.h>
|
2014-07-11 22:12:11 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#ifdef AF_LINK
|
2016-03-06 11:09:56 +01:00
|
|
|
#include <net/if_dl.h>
|
2014-07-11 22:12:11 +02:00
|
|
|
#endif
|
|
|
|
#ifdef AF_PACKET
|
2016-03-06 11:09:56 +01:00
|
|
|
#include <netpacket/packet.h>
|
2014-07-11 22:12:11 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "sock.h"
|
|
|
|
|
2016-03-06 10:30:04 +01:00
|
|
|
/**
|
|
|
|
* 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
|
2016-03-06 11:09:56 +01:00
|
|
|
*
|
2016-03-06 10:30:04 +01:00
|
|
|
* Works only for sockaddrs of type AF_INET, AF_INET6, AF_PACKET, AF_LINK.
|
|
|
|
*/
|
2016-03-06 11:09:56 +01:00
|
|
|
char *sock_addrtostr(const struct sockaddr *sa, char *s, size_t maxlen, int addport)
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-25 10:07:39 +02:00
|
|
|
char d[512];
|
2014-07-11 22:12:11 +02:00
|
|
|
int port;
|
|
|
|
|
2016-03-06 10:30:04 +01:00
|
|
|
|
2016-03-06 11:09:56 +01:00
|
|
|
switch (sa->sa_family) {
|
2014-07-11 22:12:11 +02:00
|
|
|
case AF_INET:
|
2016-03-06 11:09:56 +01:00
|
|
|
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", d);
|
2014-07-11 22:12:11 +02:00
|
|
|
break;
|
|
|
|
|
2016-03-06 11:09:56 +01:00
|
|
|
case AF_INET6:
|
|
|
|
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;
|
2014-07-11 22:12:11 +02:00
|
|
|
#ifdef AF_LINK
|
|
|
|
case AF_LINK:
|
2016-03-06 11:09:56 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_dl *sl = (struct sockaddr_dl *) sa;
|
|
|
|
sock_hwaddrtostr(((uint8_t *) sl->sdl_data) + sl->sdl_nlen,
|
|
|
|
sl->sdl_alen, s, ":");
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2016-03-06 11:09:56 +01:00
|
|
|
}
|
2014-07-11 22:12:11 +02:00
|
|
|
break;
|
|
|
|
|
2022-08-23 02:35:54 +02:00
|
|
|
#endif /* AF_LINLK */
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
#ifdef AF_PACKET
|
|
|
|
case AF_PACKET:
|
2016-03-06 11:09:56 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *sp = s;
|
|
|
|
struct sockaddr_ll *sl = (struct sockaddr_ll *) sa;
|
|
|
|
for (i = 0; i < sl->sll_halen - 1; i++) {
|
|
|
|
sprintf(sp, "%02X:", sl->sll_addr[i]);
|
|
|
|
sp += 3;
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
2016-03-06 11:09:56 +01:00
|
|
|
sprintf(sp, "%02X", sl->sll_addr[i]);
|
|
|
|
}
|
2014-07-11 22:12:11 +02:00
|
|
|
break;
|
2022-08-23 02:35:54 +02:00
|
|
|
#endif /* AF_PACKET */
|
|
|
|
case AF_UNIX:
|
|
|
|
{
|
|
|
|
struct sockaddr_un *addr = (struct sockaddr_un *) sa;
|
|
|
|
snprintf(s, maxlen, "%s", addr->sun_path);
|
|
|
|
}
|
|
|
|
break;
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
|
2016-03-06 11:09:56 +01:00
|
|
|
default:
|
2022-08-23 02:35:54 +02:00
|
|
|
snprintf(s,maxlen, "Unknown AF: %d", sa->sa_family);
|
2014-07-11 22:12:11 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
2016-03-06 10:30:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|