blob: 44929227a45fe40915c1378c43f6304ea0d35733 [file] [log] [blame]
James Kuszmaul82f6c042021-01-17 11:30:16 -08001/**
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
16struct pl;
17
18/** Socket Address flags */
19enum 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 */
26struct 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
38void sa_init(struct sa *sa, int af);
39int sa_set(struct sa *sa, const struct pl *addr, uint16_t port);
40int sa_set_str(struct sa *sa, const char *addr, uint16_t port);
41void sa_set_in(struct sa *sa, uint32_t addr, uint16_t port);
42void sa_set_in6(struct sa *sa, const uint8_t *addr, uint16_t port);
43int sa_set_sa(struct sa *sa, const struct sockaddr *s);
44void sa_set_port(struct sa *sa, uint16_t port);
45int sa_decode(struct sa *sa, const char *str, size_t len);
46
47int sa_af(const struct sa *sa);
48uint32_t sa_in(const struct sa *sa);
49void sa_in6(const struct sa *sa, uint8_t *addr);
50int sa_ntop(const struct sa *sa, char *buf, int size);
51uint16_t sa_port(const struct sa *sa);
52bool sa_isset(const struct sa *sa, int flag);
53uint32_t sa_hash(const struct sa *sa, int flag);
54
55void sa_cpy(struct sa *dst, const struct sa *src);
56bool sa_cmp(const struct sa *l, const struct sa *r, int flag);
57
58bool sa_is_linklocal(const struct sa *sa);
59bool sa_is_loopback(const struct sa *sa);
60bool sa_is_any(const struct sa *sa);
61
62struct re_printf;
63int sa_print_addr(struct re_printf *pf, const struct sa *sa);