static int raw_fileop_open(void **ref,void *fsctx_arg,char *filename,int mode) { raw_fsctx_t *fsctx; raw_file_t *file; char temp[100]; char *len; if (mode != FILE_MODE_READ) return CFE_ERR_UNSUPPORTED; fsctx = (raw_fsctx_t *) fsctx_arg; file = KMALLOC(sizeof(raw_file_t),0); if (!file) { return CFE_ERR_NOMEM; } file->raw_fileoffset = 0; file->raw_fsctx = fsctx; /* Assume the whole device. */ file->raw_baseoffset = 0; file->raw_length = -1; /* * If a filename was specified, it will be in the form * offset,length - for example, 0x10000,0x200 * Parse this into two pieces and set up our internal * file extent information. you can use either decimal * or "0x" notation. */ if (filename) { lib_trimleading(filename); strncpy(temp,filename,sizeof(temp)); len = strchr(temp,','); if (len) *len++ = '\0'; if (temp[0]) { file->raw_baseoffset = lib_atoi(temp); } if (len) { file->raw_length = lib_atoi(len); } } fsctx->raw_refcnt++; *ref = file; return 0; }
int lib_parseipaddr(const char *ipaddr,uint8_t *dest) { int a,b,c,d; char *x; /* make sure it's all digits and dots. */ x = (char *) ipaddr; while (*x) { if ((*x == '.') || ((*x >= '0') && (*x <= '9'))) { x++; continue; } return -1; } x = (char *) ipaddr; a = lib_atoi(ipaddr); x = lib_strchr(x,'.'); if (!x) return -1; b = lib_atoi(x+1); x = lib_strchr(x+1,'.'); if (!x) return -1; c = lib_atoi(x+1); x = lib_strchr(x+1,'.'); if (!x) return -1; d = lib_atoi(x+1); if ((a < 0) || (a > 255)) return -1; if ((b < 0) || (b > 255)) return -1; if ((c < 0) || (c > 255)) return -1; if ((d < 0) || (d > 255)) return -1; dest[0] = (uint8_t) a; dest[1] = (uint8_t) b; dest[2] = (uint8_t) c; dest[3] = (uint8_t) d; return 0; }
static int http_fileop_open(void **ref,void *fsctx_arg,char *filename,int mode) { http_fsctx_t *fsctx; http_file_t *file; char temp[200]; char *hostname, *filen; int hlen; int termidx; int res; int err = 0; char *hptr; char *tok; uint8_t hostaddr[IP_ADDR_LEN]; uint8_t termstr[4]; uint8_t b; if (mode != FILE_MODE_READ) return CFE_ERR_UNSUPPORTED; fsctx = (http_fsctx_t *) fsctx_arg; file = KMALLOC(sizeof(http_file_t),0); if (!file) { return CFE_ERR_NOMEM; } file->http_filename = lib_strdup(filename); if (!file->http_filename) { KFREE(file); return CFE_ERR_NOMEM; } lib_chop_filename(file->http_filename,&hostname,&filen); /* * Look up remote host */ res = dns_lookup(hostname,hostaddr); if (res < 0) { KFREE(file); return res; } file->http_socket = tcp_socket(); if (file->http_socket < 0) { KFREE(file->http_filename); KFREE(file); return -1; } /* * Connect to remote host. */ tcp_setflags(file->http_socket,0); /* set socket to blocking */ res = tcp_connect(file->http_socket,hostaddr,80); if (res < 0) { tcp_close(file->http_socket); KFREE(file->http_filename); KFREE(file); return res; } /* * Send GET command. Supply the hostname (for HTTP 1.1 requirements) * and set the connection to close as soon as all the data is received. */ hlen = sprintf(temp,"GET /%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",filen,hostname); res = tcp_send(file->http_socket,temp,hlen); if (res < 0) { tcp_close(file->http_socket); KFREE(file->http_filename); KFREE(file); return res; } /* * Read bytes until we either reach EOF or we see "\r\n\r\n" * This is the server's status string. */ termstr[0] = '\r'; termstr[1] = '\n'; termstr[2] = '\r'; termstr[3] = '\n'; termidx = 0; file->http_offset = 0; file->http_blen = 0; file->http_bptr = file->http_buffer; res = 0; for (;;) { res = tcp_recv(file->http_socket,&b,1); if (res < 0) break; if (b == termstr[termidx]) { termidx++; if (termidx == 4) break; } else { termidx = 0; } /* * Save the bytes from the header up to our buffer * size. It's okay if we don't save it all, * since all we want is the result code which comes * first. */ if (file->http_blen < (HTTP_BUFSIZE-1)) { *(file->http_bptr) = b; file->http_bptr++; file->http_blen++; } } /* * Premature EOFs are not good, bail now. */ if (res < 0) { err = CFE_ERR_EOF; goto protocolerr; } /* * Skip past the HTTP response header and grab the result code. * Sanity check it a little. */ *(file->http_bptr) = 0; hptr = file->http_buffer; tok = lib_gettoken(&hptr); if (!tok || (memcmp(tok,"HTTP",4) != 0)) { err = CFE_ERR_PROTOCOLERR; goto protocolerr; } tok = lib_gettoken(&hptr); if (!tok) { err = CFE_ERR_PROTOCOLERR; goto protocolerr; } switch (lib_atoi(tok)) { case 200: err = 0; break; case 404: err = CFE_ERR_FILENOTFOUND; break; } /* * If we get to here, the file is okay and we're about to receive data. */ if (err == 0) { *ref = file; return 0; } protocolerr: tcp_close(file->http_socket); KFREE(file->http_filename); KFREE(file); *ref = NULL; return err; }
/********************************************************************************************************* ** 函数名称: __tshellRoute ** 功能描述: 系统命令 "route" ** 输 入 : iArgC 参数个数 ** ppcArgV 参数表 ** 输 出 : ERROR ** 全局变量: ** 调用模块: *********************************************************************************************************/ INT __tshellRoute (INT iArgC, PCHAR *ppcArgV) { #define LW_RT_PRINT_SIZE 74 INT iError; FUNCPTR pfuncAddOrChange; PCHAR pcOpAddorChange; UINT uiFlag = 0; ip_addr_t ipaddr; CHAR cNetifName[IF_NAMESIZE]; if (iArgC == 1) { PCHAR pcBuffer; size_t stSize; size_t stOffset; UINT uiNumEntries; uiNumEntries = (UINT)__rtSafeRun((FUNCPTR)__rtEntryMaxNum, 0, 0, 0, 0, 0, 0); if (uiNumEntries == 0) { printf("no route entry.\n"); return (ERROR_NONE); } /* * 在 net safe 状态下不允许调用 printf 等使用 IO 的语句. 所以只能打印到缓冲区中, * 然后统一使用 IO 打印. */ stSize = LW_RT_PRINT_SIZE * uiNumEntries; pcBuffer = (PCHAR)__SHEAP_ALLOC(stSize); if (pcBuffer == LW_NULL) { fprintf(stderr, "system low memory.\n"); return (PX_ERROR); } /* * 打印 kernel route entry */ stOffset = 0; printf("kernel routing tables\n"); printf("Destination Gateway Mask Flag Interface\n"); __rtSafeRun((FUNCPTR)__rtTraversal, (void *)__rtEntryPrint, pcBuffer, (PVOID)stSize, &stOffset, 0, 0); if (stOffset > 0) { fwrite(pcBuffer, stOffset, 1, stdout); fflush(stdout); /* 这里必须确保输出完成 */ } /* * 打印 lwip build-in route entry */ stOffset = 0; printf("\nbuild-in routing tables\n"); printf("Destination Gateway Mask Flag Interface\n"); __rtSafeRun((FUNCPTR)__buildinRtPrint, pcBuffer, (PVOID)stSize, &stOffset, 0, 0, 0); if (stOffset > 0) { fwrite(pcBuffer, stOffset, 1, stdout); fflush(stdout); /* 这里必须确保输出完成 */ } __SHEAP_FREE(pcBuffer); return (ERROR_NONE); } else { /* 操作路由表 */ if (lib_strcmp(ppcArgV[1], "add") == 0) { pfuncAddOrChange = __rtAdd; pcOpAddorChange = "add"; } else if (lib_strcmp(ppcArgV[1], "change") == 0) { pfuncAddOrChange = __rtChange; pcOpAddorChange = "change"; } else { pfuncAddOrChange = LW_NULL; } if (pfuncAddOrChange && (iArgC == 6)) { /* 添加或者修改路由表 */ if (!ipaddr_aton(ppcArgV[3], &ipaddr)) { fprintf(stderr, "inet address format error.\n"); goto __error_handle; } if (lib_strcmp(ppcArgV[4], "if") == 0) { /* 使用 ifindex 查询网卡 */ INT iIndex = lib_atoi(ppcArgV[5]); if (if_indextoname(iIndex, cNetifName) == LW_NULL) { fprintf(stderr, "can not find net interface with ifindex %d.\n", iIndex); goto __error_handle; } } else if (lib_strcmp(ppcArgV[4], "dev") == 0) { lib_strlcpy(cNetifName, ppcArgV[5], IF_NAMESIZE); } else { fprintf(stderr, "net interface argument error.\n"); goto __error_handle; } if (lib_strcmp(ppcArgV[2], "-host") == 0) { /* 主机路由 */ uiFlag |= LW_RT_FLAG_H; } else if (lib_strcmp(ppcArgV[2], "-gw") == 0) { /* 设置网卡网关 */ uiFlag |= LW_RT_GW_FLAG_SET; pfuncAddOrChange = __rtSetGw; } else if (lib_strcmp(ppcArgV[2], "-net") != 0) { /* 非网络路由 */ fprintf(stderr, "route add must determine -host or -net.\n"); goto __error_handle; } iError = pfuncAddOrChange(&ipaddr, uiFlag, cNetifName); /* 操作路由表 */ if (iError >= 0) { printf("route %s %s successful.\n", ppcArgV[3], pcOpAddorChange); return (ERROR_NONE); } } else if (pfuncAddOrChange && (iArgC == 5)) { /* 设置默认网关 */ if (lib_strcmp(ppcArgV[2], "default") != 0) { goto __error_handle; } if (lib_strcmp(ppcArgV[3], "if") == 0) { /* 使用 ifindex 查询网卡 */ INT iIndex = lib_atoi(ppcArgV[4]); if (if_indextoname(iIndex, cNetifName) == LW_NULL) { fprintf(stderr, "can not find net interface with ifindex %d.\n", iIndex); goto __error_handle; } } else if (lib_strcmp(ppcArgV[3], "dev") == 0) { lib_strlcpy(cNetifName, ppcArgV[4], IF_NAMESIZE); } else { fprintf(stderr, "net interface argument error.\n"); goto __error_handle; } uiFlag |= LW_RT_GW_FLAG_DEFAULT; pfuncAddOrChange = __rtSetGw; iError = pfuncAddOrChange(&ipaddr, uiFlag, cNetifName); /* 设置默认路由出口 */ if (iError >= 0) { printf("default device set successful.\n"); return (ERROR_NONE); } } else if ((lib_strcmp(ppcArgV[1], "del") == 0) && iArgC == 3) {/* 删除一个路由表项 */ if (!ipaddr_aton(ppcArgV[2], &ipaddr)) { fprintf(stderr, "inet address format error.\n"); goto __error_handle; } iError = __rtDel(&ipaddr); if (iError >= 0) { printf("route %s delete successful.\n", ppcArgV[2]); return (ERROR_NONE); } } } __error_handle: fprintf(stderr, "argments error!\n"); return (-ERROR_TSHELL_EPARAM); }