summaryrefslogtreecommitdiff
path: root/toxcore/network.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/network.c')
-rw-r--r--toxcore/network.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/toxcore/network.c b/toxcore/network.c
index 35ef5221..5539de6b 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -784,6 +784,9 @@ void ipport_unpack(IP_Port *target, const uint8_t *data)
784/* ip_ntoa 784/* ip_ntoa
785 * converts ip into a string 785 * converts ip into a string
786 * uses a static buffer, so mustn't used multiple times in the same output 786 * uses a static buffer, so mustn't used multiple times in the same output
787 *
788 * IPv6 addresses are enclosed into square brackets, i.e. "[IPv6]"
789 * writes error message into the buffer on error
787 */ 790 */
788/* there would be INET6_ADDRSTRLEN, but it might be too short for the error message */ 791/* there would be INET6_ADDRSTRLEN, but it might be too short for the error message */
789static char addresstext[96]; 792static char addresstext[96];
@@ -816,6 +819,38 @@ const char *ip_ntoa(const IP *ip)
816} 819}
817 820
818/* 821/*
822 * ip_parse_addr
823 * parses IP structure into an address string
824 *
825 * input
826 * ip: ip of AF_INET or AF_INET6 families
827 * length: length of the address buffer
828 * Must be at least INET_ADDRSTRLEN for AF_INET
829 * and INET6_ADDRSTRLEN for AF_INET6
830 *
831 * output
832 * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6)
833 *
834 * returns 1 on success, 0 on failure
835 */
836int ip_parse_addr(const IP *ip, char *address, size_t length)
837{
838 if (!address || !ip) {
839 return 0;
840 }
841
842 if (ip->family == AF_INET) {
843 struct in_addr *addr = (struct in_addr *)&ip->ip4;
844 return inet_ntop(ip->family, addr, address, length) != NULL;
845 } else if (ip->family == AF_INET6) {
846 struct in6_addr *addr = (struct in6_addr *)&ip->ip6;
847 return inet_ntop(ip->family, addr, address, length) != NULL;
848 }
849
850 return 0;
851}
852
853/*
819 * addr_parse_ip 854 * addr_parse_ip
820 * directly parses the input into an IP structure 855 * directly parses the input into an IP structure
821 * tries IPv4 first, then IPv6 856 * tries IPv4 first, then IPv6