Refactoring validity check and prefix processing of dialed number

Command line help shows how many digits and what prefixes can be dialed.

Giving a station ID via command line will be checked for being valid.

The number to call the mobile statione will be checked for being valid.

Prefixes that are defined for a nework will be removed from station ID
automatically.

Multiple station ID lengths are supported:
 * C-Netz: 7 or 8 digits, depending on area code length
 * A-Netz: 5 or 7 digits; number is truncated to last 5 digits.
 * IMTS/MTS: 5 or 7 digits, depending on phone's selector switch.
This commit is contained in:
Andreas Eversberg
2021-10-07 19:35:56 +02:00
parent 3a73f31d7e
commit 423bc42429
41 changed files with 662 additions and 429 deletions

View File

@@ -178,6 +178,28 @@ double cnetz_kanal2freq(int kanal, int unterband)
return freq * 1e6;
}
/* check if number is a valid station ID */
const char *cnetz_number_valid(const char *number)
{
/* assume that the number has valid length(s) and digits */
if (number[0] > '7')
return "Digit 1 (mobile country code) exceeds 7.";
if (number[7]) {
if ((number[1] - '0') == 0)
return "Digit 2 and 3 (mobile network code) of 8-digit number must be at least 10.";
if ((number[1] - '0') * 10 + (number[2] - '0') > 31)
return "Digit 2 and 3 (mobile network code) of 8-digit number exceed 31.";
if (atoi(number + 3) > 65535)
return "Digit 4 to 8 (mobile subscriber suffix) of 8-digit number exceed 65535.";
} else {
if (atoi(number + 2) > 65535)
return "Digit 3 to 7 (mobile subscriber suffix) of 7-digit number exceed 65535.";
}
return NULL;
}
/* convert power level to P-bits by selecting next higher level */
static uint8_t cnetz_power2bits(int power)
{
@@ -571,39 +593,15 @@ int call_down_setup(int callref, const char __attribute__((unused)) *caller_id,
uint8_t futln_nat;
uint8_t futln_fuvst;
int futln_rest; /* use int for checking size > 65535 */
int len;
int i;
/* 1. check if number is invalid, return INVALNUMBER */
len = strlen(dialing);
if (len >= 11 && !strncmp(dialing, "0161", 4)) {
dialing += 4;
len -= 4;
}
if (len < 7 || len > 8) {
inval:
PDEBUG(DCNETZ, DEBUG_NOTICE, "Outgoing call to invalid number '%s', rejecting!\n", dialing);
return -CAUSE_INVALNUMBER;
}
for (i = 0; i < len; i++) {
if (dialing[i] < '0' || dialing[i] > '9')
goto inval;
}
/* 1. split number into elements */
futln_nat = dialing[0] - '0';
if (len == 7)
futln_fuvst = dialing[1] - '0';
else {
if (dialing[7]) {
futln_fuvst = (dialing[1] - '0') * 10 + (dialing[2] - '0');
if (futln_fuvst > 31) {
PDEBUG(DCNETZ, DEBUG_NOTICE, "Digit 2 and 3 '%02d' must not exceed '31', but they do!\n", futln_fuvst);
goto inval;
}
}
futln_rest = atoi(dialing + len - 5);
if (futln_rest > 65535) {
PDEBUG(DCNETZ, DEBUG_NOTICE, "Last 5 digits '%05d' must not exceed '65535', but they do!\n", futln_rest);
goto inval;
futln_rest = atoi(dialing + 3);
} else {
futln_fuvst = dialing[1] - '0';
futln_rest = atoi(dialing + 2);
}
/* 2. check if the subscriber is attached */

View File

@@ -133,6 +133,7 @@ struct cnetz {
transaction_t *trans_list; /* list of transactions */
};
const char *cnetz_number_valid(const char *number);
double cnetz_kanal2freq(int kanal, int unterband);
void cnetz_channel_list(void);
int cnetz_channel_by_short_name(const char *short_name);

View File

@@ -226,8 +226,7 @@ void print_help(const char *arg0)
printf(" requires a DC coupled signal, which is produced by SDR.\n");
printf(" Use 'auto' to select 'slope' for sound card input and 'level' for SDR\n");
printf(" input. (default = '%s')\n", (demod == FSK_DEMOD_LEVEL) ? "level" : (demod == FSK_DEMOD_SLOPE) ? "slope" : "auto");
printf("\nstation-id: Give 7 digit station-id, you don't need to enter it for every\n");
printf(" start of this program.\n");
main_mobile_print_station_id();
main_mobile_print_hotkeys();
printf("Press 'i' key to dump list of currently attached subscribers.\n");
}
@@ -458,10 +457,24 @@ error_fuz:
return 1;
}
static const struct number_lengths number_lengths[] = {
{ 7, "regular number format" },
{ 8, "extended number format" },
{ 0, NULL }
};
static const char *number_prefixes[] = {
"0161xxxxxxx",
"0161xxxxxxxx",
"+49161xxxxxxx",
"+49161xxxxxxxx",
NULL
};
int main(int argc, char *argv[])
{
int rc, argi;
const char *station_id = "";
const char *station_id = NULL;
int mandatory = 0;
int polarity;
int teilnehmergruppensperre = 0;
@@ -475,7 +488,7 @@ int main(int argc, char *argv[])
init_station();
main_mobile_init();
main_mobile_init("0123456789", number_lengths, number_prefixes, cnetz_number_valid);
/* handle options / config file */
add_options();
@@ -488,10 +501,9 @@ int main(int argc, char *argv[])
if (argi < argc) {
station_id = argv[argi];
if (strlen(station_id) != 7) {
printf("Given station ID '%s' does not have 7 digits\n", station_id);
return 0;
}
rc = main_mobile_number_ask(station_id, "station ID");
if (rc)
return rc;
}
/* resolve name of base station */
@@ -645,7 +657,7 @@ int main(int argc, char *argv[])
}
}
main_mobile("cnetz", &quit, NULL, station_id, 7);
main_mobile_loop("cnetz", &quit, NULL, station_id);
fail:
flush_db();