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 |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 5 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 6 | /*! \defgroup socket Socket convenience functions |
| 7 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 8 | * \file socket.h */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 9 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 10 | #include <stdint.h> |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 11 | #include <stdbool.h> |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 12 | #include <stddef.h> |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 13 | |
| 14 | struct sockaddr; |
Holger Hans Peter Freyther | 58d3153 | 2012-03-16 09:18:12 +0100 | [diff] [blame] | 15 | struct osmo_fd; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 16 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 17 | /* flags for osmo_sock_init. */ |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 18 | /*! connect the socket to a remote peer */ |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 19 | #define OSMO_SOCK_F_CONNECT (1 << 0) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 20 | /*! bind the socket to a local address/port */ |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 21 | #define OSMO_SOCK_F_BIND (1 << 1) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 22 | /*! switch socket to non-blocking mode */ |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 23 | #define OSMO_SOCK_F_NONBLOCK (1 << 2) |
Harald Welte | bc43a62 | 2017-07-13 16:20:21 +0200 | [diff] [blame] | 24 | /*! disable multiast loop (IP_MULTICAST_LOOP) */ |
| 25 | #define OSMO_SOCK_F_NO_MCAST_LOOP (1 << 3) |
Harald Welte | 37d204a | 2017-07-13 16:33:16 +0200 | [diff] [blame] | 26 | /*! disable receiving all multiast even for non-subscribed groups */ |
| 27 | #define OSMO_SOCK_F_NO_MCAST_ALL (1 << 4) |
Philipp Maier | 73196e7 | 2018-08-23 20:11:50 +0200 | [diff] [blame] | 28 | /*! use SO_REUSEADDR on UDP ports (required for multicast) */ |
| 29 | #define OSMO_SOCK_F_UDP_REUSEADDR (1 << 5) |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 30 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 31 | 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] | 32 | const char *host, uint16_t port, unsigned int flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 33 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 34 | int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto, |
| 35 | const char *local_host, uint16_t local_port, |
| 36 | const char *remote_host, uint16_t remote_port, unsigned int flags); |
| 37 | |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 38 | 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] | 39 | const char *host, uint16_t port, unsigned int flags); |
Harald Welte | 68b1574 | 2011-05-22 21:47:29 +0200 | [diff] [blame] | 40 | |
Pau Espin Pedrol | 75989e6 | 2017-05-26 12:39:53 +0200 | [diff] [blame] | 41 | int osmo_sock_init2_ofd(struct osmo_fd *ofd, int family, int type, int proto, |
| 42 | const char *local_host, uint16_t local_port, |
| 43 | const char *remote_host, uint16_t remote_port, unsigned int flags); |
| 44 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 45 | int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type, |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 46 | uint8_t proto, unsigned int flags); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 47 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 48 | int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 49 | |
Neels Hofmeyr | 59f4caf | 2018-07-19 22:13:19 +0200 | [diff] [blame] | 50 | unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port, |
| 51 | const struct sockaddr *sa); |
| 52 | |
Álvaro Neira Ayuso | 5ade61a | 2014-03-24 13:02:00 +0100 | [diff] [blame] | 53 | int osmo_sock_unix_init(uint16_t type, uint8_t proto, |
| 54 | const char *socket_path, unsigned int flags); |
| 55 | |
| 56 | int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto, |
| 57 | const char *socket_path, unsigned int flags); |
| 58 | |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 59 | char *osmo_sock_get_name(void *ctx, int fd); |
Oliver Smith | 7acd5d0 | 2018-10-25 11:16:36 +0200 | [diff] [blame] | 60 | int osmo_sock_get_local_ip(int fd, char *host, size_t len); |
| 61 | int osmo_sock_get_local_ip_port(int fd, char *port, size_t len); |
| 62 | int osmo_sock_get_remote_ip(int fd, char *host, size_t len); |
| 63 | int osmo_sock_get_remote_ip_port(int fd, char *port, size_t len); |
| 64 | |
Harald Welte | 48f5583 | 2017-01-26 00:03:10 +0100 | [diff] [blame] | 65 | |
Harald Welte | e30d7e6 | 2017-07-13 16:02:50 +0200 | [diff] [blame] | 66 | int osmo_sock_mcast_loop_set(int fd, bool enable); |
| 67 | int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl); |
| 68 | int osmo_sock_mcast_all_set(int fd, bool enable); |
| 69 | int osmo_sock_mcast_subscribe(int fd, const char *grp_addr); |
| 70 | |
Philipp Maier | 2d2490e | 2017-10-20 19:41:26 +0200 | [diff] [blame] | 71 | int osmo_sock_local_ip(char *local_ip, const char *remote_ip); |
| 72 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 73 | /*! @} */ |