@
yexm0 修改 asn.c ,新增一个 get_asn_name 函数。 随手改的,自己用,没有测-y 非 0 的情况。基于 mtr-0.85_p20140126 版本,其他版本估计也能直接打这个补丁。原作者-y 0 参数的输出都是 hardcode 6 位 AS 输出,因为 asn name 不定长,所以这样改有可能后面的项会移位。如果原作者想真正加这个功能恐怕会改其他输出的代码。
--- asn.c.bak 2016-09-02 11:25:32.733209580 +0800
+++ asn.c 2016-09-17 19:59:55.710935263 +0800
@@ -56,16 +56,18 @@
int ipinfo_no = -1;
int ipinfo_max = -1;
int iihash = 0;
-char fmtinfo[32];
+char fmtinfo[96];
+char fmtinfo2[96];
extern int af; /* address family of remote target */
-// items width: ASN, Route, Country, Registry, Allocated
-int iiwidth[] = { 6, 19, 4, 8, 11}; // item len + space
+// items width: ASN, Route, Country, Registry, Allocated
+int iiwidth[] = {66, 19, 4, 8, 11}; // item len + space
int iiwidth_len = sizeof(iiwidth)/sizeof((iiwidth)[0]);
typedef char* items_t[ITEMSMAX + 1];
items_t items_a; // without hash: items
char txtrec[NAMELEN + 1]; // without hash: txtrec
+char txtrec2[NAMELEN + 1];
items_t* items = &items_a;
@@ -129,7 +131,7 @@
txtlen = NAMELEN;
if (iihash) {
- if (!(txt = malloc(txtlen + 1)))
+ if (!(txt = malloc(txtlen + 1 + 96)))
return NULL;
} else
txt = (char*)txtrec;
@@ -156,6 +158,76 @@
return p;
}
+char * get_asn_name(char *asn) {
+ char lookup_key[NAMELEN];
+ char asn1[6];
+
+ unsigned char answer[PACKETSZ], *pt;
+ char host[128];
+ int len, exp, size, txtlen, type;
+
+ if (!asn)
+ return NULL;
+
+ strcpy(asn1, asn);
+
+ if (snprintf(lookup_key, NAMELEN, "AS%
s.asn.cymru.com", asn) >= NAMELEN)
+ return NULL;
+
+ if(res_init() < 0) {
+ fprintf(stderr,"@res_init failed\n");
+ return NULL;
+ }
+ memset(answer, 0, PACKETSZ);
+ if((len = res_query(lookup_key, C_IN, T_TXT, answer, PACKETSZ)) < 0) {
+ return UNKN;
+ }
+
+ pt = answer + sizeof(HEADER);
+
+ if((exp = dn_expand(answer, answer + len, pt, host, sizeof(host))) < 0) {
+ printf("@dn_expand failed\n"); return NULL;
+ }
+
+ pt += exp;
+
+ GETSHORT(type, pt);
+ if(type != T_TXT) {
+ printf("@Broken DNS reply.\n"); return NULL;
+ }
+
+ pt += INT16SZ; /* class */
+
+ if((exp = dn_expand(answer, answer + len, pt, host, sizeof(host))) < 0) {
+ printf("@second dn_expand failed\n"); return NULL;
+ }
+
+ pt += exp;
+ GETSHORT(type, pt);
+ if(type != T_TXT) {
+ printf("@Not a TXT record\n"); return NULL;
+ }
+
+ pt += INT16SZ; /* class */
+ pt += INT32SZ; /* ttl */
+ GETSHORT(size, pt);
+ txtlen = *pt;
+
+
+ if(txtlen >= size || !txtlen) {
+ printf("@Broken TXT record (txtlen = %d, size = %d)\n", txtlen, size); return NULL;
+ }
+
+ if (txtlen > NAMELEN)
+ txtlen = NAMELEN;
+
+ pt++;
+ strncpy(txtrec2, (char*) pt, txtlen);
+ txtrec2[txtlen] = 0;
+
+ strcat(asn, strrchr(txtrec2, ITEMSEP));
+ return strrchr(txtrec2, ITEMSEP);
+}
//
originX.asn.cymru.com txtrec: ASN | Route | Country | Registry | Allocated
char* split_txtrec(char *txtrec) {
if (!txtrec)
@@ -187,6 +259,7 @@
for (j = i; j <= ITEMSMAX; j++)
(*items)[j] = NULL;
+ get_asn_name((*items)[0]);
if (i > ipinfo_max)
ipinfo_max = i;
printf("ipinfo_no=%d\n", ipinfo_no);
@@ -281,7 +354,7 @@
char *ipinfo = get_ipinfo(addr);
char fmt[8];
snprintf(fmt, sizeof(fmt), "%s%%-%ds", ipinfo_no?"":"AS", get_iiwidth());
- snprintf(fmtinfo, sizeof(fmtinfo), fmt, ipinfo?ipinfo:UNKN);
+ snprintf(fmtinfo, sizeof(fmtinfo2), fmt, ipinfo?ipinfo:UNKN);
return fmtinfo;
}