static int __inetBiosNameSet (int iArgC, char *pcArgV[]) { ULONG ulError; if (iArgC == 1) { printf("local host NetBIOS name is : %s\n", _G_cNetBiosLocalName); return (ERROR_NONE); } else if (iArgC == 2) { ulError = API_INetNetBiosNameSet(pcArgV[1]); if (ulError) { fprintf(stderr, "fail. error : %s\n", lib_strerror(errno)); return (PX_ERROR); } else { return (ERROR_NONE); } } else { fprintf(stderr, "argument error.\n"); return (PX_ERROR); } }
/********************************************************************************************************* ** 函数名称: API_INetPing6 ** 功能描述: internet ipv6 ping ** 输 入 : pinaddr 目标 ip 地址. ** iTimes 次数 ** iDataSize 数据大小 ** iTimeout 超时时间 ** pcNetif 网络接口名 (NULL 表示自动确定接口) ** 输 出 : ERROR ** 全局变量: ** 调用模块: API 函数 *********************************************************************************************************/ LW_API INT API_INetPing6 (struct in6_addr *pin6addr, INT iTimes, INT iDataSize, INT iTimeout, CPCHAR pcNetif) { CHAR cInetAddr[INET6_ADDRSTRLEN]; /* IP 地址缓存 */ REGISTER INT iSock; INT i; UINT16 usSeqRecv = 0; ULONG ulTime1; ULONG ulTime2; INT iSuc = 0; INT iHLRecv; struct sockaddr_in6 sockaddrin6To; REGISTER ULONG ulTimeMax = 0; REGISTER ULONG ulTimeMin = 0xFFFFFFFF; REGISTER ULONG ulTimeAvr = 0; if ((iDataSize >= (64 * LW_CFG_KB_SIZE)) || (iDataSize < 0)) { /* 0 - 64KB */ _ErrorHandle(EINVAL); return (PX_ERROR); } sockaddrin6To.sin6_len = sizeof(struct sockaddr_in6); sockaddrin6To.sin6_family = AF_INET6; sockaddrin6To.sin6_addr = *pin6addr; iSock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); /* 必须原子运行到 push */ if (iSock < 0) { return (PX_ERROR); } API_ThreadCleanupPush(__inetPing6Cleanup, (PVOID)iSock); /* 加入清除函数 */ setsockopt(iSock, SOL_SOCKET, SO_RCVTIMEO, &iTimeout, sizeof(INT)); connect(iSock, (struct sockaddr *)&sockaddrin6To, sizeof(struct sockaddr_in6)); /* 设定目标 */ printf("Pinging %s\n\n", inet6_ntoa_r(*pin6addr, cInetAddr, sizeof(cInetAddr))); for (i = 0; ;) { if (__inetPing6Send(iSock, pin6addr, iDataSize, pcNetif, &usSeqRecv) < 0) { /* 发送 icmp 数据包 */ fprintf(stderr, "error : %s.\n", lib_strerror(errno)); i++; if (i >= iTimes) { break; } API_TimeSSleep(1); /* 等待 1 S */ continue; } else { i++; /* 发送次数 ++ */ } ulTime1 = API_TimeGet(); if (__inetPing6Recv(iSock, usSeqRecv, &iHLRecv) < 0) { /* 接收 icmp 数据包 */ printf("Request time out.\n"); /* timeout */ if (i >= iTimes) { break; /* ping 结束 */ } } else { ulTime2 = API_TimeGet(); ulTime2 = (ulTime2 >= ulTime1) ? (ulTime2 - ulTime1) : ((__ARCH_ULONG_MAX - ulTime1) + ulTime2 + 1); ulTime2 = ((ulTime2 * 1000) / LW_TICK_HZ); /* 转为毫秒数 */ printf("Reply from %s: bytes=%d time=%ldms hoplim=%d\n", inet6_ntoa_r(*pin6addr, cInetAddr, sizeof(cInetAddr)), iDataSize, ulTime2, iHLRecv); iSuc++; ulTimeAvr += ulTime2; ulTimeMax = (ulTimeMax > ulTime2) ? ulTimeMax : ulTime2; ulTimeMin = (ulTimeMin < ulTime2) ? ulTimeMin : ulTime2; if (i >= iTimes) { break; /* ping 结束 */ } else { API_TimeSSleep(1); /* 等待 1 S */ } } } API_ThreadCleanupPop(LW_TRUE); /* ping 清除 */ /* * 打印总结信息 */ printf("\nPing statistics for %s:\n", inet6_ntoa_r(*pin6addr, cInetAddr, sizeof(cInetAddr))); printf(" Packets: Send = %d, Received = %d, Lost = %d(%d%% loss),\n", iTimes, iSuc, (iTimes - iSuc), (((iTimes - iSuc) * 100) / iTimes)); if (iSuc == 0) { /* 没有一次成功 */ return (PX_ERROR); } ulTimeAvr = ulTimeAvr / iSuc; printf("Approximate round trip times in milli-seconds:\n"); printf(" Minimum = %ldms, Maximum = %ldms, Average = %ldms\r\n\r\n", ulTimeMin, ulTimeMax, ulTimeAvr); return (ERROR_NONE); }
/********************************************************************************************************* ** 函数名称: __tshellFsCmdXmodemr ** 功能描述: 系统命令 "xmodemr" ** 输 入 : iArgC 参数个数 ** ppcArgV 参数表 ** 输 出 : 0 ** 全局变量: ** 调用模块: *********************************************************************************************************/ static INT __tshellFsCmdXmodemr (INT iArgC, PCHAR ppcArgV[]) { INT i; INT j; INT iFile; INT iRetVal; UCHAR ucTemp[__LW_XMODEM_PACKET_LEN]; UCHAR ucSeq = 1; UCHAR ucChkSum; ssize_t sstRecvNum; ssize_t sstWriteNum; size_t stTotalNum = 0; fd_set fdsetRead; struct timeval timevalTO = {__LW_XMODEM_TIMEOUT, 0}; if (iArgC != 2) { fprintf(stderr, "argments error!\n"); return (-ERROR_TSHELL_EPARAM); } /* * 检测文件是否存在 */ iFile = open(ppcArgV[1], O_RDONLY); if (iFile >= 0) { close(iFile); /* 关闭文件 */ __re_select: printf("destination file is exist, overwrite? (Y/N)\n"); read(0, ucTemp, __LW_XMODEM_DATA_LEN); if ((ucTemp[0] == 'N') || (ucTemp[0] == 'n')) { /* 不覆盖 */ return (ERROR_NONE); } else if ((ucTemp[0] != 'Y') && (ucTemp[0] != 'y')) { /* 选择错误 */ goto __re_select; } } iFile = open(ppcArgV[1], (O_WRONLY | O_CREAT | O_TRUNC), 0666); /* 创建文件 */ if (iFile < 0) { fprintf(stderr, "can not open destination file!\n"); return (-ERROR_TSHELL_EPARAM); } API_ThreadCleanupPush(__tshellXmodemCleanup, (PVOID)iFile); /* 加入清除函数 */ ioctl(STD_IN, FIOSETOPTIONS, OPT_RAW); /* 将标准文件改为 raw 模式 */ ioctl(STD_OUT, FIOSETOPTIONS, OPT_RAW); /* 将标准文件改为 raw 模式 */ __LW_XMODEM_SEND_CMD(__LW_XMODEM_NAK); /* 启动接收 */ FD_ZERO(&fdsetRead); /* * 超时次数默认为 20 次 */ for (i = 0; i < 20; i++) { FD_SET(STD_IN, &fdsetRead); iRetVal = select(1, &fdsetRead, LW_NULL, LW_NULL, &timevalTO); /* 等待可读 */ if (iRetVal != 1) { stTotalNum = 0; /* 清除已接收的数据 */ __LW_XMODEM_SEND_CMD(__LW_XMODEM_NAK); /* 重新请求接收数据包 */ continue; /* 接收超时 */ } i = 0; /* 重置超时次数 */ sstRecvNum = read(STD_IN, &ucTemp[stTotalNum], __LW_XMODEM_PACKET_LEN - stTotalNum); if (sstRecvNum <= 0) { API_ThreadCleanupPop(LW_TRUE); fprintf(stderr, "standard in device error!\n"); return (PX_ERROR); } if (ucTemp[0] == __LW_XMODEM_EOT) { /* 接收结束 */ __LW_XMODEM_SEND_CMD(__LW_XMODEM_ACK); break; } stTotalNum += (size_t)sstRecvNum; if (stTotalNum < __LW_XMODEM_PACKET_LEN) { /* 数据包不完整 */ continue; } else { stTotalNum = 0; /* 已经接到一个完整的数据包 */ } /* * 开始判断数据包正确性 */ if (ucTemp[1] != ucSeq) { /* 序列号是否错误 */ if (ucTemp[1] == (ucSeq - 1)) { __LW_XMODEM_SEND_CMD(__LW_XMODEM_ACK); /* 需要下一包数据 */ } else { __LW_XMODEM_SEND_CMD(__LW_XMODEM_CAN); /* 结束通信 */ API_ThreadCleanupPop(LW_TRUE); fprintf(stderr, "sequence number error!\n"); return (PX_ERROR); } continue; } if (~ucTemp[1] == ucTemp[2]) { /* 序列号校验错误 */ __LW_XMODEM_SEND_CMD(__LW_XMODEM_NAK); continue; } ucChkSum = 0; for (j = 3; j < (__LW_XMODEM_DATA_LEN + 3); j++) { ucChkSum = (UCHAR)(ucChkSum + ucTemp[j]); /* 计算校验和 */ } if (ucTemp[__LW_XMODEM_DATA_LEN + 3] != ucChkSum) { /* 校验错误 */ __LW_XMODEM_SEND_CMD(__LW_XMODEM_NAK); continue; } /* * 将数据写入目标文件 */ sstWriteNum = write(iFile, &ucTemp[3], __LW_XMODEM_DATA_LEN); if (sstWriteNum != __LW_XMODEM_DATA_LEN) { INT iErrNo = errno; __LW_XMODEM_SEND_CMD(__LW_XMODEM_CAN); /* 结束通信 */ API_ThreadCleanupPop(LW_TRUE); fprintf(stderr, "write file error %s!\n", lib_strerror(iErrNo)); return (PX_ERROR); } ucSeq++; __LW_XMODEM_SEND_CMD(__LW_XMODEM_ACK); /* 需要下一包数据 */ } API_ThreadCleanupPop(LW_TRUE); return (ERROR_NONE); }
/********************************************************************************************************* ** 函数名称: __untarFile ** 功能描述: unpackage a .tar file. ** 输 入 : pcTarFile tar file ** pcDestPath destination directory ** 输 出 : 0 or -1 ** 全局变量: ** 调用模块: *********************************************************************************************************/ static INT __untarFile (CPCHAR pcTarFile, CPCHAR pcDestPath) { INT iFdTar; char *pcBuf; ssize_t sstN; char cFname[100]; char cLinkname[100]; int iSum; int iHdrChksum; int iRetVal; unsigned long ulI; unsigned long ulNblocks; unsigned long ulSize; unsigned char ucLinkflag; ULONG ulTotalFile = 0ul; ULONG ulTotalDir = 0ul; iFdTar = open(pcTarFile, O_RDONLY); if (iFdTar < 0) { fprintf(stderr, "can not open : %s : %s\n", pcTarFile, lib_strerror(errno)); return (PX_ERROR); } pcBuf = (char *)__SHEAP_ALLOC(512); if (pcBuf == LW_NULL) { close(iFdTar); fprintf(stderr, "system low memory.\n"); return (PX_ERROR); } iRetVal = ERROR_NONE; while (1) { char cOutFile[PATH_MAX + 1]; mode_t mode; if ((sstN = read(iFdTar, pcBuf, 512)) != 512) { break; } if (lib_strncmp(&pcBuf[257], TMAGIC, 5)) { break; } lib_strlcpy(cFname, pcBuf, 100); ucLinkflag = pcBuf[156]; ulSize = __untarOctal2Long(&pcBuf[124], 12); iHdrChksum = (int)__untarOctal2Long(&pcBuf[148], 8); iSum = __untarHeaderChksum(pcBuf); if (iSum != iHdrChksum) { fprintf(stderr, "tar file : %s chksum error.\n", pcTarFile); iRetVal = PX_ERROR; break; } mode = (int)__untarOctal2Long(&pcBuf[100], 8); if (pcDestPath) { if (lib_strcmp(pcDestPath, PX_STR_ROOT) == 0) { if (cFname[0] == PX_ROOT) { lib_strlcpy(cOutFile, cFname, PATH_MAX + 1); } else { snprintf(cOutFile, PATH_MAX + 1, "%s%s", pcDestPath, cFname); } } else { snprintf(cOutFile, PATH_MAX + 1, "%s/%s", pcDestPath, cFname); } } else { lib_strlcpy(cOutFile, cFname, PATH_MAX + 1); } if (ucLinkflag == SYMTYPE) { printf("unpackage %s <LNK> ...\n", cOutFile); lib_strlcpy(cLinkname, &pcBuf[157], 100); symlink(cLinkname, cOutFile); ulTotalFile++; } else if (ucLinkflag == REGTYPE) { INT iFdOut; printf("unpackage %s size : %ld ...\n", cOutFile, ulSize); ulNblocks = (((ulSize) + 511) & ~511) / 512; if ((iFdOut = creat(cOutFile, mode)) < 0) { fprintf(stderr, "can not create : %s\n", cOutFile); lseek(iFdTar, (off_t)(ulNblocks * 512), SEEK_CUR); } else { for (ulI = 0; ulI < ulNblocks; ulI++) { sstN = read(iFdTar, pcBuf, 512); sstN = min(sstN, ulSize - (ulI * 512)); write(iFdOut, pcBuf, (size_t)sstN); } close(iFdOut); ulTotalFile++; } } else if (ucLinkflag == DIRTYPE) { printf("unpackage %s <DIR> ...\n", cOutFile); mkdir(cOutFile, mode); ulTotalDir++; } } __SHEAP_FREE(pcBuf); close(iFdTar); printf("unpackage total %lu files %lu directory.\n", ulTotalFile, ulTotalDir); return (iRetVal); }
/********************************************************************************************************* ** 函数名称: __tshellArp ** 功能描述: 系统命令 "arp" ** 输 入 : iArgC 参数个数 ** ppcArgV 参数表 ** 输 出 : 0 ** 全局变量: ** 调用模块: *********************************************************************************************************/ static INT __tshellArp (INT iArgC, PCHAR *ppcArgV) { if (iArgC < 2) { fprintf(stderr, "argments error!\n"); return (-ERROR_TSHELL_EPARAM); } if (lib_strcmp(ppcArgV[1], "-a") == 0) { /* 显示 arp 表 */ INT iFd; CHAR cBuffer[512]; ssize_t sstNum; iFd = open("/proc/net/arp", O_RDONLY); if (iFd < 0) { fprintf(stderr, "can not open /proc/net/arp : %s\n", lib_strerror(errno)); return (PX_ERROR); } do { sstNum = read(iFd, cBuffer, sizeof(cBuffer)); if (sstNum > 0) { write(STDOUT_FILENO, cBuffer, (size_t)sstNum); } } while (sstNum > 0); close(iFd); return (ERROR_NONE); } else if (lib_strcmp(ppcArgV[1], "-s") == 0) { /* 加入一个静态转换关系 */ INT i; INT iTemp[6]; ip_addr_t ipaddr; struct eth_addr ethaddr; err_t err; if (iArgC != 4) { fprintf(stderr, "argments error!\n"); return (-ERROR_TSHELL_EPARAM); } ipaddr.addr = ipaddr_addr(ppcArgV[2]); if (ipaddr.addr == IPADDR_NONE) { fprintf(stderr, "bad inet address : %s\n", ppcArgV[2]); return (-ERROR_TSHELL_EPARAM); } if (sscanf(ppcArgV[3], "%02x:%02x:%02x:%02x:%02x:%02x", &iTemp[0], &iTemp[1], &iTemp[2], &iTemp[3], &iTemp[4], &iTemp[5]) != 6) { fprintf(stderr, "bad physical address : %s\n", ppcArgV[3]); return (-ERROR_TSHELL_EPARAM); } for (i = 0; i < 6; i++) { ethaddr.addr[i] = (u8_t)iTemp[i]; } LOCK_TCPIP_CORE(); err = etharp_add_static_entry(&ipaddr, ðaddr); UNLOCK_TCPIP_CORE(); return (err ? PX_ERROR : ERROR_NONE); } else if (lib_strcmp(ppcArgV[1], "-d") == 0) { /* 删除一个静态转换关系 */ ip_addr_t ipaddr; err_t err; if (iArgC != 3) { /* 删除全部转换关系 */ struct netif *netif; LWIP_NETIF_LOCK(); for (netif = netif_list; netif != LW_NULL; netif = netif->next) { if (netif->flags & NETIF_FLAG_ETHARP) { netifapi_netif_common(netif, etharp_cleanup_netif, LW_NULL); } } LWIP_NETIF_UNLOCK(); return (ERROR_NONE); } ipaddr.addr = ipaddr_addr(ppcArgV[2]); if (ipaddr.addr == IPADDR_NONE) { fprintf(stderr, "bad inet address : %s\n", ppcArgV[2]); return (-ERROR_TSHELL_EPARAM); } LOCK_TCPIP_CORE(); err = etharp_remove_static_entry(&ipaddr); UNLOCK_TCPIP_CORE(); return (err ? PX_ERROR : ERROR_NONE); } else { fprintf(stderr, "argments error!\n"); return (-ERROR_TSHELL_EPARAM); } }