Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2017 by Harald Welte <laforge@gnumonks.org> |
| 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, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <string.h> |
| 25 | #include <fcntl.h> |
| 26 | |
| 27 | #include <sys/socket.h> |
| 28 | #include <arpa/inet.h> |
| 29 | #include <netinet/in.h> |
| 30 | |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 31 | #include <osmocom/core/application.h> |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 32 | #include <osmocom/core/utils.h> |
| 33 | #include <osmocom/core/socket.h> |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 34 | #include <osmocom/core/logging.h> |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 35 | |
| 36 | #include "../config.h" |
| 37 | |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 38 | void *ctx = NULL; |
| 39 | |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 40 | static int test_sockinit(void) |
| 41 | { |
| 42 | int fd, rc; |
| 43 | char *name; |
| 44 | |
| 45 | printf("Checking osmo_sock_init() with bind to a random local UDP port\n"); |
| 46 | fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 47 | "0.0.0.0", 0, OSMO_SOCK_F_BIND); |
| 48 | OSMO_ASSERT(fd >= 0); |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 49 | name = osmo_sock_get_name(ctx, fd); |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 50 | /* expect it to be not connected. We cannot match on INADDR_ANY, |
| 51 | * as apparently that won't work on FreeBSD if there's only one |
| 52 | * address (e.g. 127.0.0.1) assigned to the entire system, like |
| 53 | * the Osmocom FreeBSD build slaves */ |
Neels Hofmeyr | 1f82d0a | 2017-06-21 22:54:46 +0200 | [diff] [blame] | 54 | OSMO_ASSERT(!strncmp(name, "(r=NULL<->", 9)); |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 55 | talloc_free(name); |
| 56 | /* expect it to be blocking */ |
| 57 | rc = fcntl(fd, F_GETFL); |
| 58 | OSMO_ASSERT(!(rc & O_NONBLOCK)); |
| 59 | close(fd); |
| 60 | |
| 61 | printf("Checking for OSMO_SOCK_F_NONBLOCK\n"); |
| 62 | fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 63 | "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK); |
| 64 | OSMO_ASSERT(fd >= 0); |
| 65 | /* expect it to be blocking */ |
| 66 | rc = fcntl(fd, F_GETFL); |
| 67 | OSMO_ASSERT(rc & O_NONBLOCK); |
| 68 | close(fd); |
| 69 | |
| 70 | printf("Checking for invalid flags\n"); |
| 71 | fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 72 | "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT); |
| 73 | OSMO_ASSERT(fd < 0); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 78 | static int test_sockinit2(void) |
| 79 | { |
| 80 | int fd, rc; |
| 81 | char *name; |
| 82 | |
| 83 | printf("Checking osmo_sock_init2() with bind to a random local UDP port\n"); |
| 84 | fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 85 | "0.0.0.0", 0, NULL, 0, OSMO_SOCK_F_BIND); |
| 86 | OSMO_ASSERT(fd >= 0); |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 87 | name = osmo_sock_get_name(ctx, fd); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 88 | /* expect it to be not connected. We cannot match on INADDR_ANY, |
| 89 | * as apparently that won't work on FreeBSD if there's only one |
| 90 | * address (e.g. 127.0.0.1) assigned to the entire system, like |
| 91 | * the Osmocom FreeBSD build slaves */ |
Neels Hofmeyr | 1f82d0a | 2017-06-21 22:54:46 +0200 | [diff] [blame] | 92 | OSMO_ASSERT(!strncmp(name, "(r=NULL<->", 9)); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 93 | talloc_free(name); |
| 94 | /* expect it to be blocking */ |
| 95 | rc = fcntl(fd, F_GETFL); |
| 96 | OSMO_ASSERT(!(rc & O_NONBLOCK)); |
| 97 | close(fd); |
| 98 | |
| 99 | printf("Checking osmo_sock_init2() for OSMO_SOCK_F_NONBLOCK\n"); |
| 100 | fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 101 | "0.0.0.0", 0, NULL, 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK); |
| 102 | OSMO_ASSERT(fd >= 0); |
| 103 | /* expect it to be blocking */ |
| 104 | rc = fcntl(fd, F_GETFL); |
| 105 | OSMO_ASSERT(rc & O_NONBLOCK); |
| 106 | close(fd); |
| 107 | |
| 108 | printf("Checking osmo_sock_init2() for invalid flags\n"); |
| 109 | fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "0.0.0.0", 0, NULL, 0, 0); |
| 110 | OSMO_ASSERT(fd < 0); |
| 111 | |
| 112 | printf("Checking osmo_sock_init2() for combined BIND + CONNECT\n"); |
| 113 | fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "127.0.0.1", 0, "127.0.0.1", 53, |
| 114 | OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT); |
| 115 | OSMO_ASSERT(fd >= 0); |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 116 | name = osmo_sock_get_name(ctx, fd); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 117 | #ifndef __FreeBSD__ |
| 118 | /* For some reason, on the jenkins.osmocom.org build slave with |
| 119 | * FreeBSD 10 inside a jail, it fails. Works fine on laforge's |
| 120 | * FreeBSD 10 or 11 VM at home */ |
Neels Hofmeyr | 1f82d0a | 2017-06-21 22:54:46 +0200 | [diff] [blame] | 121 | OSMO_ASSERT(!strncmp(name, "(r=127.0.0.1:53<->l=127.0.0.1", 29)); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 122 | #endif |
| 123 | talloc_free(name); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 129 | const struct log_info_cat default_categories[] = { |
| 130 | }; |
| 131 | |
| 132 | static struct log_info info = { |
| 133 | .cat = default_categories, |
| 134 | .num_cat = ARRAY_SIZE(default_categories), |
| 135 | }; |
| 136 | |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 137 | int main(int argc, char *argv[]) |
| 138 | { |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 139 | ctx = talloc_named_const(NULL, 0, "socket_test"); |
| 140 | osmo_init_logging2(ctx, &info); |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 141 | log_set_use_color(osmo_stderr_target, 0); |
| 142 | log_set_print_filename(osmo_stderr_target, 0); |
| 143 | |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 144 | test_sockinit(); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 145 | test_sockinit2(); |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 146 | |
| 147 | return EXIT_SUCCESS; |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 148 | } |