From f0448448b01cf04cc7b04a81c4480734c5bfe84b Mon Sep 17 00:00:00 2001 From: zfl9 Date: Sat, 31 Aug 2019 13:55:12 +0800 Subject: add netutils --- Makefile | 2 +- netutils.c | 43 +++++++++++++++++++++++++++++++++++++++++++ netutils.h | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 netutils.c create mode 100644 netutils.h diff --git a/Makefile b/Makefile index ff112d4..99eaa57 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = gcc CFLAGS = -std=c99 -Wall -Wextra -O3 -SRCS = dns2tcp.c +SRCS = netutils.c dns2tcp.c OBJS = $(SRCS:.c=.o) MAIN = dns2tcp DESTDIR = /usr/local/bin diff --git a/netutils.c b/netutils.c new file mode 100644 index 0000000..03fbb44 --- /dev/null +++ b/netutils.c @@ -0,0 +1,43 @@ +#define _GNU_SOURCE +#include "netutils.h" +#include +#undef _GNU_SOURCE + +/* build ipv4 socket address from ipstr and portno */ +void build_ipv4_addr(skaddr4_t *addr, const char *ipstr, portno_t portno) { + addr->sin_family = AF_INET; + inet_pton(AF_INET, ipstr, &addr->sin_addr); + addr->sin_port = htons(portno); +} + +/* build ipv6 socket address from ipstr and portno */ +void build_ipv6_addr(skaddr6_t *addr, const char *ipstr, portno_t portno) { + addr->sin6_family = AF_INET6; + inet_pton(AF_INET6, ipstr, &addr->sin6_addr); + addr->sin6_port = htons(portno); +} + +/* parse ipstr and portno from ipv4 socket address */ +void parse_ipv4_addr(const skaddr4_t *addr, char *ipstr, portno_t *portno) { + inet_ntop(AF_INET, &addr->sin_addr, ipstr, IP4STRLEN); + *portno = ntohs(addr->sin_port); +} + +/* parse ipstr and portno from ipv6 socket address */ +void parse_ipv6_addr(const skaddr6_t *addr, char *ipstr, portno_t *portno) { + inet_ntop(AF_INET6, &addr->sin6_addr, ipstr, IP6STRLEN); + *portno = ntohs(addr->sin6_port); +} + +/* AF_INET or AF_INET6 or -1(invalid ip string) */ +int get_ipstr_family(const char *ipstr) { + if (!ipstr) return -1; + uint8_t ipaddr[16]; /* save ipv4/ipv6 addr */ + if (inet_pton(AF_INET, ipstr, &ipaddr) == 1) { + return AF_INET; + } else if (inet_pton(AF_INET6, ipstr, &ipaddr) == 1) { + return AF_INET6; + } else { + return -1; + } +} diff --git a/netutils.h b/netutils.h new file mode 100644 index 0000000..1645701 --- /dev/null +++ b/netutils.h @@ -0,0 +1,38 @@ +#ifndef DNS2TCP_NETUTILS_H +#define DNS2TCP_NETUTILS_H + +#define _GNU_SOURCE +#include +#include +#undef _GNU_SOURCE + +/* ipaddr string len */ +#define IP4STRLEN INET_ADDRSTRLEN +#define IP6STRLEN INET6_ADDRSTRLEN + +/* portno string len */ +#define PORTSTRLEN 6 + +/* port number typedef */ +typedef uint16_t portno_t; + +/* sockaddr type alias */ +typedef struct sockaddr_in skaddr4_t; +typedef struct sockaddr_in6 skaddr6_t; + +/* build ipv4 socket address from ipstr and portno */ +void build_ipv4_addr(skaddr4_t *addr, const char *ipstr, portno_t portno); + +/* build ipv6 socket address from ipstr and portno */ +void build_ipv6_addr(skaddr6_t *addr, const char *ipstr, portno_t portno); + +/* parse ipstr and portno from ipv4 socket address */ +void parse_ipv4_addr(const skaddr4_t *addr, char *ipstr, portno_t *portno); + +/* parse ipstr and portno from ipv6 socket address */ +void parse_ipv6_addr(const skaddr6_t *addr, char *ipstr, portno_t *portno); + +/* AF_INET or AF_INET6 or -1(invalid ip string) */ +int get_ipstr_family(const char *ipstr); + +#endif -- cgit v1.2.3