Index: busybox-1_20_1/networking/ftpgetput.c =================================================================== --- busybox-1_20_1.orig/networking/ftpgetput.c 2012-05-27 17:48:55.000000000 -0700 +++ busybox-1_20_1/networking/ftpgetput.c 2015-03-19 03:20:56.704298772 -0700 @@ -51,6 +51,29 @@ #include "libbb.h" +#ifdef ZCFG_SUPPORT +#include +#include "zcfg_common.h" +#include "zcfg_net.h" +#include "zcfg_msg.h" +#include "zcfg_rdm_oid.h" +#include "zcfg_rdm_obj.h" +#include "zcfg_fe_rdm_struct.h" + +enum { + STATUS_ERR_INIT_CONN_FAILED = 1, + STATUS_ERR_NO_RESP, + STATUS_ERR_PASSWD_REQ_FAILED, + STATUS_ERR_LOGIN_FAILED, + STATUS_ERR_NO_TRANS_MODE, + STATUS_ERR_NO_PASV, + STATUS_ERR_NO_CWD, + STATUS_ERR_NO_STOR, + STATUS_ERR_NO_TRANS_COMPLETE +}; + +#endif + struct globals { const char *user; const char *password; @@ -58,6 +81,13 @@ FILE *control_stream; int verbose_flag; int do_continue; +#ifdef ZCFG_SUPPORT + const char *intf; + const char *url; + const char *dscp; + const char *ethpriority; + const char *dummy_size; +#endif char buf[4]; /* actually [BUFSZ] */ } FIX_ALIASING; #define G (*(struct globals*)&bb_common_bufsiz1) @@ -72,7 +102,22 @@ #define verbose_flag (G.verbose_flag ) #define do_continue (G.do_continue ) #define buf (G.buf ) +#ifdef ZCFG_SUPPORT +#define intf (G.intf ) +#define url (G.url ) +#define dscp (G.dscp ) +#define ethpriority (G.ethpriority ) +#define dummy_size (G.dummy_size ) +#endif #define INIT_G() do { } while (0) +#ifdef ZCFG_SUPPORT +static rdm_IpDiagUlDiag_t *ipDiagUlObj = NULL; +static unsigned int testSendLength = 0; +static char ipDscp = 0, ePriority = 0; +static int diagStateErrCode = 0; + +static void outputDiagUlData(int); +#endif static void ftp_die(const char *msg) NORETURN; @@ -84,8 +129,14 @@ while (*cp >= ' ' && *cp < '\x7f') cp++; *cp = '\0'; +#ifdef ZCFG_SUPPORT + printf("unexpected server response%s%s: %s\n", (msg ? " to " : ""), (msg ? msg : ""), buf); + outputDiagUlData(EXIT_FAILURE); + exit(0); +#else bb_error_msg_and_die("unexpected server response%s%s: %s", (msg ? " to " : ""), (msg ? msg : ""), buf); +#endif } static int ftpcmd(const char *s1, const char *s2) @@ -115,13 +166,32 @@ return n; } +#ifdef ZCFG_SUPPORT + +static int connect_stream(const struct len_and_sockaddr *slasa) +{ + int fd = socket(slasa->u.sa.sa_family, SOCK_STREAM, 0); + if(connect(fd, &slasa->u.sa, slasa->len) < 0) return 0; + else return fd; +} +#endif + static void ftp_login(void) { /* Connect to the command socket */ +#ifdef ZCFG_SUPPORT + control_stream = fdopen(connect_stream(lsa), "r+"); + if (control_stream == NULL) { + /* fdopen failed - extremely unlikely */ + diagStateErrCode = STATUS_ERR_INIT_CONN_FAILED; + outputDiagUlData(EXIT_FAILURE); + exit(0); +#else control_stream = fdopen(xconnect_stream(lsa), "r+"); if (control_stream == NULL) { /* fdopen failed - extremely unlikely */ bb_perror_nomsg_and_die(); +#endif } if (ftpcmd(NULL, NULL) != 220) { @@ -134,10 +204,16 @@ break; case 331: if (ftpcmd("PASS", password) != 230) { +#ifdef ZCFG_SUPPORT + diagStateErrCode = STATUS_ERR_PASSWD_REQ_FAILED; +#endif ftp_die("PASS"); } break; default: +#ifdef ZCFG_SUPPORT + diagStateErrCode = STATUS_ERR_LOGIN_FAILED; +#endif ftp_die("USER"); } @@ -170,6 +246,9 @@ */ if (ftpcmd("PASV", NULL) != 227) { +#ifdef ZCFG_SUPPORT + diagStateErrCode = STATUS_ERR_NO_PASV; +#endif ftp_die("PASV"); } @@ -191,6 +270,200 @@ return xconnect_stream(lsa); } +#ifdef ZCFG_SUPPORT +#define DUMMY_DATA_UNIT_SIZE 5120 +#define TIME_STAMP_DATA_SIZE 32 + +static void makeTimeStamp(char *timeStamp, unsigned int timeStampSz) +{ + struct tm *gmtm = NULL; + struct timeval tv; + unsigned int outputTmSpLen = 0; + char tmsp[TIME_STAMP_DATA_SIZE+10] = {0}; + + if(!timeStamp) return; + + if(!timeStampSz) return; + + gettimeofday(&tv, NULL); + gmtm = gmtime((const time_t *)&tv.tv_sec); + sprintf(tmsp, "%d-%02d-%02dT%02d:%02d:%02d.%ld", (gmtm->tm_year+1900), (gmtm->tm_mon+1), + gmtm->tm_mday, gmtm->tm_hour, gmtm->tm_min, gmtm->tm_sec, (long)tv.tv_usec); + outputTmSpLen = ((timeStampSz-1) < strlen(tmsp)) ? (timeStampSz-1) : strlen(tmsp); + strncpy(timeStamp, tmsp, outputTmSpLen); + timeStamp[outputTmSpLen] = '\0'; +} + +static int sendEsmdMsg(const char *data, unsigned int dataLen) +{ + char *recvBuf = NULL; + zcfgMsg_t *sendMsgHdr = NULL; + + + if(!dataLen) { + return ZCFG_INTERNAL_ERROR; + } + + sendMsgHdr = (zcfgMsg_t *)malloc(sizeof(zcfgMsg_t) + dataLen); + sendMsgHdr->type = ZCFG_MSG_UPLOAD_DIAG_COMPLETE_EVENT; + sendMsgHdr->length = dataLen; + sendMsgHdr->srcEid = ZCFG_EID_FTPGP; + sendMsgHdr->dstEid = ZCFG_EID_ESMD; + + memcpy((char *)(sendMsgHdr + 1), data, dataLen); + + return zcfgMsgSendAndGetReply(sendMsgHdr, (zcfgMsg_t **)&recvBuf, 0); +} + +static void outputDiagUlData(int rt) +{ +#ifdef SET_TEST_DATA_RDM + objIndex_t objIid; + rdm_IpDiagUlDiag_t *data; +#endif + zcfgRet_t rst; + + if(rt == EXIT_SUCCESS) { + strcpy(ipDiagUlObj->DiagnosticsState, "Completed"); + }else { + switch(diagStateErrCode) { + case STATUS_ERR_INIT_CONN_FAILED: + strcpy(ipDiagUlObj->DiagnosticsState, "Error_InitConnectionFailed"); + break; + case STATUS_ERR_NO_RESP: + strcpy(ipDiagUlObj->DiagnosticsState, "Error_NoResponse"); + break; + case STATUS_ERR_PASSWD_REQ_FAILED: + strcpy(ipDiagUlObj->DiagnosticsState, "Error_PasswordRequestFailed"); + break; + case STATUS_ERR_LOGIN_FAILED: + strcpy(ipDiagUlObj->DiagnosticsState, "Error_LoginFailed"); + break; + case STATUS_ERR_NO_TRANS_MODE: + strcpy(ipDiagUlObj->DiagnosticsState, "Error_NoTransferMode"); + break; + case STATUS_ERR_NO_PASV: + strcpy(ipDiagUlObj->DiagnosticsState, "Error_NoPASV"); + break; + case STATUS_ERR_NO_CWD: + strcpy(ipDiagUlObj->DiagnosticsState, "Error_NoCWD"); + break; + case STATUS_ERR_NO_STOR: + strcpy(ipDiagUlObj->DiagnosticsState, "Error_NoSTOR"); + break; + case STATUS_ERR_NO_TRANS_COMPLETE: + strcpy(ipDiagUlObj->DiagnosticsState, "Error_NoTransferComplete"); + break; + default: + strcpy(ipDiagUlObj->DiagnosticsState, "None"); + break; + } + } + + printf("DiagnosticsState: %s\n", ipDiagUlObj->DiagnosticsState); + printf("Interface: %s\n", ipDiagUlObj->Interface); + printf("UploadURL: %s\n", ipDiagUlObj->UploadURL); + printf("UploadTransports: %s\n", ipDiagUlObj->UploadTransports); + printf("DSCP: %d\n", ipDiagUlObj->DSCP); + printf("EthernetPriority: %d\n", ipDiagUlObj->EthernetPriority); + printf("TestFileLength: %u\n", ipDiagUlObj->TestFileLength); + printf("ROMTime: %s\n", ipDiagUlObj->ROMTime); + printf("BOMTime: %s\n", ipDiagUlObj->BOMTime); + printf("EOMTime: %s\n", ipDiagUlObj->EOMTime); + printf("TotalBytesSent: %u\n", ipDiagUlObj->TotalBytesSent); + printf("TCPOpenRequestTime: %s\n", ipDiagUlObj->TCPOpenRequestTime); + printf("TCPOpenResponseTime: %s\n", ipDiagUlObj->TCPOpenResponseTime); + + rst = sendEsmdMsg((const char *)ipDiagUlObj, sizeof(rdm_IpDiagUlDiag_t)); + if((rst != ZCFG_SUCCESS) && (rst != ZCFG_SUCCESS_AND_NO_REPLY)) { + printf("'ftpput' sending Esmd message failed!\n"); + } + + free(ipDiagUlObj); + +#ifdef SET_TEST_DATA_RDM + IID_INIT(objIid); + + if(zcfgFeObjStructGet(RDM_OID_IP_DIAG_UL_DIAG, &objIid, (void *)&data) != ZCFG_SUCCESS) { + printf("'ftpput' get UploadDiagnostics test data to rdm fail\n"); + return; + } + + data->TestFileLength = ipDiagUlObj->TestFileLength; + strcpy(data->DiagnosticsState, ipDiagUlObj->DiagnosticsState); + strcpy(data->Interface, ipDiagUlObj->Interface); + strcpy(data->UploadURL, ipDiagUlObj->UploadURL); +/* + strcpy(data->ROMTime, ipDiagUlObj->ROMTime); + strcpy(data->BOMTime, ipDiagUlObj->BOMTime); + strcpy(data->EOMTime, ipDiagUlObj->EOMTime); + strcpy(data->TCPOpenRequestTime, ipDiagUlObj->TCPOpenRequestTime); + strcpy(data->TCPOpenResponseTime, ipDiagUlObj->TCPOpenResponseTime); + data->DSCP = ipDiagUlObj->DSCP; + data->EthernetPriority = ipDiagUlObj->EthernetPriority; + data->TotalBytesSent = ipDiagUlObj->TotalBytesSent; + strcpy(data->UploadTransports, ipDiagUlObj->UploadTransports); +*/ + + if(zcfgFeObjStructSetWithoutApply(RDM_OID_IP_DIAG_UL_DIAG, &objIid, (void *)data, NULL) != ZCFG_SUCCESS) { + printf("'ftpput' set UploadDiagnostics test data to rdm fail\n"); + } + free(data); +#endif +} + +static int send_dummy_data_and_QUIT(const unsigned int data_size, int to) +{ + int rt = EXIT_SUCCESS; + char *dummy_data = NULL; + unsigned int send_data_size = 0, sent_data_size = 0; + + if(!data_size || !to) { + rt = EXIT_FAILURE; + goto complete; + } + + dummy_data = xmalloc(DUMMY_DATA_UNIT_SIZE); + if(!dummy_data) { + rt = EXIT_FAILURE; + goto complete; + } + + send_data_size = (data_size > DUMMY_DATA_UNIT_SIZE) ? DUMMY_DATA_UNIT_SIZE : data_size; + while(sent_data_size < data_size) { + unsigned int this_sent_data_size = 0; + + this_sent_data_size = safe_write(to, (const void *)dummy_data, send_data_size); + if(this_sent_data_size != send_data_size) { + break; + } + + sent_data_size += this_sent_data_size; + + send_data_size = (sent_data_size == data_size) ? 0 : ( + ((sent_data_size + DUMMY_DATA_UNIT_SIZE) > data_size) ? (data_size-sent_data_size) : DUMMY_DATA_UNIT_SIZE); + } + + ipDiagUlObj->TotalBytesSent = sent_data_size; + +complete: + /* close data connection */ + close(to); + + /* does server confirm that transfer is finished? */ + if (ftpcmd(NULL, NULL) != 226) { + ftp_die(NULL); + }else { + makeTimeStamp(ipDiagUlObj->EOMTime, sizeof(ipDiagUlObj->EOMTime)); + } + ftpcmd("QUIT", NULL); + + if(dummy_data) free(dummy_data); + + return rt; +} +#endif + static int pump_data_and_QUIT(int from, int to) { /* copy the file */ @@ -238,7 +511,12 @@ struct stat sbuf; /* lstat would be wrong here! */ if (stat(local_path, &sbuf) < 0) { +#ifdef ZCFG_SUPPORT + outputDiagUlData(EXIT_FAILURE); + exit(0); +#else bb_perror_msg_and_die("stat"); +#endif } if (sbuf.st_size > 0) { beg_range = sbuf.st_size; @@ -279,14 +557,30 @@ int fd_data; int fd_local; int response; +#ifdef ZCFG_SUPPORT + int rt; + + makeTimeStamp(ipDiagUlObj->TCPOpenRequestTime, sizeof(ipDiagUlObj->TCPOpenRequestTime)); +#endif + /* connect to the data socket */ fd_data = xconnect_ftpdata(); + +#ifdef ZCFG_SUPPORT + makeTimeStamp(ipDiagUlObj->TCPOpenResponseTime, sizeof(ipDiagUlObj->TCPOpenResponseTime)); +#endif + /* get the local file */ fd_local = STDIN_FILENO; if (NOT_LONE_DASH(local_path)) fd_local = xopen(local_path, O_RDONLY); +#ifdef ZCFG_SUPPORT + else { + fd_local = -1; + } +#endif response = ftpcmd("STOR", server_path); switch (response) { @@ -294,20 +588,45 @@ case 150: break; default: +#ifdef ZCFG_SUPPORT + diagStateErrCode = STATUS_ERR_NO_STOR; +#endif ftp_die("STOR"); } +#ifdef ZCFG_SUPPORT + if(testSendLength) { + makeTimeStamp(ipDiagUlObj->ROMTime, sizeof(ipDiagUlObj->ROMTime)); + + if(setsockopt(fd_data, IPPROTO_IP, IP_TOS, &ipDscp, sizeof(ipDscp)) != 0) { + printf("Set ftp ip dscp failed!\n"); + } + + makeTimeStamp(ipDiagUlObj->BOMTime, sizeof(ipDiagUlObj->BOMTime)); + rt = send_dummy_data_and_QUIT((const unsigned int)testSendLength, fd_data); + + return rt; + }else return pump_data_and_QUIT(fd_local, fd_data); +#else return pump_data_and_QUIT(fd_local, fd_data); +#endif } #endif #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS static const char ftpgetput_longopts[] ALIGN1 = - "continue\0" Required_argument "c" - "verbose\0" No_argument "v" - "username\0" Required_argument "u" - "password\0" Required_argument "p" - "port\0" Required_argument "P" + "continue\0" Required_argument "c" + "verbose\0" No_argument "v" + "username\0" Required_argument "u" + "password\0" Required_argument "p" + "port\0" Required_argument "P" +#ifdef ZCFG_SUPPORT + "intf\0" Required_argument "n" + "url\0" Required_argument "l" + "dscp\0" Required_argument "d" + "ethpriority\0" Required_argument "r" + "dummy_size\0" Required_argument "m" +#endif ; #endif @@ -342,19 +661,91 @@ applet_long_options = ftpgetput_longopts; #endif opt_complementary = "-2:vv:cc"; /* must have 2 to 3 params; -v and -c count */ +#ifdef ZCFG_SUPPORT + getopt32(argv, "cvu:p:P:n:l:d:r:m:", &user, &password, &port, &intf, &url, &dscp, ðpriority, &dummy_size, + &verbose_flag, &do_continue); +#else getopt32(argv, "cvu:p:P:", &user, &password, &port, &verbose_flag, &do_continue); +#endif argv += optind; +#ifdef ZCFG_SUPPORT + printf("Interface: %s\n", intf); + + printf("URL: %s\n", url); + + ipDscp = (dscp) ? xatou_range(dscp, 0, 255) : 0; + printf("ipDscp: %u\n", ipDscp); + + ePriority = (ethpriority) ? xatou_range(ethpriority, 0, 255) : 0; + printf("ePriority: %u\n", ePriority); + + testSendLength = (dummy_size) ? xatou32(dummy_size) : 0; + printf("testSendLength: %u\n", testSendLength); + + ipDiagUlObj = (rdm_IpDiagUlDiag_t *)xmalloc(sizeof(rdm_IpDiagUlDiag_t)); + if(!ipDiagUlObj) { + printf("ftpput: Memory alloca fail\n"); + return EXIT_FAILURE; + } + memset(ipDiagUlObj, 0, sizeof(rdm_IpDiagUlDiag_t)); + + if(intf) { + strncpy(ipDiagUlObj->Interface, intf, (strlen(intf) > sizeof(ipDiagUlObj->Interface)) ? + sizeof(ipDiagUlObj->Interface) : strlen(intf)); + } + + if(url) { + strncpy(ipDiagUlObj->UploadURL, url, (strlen(url) > sizeof(ipDiagUlObj->UploadURL)) ? + sizeof(ipDiagUlObj->UploadURL) : strlen(url)); + } + + ipDiagUlObj->DSCP = ipDscp; + ipDiagUlObj->TestFileLength = testSendLength; + ipDiagUlObj->EthernetPriority = ePriority; + + zcfgEidInit(ZCFG_EID_FTPGP, (const char *)"ftpgp"); + if(zcfgMsgServerInit() != ZCFG_SUCCESS){ + printf("'ftpput' msg server init fail\n"); + outputDiagUlData(EXIT_FAILURE); + return EXIT_FAILURE; + } +#endif /* We want to do exactly _one_ DNS lookup, since some * sites (i.e. ftp.us.debian.org) use round-robin DNS * and we want to connect to only one IP... */ lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21)); +#ifdef ZCFG_SUPPORT + if(!lsa) { + diagStateErrCode = STATUS_ERR_INIT_CONN_FAILED; + outputDiagUlData(EXIT_FAILURE); + return EXIT_FAILURE; + } +#endif if (verbose_flag) { printf("Connecting to %s (%s)\n", argv[0], xmalloc_sockaddr2dotted(&lsa->u.sa)); } ftp_login(); +#ifdef ZCFG_SUPPORT + if(testSendLength) { + int rt; + + if(!diagStateErrCode) { + rt = ftp_action(argv[1], argv[2] ? argv[2] : argv[1]); + }else rt = EXIT_FAILURE; + + //output test data + outputDiagUlData(rt); + + return rt; + }else { + outputDiagUlData(EXIT_FAILURE); + return EXIT_FAILURE; + } +#else return ftp_action(argv[1], argv[2] ? argv[2] : argv[1]); +#endif } Index: busybox-1_20_1/networking/wget.c =================================================================== --- busybox-1_20_1.orig/networking/wget.c 2015-03-19 03:20:56.684298772 -0700 +++ busybox-1_20_1/networking/wget.c 2015-03-19 03:21:44.224192219 -0700 @@ -101,16 +101,19 @@ #if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic #define DEFAULT_DATETIME "0001-01-01T00:00:00Z" -static char DiagnosticsState[64] = ""; +static char DiagnosticsState[64] = "Requested"; static char ROMTime[64] = DEFAULT_DATETIME; static char BOMTime[64] = DEFAULT_DATETIME; static char EOMTime[64] = DEFAULT_DATETIME; static char TCPOpenRequestTime[64] = DEFAULT_DATETIME; static char TCPOpenResponseTime[64] = DEFAULT_DATETIME; static unsigned long TotalBytesReceived = 0; +static unsigned long TestBytesReceived = 0; static unsigned long ifBytesReceivedStart = 0; static unsigned long ifBytesReceivedEnd = 0; +static char ipDscp = 0; + int priorityMark=0; char dstIP[32]={0}; @@ -152,6 +155,38 @@ #if defined(ZCFG_SUPPORT)//TR143 +static void outputDiagDlData(void) +{ + struct json_object *diag_result = NULL; + const char *payload = NULL; + int payloadLen = 0; + + if(!strcmp(DiagnosticsState, "Requested")) { + strcpy(DiagnosticsState, "Completed"); + }else if(strstr(DiagnosticsState, "None")) { + DiagnosticsState[strlen("None")] = '\0'; + } + + diag_result = json_object_new_object(); + json_object_object_add(diag_result, "DiagnosticsState", json_object_new_string(DiagnosticsState)); + json_object_object_add(diag_result, "ROMTime", json_object_new_string(ROMTime)); + json_object_object_add(diag_result, "BOMTime", json_object_new_string(BOMTime)); + json_object_object_add(diag_result, "EOMTime", json_object_new_string(EOMTime)); + json_object_object_add(diag_result, "TotalBytesReceived", json_object_new_int(TotalBytesReceived)); + json_object_object_add(diag_result, "TestBytesReceived", json_object_new_int(TestBytesReceived)); + json_object_object_add(diag_result, "TCPOpenRequestTime", json_object_new_string(TCPOpenRequestTime)); + json_object_object_add(diag_result, "TCPOpenResponseTime", json_object_new_string(TCPOpenResponseTime)); + + payload = json_object_to_json_string(diag_result); + payloadLen = strlen(payload) + 1; + ResultMsgSend(ZCFG_MSG_DOWNLOAD_DIAG_COMPLETE_EVENT, payloadLen, payload); + + json_object_put(diag_result); + set_priorityMark(0); + + exit(0); +} + zcfgRet_t ResultMsgSend(int msg_type, int payloadLen, const char *payload) { zcfgRet_t ret; @@ -345,16 +380,37 @@ return s; } +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + +static int connect_stream(const len_and_sockaddr *lsa) +{ + int fd = socket(lsa->u.sa.sa_family, SOCK_STREAM, 0); + if(connect(fd, &lsa->u.sa, lsa->len) < 0) return 0; + else return fd; +} + +static FILE *open_socket(len_and_sockaddr *lsa, int *sk) +#else static FILE *open_socket(len_and_sockaddr *lsa) +#endif { FILE *fp; /* glibc 2.4 seems to try seeking on it - ??! */ /* hopefully it understands what ESPIPE means... */ +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + int stream_sk = connect_stream(lsa); + if(!stream_sk) return NULL; + if(sk) *sk = stream_sk; + + fp = fdopen(stream_sk, "r+"); + if (fp == NULL) + printf("%s\n", bb_msg_memory_exhausted); +#else fp = fdopen(xconnect_stream(lsa), "r+"); if (fp == NULL) bb_perror_msg_and_die(bb_msg_memory_exhausted); - +#endif return fp; } @@ -368,6 +424,8 @@ { #if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic strcpy(DiagnosticsState, "Error_NoResponse"); + printf("error getting response\n"); + outputDiagDlData(); #endif bb_perror_msg_and_die("error getting response"); } @@ -420,7 +478,15 @@ h->host = url + 6; h->is_ftp = 1; } else +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + { + strcpy(DiagnosticsState, "None"); + printf("not an http or ftp url: %s\n", sanitize_string(url)); + outputDiagDlData(); + } +#else bb_error_msg_and_die("not an http or ftp url: %s", sanitize_string(url)); +#endif // FYI: // "Real" wget 'http://busybox.net?var=a/b' sends this request: @@ -492,7 +558,15 @@ /* verify we are at the end of the header name */ if (*s != ':') +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + { + strcpy(DiagnosticsState, "None"); + printf("bad header line: %s\n", sanitize_string(G.wget_buf)); + outputDiagDlData(); + } +#else bb_error_msg_and_die("bad header line: %s", sanitize_string(G.wget_buf)); +#endif /* locate the start of the header value */ *s++ = '\0'; @@ -516,9 +590,39 @@ if (!target->user) target->user = xstrdup("anonymous:busybox@"); +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + { + int sk = 0; + + getDateTime(TCPOpenRequestTime); + sfp = open_socket(lsa, &sk); + if((sk>0) && strcmp(G.ifName, "")) { + setsockopt_bindtodevice(sk, (const char *)G.ifName); + } + if((sk>0) && ipDscp) { + if(setsockopt(sk, IPPROTO_IP, IP_TOS, &ipDscp, sizeof(ipDscp)) != 0) + printf("Set ip dscp failed!\n"); + } + if (sfp == NULL) + { + strcpy(DiagnosticsState, "Error_InitConnectionFailed"); + outputDiagDlData(); + } + getDateTime(TCPOpenResponseTime); + } +#else sfp = open_socket(lsa); +#endif if (ftpcmd(NULL, NULL, sfp) != 220) +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + { + printf("%s\n", sanitize_string(G.wget_buf + 4)); + strcpy(DiagnosticsState, "Error_TransferFailed"); + outputDiagDlData(); + } +#else bb_error_msg_and_die("%s", sanitize_string(G.wget_buf + 4)); +#endif /* * Splitting username:password pair, @@ -535,7 +639,15 @@ break; /* fall through (failed login) */ default: +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + { + strcpy(DiagnosticsState, "Error_LoginFailed"); + printf("ftp login: %s\n", sanitize_string(G.wget_buf + 4)); + outputDiagDlData(); + } +#else bb_error_msg_and_die("ftp login: %s", sanitize_string(G.wget_buf + 4)); +#endif } ftpcmd("TYPE I", NULL, sfp); @@ -546,7 +658,13 @@ if (ftpcmd("SIZE ", target->path, sfp) == 213) { G.content_len = BB_STRTOOFF(G.wget_buf + 4, NULL, 10); if (G.content_len < 0 || errno) { +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + strcpy(DiagnosticsState, "Error_IncorrectSize"); + printf("SIZE value is garbage\n"); + outputDiagDlData(); +#else bb_error_msg_and_die("SIZE value is garbage"); +#endif } G.got_clen = 1; } @@ -556,7 +674,13 @@ */ if (ftpcmd("PASV", NULL, sfp) != 227) { pasv_error: +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + strcpy(DiagnosticsState, "Error_NoPASV"); + printf("bad response to %s: %s", "PASV\n", sanitize_string(G.wget_buf)); + outputDiagDlData(); +#else bb_error_msg_and_die("bad response to %s: %s", "PASV", sanitize_string(G.wget_buf)); +#endif } // Response is "227 garbageN1,N2,N3,N4,P1,P2[)garbage] // Server's IP is N1.N2.N3.N4 (we ignore it) @@ -571,8 +695,30 @@ if (!str) goto pasv_error; port += xatou_range(str+1, 0, 255) * 256; set_nport(&lsa->u.sa, htons(port)); +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + { + int sk = 0; + + *dfpp = open_socket(lsa, &sk); + + if(*dfpp == NULL) { + strcpy(DiagnosticsState, "Error_NoPASV"); + printf("Failed to build PASV connection\n"); + outputDiagDlData(); + } + + if((sk>0) && strcmp(G.ifName, "")) { + setsockopt_bindtodevice(sk, (const char *)G.ifName); + } + if((sk>0) && ipDscp) { + if(setsockopt(sk, IPPROTO_IP, IP_TOS, &ipDscp, sizeof(ipDscp)) != 0) + printf("Set ip dscp failed!\n"); + } - *dfpp = open_socket(lsa); + } +#else + *dfpp = open_socket(lsa); +#endif if (G.beg_range) { sprintf(G.wget_buf, "REST %"OFF_FMT"u", G.beg_range); @@ -580,9 +726,26 @@ G.content_len -= G.beg_range; } +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + // retrieve receive information here with system's proc update timing + if (option_mask32 & WGET_OPT_INTERFACE) { + ifBytesReceivedStart = getReceiveByte(G.ifName); + printf("Receive data start: %ld\n", ifBytesReceivedStart); + } + + getDateTime(ROMTime); +#endif + if (ftpcmd("RETR ", target->path, sfp) > 150) +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + { + strcpy(DiagnosticsState, "None"); + printf("bad response to %s: %s\n", "RETR", sanitize_string(G.wget_buf)); + outputDiagDlData(); + } +#else bb_error_msg_and_die("bad response to %s: %s", "RETR", sanitize_string(G.wget_buf)); - +#endif return sfp; } @@ -638,7 +801,13 @@ # if ENABLE_FEATURE_WGET_TIMEOUT if (second_cnt != 0 && --second_cnt == 0) { progress_meter(PROGRESS_END); +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + strcpy(DiagnosticsState, "Error_Timeout"); + printf("download timed out\n"); + outputDiagDlData(); +#else bb_error_msg_and_die("download timed out"); +#endif } # endif /* Needed for "stalled" indicator */ @@ -669,9 +838,12 @@ #endif if (ferror(dfp)){ #if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic - set_priorityMark(0); -#endif + strcpy(DiagnosticsState, "None"); + printf("%s\n", bb_msg_read_error); + outputDiagDlData(); +#else bb_perror_msg_and_die(bb_msg_read_error); +#endif } break; /* EOF, not error */ } @@ -679,14 +851,17 @@ #if defined(ZCFG_SUPPORT) //TR143 if (safe_fwrite(G.wget_buf, 1, n, G.output_fd) != n) { - set_priorityMark(0); strcpy(DiagnosticsState, "Error_TransferFailed"); + outputDiagDlData(); } #else xwrite(G.output_fd, G.wget_buf, n); #endif #if ENABLE_FEATURE_WGET_STATUSBAR + #if defined(ZCFG_SUPPORT) //TR143 + TestBytesReceived += n; + #endif G.transferred += n; progress_meter(PROGRESS_BUMP); #endif @@ -696,10 +871,6 @@ break; } } -#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic - TotalBytesReceived = ifBytesReceivedStart - ifBytesReceivedEnd; - getDateTime(EOMTime); -#endif #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT clearerr(dfp); ndelay_off(polldata.fd); /* else fgets can get very unhappy */ @@ -735,12 +906,6 @@ char *redirected_path = NULL; struct host_info server; struct host_info target; -#if defined(ZCFG_SUPPORT) //tr143 - char cmd[128]; - struct json_object *diag_result = NULL; - const char *payload = NULL; - int payloadLen = 0; -#endif server.allocated = NULL; target.allocated = NULL; server.user = NULL; @@ -804,6 +969,12 @@ redir_limit = 5; resolve_lsa: lsa = xhost2sockaddr(server.host, server.port); +#if defined(ZCFG_SUPPORT) //TR143 + if(!lsa) { + strcpy(DiagnosticsState, "Error_InitConnectionFailed"); + outputDiagDlData(); + } +#endif if (!(option_mask32 & WGET_OPT_QUIET)) { char *s = xmalloc_sockaddr2dotted(&lsa->u.sa); fprintf(stderr, "Connecting to %s (%s)\n", server.host, s); @@ -823,18 +994,27 @@ */ char *str; int status; - -#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic - getDateTime(TCPOpenRequestTime); -#endif /* Open socket to http server */ - sfp = open_socket(lsa); #if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + int sk = 0; + + getDateTime(TCPOpenRequestTime); + sfp = open_socket(lsa, &sk); + if((sk>0) && strcmp(G.ifName, "")) { + setsockopt_bindtodevice(sk, (const char *)G.ifName); + } + if((sk>0) && ipDscp) { + if(setsockopt(sk, IPPROTO_IP, IP_TOS, &ipDscp, sizeof(ipDscp)) != 0) + printf("Set ip dscp failed!\n"); + } if (sfp == NULL) { strcpy(DiagnosticsState, "Error_InitConnectionFailed"); + outputDiagDlData(); } getDateTime(TCPOpenResponseTime); +#else + sfp = open_socket(lsa); #endif /* Send HTTP request */ @@ -873,9 +1053,10 @@ #if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic getDateTime(ROMTime); - + //http if (option_mask32 & WGET_OPT_INTERFACE){ ifBytesReceivedStart = getReceiveByte(G.ifName); + printf("Receive data start: %ld\n", ifBytesReceivedStart); } #endif @@ -919,7 +1100,7 @@ goto read_response; case 200: #if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic - getDateTime(BOMTime); + getDateTime(BOMTime); #endif /* Response 204 doesn't say "null file", it says "metadata @@ -957,7 +1138,15 @@ break; /* fall through */ default: +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + { + strcpy(DiagnosticsState, "None"); + printf("server returned error: %s\n", sanitize_string(G.wget_buf)); + outputDiagDlData(); + } +#else bb_error_msg_and_die("server returned error: %s", sanitize_string(G.wget_buf)); +#endif } /* @@ -983,19 +1172,41 @@ if (key == KEY_content_length) { G.content_len = BB_STRTOOFF(str, NULL, 10); if (G.content_len < 0 || errno) { +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + strcpy(DiagnosticsState, "Error_IncorrectSize"); + printf("content-length %s is garbage\n", sanitize_string(str)); + outputDiagDlData(); +#else bb_error_msg_and_die("content-length %s is garbage", sanitize_string(str)); +#endif } G.got_clen = 1; continue; } if (key == KEY_transfer_encoding) { if (strcmp(str_tolower(str), "chunked") != 0) +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + { + strcpy(DiagnosticsState, "None"); + printf("transfer encoding '%s' is not supported\n", sanitize_string(str)); + outputDiagDlData(); + } +#else bb_error_msg_and_die("transfer encoding '%s' is not supported", sanitize_string(str)); +#endif G.chunked = 1; } if (key == KEY_location && status >= 300) { if (--redir_limit == 0) +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + { + strcpy(DiagnosticsState, "None"); + printf("too many redirections\n"); + outputDiagDlData(); + } +#else bb_error_msg_and_die("too many redirections"); +#endif fclose(sfp); if (str[0] == '/') { free(redirected_path); @@ -1028,6 +1239,9 @@ * FTP session */ sfp = prepare_ftp_session(&dfp, &target, lsa); +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + getDateTime(BOMTime); +#endif } free(lsa); @@ -1041,10 +1255,6 @@ #endif } retrieve_file_data(dfp); -#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic - if (option_mask32 & WGET_OPT_INTERFACE) - ifBytesReceivedEnd = getReceiveByte(G.ifName); -#endif if (!(option_mask32 & WGET_OPT_OUTNAME)) { #if defined(ZCFG_SUPPORT) //TR143 fclose(G.output_fd); @@ -1059,29 +1269,29 @@ /* It's ftp. Close data connection properly */ fclose(dfp); if (ftpcmd(NULL, NULL, sfp) != 226) +#if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic + { + strcpy(DiagnosticsState, "None"); + printf("ftp error: %s\n", sanitize_string(G.wget_buf + 4)); + outputDiagDlData(); + } +#else bb_error_msg_and_die("ftp error: %s", sanitize_string(G.wget_buf + 4)); +#endif /* ftpcmd("QUIT", NULL, sfp); - why bother? */ } fclose(sfp); #if defined(ZCFG_SUPPORT) //TR143:DownloadDiagnstic - strcpy(DiagnosticsState, "Completed"); - - diag_result = json_object_new_object(); - json_object_object_add(diag_result, "DiagnosticsState", json_object_new_string(DiagnosticsState)); - json_object_object_add(diag_result, "ROMTime", json_object_new_string(ROMTime)); - json_object_object_add(diag_result, "BOMTime", json_object_new_string(BOMTime)); - json_object_object_add(diag_result, "EOMTime", json_object_new_string(EOMTime)); - json_object_object_add(diag_result, "TotalBytesReceived", json_object_new_int(TotalBytesReceived)); - json_object_object_add(diag_result, "TCPOpenRequestTime", json_object_new_string(TCPOpenRequestTime)); - json_object_object_add(diag_result, "TCPOpenResponseTime", json_object_new_string(TCPOpenResponseTime)); - - payload = json_object_to_json_string(diag_result); - payloadLen = strlen(payload) + 1; - ResultMsgSend(ZCFG_MSG_DOWNLOAD_DIAG_COMPLETE_EVENT, payloadLen, payload); + if (option_mask32 & WGET_OPT_INTERFACE) { + ifBytesReceivedEnd = getReceiveByte(G.ifName); + TotalBytesReceived = (ifBytesReceivedEnd > ifBytesReceivedStart) ? + (ifBytesReceivedEnd - ifBytesReceivedStart) : 0; + printf("Receive data end: %ld\n", ifBytesReceivedEnd); + } + getDateTime(EOMTime); - json_object_put(diag_result); - set_priorityMark(0); + outputDiagDlData(); #endif free(server.allocated); @@ -1151,6 +1361,10 @@ IF_FEATURE_WGET_LONG_OPTIONS(, &headers_llist) IF_FEATURE_WGET_LONG_OPTIONS(, &G.post_data) ); + + ipDscp = (G.dscp) ? xatou_range(G.dscp, 0, 255) : 0; + printf("Dscp: %u\n", ipDscp); + #else getopt32(argv, "csqO:P:Y:U:T:" /*ignored:*/ "t:", &G.fname_out, &G.dir_prefix,