Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file socket.h |
| 2 | * Osmocom socket convenience functions. */ |
| 3 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 4 | #pragma once |
Alexander Couzens | 8419bb2 | 2020-06-07 18:10:13 +0200 | [diff] [blame] | 5 | #if (!EMBEDDED) |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 6 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 7 | /*! \defgroup socket Socket convenience functions |
| 8 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 9 | * \file socket.h */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 10 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 11 | #include <stdint.h> |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 12 | #include <stdbool.h> |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 13 | #include <stddef.h> |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 14 | |
Max | b3e9b31 | 2019-02-04 14:06:31 +0100 | [diff] [blame] | 15 | #include <arpa/inet.h> |
| 16 | |
| 17 | /*! maximum length of a socket name ("r=1.2.3.4:123<->l=5.6.7.8:987") */ |
| 18 | #define OSMO_SOCK_NAME_MAXLEN (2 + INET6_ADDRSTRLEN + 1 + 5 + 3 + 2 + INET6_ADDRSTRLEN + 1 + 5 + 1) |
Max | b3e9b31 | 2019-02-04 14:06:31 +0100 | [diff] [blame] | 19 | |
Max | 9d7a247 | 2018-11-20 15:18:31 +0100 | [diff] [blame] | 20 | struct sockaddr_in; |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 21 | struct sockaddr; |
Holger Hans Peter Freyther | 58d3153 | 2012-03-16 09:18:12 +0100 | [diff] [blame] | 22 | struct osmo_fd; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 23 | |
Alexander Couzens | 8419bb2 | 2020-06-07 18:10:13 +0200 | [diff] [blame] | 24 | struct osmo_sockaddr { |
| 25 | union { |
| 26 | struct sockaddr sa; |
| 27 | struct sockaddr_storage sas; |
| 28 | struct sockaddr_in sin; |
| 29 | struct sockaddr_in6 sin6; |
| 30 | } u; |
| 31 | }; |
| 32 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 33 | /* flags for osmo_sock_init. */ |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 34 | /*! connect the socket to a remote peer */ |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 35 | #define OSMO_SOCK_F_CONNECT (1 << 0) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 36 | /*! bind the socket to a local address/port */ |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 37 | #define OSMO_SOCK_F_BIND (1 << 1) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 38 | /*! switch socket to non-blocking mode */ |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 39 | #define OSMO_SOCK_F_NONBLOCK (1 << 2) |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 40 | /*! disable multiast loop (IP_MULTICAST_LOOP) */ |
| 41 | #define OSMO_SOCK_F_NO_MCAST_LOOP (1 << 3) |
Harald Welte | 37d204a | 2017-07-13 16:33:16 +0200 | [diff] [blame] | 42 | /*! disable receiving all multiast even for non-subscribed groups */ |
| 43 | #define OSMO_SOCK_F_NO_MCAST_ALL (1 << 4) |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 44 | /*! use SO_REUSEADDR on UDP ports (required for multicast) */ |
| 45 | #define OSMO_SOCK_F_UDP_REUSEADDR (1 << 5) |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 46 | |
Harald Welte | c545ff6 | 2021-04-28 13:09:49 +0200 | [diff] [blame] | 47 | /*! use OSMO_SOCK_F_DSCP(x) to set IP DSCP 'x' for packets transmitted on the socket */ |
| 48 | #define OSMO_SOCK_F_DSCP(x) (((x)&0x3f) << 24) |
| 49 | #define GET_OSMO_SOCK_F_DSCP(f) (((f) >> 24) & 0x3f) |
| 50 | |
| 51 | /*! use OSMO_SOCK_F_PRIO(x) to set priority 'x' for packets transmitted on the socket */ |
| 52 | #define OSMO_SOCK_F_PRIO(x) (((x)&0xff) << 16) |
| 53 | #define GET_OSMO_SOCK_F_PRIO(f) (((f) >> 16) & 0xff) |
| 54 | |
| 55 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 56 | /*! maximum number of local or remote addresses supported by an osmo_sock instance */ |
| 57 | #define OSMO_SOCK_MAX_ADDRS 32 |
| 58 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 59 | int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 60 | const char *host, uint16_t port, unsigned int flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 61 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 62 | int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto, |
| 63 | const char *local_host, uint16_t local_port, |
| 64 | const char *remote_host, uint16_t remote_port, unsigned int flags); |
| 65 | |
Pau Espin Pedrol | 3f464fc | 2019-10-10 17:38:35 +0200 | [diff] [blame] | 66 | int osmo_sock_init2_multiaddr(uint16_t family, uint16_t type, uint8_t proto, |
| 67 | const char **local_hosts, size_t local_hosts_cnt, uint16_t local_port, |
| 68 | const char **remote_hosts, size_t remote_hosts_cnt, uint16_t remote_port, unsigned int flags); |
| 69 | |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 70 | int osmo_sock_init_osa(uint16_t type, uint8_t proto, |
| 71 | const struct osmo_sockaddr *local, |
| 72 | const struct osmo_sockaddr *remote, |
| 73 | unsigned int flags); |
| 74 | |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 75 | int osmo_sock_init_ofd(struct osmo_fd *ofd, int family, int type, int proto, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 76 | const char *host, uint16_t port, unsigned int flags); |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 77 | |
Pau Espin Pedrol | 75989e6 | 2017-05-26 12:39:53 +0200 | [diff] [blame] | 78 | int osmo_sock_init2_ofd(struct osmo_fd *ofd, int family, int type, int proto, |
| 79 | const char *local_host, uint16_t local_port, |
| 80 | const char *remote_host, uint16_t remote_port, unsigned int flags); |
| 81 | |
Alexander Couzens | 43957e6 | 2020-08-01 21:56:45 +0200 | [diff] [blame] | 82 | int osmo_sock_init_osa_ofd(struct osmo_fd *ofd, int type, int proto, |
| 83 | const struct osmo_sockaddr *local, |
| 84 | const struct osmo_sockaddr *remote, |
| 85 | unsigned int flags); |
| 86 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 87 | int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 88 | uint8_t proto, unsigned int flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 89 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 90 | int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 91 | |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 92 | unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 93 | const struct sockaddr *sa); |
Max | 9d7a247 | 2018-11-20 15:18:31 +0100 | [diff] [blame] | 94 | size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 95 | const struct sockaddr_in *sin); |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 96 | |
Pau Espin Pedrol | 5cc4fe4 | 2020-08-31 12:52:06 +0200 | [diff] [blame] | 97 | const char *osmo_sockaddr_ntop(const struct sockaddr *sa, char *dst); |
| 98 | uint16_t osmo_sockaddr_port(const struct sockaddr *sa); |
Neels Hofmeyr | 9c7f7f8 | 2022-01-11 19:25:40 +0100 | [diff] [blame] | 99 | void osmo_sockaddr_set_port(struct sockaddr *sa, uint16_t port); |
Pau Espin Pedrol | 5cc4fe4 | 2020-08-31 12:52:06 +0200 | [diff] [blame] | 100 | |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 101 | int osmo_sock_unix_init(uint16_t type, uint8_t proto, |
| 102 | const char *socket_path, unsigned int flags); |
| 103 | |
| 104 | int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto, |
| 105 | const char *socket_path, unsigned int flags); |
| 106 | |
Harald Welte | c0dfc9d | 2019-03-18 18:29:43 +0100 | [diff] [blame] | 107 | char *osmo_sock_get_name(const void *ctx, int fd); |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 108 | const char *osmo_sock_get_name2(int fd); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 109 | char *osmo_sock_get_name2_c(const void *ctx, int fd); |
Neels Hofmeyr | 0145751 | 2018-12-12 01:48:54 +0100 | [diff] [blame] | 110 | int osmo_sock_get_name_buf(char *str, size_t str_len, int fd); |
| 111 | int osmo_sock_get_ip_and_port(int fd, char *ip, size_t ip_len, char *port, size_t port_len, bool local); |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 112 | int osmo_sock_get_local_ip(int fd, char *host, size_t len); |
| 113 | int osmo_sock_get_local_ip_port(int fd, char *port, size_t len); |
| 114 | int osmo_sock_get_remote_ip(int fd, char *host, size_t len); |
| 115 | int osmo_sock_get_remote_ip_port(int fd, char *port, size_t len); |
| 116 | |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 117 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 118 | int osmo_sock_mcast_loop_set(int fd, bool enable); |
| 119 | int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl); |
| 120 | int osmo_sock_mcast_all_set(int fd, bool enable); |
Harald Welte | 44b9926 | 2020-03-07 14:59:05 +0100 | [diff] [blame] | 121 | int osmo_sock_mcast_iface_set(int fd, const char *ifname); |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 122 | int osmo_sock_mcast_subscribe(int fd, const char *grp_addr); |
| 123 | |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 124 | int osmo_sock_local_ip(char *local_ip, const char *remote_ip); |
| 125 | |
Alexander Couzens | 0f36421 | 2020-07-27 22:33:01 +0200 | [diff] [blame] | 126 | int osmo_sockaddr_local_ip(struct osmo_sockaddr *local_ip, |
| 127 | const struct osmo_sockaddr *remote_ip); |
Vadim Yanitskiy | def5a40 | 2020-10-09 21:40:47 +0700 | [diff] [blame] | 128 | int osmo_sockaddr_cmp(const struct osmo_sockaddr *a, |
| 129 | const struct osmo_sockaddr *b); |
Alexander Couzens | e4181ea | 2020-07-20 00:03:16 +0200 | [diff] [blame] | 130 | |
Neels Hofmeyr | cf7f792 | 2022-02-02 03:08:40 +0100 | [diff] [blame] | 131 | int osmo_sockaddr_to_octets(uint8_t *dst, size_t dst_maxlen, const struct osmo_sockaddr *os); |
| 132 | int osmo_sockaddr_from_octets(struct osmo_sockaddr *os, const void *src, size_t src_len); |
| 133 | |
Alexander Couzens | 80788fa | 2020-10-12 01:11:20 +0200 | [diff] [blame] | 134 | const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *sockaddr); |
| 135 | char *osmo_sockaddr_to_str_buf(char *buf, size_t buf_len, |
| 136 | const struct osmo_sockaddr *sockaddr); |
Neels Hofmeyr | 09d6574 | 2021-12-01 10:25:02 +0100 | [diff] [blame] | 137 | int osmo_sockaddr_to_str_buf2(char *buf, size_t buf_len, const struct osmo_sockaddr *sockaddr); |
| 138 | char *osmo_sockaddr_to_str_c(void *ctx, const struct osmo_sockaddr *sockaddr); |
Alexander Couzens | 80788fa | 2020-10-12 01:11:20 +0200 | [diff] [blame] | 139 | |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 140 | int osmo_sock_set_dscp(int fd, uint8_t dscp); |
Harald Welte | ecc0bd8 | 2021-04-27 22:24:08 +0200 | [diff] [blame] | 141 | int osmo_sock_set_priority(int fd, int prio); |
Harald Welte | ce53e03 | 2021-04-27 21:44:34 +0200 | [diff] [blame] | 142 | |
Alexander Couzens | 8419bb2 | 2020-06-07 18:10:13 +0200 | [diff] [blame] | 143 | #endif /* (!EMBEDDED) */ |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 144 | /*! @} */ |