Neels Hofmeyr | 0c7826e | 2019-02-25 02:45:06 +0100 | [diff] [blame] | 1 | /*! \file sockaddr_str.h |
| 2 | * Common API to store an IP address and port. |
| 3 | */ |
| 4 | /* |
| 5 | * (C) 2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 6 | * |
| 7 | * Author: neels@hofmeyr.de |
| 8 | * |
| 9 | * All Rights Reserved |
| 10 | * |
| 11 | * SPDX-License-Identifier: GPL-2.0+ |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #pragma once |
| 30 | |
| 31 | #include <stdint.h> |
| 32 | #include <stdbool.h> |
| 33 | #include <arpa/inet.h> |
| 34 | |
| 35 | struct in_addr; |
| 36 | struct in6_addr; |
| 37 | struct sockaddr_storage; |
| 38 | struct sockaddr_in; |
| 39 | struct sockaddr_in6; |
| 40 | |
| 41 | /*! \defgroup sockaddr_str IP address/port utilities. |
| 42 | * @{ |
| 43 | * \file sockaddr_str.h |
| 44 | */ |
| 45 | |
| 46 | int osmo_ip_str_type(const char *ip); |
| 47 | |
| 48 | struct osmo_sockaddr_str { |
| 49 | /*! AF_INET for IPv4 address, or AF_INET6 for IPv6 address. */ |
| 50 | int af; |
| 51 | /*! NUL terminated string of the IPv4 or IPv6 address. */ |
| 52 | char ip[INET6_ADDRSTRLEN]; |
| 53 | /*! Port number */ |
| 54 | uint16_t port; |
| 55 | }; |
| 56 | |
| 57 | /*! Format string to print an osmo_sockaddr_str. |
| 58 | * |
| 59 | * For example: |
| 60 | * |
| 61 | * struct osmo_sockaddr_str *my_sockaddr_str = ...; |
| 62 | * printf("got " OSMO_SOCKADDR_STR_FMT "\n", OSMO_SOCKADDR_STR_FMT_ARGS(my_sockaddr_str)); |
| 63 | */ |
| 64 | #define OSMO_SOCKADDR_STR_FMT "%s:%u" |
| 65 | #define OSMO_SOCKADDR_STR_FMT_ARGS(R) ((R)->ip ? : ""), (R)->port |
| 66 | |
| 67 | bool osmo_sockaddr_str_is_set(const struct osmo_sockaddr_str *sockaddr_str); |
| 68 | |
| 69 | int osmo_sockaddr_str_from_str(struct osmo_sockaddr_str *sockaddr_str, const char *ip, uint16_t port); |
| 70 | |
| 71 | int osmo_sockaddr_str_from_in_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in_addr *addr, uint16_t port); |
| 72 | int osmo_sockaddr_str_from_in6_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in6_addr *addr, uint16_t port); |
| 73 | int osmo_sockaddr_str_from_32(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port); |
| 74 | int osmo_sockaddr_str_from_32n(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port); |
| 75 | int osmo_sockaddr_str_from_sockaddr_in(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in *src); |
| 76 | int osmo_sockaddr_str_from_sockaddr_in6(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in6 *src); |
| 77 | int osmo_sockaddr_str_from_sockaddr(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_storage *src); |
| 78 | |
| 79 | int osmo_sockaddr_str_to_in_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in_addr *dst); |
| 80 | int osmo_sockaddr_str_to_in6_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in6_addr *dst); |
| 81 | int osmo_sockaddr_str_to_32(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip); |
| 82 | int osmo_sockaddr_str_to_32n(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip); |
| 83 | int osmo_sockaddr_str_to_sockaddr_in(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in *dst); |
| 84 | int osmo_sockaddr_str_to_sockaddr_in6(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in6 *dst); |
| 85 | int osmo_sockaddr_str_to_sockaddr(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_storage *dst); |
| 86 | |
| 87 | /*! @} */ |