summaryrefslogtreecommitdiff
path: root/network.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2022-08-02 22:18:07 -0300
committermrb0nk500 <b0nk@b0nk.xyz>2022-08-02 22:18:07 -0300
commit7aa4b350e62d3c145015ca8a5d34ee9a45bc42f3 (patch)
tree3e5525f5f54e975d6d3738ac294578f61c0e62a0 /network.c
parentafe414d90ef6ce9a1c8437629b8299c126fd7044 (diff)
network: Add `addr_to_str()`
This function takes a `sockaddr`, along with the length of the `sockaddr`, and creates a string from that address.
Diffstat (limited to 'network.c')
-rw-r--r--network.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/network.c b/network.c
index 6bfd9d7..f7b5ecb 100644
--- a/network.c
+++ b/network.c
@@ -5,3 +5,16 @@
#include <unistd.h>
#include "macros.h"
#include "network.h"
+
+const char *addr_to_str(struct sockaddr *sa, socklen_t len) {
+ const int family = sa->sa_family;
+ static char ip[INET6_ADDRSTRLEN];
+ char *str = ip;
+
+ switch (family) {
+ case AF_INET6 : inet_ntop(family, &((struct sockaddr_in6 *)sa)->sin6_addr, ip, len); break;
+ case AF_INET : inet_ntop(family, &((struct sockaddr_in *)sa)->sin_addr, ip, len); break;
+ case AF_UNIX : str = ((struct sockaddr_un *)sa)->sun_path; break;
+ }
+ return str;
+}