Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file apn.c */ |
| 2 | |
Harald Welte | 908085c | 2014-10-01 16:18:11 +0800 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <stdint.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include <osmocom/gsm/apn.h> |
| 9 | |
| 10 | #define APN_OI_GPRS_FMT "mnc%03u.mcc%03u.gprs" |
| 11 | #define APN_GPRS_FMT "%s.mnc%03u.mcc%03u.gprs" |
| 12 | |
| 13 | static char apn_strbuf[APN_MAXLEN+1]; |
| 14 | |
| 15 | char *osmo_apn_qualify(unsigned int mcc, unsigned int mnc, const char *ni) |
| 16 | { |
| 17 | snprintf(apn_strbuf, sizeof(apn_strbuf)-1, APN_GPRS_FMT, |
| 18 | ni, mnc, mcc); |
| 19 | apn_strbuf[sizeof(apn_strbuf)-1] = '\0'; |
| 20 | |
| 21 | return apn_strbuf; |
| 22 | } |
| 23 | |
| 24 | char *osmo_apn_qualify_from_imsi(const char *imsi, |
| 25 | const char *ni, int have_3dig_mnc) |
| 26 | { |
| 27 | char cbuf[3+1], nbuf[3+1]; |
| 28 | |
| 29 | strncpy(cbuf, imsi, 3); |
| 30 | cbuf[3] = '\0'; |
| 31 | |
| 32 | if (have_3dig_mnc) { |
| 33 | strncpy(nbuf, imsi+3, 3); |
| 34 | nbuf[3] = '\0'; |
| 35 | } else { |
| 36 | strncpy(nbuf, imsi+3, 2); |
| 37 | nbuf[2] = '\0'; |
| 38 | } |
| 39 | return osmo_apn_qualify(atoi(cbuf), atoi(nbuf), ni); |
| 40 | } |
Jacob Erlbeck | 8114294 | 2015-11-17 08:42:05 +0100 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Convert an encoded APN into a dot-separated string. |
| 44 | * |
| 45 | * \param out_str the destination buffer (size must be >= max(app_enc_len,1)) |
| 46 | * \param apn_enc the encoded APN |
| 47 | * \param apn_enc_len the length of the encoded APN |
| 48 | * |
| 49 | * \returns out_str on success and NULL otherwise |
| 50 | */ |
| 51 | char * osmo_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t apn_enc_len) |
| 52 | { |
| 53 | char *str = out_str; |
| 54 | size_t rest_chars = apn_enc_len; |
| 55 | |
| 56 | while (rest_chars > 0 && apn_enc[0]) { |
| 57 | size_t label_size = apn_enc[0]; |
| 58 | if (label_size + 1 > rest_chars) |
| 59 | return NULL; |
| 60 | |
| 61 | memmove(str, apn_enc + 1, label_size); |
| 62 | str += label_size; |
| 63 | rest_chars -= label_size + 1; |
| 64 | apn_enc += label_size + 1; |
| 65 | |
| 66 | if (rest_chars) |
| 67 | *(str++) = '.'; |
| 68 | } |
| 69 | str[0] = '\0'; |
| 70 | |
| 71 | return out_str; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Convert a dot-separated string into an encoded APN. |
| 76 | * |
| 77 | * \param apn_enc the encoded APN |
| 78 | * \param max_apn_enc_len the size of the apn_enc buffer |
| 79 | * \param str the source string |
| 80 | * |
| 81 | * \returns out_str on success and NULL otherwise |
| 82 | */ |
| 83 | int osmo_apn_from_str(uint8_t *apn_enc, size_t max_apn_enc_len, const char *str) |
| 84 | { |
| 85 | uint8_t *last_len_field; |
| 86 | int len; |
| 87 | |
| 88 | /* Can we even write the length field to the output? */ |
| 89 | if (max_apn_enc_len == 0) |
| 90 | return -1; |
| 91 | |
| 92 | /* Remember where we need to put the length once we know it */ |
| 93 | last_len_field = apn_enc; |
| 94 | len = 1; |
| 95 | apn_enc += 1; |
| 96 | |
| 97 | while (str[0]) { |
| 98 | if (len >= max_apn_enc_len) |
| 99 | return -1; |
| 100 | |
| 101 | if (str[0] == '.') { |
| 102 | *last_len_field = (apn_enc - last_len_field) - 1; |
| 103 | last_len_field = apn_enc; |
| 104 | } else { |
| 105 | *apn_enc = str[0]; |
| 106 | } |
| 107 | apn_enc += 1; |
| 108 | str += 1; |
| 109 | len += 1; |
| 110 | } |
| 111 | |
| 112 | *last_len_field = (apn_enc - last_len_field) - 1; |
| 113 | |
| 114 | return len; |
| 115 | } |