Oliver Smith | 06455ea | 2019-11-20 10:56:35 +0100 | [diff] [blame] | 1 | /* Copyright 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <arpa/inet.h> |
| 27 | #include <netinet/in.h> |
| 28 | #include <netdb.h> |
| 29 | #include <stdbool.h> |
| 30 | #include <talloc.h> |
| 31 | #include <osmocom/core/utils.h> |
| 32 | #include <osmocom/core/select.h> |
| 33 | #include <osmocom/hlr/logging.h> |
| 34 | #include <osmocom/mslookup/mdns_sock.h> |
| 35 | |
| 36 | /*! Open socket to send and receive multicast data. |
| 37 | * |
| 38 | * The socket is opened with SO_REUSEADDR, so we can bind to the same IP and port multiple times. This socket receives |
| 39 | * everything sent to that multicast IP/port, including its own data data sent from osmo_mdns_sock_send(). So whenever |
| 40 | * sending something, the receive callback will be called with the same data and should discard it. |
| 41 | * |
| 42 | * \param[in] ip multicast IPv4 or IPv6 address. |
| 43 | * \param[in] port port number. |
| 44 | * \param[in] cb callback for incoming data that will be passed to osmo_fd_setup (should read from osmo_fd->fd). |
| 45 | * \param[in] data userdata passed to osmo_fd (available in cb as osmo_fd->data). |
| 46 | * \param[in] priv_nr additional userdata integer passed to osmo_fd (available in cb as osmo_fd->priv_nr). |
| 47 | * \returns allocated osmo_mdns_sock, NULL on error. |
| 48 | */ |
| 49 | struct osmo_mdns_sock *osmo_mdns_sock_init(void *ctx, const char *ip, unsigned int port, |
| 50 | int (*cb)(struct osmo_fd *fd, unsigned int what), |
| 51 | void *data, unsigned int priv_nr) |
| 52 | { |
| 53 | struct osmo_mdns_sock *ret; |
| 54 | int sock, rc; |
| 55 | struct addrinfo hints = {0}; |
| 56 | struct ip_mreq multicast_req = {0}; |
| 57 | in_addr_t iface = INADDR_ANY; |
| 58 | char portbuf[10]; |
| 59 | int y = 1; |
| 60 | |
| 61 | snprintf(portbuf, sizeof(portbuf) -1, "%u", port); |
| 62 | ret = talloc_zero(ctx, struct osmo_mdns_sock); |
| 63 | OSMO_ASSERT(ret); |
| 64 | |
| 65 | /* Fill addrinfo */ |
| 66 | hints.ai_family = PF_UNSPEC; |
| 67 | hints.ai_socktype = SOCK_DGRAM; |
| 68 | hints.ai_flags = (AI_PASSIVE | AI_NUMERICHOST); |
| 69 | rc = getaddrinfo(ip, portbuf, &hints, &ret->ai); |
| 70 | if (rc != 0) { |
| 71 | LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: getaddrinfo: %s\n", gai_strerror(rc)); |
| 72 | ret->ai = NULL; |
| 73 | goto error; |
| 74 | } |
| 75 | |
| 76 | /* Open socket */ |
| 77 | sock = socket(ret->ai->ai_family, ret->ai->ai_socktype, 0); |
| 78 | if (sock == -1) { |
| 79 | LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: socket: %s\n", strerror(errno)); |
| 80 | goto error; |
| 81 | } |
| 82 | |
| 83 | /* Set multicast options */ |
| 84 | rc = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (char*)&iface, sizeof(iface)); |
| 85 | if (rc == -1) { |
| 86 | LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: setsockopt: %s\n", strerror(errno)); |
| 87 | goto error; |
| 88 | } |
| 89 | memcpy(&multicast_req.imr_multiaddr, &((struct sockaddr_in*)(ret->ai->ai_addr))->sin_addr, |
| 90 | sizeof(multicast_req.imr_multiaddr)); |
| 91 | multicast_req.imr_interface.s_addr = htonl(INADDR_ANY); |
| 92 | rc = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multicast_req, sizeof(multicast_req)); |
| 93 | if (rc == -1) { |
| 94 | LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: setsockopt: %s\n", strerror(errno)); |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | /* Always allow binding the same IP and port twice. This is needed in OsmoHLR (where the code becomes cleaner by |
| 99 | * just using a different socket for server and client code) and in the mslookup_client_mdns_test. Also for |
| 100 | * osmo-mslookup-client if it is running multiple times in parallel (i.e. two incoming calls almost at the same |
| 101 | * time need to be resolved with the simple dialplan example that just starts new processes). */ |
| 102 | rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&y, sizeof(y)); |
| 103 | if (rc == -1) { |
| 104 | LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: setsockopt: %s\n", strerror(errno)); |
| 105 | goto error; |
| 106 | } |
| 107 | |
| 108 | /* Bind and register osmo_fd callback */ |
| 109 | rc = bind(sock, ret->ai->ai_addr, ret->ai->ai_addrlen); |
| 110 | if (rc == -1) { |
| 111 | LOGP(DMSLOOKUP, LOGL_ERROR, "osmo_mdns_sock_init: bind: %s\n", strerror(errno)); |
| 112 | goto error; |
| 113 | } |
| 114 | osmo_fd_setup(&ret->osmo_fd, sock, OSMO_FD_READ, cb, data, priv_nr); |
| 115 | if (osmo_fd_register(&ret->osmo_fd) != 0) |
| 116 | goto error; |
| 117 | |
| 118 | return ret; |
| 119 | error: |
| 120 | if (ret->ai) |
| 121 | freeaddrinfo(ret->ai); |
| 122 | talloc_free(ret); |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | /*! Send msgb over mdns_sock and consume msgb. |
| 127 | * \returns 0 on success, -1 on error. |
| 128 | */ |
| 129 | int osmo_mdns_sock_send(const struct osmo_mdns_sock *mdns_sock, struct msgb *msg) |
| 130 | { |
| 131 | size_t len = msgb_length(msg); |
| 132 | int rc = sendto(mdns_sock->osmo_fd.fd, msgb_data(msg), len, 0, mdns_sock->ai->ai_addr, |
| 133 | mdns_sock->ai->ai_addrlen); |
| 134 | msgb_free(msg); |
| 135 | return (rc == len) ? 0 : -1; |
| 136 | } |
| 137 | |
| 138 | /*! Tear down osmo_mdns_sock. */ |
| 139 | void osmo_mdns_sock_cleanup(struct osmo_mdns_sock *mdns_sock) |
| 140 | { |
| 141 | osmo_fd_close(&mdns_sock->osmo_fd); |
| 142 | freeaddrinfo(mdns_sock->ai); |
| 143 | talloc_free(mdns_sock); |
| 144 | } |