Index: busybox-1.31.1/networking/nslookup.c =================================================================== --- busybox-1.31.1.orig/networking/nslookup.c 2019-06-10 18:50:53.000000000 +0800 +++ busybox-1.31.1/networking/nslookup.c 2021-03-18 10:03:40.374534648 +0800 @@ -44,6 +44,17 @@ #if !ENABLE_FEATURE_NSLOOKUP_BIG +/*ZyXEL zcfg*/ +#ifdef ZCFG_SUPPORT +#include "time.h" + +#include "zcfg_common.h" +#include "zcfg_msg.h" +#include "zcfg_net.h" + +#include +#endif + /* * Mini nslookup implementation for busybox * @@ -91,8 +102,52 @@ * ns2.kernel.org internet address = 204.152.191.4 * ns3.kernel.org internet address = 204.152.191.36 */ +#ifdef ZCFG_SUPPORT +void usage() +{ + fprintf(stderr, "\nUsage: nslookup -h [HOST] <-s [DNS server]>\n"); + fprintf(stderr, "Queries the nameserver for the IP address of the given HOST\n"); + fprintf(stderr, "optionally using a specified DNS server\n"); +} + +zcfgRet_t nslookupMsgSend(int msg_type, int payloadLen, const char *payload) +{ + zcfgRet_t ret; + void *sendBuf = NULL; + void *recvBuf = NULL; + zcfgMsg_t *sendMsgHdr = NULL; + char *recv_str = NULL; + int buf_size = 0; + + buf_size = sizeof(zcfgMsg_t)+payloadLen; + sendBuf = malloc(buf_size); + sendMsgHdr = (zcfgMsg_t*)sendBuf; + + sendMsgHdr->type = msg_type; + sendMsgHdr->length = payloadLen; + sendMsgHdr->srcEid = ZCFG_EID_NSLOOKUP; + sendMsgHdr->dstEid = ZCFG_EID_ESMD; + + if(payload != NULL) + memcpy(sendBuf+sizeof(zcfgMsg_t), payload, payloadLen); + + ret = zcfgMsgSendAndGetReply(sendMsgHdr, (zcfgMsg_t **)&recvBuf, 0); + + if(ret == ZCFG_SUCCESS) { + recv_str = (char *)recvBuf+sizeof(zcfgMsg_t); + printf("Receive message : %s\n", recv_str); + } + + return ret; +} +#endif + +#ifdef ZCFG_SUPPORT +static int print_host(const char *hostname, const char *header, char *iplist) +#else static int print_host(const char *hostname, const char *header) +#endif { /* We can't use xhost2sockaddr() - we want to get ALL addresses, * not just one */ @@ -118,7 +173,12 @@ char *dotted, *revhost; dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr); revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr); - +#ifdef ZCFG_SUPPORT + if(iplist!=NULL){ + if(strlen(iplist)>0) strcat(iplist, ","); + strcat(iplist, dotted); + } +#endif printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n'); if (revhost) { puts(revhost); @@ -142,22 +202,34 @@ } /* lookup the default nameserver and display it */ +#ifdef ZCFG_SUPPORT +static int server_print(char *srvip) +#else static void server_print(void) +#endif { char *server; struct sockaddr *sa; - +#ifdef ZCFG_SUPPORT + int ret = 0; +#endif #if ENABLE_FEATURE_IPV6 sa = (struct sockaddr*)_res._u._ext.nsaddrs[0]; if (!sa) #endif sa = (struct sockaddr*)&_res.nsaddr_list[0]; server = xmalloc_sockaddr2dotted_noport(sa); - +#ifdef ZCFG_SUPPORT + ret = print_host(server, "Server:", srvip); +#else print_host(server, "Server:"); +#endif if (ENABLE_FEATURE_CLEAN_UP) free(server); bb_putchar('\n'); +#ifdef ZCFG_SUPPORT + return ret; +#endif } /* alter the global _res nameserver structure to use @@ -195,15 +267,67 @@ } int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; -int nslookup_main(int argc, char **argv) -{ +int nslookup_main(int argc, char **argv){ +#ifdef ZCFG_SUPPORT + char *server = NULL, *hostname = NULL; + unsigned int nscount = 1, deadline = 1000; + char *str_I = NULL; + int opt; + int ret; + struct json_object *lookup_result = NULL, *lookup_stat = NULL; + const char *payload = NULL; + int payloadLen = 0; + unsigned long timevals, timevale, timeintv; + char iplist[46] = {0}, srvip[46] = {0}; + int c = 0, successc = 0; + char cStr[4] = {0}; + char syscmd[128] = {0}; + + zcfgEidInit(ZCFG_EID_NSLOOKUP, NULL); +#endif /* We allow 1 or 2 arguments. * The first is the name to be looked up and the second is an * optional DNS server with which to do the lookup. * More than 3 arguments is an error to follow the pattern of the * standard nslookup */ +#ifdef ZCFG_SUPPORT + if(argc <=2){ + usage(); + return; + } + + while((opt = getopt(argc, argv, "h:s:c:w:I:")) != -1) + { + switch(opt) + { + case 'h': + hostname = optarg; + break; + case 's': + server = optarg; + break; + case 'c': + nscount = atoi(optarg); + break; + case 'w': + deadline = atoi(optarg); + break; + case 'I': /*not yet support now*/ + str_I= optarg; + break; + default: + break; + } + } + if(nscount<=0) nscount = 1; /*at least one time*/ + if(!hostname){ + usage(); + return; + } +#else if (!argv[1] || argv[1][0] == '-' || argc > 3) bb_show_usage(); +#endif /* initialize DNS structure _res used in printing the default * name server and in the explicit name server option feature. */ @@ -211,7 +335,82 @@ /* rfc2133 says this enables IPv6 lookups */ /* (but it also says "may be enabled in /etc/resolv.conf") */ /*_res.options |= RES_USE_INET6;*/ +#ifdef ZCFG_SUPPORT + lookup_stat = json_object_new_object(); + + if (server) + set_default_dns(server); + + ret = server_print(srvip); + + if (server){ + /*work around, because the server option not work ?*/ + snprintf(syscmd, sizeof(syscmd),"echo nameserver %s >> /var/fyi/sys/dns", srvip); + system(syscmd); + } + + if(ret != 0){ + json_object_object_add(lookup_stat, "DiagnosticsState", json_object_new_string(DIAG_DNS_SERVER_NOT_RESOLVE)); + json_object_object_add(lookup_stat, "SuccessCount", json_object_new_int(0)); + payload = json_object_to_json_string(lookup_stat); + payloadLen = strlen(payload) + 1; + nslookupMsgSend(ZCFG_MSG_NSLOOKUP_MSG, payloadLen, payload); + json_object_put(lookup_stat); + + return; + } + + c = 0; + while (c0) && ((timevale - timevals) >= deadline)){ + json_object_object_add(lookup_result, "Status", json_object_new_string(DIAG_TIMEOUT)); + } + else{ + json_object_object_add(lookup_result, "Status", json_object_new_string(DIAG_HOST_NAME_NOT_RESOLV)); + } + sprintf(cStr,"%d", c); + json_object_object_add(lookup_stat, cStr, lookup_result); + break; + } + sprintf(cStr,"%d", c); + json_object_object_add(lookup_stat, cStr, lookup_result); + + c++; + } + + json_object_object_add(lookup_stat, "DiagnosticsState", json_object_new_string(DIAG_COMPLETE)); + json_object_object_add(lookup_stat, "SuccessCount", json_object_new_int(successc)); + + payload = json_object_to_json_string(lookup_stat); + payloadLen = strlen(payload) + 1; + nslookupMsgSend(ZCFG_MSG_NSLOOKUP_MSG, payloadLen, payload); + json_object_put(lookup_stat); + + return ret; +#else set_default_dns(argv[2]); server_print(); @@ -225,6 +424,7 @@ set_default_dns(argv[2]); return print_host(argv[1], "Name:"); +#endif } Index: busybox-1.31.1/networking/traceroute.c =================================================================== --- busybox-1.31.1.orig/networking/traceroute.c 2019-06-10 18:50:53.000000000 +0800 +++ busybox-1.31.1/networking/traceroute.c 2021-03-18 09:39:56.743076800 +0800 @@ -334,6 +334,15 @@ # define SOL_RAW IPPROTO_RAW #endif +/*ZyXEL zcfg*/ +#ifdef ZCFG_SUPPORT +#include "zcfg_common.h" +#include "zcfg_msg.h" +#include "zcfg_net.h" + +#include +#endif +/*End ZyXEL zcfg*/ #define OPT_STRING \ "FIlnrdvxt:i:m:p:q:s:w:z:f:" \ @@ -418,6 +427,8 @@ #define outicmp ((struct icmp *)(outip + 1)) #define outudp ((struct udphdr *)(outip + 1)) +zcfgRet_t tracertMsgSend(int msg_type, int payloadLen, const char *payload); + static int wait_for_reply(len_and_sockaddr *from_lsa, struct sockaddr *to, unsigned *timestamp_us, int *left_ms) @@ -759,7 +770,11 @@ * numeric value, otherwise try for symbolic name. */ static void +#ifdef ZCFG_SUPPORT +print_inetname(const struct sockaddr *from, struct json_object *result) +#else print_inetname(const struct sockaddr *from) +#endif { char *ina = xmalloc_sockaddr2dotted_noport(from); @@ -775,15 +790,27 @@ n = xmalloc_sockaddr2host_noport((struct sockaddr*)from); } printf(" %s (%s)", (n ? n : ina), ina); +#ifdef ZCFG_SUPPORT + json_object_object_add(result, "Host", json_object_new_string((n ? n : ina))); + json_object_object_add(result, "HostAddress", json_object_new_string(ina)); +#endif free(n); } free(ina); } static void +#ifdef ZCFG_SUPPORT +print(int read_len, const struct sockaddr *from, const struct sockaddr *to, struct json_object *result) +#else print(int read_len, const struct sockaddr *from, const struct sockaddr *to) +#endif { +#ifdef ZCFG_SUPPORT + print_inetname(from, result); +#else print_inetname(from); +#endif if (verbose) { char *ina = xmalloc_sockaddr2dotted_noport(to); @@ -802,12 +829,62 @@ } static void +#ifdef ZCFG_SUPPORT +print_delta_ms(unsigned t1p, unsigned t2p, char *rtt) +#else print_delta_ms(unsigned t1p, unsigned t2p) +#endif { unsigned tt = t2p - t1p; +#ifdef ZCFG_SUPPORT + char rttime[16] = {0}; + + sprintf(rttime, "%u", tt/1000); + + if(!strcmp(rtt, "")) { + strcpy(rtt, rttime); + } + else { + strcat(rtt, ","); + strcat(rtt, rttime); + } +#endif printf(" %u.%03u ms", tt / 1000, tt % 1000); + } +#ifdef ZCFG_SUPPORT +zcfgRet_t tracertMsgSend(int msg_type, int payloadLen, const char *payload) +{ + zcfgRet_t ret; + void *sendBuf = NULL; + void *recvBuf = NULL; + zcfgMsg_t *sendMsgHdr = NULL; + char *recv_str = NULL; + int buf_size = 0; + + buf_size = sizeof(zcfgMsg_t)+payloadLen; + sendBuf = malloc(buf_size); + sendMsgHdr = (zcfgMsg_t*)sendBuf; + + sendMsgHdr->type = msg_type; + sendMsgHdr->length = payloadLen; + sendMsgHdr->srcEid = ZCFG_EID_TRACERT; + sendMsgHdr->dstEid = ZCFG_EID_ESMD; + + if(payload != NULL) + memcpy(sendBuf+sizeof(zcfgMsg_t), payload, payloadLen); + + ret = zcfgMsgSendAndGetReply(sendMsgHdr, (zcfgMsg_t **)&recvBuf, 0); + + if(ret == ZCFG_SUCCESS) { + recv_str = (char *)recvBuf+sizeof(zcfgMsg_t); + printf("Receive message : %s\n", recv_str); + } + + return ret; +} +#endif /* * Usage: [-dFIlnrvx] [-g gateway] [-i iface] [-f first_ttl] * [-m max_ttl] [ -p port] [-q nqueries] [-s src_addr] [-t tos] @@ -844,7 +921,11 @@ len_and_sockaddr *from_lsa; struct sockaddr *lastaddr; struct sockaddr *to; - +#ifdef ZCFG_SUPPORT + struct json_object *tracert_msg = NULL, *rt_hops = NULL, *result = NULL; + const char *payload = NULL; + int payloadLen = 0; +#endif INIT_G(); op |= getopt32(argv, "^" @@ -1056,12 +1137,23 @@ lastaddr = xzalloc(dest_lsa->len); to = xzalloc(dest_lsa->len); seq = 0; + +#ifdef ZCFG_SUPPORT + tracert_msg = json_object_new_object(); + rt_hops = json_object_new_array(); + json_object_object_add(tracert_msg, "RouteHops", rt_hops); +#endif for (ttl = first_ttl; ttl <= max_ttl; ++ttl) { int probe; int unreachable = 0; /* counter */ int gotlastaddr = 0; /* flags */ int got_there = 0; +#ifdef ZCFG_SUPPORT + char rtt[272] = {0}; + result = json_object_new_object(); + json_object_array_add(rt_hops, result); +#endif printf("%2d", ttl); for (probe = 0; probe < nprobes; ++probe) { int read_len; @@ -1094,12 +1186,24 @@ if (!gotlastaddr || (memcmp(lastaddr, &from_lsa->u.sa, from_lsa->len) != 0) ) { +#ifdef ZCFG_SUPPORT + print(read_len, &from_lsa->u.sa, to, result); +#else print(read_len, &from_lsa->u.sa, to); +#endif memcpy(lastaddr, &from_lsa->u.sa, from_lsa->len); gotlastaddr = 1; } +#ifdef ZCFG_SUPPORT + print_delta_ms(t1, t2, rtt); + if (icmp_code != -1) + json_object_object_add(result, "ErrorCode", json_object_new_int(icmp_code)); + else + json_object_object_add(result, "ErrorCode", json_object_new_int(0)); +#else print_delta_ms(t1, t2); +#endif ip = (struct ip *)recv_pkt; if (from_lsa->u.sa.sa_family == AF_INET) @@ -1194,7 +1298,9 @@ if (read_len == 0) printf(" *"); } /* for (nprobes) */ - +#ifdef ZCFG_SUPPORT + json_object_object_add(result, "RTTimes", json_object_new_string(rtt)); +#endif bb_putchar('\n'); if (got_there || (unreachable > 0 && unreachable >= nprobes - 1) @@ -1209,6 +1315,15 @@ free(from_lsa); } +#ifdef ZCFG_SUPPORT + json_object_object_add(tracert_msg, "DiagnosticsState", json_object_new_string(DIAG_COMPLETE)); + + payload = json_object_to_json_string(tracert_msg); + + payloadLen = strlen(payload) + 1; + tracertMsgSend(ZCFG_MSG_TRACERT_MSG, payloadLen, payload); + json_object_put(tracert_msg); +#endif return 0; } @@ -1216,6 +1331,9 @@ int traceroute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int traceroute_main(int argc UNUSED_PARAM, char **argv) { +#ifdef ZCFG_SUPPORT + zcfgEidInit(ZCFG_EID_TRACERT, NULL); +#endif return common_traceroute_main(0, argv); } #endif Index: busybox-1.31.1/networking/ping.c =================================================================== --- busybox-1.31.1.orig/networking/ping.c 2019-06-10 18:50:53.000000000 +0800 +++ busybox-1.31.1/networking/ping.c 2021-03-18 10:03:40.374534648 +0800 @@ -123,6 +123,15 @@ #include "libbb.h" #include "common_bufsiz.h" +/*ZyXEL zcfg*/ +#ifdef ZCFG_SUPPORT +#include "zcfg_common.h" +#include "zcfg_msg.h" +#include "zcfg_net.h" + +#include +#endif + #ifdef __BIONIC__ /* should be in netinet/ip_icmp.h */ # define ICMP_DEST_UNREACH 3 /* Destination Unreachable */ @@ -406,6 +415,9 @@ #define source_lsa (G.source_lsa ) #define str_I (G.str_I ) #define datalen (G.datalen ) +#define ntransmitted (G.ntransmitted) +#define nreceived (G.nreceived ) +#define nrepeats (G.nrepeats ) #define pingcount (G.pingcount ) #define opt_ttl (G.opt_ttl ) #define myid (G.myid ) @@ -432,33 +444,106 @@ #define CLR(bit) (BYTE(bit) &= (~MASK(bit))) #define TST(bit) (BYTE(bit) & MASK(bit)) +#ifdef ZCFG_SUPPORT +zcfgRet_t pingMsgSend(int msg_type, int payloadLen, const char *payload); +zcfgRet_t pingMsgSend(int msg_type, int payloadLen, const char *payload) +{ + zcfgRet_t ret; + void *sendBuf = NULL; + void *recvBuf = NULL; + zcfgMsg_t *sendMsgHdr = NULL; + char *recv_str = NULL; + int buf_size = 0; + + buf_size = sizeof(zcfgMsg_t)+payloadLen; + sendBuf = malloc(buf_size); + sendMsgHdr = (zcfgMsg_t*)sendBuf; + sendMsgHdr->type = msg_type; + sendMsgHdr->length = payloadLen; + sendMsgHdr->srcEid = ZCFG_EID_PING; + sendMsgHdr->dstEid = ZCFG_EID_ESMD; + + if(payload != NULL) + memcpy(sendBuf+sizeof(zcfgMsg_t), payload, payloadLen); + + ret = zcfgMsgSendAndGetReply(sendMsgHdr, (zcfgMsg_t **)&recvBuf, 0); + + if(ret == ZCFG_SUCCESS) { + recv_str = (char *)recvBuf+sizeof(zcfgMsg_t); + printf("Receive message : %s\n", recv_str); + } + + return ret; +} +#endif + static void print_stats_and_exit(int junk) NORETURN; static void print_stats_and_exit(int junk UNUSED_PARAM) { - unsigned long ul; - unsigned long nrecv; + unsigned long nrecv = 0; + unsigned long ul = 0; +#ifdef ZCFG_SUPPORT + unsigned long send_count = 0, recv_count = 0; + unsigned tavg = 0; + struct json_object *ping_stat = NULL; + const char *payload = NULL; + int payloadLen = 0; +#endif signal(SIGINT, SIG_IGN); - nrecv = G.nreceived; +#ifdef ZCFG_SUPPORT + send_count = ntransmitted; + recv_count = nreceived; +#endif + + nrecv = nreceived; printf("\n--- %s ping statistics ---\n" "%lu packets transmitted, " "%lu packets received, ", - hostname, G.ntransmitted, nrecv + hostname, ntransmitted, nrecv ); - if (G.nrepeats) - printf("%lu duplicates, ", G.nrepeats); - ul = G.ntransmitted; + if (nrepeats) + printf("%lu duplicates, ", nrepeats); + ul = ntransmitted; if (ul != 0) ul = (ul - nrecv) * 100 / ul; printf("%lu%% packet loss\n", ul); if (tmin != UINT_MAX) { - unsigned tavg = tsum / (nrecv + G.nrepeats); +#ifndef ZCFG_SUPPORT + unsigned tavg = tsum / (nrecv + nrepeats); +#else + tavg = tsum / (nrecv + nrepeats); +#endif printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n", tmin / 1000, tmin % 1000, tavg / 1000, tavg % 1000, tmax / 1000, tmax % 1000); } + +#ifdef ZCFG_SUPPORT + ping_stat = json_object_new_object(); + json_object_object_add(ping_stat, "DiagnosticsState", json_object_new_string(DIAG_COMPLETE)); + json_object_object_add(ping_stat, "SuccessCount", json_object_new_int(recv_count)); + json_object_object_add(ping_stat, "FailureCount", json_object_new_int(send_count - recv_count)); + if (tmin != UINT_MAX){ + json_object_object_add(ping_stat, "AverageResponseTime", json_object_new_int(tavg)); + json_object_object_add(ping_stat, "MinimumResponseTime", json_object_new_int(tmin)); + json_object_object_add(ping_stat, "MaximumResponseTime", json_object_new_int(tmax)); + } + else{ + json_object_object_add(ping_stat, "AverageResponseTime", json_object_new_int(0)); + json_object_object_add(ping_stat, "MinimumResponseTime", json_object_new_int(0)); + json_object_object_add(ping_stat, "MaximumResponseTime", json_object_new_int(0)); + } + + payload = json_object_to_json_string(ping_stat); + + payloadLen = strlen(payload) + 1; + pingMsgSend(ZCFG_MSG_PING_MSG, payloadLen, payload); + + json_object_put(ping_stat); +#endif /* if condition is true, exit with 1 -- 'failure' */ exit(nrecv == 0 || (G.deadline_us && nrecv < pingcount)); } @@ -467,8 +552,8 @@ { int sz; - CLR((uint16_t)G.ntransmitted % MAX_DUP_CHK); - G.ntransmitted++; + CLR((uint16_t)ntransmitted % MAX_DUP_CHK); + ntransmitted++; size_pkt += datalen; @@ -484,7 +569,8 @@ if (sz != size_pkt) bb_error_msg_and_die(bb_msg_write_error); - if (pingcount == 0 || G.ntransmitted < pingcount) { + + if (pingcount == 0 || ntransmitted < pingcount) { /* Didn't send all pings yet - schedule next in -i SEC interval */ struct itimerval i; signal(SIGALRM, sp); @@ -501,7 +587,7 @@ * otherwise ping waits for two RTTs. */ unsigned expire = timeout; - if (G.nreceived) { + if (nreceived) { /* approx. 2*tmax, in seconds (2 RTT) */ expire = tmax / (512*1024); if (expire == 0) @@ -520,7 +606,7 @@ pkt->icmp_type = ICMP_ECHO; /*pkt->icmp_code = 0;*/ pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */ - pkt->icmp_seq = htons(G.ntransmitted); /* don't ++ here, it can be a macro */ + pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */ pkt->icmp_id = myid; /* If datalen < 4, we store timestamp _past_ the packet, @@ -543,7 +629,8 @@ pkt->icmp6_type = ICMP6_ECHO_REQUEST; /*pkt->icmp6_code = 0;*/ /*pkt->icmp6_cksum = 0;*/ - pkt->icmp6_seq = htons(G.ntransmitted); /* don't ++ here, it can be a macro */ + pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */ + pkt->icmp6_id = myid; /*if (datalen >= 4)*/ @@ -626,11 +713,11 @@ m = MASK(recv_seq % MAX_DUP_CHK); /*if TST(recv_seq % MAX_DUP_CHK):*/ if (*b & m) { - ++G.nrepeats; + ++nrepeats; } else { /*SET(recv_seq % MAX_DUP_CHK):*/ *b |= m; - ++G.nreceived; + ++nreceived; dupmsg += 7; } @@ -761,7 +848,7 @@ continue; } c = unpack4(G.rcv_packet, c, &from); - if (pingcount && G.nreceived >= pingcount) + if (pingcount && nreceived >= pingcount) break; if (c && (option_mask32 & OPT_A)) { goto send_ping; @@ -852,7 +939,8 @@ } } c = unpack6(G.rcv_packet, c, &from, hoplimit); - if (pingcount && G.nreceived >= pingcount) + + if (pingcount && nreceived >= pingcount) break; if (c && (option_mask32 & OPT_A)) { goto send_ping; @@ -958,6 +1046,9 @@ int ping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int ping_main(int argc UNUSED_PARAM, char **argv) { +#ifdef ZCFG_SUPPORT + zcfgEidInit(ZCFG_EID_PING, NULL); +#endif # if !ENABLE_FEATURE_FANCY_PING return common_ping_main(AF_UNSPEC, argv); # else