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