Oliver Smith | bf7deda | 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 Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 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 Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <sys/time.h> |
| 21 | #include <string.h> |
| 22 | #include <osmocom/core/application.h> |
| 23 | #include <osmocom/core/select.h> |
| 24 | #include <osmocom/hlr/logging.h> |
| 25 | #include <osmocom/mslookup/mslookup_client_fake.h> |
| 26 | #include <osmocom/mslookup/mslookup_client.h> |
| 27 | |
| 28 | #define SERVICE_HLR_GSUP "gsup.hlr" |
| 29 | #define SERVICE_SIP "sip.voice" |
| 30 | |
| 31 | void *ctx = NULL; |
| 32 | |
| 33 | static struct osmo_mslookup_fake_response fake_lookup_responses[] = { |
| 34 | { |
| 35 | .time_to_reply = { .tv_sec = 1, }, |
| 36 | .for_id = { |
| 37 | .type = OSMO_MSLOOKUP_ID_IMSI, |
| 38 | .imsi = "1234567", |
| 39 | }, |
| 40 | .for_service = SERVICE_HLR_GSUP, |
| 41 | .result = { |
| 42 | .rc = OSMO_MSLOOKUP_RC_RESULT, |
| 43 | .host_v4 = { |
| 44 | .af = AF_INET, |
| 45 | .ip = "12.34.56.7", |
| 46 | .port = 42, |
| 47 | }, |
| 48 | .host_v6 = { |
| 49 | .af = AF_INET6, |
| 50 | .ip = "be:ef:ed:ca:fe:fa:ce::1", |
| 51 | .port = 42, |
| 52 | }, |
| 53 | .age = 0, |
| 54 | }, |
| 55 | }, |
| 56 | { |
| 57 | .time_to_reply = { .tv_usec = 600 * 1000, }, |
| 58 | .for_id = { |
| 59 | .type = OSMO_MSLOOKUP_ID_MSISDN, |
| 60 | .msisdn = "112", |
| 61 | }, |
| 62 | .for_service = SERVICE_SIP, |
| 63 | .result = { |
| 64 | .rc = OSMO_MSLOOKUP_RC_RESULT, |
| 65 | .host_v4 = { |
| 66 | .af = AF_INET, |
| 67 | .ip = "66.66.66.66", |
| 68 | .port = 666, |
| 69 | }, |
| 70 | .host_v6 = { |
| 71 | .af = AF_INET, |
| 72 | .ip = "6666:6666:6666::6", |
| 73 | .port = 666, |
| 74 | }, |
| 75 | .age = 423, |
| 76 | }, |
| 77 | }, |
| 78 | { |
| 79 | .time_to_reply = { .tv_usec = 800 * 1000, }, |
| 80 | .for_id = { |
| 81 | .type = OSMO_MSLOOKUP_ID_MSISDN, |
| 82 | .msisdn = "112", |
| 83 | }, |
| 84 | .for_service = SERVICE_SIP, |
| 85 | .result = { |
| 86 | .rc = OSMO_MSLOOKUP_RC_RESULT, |
| 87 | .host_v4 = { |
| 88 | .af = AF_INET, |
| 89 | .ip = "112.112.112.112", |
| 90 | .port = 23, |
| 91 | }, |
| 92 | .age = 235, |
| 93 | }, |
| 94 | }, |
| 95 | { |
| 96 | .time_to_reply = { .tv_sec = 1, .tv_usec = 200 * 1000, }, |
| 97 | .for_id = { |
| 98 | .type = OSMO_MSLOOKUP_ID_MSISDN, |
| 99 | .msisdn = "112", |
| 100 | }, |
| 101 | .for_service = SERVICE_SIP, |
| 102 | .result = { |
| 103 | .rc = OSMO_MSLOOKUP_RC_RESULT, |
| 104 | .host_v4 = { |
| 105 | .af = AF_INET, |
| 106 | .ip = "99.99.99.99", |
| 107 | .port = 999, |
| 108 | }, |
| 109 | .host_v6 = { |
| 110 | .af = AF_INET, |
| 111 | .ip = "9999:9999:9999::9", |
| 112 | .port = 999, |
| 113 | }, |
| 114 | .age = 335, |
| 115 | }, |
| 116 | }, |
| 117 | { |
| 118 | .time_to_reply = { .tv_sec = 1, .tv_usec = 500 * 1000, }, |
| 119 | .for_id = { |
| 120 | .type = OSMO_MSLOOKUP_ID_MSISDN, |
| 121 | .msisdn = "112", |
| 122 | }, |
| 123 | .for_service = SERVICE_SIP, |
| 124 | .result = { |
| 125 | .rc = OSMO_MSLOOKUP_RC_RESULT, |
| 126 | .host_v4 = { |
| 127 | .af = AF_INET, |
| 128 | .ip = "99.99.99.99", |
| 129 | .port = 999, |
| 130 | }, |
| 131 | .age = 999, |
| 132 | }, |
| 133 | }, |
| 134 | }; |
| 135 | |
| 136 | const struct timeval fake_time_start_time = { 0, 0 }; |
| 137 | |
| 138 | #define fake_time_passes(secs, usecs) do \ |
| 139 | { \ |
| 140 | struct timeval diff; \ |
| 141 | osmo_gettimeofday_override_add(secs, usecs); \ |
| 142 | osmo_clock_override_add(CLOCK_MONOTONIC, secs, usecs * 1000); \ |
| 143 | timersub(&osmo_gettimeofday_override_time, &fake_time_start_time, &diff); \ |
| 144 | LOGP(DMSLOOKUP, LOGL_DEBUG, "Total time passed: %d.%06d s\n", \ |
| 145 | (int)diff.tv_sec, (int)diff.tv_usec); \ |
| 146 | osmo_timers_prepare(); \ |
| 147 | osmo_timers_update(); \ |
| 148 | } while (0) |
| 149 | |
| 150 | static void fake_time_start() |
| 151 | { |
| 152 | struct timespec *clock_override; |
| 153 | |
| 154 | osmo_gettimeofday_override_time = fake_time_start_time; |
| 155 | osmo_gettimeofday_override = true; |
| 156 | clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC); |
| 157 | OSMO_ASSERT(clock_override); |
| 158 | clock_override->tv_sec = fake_time_start_time.tv_sec; |
| 159 | clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000; |
| 160 | osmo_clock_override_enable(CLOCK_MONOTONIC, true); |
| 161 | fake_time_passes(0, 0); |
| 162 | } |
| 163 | |
| 164 | static void result_cb_once(struct osmo_mslookup_client *client, |
| 165 | uint32_t request_handle, |
| 166 | const struct osmo_mslookup_query *query, |
| 167 | const struct osmo_mslookup_result *result) |
| 168 | { |
| 169 | LOGP(DMSLOOKUP, LOGL_DEBUG, "result_cb(): %s\n", osmo_mslookup_result_name_c(ctx, query, result)); |
| 170 | } |
| 171 | |
| 172 | int main() |
| 173 | { |
| 174 | ctx = talloc_named_const(NULL, 0, "main"); |
| 175 | osmo_init_logging2(ctx, NULL); |
| 176 | |
| 177 | log_set_print_filename(osmo_stderr_target, 0); |
| 178 | log_set_print_level(osmo_stderr_target, 0); |
| 179 | log_set_print_category(osmo_stderr_target, 0); |
| 180 | log_set_print_category_hex(osmo_stderr_target, 0); |
| 181 | log_set_use_color(osmo_stderr_target, 0); |
| 182 | log_set_category_filter(osmo_stderr_target, DMSLOOKUP, true, LOGL_DEBUG); |
| 183 | |
| 184 | fake_time_start(); |
| 185 | |
| 186 | struct osmo_mslookup_client *client = osmo_mslookup_client_new(ctx); |
| 187 | osmo_mslookup_client_add_fake(client, fake_lookup_responses, ARRAY_SIZE(fake_lookup_responses)); |
| 188 | |
| 189 | /* Place some requests to be replied upon asynchronously */ |
| 190 | |
| 191 | struct osmo_mslookup_query_handling handling = { |
| 192 | .result_timeout_milliseconds = 1, /* set some timeout < min_wait_milliseconds */ |
| 193 | .min_wait_milliseconds = 2000, |
| 194 | .result_cb = result_cb_once, |
| 195 | }; |
| 196 | |
| 197 | struct osmo_mslookup_query q1 = { |
| 198 | .service = SERVICE_HLR_GSUP, |
| 199 | .id = { |
| 200 | .type = OSMO_MSLOOKUP_ID_IMSI, |
| 201 | .imsi = "1234567", |
| 202 | }, |
| 203 | }; |
| 204 | OSMO_ASSERT(osmo_mslookup_client_request(client, &q1, &handling)); |
| 205 | |
| 206 | struct osmo_mslookup_query q2 = { |
| 207 | .service = SERVICE_SIP, |
| 208 | .id = { |
| 209 | .type = OSMO_MSLOOKUP_ID_MSISDN, |
| 210 | .msisdn = "112", |
| 211 | }, |
| 212 | }; |
| 213 | handling.min_wait_milliseconds = 3000; |
| 214 | OSMO_ASSERT(osmo_mslookup_client_request(client, &q2, &handling)); |
| 215 | |
| 216 | struct osmo_mslookup_query q3 = { |
| 217 | .service = "smpp.sms", |
| 218 | .id = { |
| 219 | .type = OSMO_MSLOOKUP_ID_MSISDN, |
| 220 | .msisdn = "00000", |
| 221 | }, |
| 222 | }; |
| 223 | handling.min_wait_milliseconds = 5000; |
| 224 | OSMO_ASSERT(osmo_mslookup_client_request(client, &q3, &handling)); |
| 225 | |
| 226 | struct osmo_mslookup_query q4 = { |
| 227 | .service = SERVICE_HLR_GSUP, |
| 228 | .id = { |
| 229 | .type = OSMO_MSLOOKUP_ID_MSISDN, |
| 230 | .msisdn = "666", |
| 231 | }, |
| 232 | }; |
| 233 | handling.min_wait_milliseconds = 10000; |
| 234 | uint32_t q4_handle; |
| 235 | OSMO_ASSERT((q4_handle = osmo_mslookup_client_request(client, &q4, &handling))); |
| 236 | |
| 237 | while (osmo_gettimeofday_override_time.tv_sec < 6) { |
| 238 | log_reset_context(); |
| 239 | fake_time_passes(0, 1e6 / 5); |
| 240 | } |
| 241 | |
| 242 | osmo_mslookup_client_request_cancel(client, q4_handle); |
| 243 | |
| 244 | return 0; |
| 245 | } |