James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file re_sa.h Interface to Socket Address |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | #if defined(WIN32) |
| 7 | #include <winsock2.h> |
| 8 | #include <ws2tcpip.h> |
| 9 | #else |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/socket.h> |
| 12 | #include <netinet/in.h> |
| 13 | #endif |
| 14 | |
| 15 | |
| 16 | struct pl; |
| 17 | |
| 18 | /** Socket Address flags */ |
| 19 | enum sa_flag { |
| 20 | SA_ADDR = 1<<0, |
| 21 | SA_PORT = 1<<1, |
| 22 | SA_ALL = SA_ADDR | SA_PORT |
| 23 | }; |
| 24 | |
| 25 | /** Defines a Socket Address */ |
| 26 | struct sa { |
| 27 | union { |
| 28 | struct sockaddr sa; |
| 29 | struct sockaddr_in in; |
| 30 | #ifdef HAVE_INET6 |
| 31 | struct sockaddr_in6 in6; |
| 32 | #endif |
| 33 | uint8_t padding[28]; |
| 34 | } u; |
| 35 | socklen_t len; |
| 36 | }; |
| 37 | |
| 38 | void sa_init(struct sa *sa, int af); |
| 39 | int sa_set(struct sa *sa, const struct pl *addr, uint16_t port); |
| 40 | int sa_set_str(struct sa *sa, const char *addr, uint16_t port); |
| 41 | void sa_set_in(struct sa *sa, uint32_t addr, uint16_t port); |
| 42 | void sa_set_in6(struct sa *sa, const uint8_t *addr, uint16_t port); |
| 43 | int sa_set_sa(struct sa *sa, const struct sockaddr *s); |
| 44 | void sa_set_port(struct sa *sa, uint16_t port); |
| 45 | int sa_decode(struct sa *sa, const char *str, size_t len); |
| 46 | |
| 47 | int sa_af(const struct sa *sa); |
| 48 | uint32_t sa_in(const struct sa *sa); |
| 49 | void sa_in6(const struct sa *sa, uint8_t *addr); |
| 50 | int sa_ntop(const struct sa *sa, char *buf, int size); |
| 51 | uint16_t sa_port(const struct sa *sa); |
| 52 | bool sa_isset(const struct sa *sa, int flag); |
| 53 | uint32_t sa_hash(const struct sa *sa, int flag); |
| 54 | |
| 55 | void sa_cpy(struct sa *dst, const struct sa *src); |
| 56 | bool sa_cmp(const struct sa *l, const struct sa *r, int flag); |
| 57 | |
| 58 | bool sa_is_linklocal(const struct sa *sa); |
| 59 | bool sa_is_loopback(const struct sa *sa); |
| 60 | bool sa_is_any(const struct sa *sa); |
| 61 | |
| 62 | struct re_printf; |
| 63 | int sa_print_addr(struct re_printf *pf, const struct sa *sa); |