summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Fuhrmannek2022-02-05 12:17:42 +0100
committerBen Fuhrmannek2022-02-05 12:17:42 +0100
commitdece0e45b7f66cc51bcbe590240eab3f82da900c (patch)
treec717cf1b6510b547a4042315f08402c0b2675bd4
parent68944ff47089f509d6c7fde1bd98cd8c935acdf6 (diff)
convert cidr to string
-rw-r--r--src/sp_network_utils.c27
-rw-r--r--src/sp_network_utils.h1
2 files changed, 28 insertions, 0 deletions
diff --git a/src/sp_network_utils.c b/src/sp_network_utils.c
index 0a26254..943c418 100644
--- a/src/sp_network_utils.c
+++ b/src/sp_network_utils.c
@@ -121,3 +121,30 @@ int get_ip_and_cidr(char *ip, sp_cidr *cidr) {
121 121
122 return 0; 122 return 0;
123} 123}
124
125bool /* success */ get_ip_str(char *dst, size_t dst_len, sp_cidr *cidr) {
126 size_t ipstr_len = 0;
127 void *ip = NULL;
128 switch (cidr->ip_version) {
129 case AF_INET:
130 ipstr_len = INET_ADDRSTRLEN;
131 ip = &cidr->ip.ipv4;
132 break;
133 case AF_INET6:
134 ipstr_len = INET6_ADDRSTRLEN;
135 ip = &cidr->ip.ipv6;
136 break;
137 default:
138 return false;
139 }
140
141 if (dst_len < ipstr_len + 1 + 3 + 1) {
142 return false;
143 }
144 if (!inet_ntop(cidr->ip_version, ip, dst, ipstr_len)) {
145 return false;
146 }
147 ipstr_len = strlen(dst);
148 snprintf(dst + ipstr_len, dst_len - ipstr_len, "/%d", cidr->mask);
149 return true;
150} \ No newline at end of file
diff --git a/src/sp_network_utils.h b/src/sp_network_utils.h
index 2c1062a..69789a6 100644
--- a/src/sp_network_utils.h
+++ b/src/sp_network_utils.h
@@ -3,5 +3,6 @@
3 3
4int get_ip_and_cidr(char *, sp_cidr *); 4int get_ip_and_cidr(char *, sp_cidr *);
5bool cidr_match(const char *, const sp_cidr *); 5bool cidr_match(const char *, const sp_cidr *);
6bool /* success */ get_ip_str(char *dst, size_t dst_len, sp_cidr *cidr);
6 7
7#endif /*SP_NETWORK_UTILS_H*/ 8#endif /*SP_NETWORK_UTILS_H*/