Skip to content

Commit 155fb98

Browse files
committed
CEDGE-604 Add --ipinfo-spec-ip option
1 parent a5d9a0f commit 155fb98

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

man/mtr.8.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ You can combine this option with other --ipinfo- or -y options.
412412
.B -\-ipinfo\-dns \fIDOMAIN_NAME
413413
Specify special DOMAIN_NAME to query the DNS data. The default value for this parameter is origin6.asn.cymru.com .
414414
.TP
415+
.B -\-ipinfo\-spec\-ip \fIIP\fR/\fISUBNET-MASK\fR[,\fIIP\fR/\fISUBNET-MASK\fR,...]
416+
Reserved for Netpas. Specify a part of the private address that still sends a DNS request when encounters such an address.
417+
.TP
415418
.B \-z\fR, \fB\-\-aslookup
416419
Displays the Autonomous System (AS) number alongside each hop. Equivalent to \fB\-\-ipinfo 0\fR.
417420
.IP

ui/asn.c

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@
6565
#define ITEMSMAX 15
6666
#define ITEMSEP '|'
6767
#define NAMELEN 127
68+
#define SPECIP_MAX 10
6869
#define UNKN "???"
6970
#define EMPTY "--"
7071
#define SEMPATH "sem"
71-
#define NETPAS_DOMAIN "ip.xelerate.ai"
72+
#define NETPAS_DOMAIN "ip.xelerate.ai"
7273

7374
typedef char *items_t[ITEMSMAX + 1];
7475
struct comparm {
@@ -89,6 +90,10 @@ static const int iiwidth[] = {9, 18, 6, 7, 13, 8, 10, 15}; /* item len + space
8990
static int prefix_arr[4] = {8, 12, 16, 16};
9091
static unsigned int mask_ip_scope[4];
9192
static unsigned int private_ip_scope[4];
93+
/* used for specific ip segment in private ip */
94+
static int spec_prefix_arr[SPECIP_MAX];
95+
static unsigned int spec_mask_ip_scope[SPECIP_MAX];
96+
static unsigned int spec_ip_scope[SPECIP_MAX];
9297

9398
#ifdef ENABLE_IPV6
9499
char ipinfo_domain6[128] = "origin6.asn.cymru.com";
@@ -255,6 +260,41 @@ static void reverse_host6(
255260
}
256261
#endif
257262

263+
void process_ip_prefix(char *ipprefix)
264+
{
265+
char *p = NULL;
266+
static int index = 0;
267+
268+
if (index >= (sizeof(spec_prefix_arr)/sizeof(spec_prefix_arr[0])))
269+
return;
270+
271+
p = strrchr(ipprefix, '/');
272+
if (p == NULL) {
273+
error(EXIT_FAILURE, 0, "invalid argument:%s", ipprefix);
274+
}
275+
*p++ = '\0';
276+
277+
spec_prefix_arr[index] = strtonum_or_err(p, "invalid argument", STRTO_INT);
278+
if (spec_prefix_arr[index] <= 0 || spec_prefix_arr[index] >= 32) {
279+
*(--p) = '/';
280+
error(EXIT_FAILURE, 0, "invalid argument:%s", ipprefix);
281+
}
282+
283+
spec_mask_ip_scope[index] = 0xffffffff;
284+
spec_mask_ip_scope[index] <<= (32 - spec_prefix_arr[index]);
285+
spec_mask_ip_scope[index] &= 0xffffffff;
286+
287+
spec_ip_scope[index] = inet_addr(ipprefix);
288+
if (spec_ip_scope[index] == INADDR_NONE) {
289+
*(--p) = '/';
290+
error(EXIT_FAILURE, 0, "invalid argument:%s", ipprefix);
291+
}
292+
spec_ip_scope[index] = htonl(spec_ip_scope[index]);
293+
spec_ip_scope[index] &= spec_mask_ip_scope[index];
294+
295+
index++;
296+
}
297+
258298
static void init_private_ip(void)
259299
{
260300
int i;
@@ -282,6 +322,15 @@ static int is_private_ip(ip_t *addr)
282322

283323
ipaddr = htonl((*(struct in_addr *)addr).s_addr);
284324

325+
if (strcmp(ipinfo_domain, NETPAS_DOMAIN) == 0) {
326+
i = 0;
327+
while (spec_prefix_arr[i] != 0) {
328+
if ((ipaddr & spec_mask_ip_scope[i]) == spec_ip_scope[i])
329+
return 0;
330+
i++;
331+
}
332+
}
333+
285334
for (i = 0; i < 4; i++) {
286335
if ((ipaddr & mask_ip_scope[i]) == private_ip_scope[i])
287336
return 1;

ui/asn.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ extern int get_ipinfo_compose(
5858
char *buf,
5959
int buflen,
6060
int hops);
61+
extern void process_ip_prefix(
62+
char *ipprefix);

ui/mtr.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
240240
fputs(" --ipinfo-carrier display carrier, equal to -y 6\n", out);
241241
fputs(" --ipinfo-geo display geo, equal to -y 7\n", out);
242242
fputs(" --ipinfo-dns DOMAIN_NAME Specify DOMAIN_NAME to query DNS data\n", out);
243+
fputs(" --ipinfo-spec-ip IP/SUBNET-MASK[,IP/SUBNET-MASK,...]\n"
244+
" reserved for Netpas\n", out);
243245
fputs(" -z, --aslookup display AS number\n", out);
244246
#endif
245247
fputs(" -h, --help display this help and exit\n", out);
@@ -397,6 +399,21 @@ static void process_ipinfo_arr(struct mtr_ctl *ctl, char *optarg)
397399
}
398400
}
399401

402+
static void process_specific_ip_segment(char *optarg)
403+
{
404+
char *t;
405+
406+
if (optarg == NULL)
407+
return;
408+
409+
t = strtok(optarg, ",");
410+
while (t != NULL) {
411+
process_ip_prefix(t);
412+
413+
t = strtok(NULL, ",");
414+
}
415+
}
416+
400417
static void parse_arg(
401418
struct mtr_ctl *ctl,
402419
names_t ** names,
@@ -421,7 +438,8 @@ static void parse_arg(
421438
OPT_CITY,
422439
OPT_CARRIER,
423440
OPT_GEO,
424-
OPT_DNS
441+
OPT_DNS,
442+
OPT_SPECIP
425443
};
426444
static const struct option long_options[] = {
427445
/* option name, has argument, NULL, short name */
@@ -465,6 +483,7 @@ static void parse_arg(
465483
{"ipinfo-carrier", 0, NULL, OPT_CARRIER},
466484
{"ipinfo-geo", 0, NULL, OPT_GEO},
467485
{"ipinfo-dns", 1, NULL, OPT_DNS},
486+
{"ipinfo-spec-ip", 1, NULL, OPT_SPECIP},
468487
#endif
469488

470489
{"interval", 1, NULL, 'i'},
@@ -762,6 +781,11 @@ static void parse_arg(
762781
strncpy(ipinfo_domain, optarg, sizeof(ipinfo_domain) - 1);
763782
}
764783
break;
784+
case OPT_SPECIP:
785+
if (optarg) {
786+
process_specific_ip_segment(optarg);
787+
}
788+
break;
765789
#endif
766790
#ifdef SO_MARK
767791
case 'M':

0 commit comments

Comments
 (0)