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 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 | /*! \defgroup mslookup Distributed GSM: finding subscribers |
| 20 | * @{ |
| 21 | * \file mslookup.h |
| 22 | */ |
| 23 | |
| 24 | #pragma once |
| 25 | |
| 26 | #include <osmocom/core/utils.h> |
| 27 | #include <osmocom/core/sockaddr_str.h> |
| 28 | #include <osmocom/gsm/protocol/gsm_23_003.h> |
| 29 | |
| 30 | #define OSMO_MSLOOKUP_SERVICE_MAXLEN 64 |
| 31 | |
| 32 | bool osmo_mslookup_service_valid(const char *service); |
| 33 | |
| 34 | enum osmo_mslookup_id_type { |
| 35 | OSMO_MSLOOKUP_ID_NONE = 0, |
| 36 | OSMO_MSLOOKUP_ID_IMSI, |
| 37 | OSMO_MSLOOKUP_ID_MSISDN, |
| 38 | }; |
| 39 | |
| 40 | extern const struct value_string osmo_mslookup_id_type_names[]; |
| 41 | static inline const char *osmo_mslookup_id_type_name(enum osmo_mslookup_id_type val) |
| 42 | { return get_value_string(osmo_mslookup_id_type_names, val); } |
| 43 | |
| 44 | struct osmo_mslookup_id { |
| 45 | enum osmo_mslookup_id_type type; |
| 46 | union { |
| 47 | char imsi[GSM23003_IMSI_MAX_DIGITS+1]; |
| 48 | char msisdn[GSM23003_MSISDN_MAX_DIGITS+1]; |
| 49 | }; |
| 50 | }; |
| 51 | |
| 52 | int osmo_mslookup_id_cmp(const struct osmo_mslookup_id *a, const struct osmo_mslookup_id *b); |
| 53 | bool osmo_mslookup_id_valid(const struct osmo_mslookup_id *id); |
| 54 | |
| 55 | enum osmo_mslookup_result_code { |
| 56 | OSMO_MSLOOKUP_RC_NONE = 0, |
| 57 | /*! An intermediate valid result. The request is still open for more results. */ |
| 58 | OSMO_MSLOOKUP_RC_RESULT, |
| 59 | /*! Returned when the final request timeout has elapsed without results. */ |
| 60 | OSMO_MSLOOKUP_RC_NOT_FOUND, |
| 61 | }; |
| 62 | |
| 63 | extern const struct value_string osmo_mslookup_result_code_names[]; |
| 64 | static inline const char *osmo_mslookup_result_code_name(enum osmo_mslookup_result_code val) |
| 65 | { return get_value_string(osmo_mslookup_result_code_names, val); } |
| 66 | |
| 67 | /*! Information to request from a lookup. */ |
| 68 | struct osmo_mslookup_query { |
| 69 | /*! Which service to request, by freely invented names. For service name conventions (for voice, SMS, HLR,...), |
| 70 | * refer to the OsmoHLR user's manual http://ftp.osmocom.org/docs/latest/osmohlr-usermanual.pdf */ |
| 71 | char service[OSMO_MSLOOKUP_SERVICE_MAXLEN + 1]; |
| 72 | /*! IMSI or MSISDN to look up. */ |
| 73 | struct osmo_mslookup_id id; |
| 74 | |
| 75 | /*! Caller provided private data, if desired. */ |
| 76 | void *priv; |
| 77 | }; |
| 78 | |
| 79 | /*! Result data as passed back to a lookup client that invoked an osmo_mslookup_client_request. */ |
| 80 | struct osmo_mslookup_result { |
| 81 | /*! Outcome of the request. */ |
| 82 | enum osmo_mslookup_result_code rc; |
| 83 | |
| 84 | /*! IP address and port to reach the given service via IPv4, if any. */ |
| 85 | struct osmo_sockaddr_str host_v4; |
| 86 | |
| 87 | /*! IP address and port to reach the given service via IPv6, if any. */ |
| 88 | struct osmo_sockaddr_str host_v6; |
| 89 | |
| 90 | /*! How long ago the service last verified presence of the subscriber, in seconds, or zero if the presence is |
| 91 | * invariable (like the home HLR record for an IMSI). |
| 92 | * If a subscriber has recently moved to a different location, we get multiple replies and want to choose the |
| 93 | * most recent one. If this were a timestamp, firstly the time zones would need to be taken care of. |
| 94 | * Even if we choose UTC, a service provider with an inaccurate date/time would end up affecting the result. |
| 95 | * The least susceptible to configuration errors or difference in local and remote clock is a value that |
| 96 | * indicates the actual age of the record in seconds. The time that the lookup query took to be answered should |
| 97 | * be neglectable here, since we would typically wait one second (or very few seconds) for lookup replies, |
| 98 | * while typical Location Updating periods are in the range of 15 minutes. */ |
| 99 | uint32_t age; |
| 100 | |
| 101 | /*! Whether this is the last result returned for this request. */ |
| 102 | bool last; |
| 103 | }; |
| 104 | |
| 105 | int osmo_mslookup_query_init_from_domain_str(struct osmo_mslookup_query *q, const char *domain); |
| 106 | |
| 107 | size_t osmo_mslookup_id_name_buf(char *buf, size_t buflen, const struct osmo_mslookup_id *id); |
| 108 | char *osmo_mslookup_id_name_c(void *ctx, const struct osmo_mslookup_id *id); |
| 109 | char *osmo_mslookup_id_name_b(char *buf, size_t buflen, const struct osmo_mslookup_id *id); |
| 110 | |
| 111 | size_t osmo_mslookup_result_to_str_buf(char *buf, size_t buflen, |
| 112 | const struct osmo_mslookup_query *query, |
| 113 | const struct osmo_mslookup_result *result); |
| 114 | char *osmo_mslookup_result_name_c(void *ctx, |
| 115 | const struct osmo_mslookup_query *query, |
| 116 | const struct osmo_mslookup_result *result); |
| 117 | char *osmo_mslookup_result_name_b(char *buf, size_t buflen, |
| 118 | const struct osmo_mslookup_query *query, |
| 119 | const struct osmo_mslookup_result *result); |
| 120 | |
| 121 | /*! @} */ |