diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-06-27 12:52:31 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-06-27 12:52:31 -0400 |
commit | b9b05e437d09c4d06b554d0484c7ae1a3aa1d647 (patch) | |
tree | ea45c980cb972a74be77ae7813dc249ff31ab4e8 /src/lib/net/ipv4.c | |
parent | 300e3bebd19b2686501d5400da1ca4a748027ba4 (diff) | |
parent | d893be190fc244330543c9e98613a3f0daebc6ed (diff) | |
download | tor-b9b05e437d09c4d06b554d0484c7ae1a3aa1d647.tar.gz tor-b9b05e437d09c4d06b554d0484c7ae1a3aa1d647.zip |
Merge branch 'net_refactor'
Diffstat (limited to 'src/lib/net/ipv4.c')
-rw-r--r-- | src/lib/net/ipv4.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/lib/net/ipv4.c b/src/lib/net/ipv4.c new file mode 100644 index 0000000000..18e69761e2 --- /dev/null +++ b/src/lib/net/ipv4.c @@ -0,0 +1,52 @@ +/* Copyright (c) 2003-2004, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#include "orconfig.h" +#include "lib/cc/torint.h" +#include "lib/net/ipv4.h" +#include "lib/string/printf.h" +#include "lib/string/scanf.h" + +#ifdef HAVE_ARPA_INET_H +#include <arpa/inet.h> +#endif +#ifdef _WIN32 +#include <winsock2.h> +#endif + +/** Set *addr to the IP address (in dotted-quad notation) stored in *str. + * Return 1 on success, 0 if *str is badly formatted. + * (Like inet_aton(str,addr), but works on Windows and Solaris.) + */ +int +tor_inet_aton(const char *str, struct in_addr* addr) +{ + unsigned a,b,c,d; + char more; + if (tor_sscanf(str, "%3u.%3u.%3u.%3u%c", &a,&b,&c,&d,&more) != 4) + return 0; + if (a > 255) return 0; + if (b > 255) return 0; + if (c > 255) return 0; + if (d > 255) return 0; + addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d); + return 1; +} + +/** Given an IPv4 in_addr struct *<b>in</b> (in network order, as usual), + * write it as a string into the <b>buf_len</b>-byte buffer in + * <b>buf</b>. Returns a non-negative integer on success. + * Returns -1 on failure. + */ +int +tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len) +{ + uint32_t a = ntohl(in->s_addr); + return tor_snprintf(buf, buf_len, "%d.%d.%d.%d", + (int)(uint8_t)((a>>24)&0xff), + (int)(uint8_t)((a>>16)&0xff), + (int)(uint8_t)((a>>8 )&0xff), + (int)(uint8_t)((a )&0xff)); +} |