更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
api_shell.h 文件参考

浏览源代码.

函数

u32_t lwip_ifconfig (int argc, const char **argv)
 
u32_t lwip_arp (int argc, const char **argv)
 
u32_t osShellNetIfUp (int argc, const char **argv)
 
u32_t osShellNetIfDown (int argc, const char **argv)
 
u32_t osShellPing (int argc, const char **argv)
 
u32_t osShellTftp (int argc, const char **argv)
 
u32_t osShellNtpdate (int argc, const char **argv)
 
u32_t osShellDns (int argc, const char **argv)
 
u32_t osShellNetstat (int argc, const char **argv)
 
void netstat_internal (void *ctx)
 

函数说明

◆ lwip_arp()

u32_t lwip_arp ( int  argc,
const char **  argv 
)

在文件 api_shell.c1347 行定义.

1348{
1349 int i;
1350 struct arp_option arp_cmd;
1351 err_t ret;
1352 size_t interface_len = 0;
1353
1354 (void)memset_s(&arp_cmd, sizeof(struct arp_option), 0, sizeof(struct arp_option));
1355 if (!tcpip_init_finish) {
1356 PRINTK("%s: tcpip_init have not been called\n", __FUNCTION__);
1357 return LOS_NOK;
1358 }
1359
1360 arp_cmd.iface[0] = 'd';
1361 arp_cmd.iface[1] = 'e';
1362 arp_cmd.iface[2] = '0';
1363 arp_cmd.option = ARP_OPTION_SHOW;
1364 arp_cmd.print_buf_len = 0;
1365 if (sys_sem_new(&arp_cmd.cb_completed, 0) != ERR_OK) {
1366 PRINTK("%s: sys_sem_new fail\n", __FUNCTION__);
1367 return 1;
1368 }
1369
1370 i = 0;
1371 while (argc > 0) {
1372 if (strcmp("-i", argv[i]) == 0 && (argc > 1)) {
1373 /* get the network interface's name */
1374 interface_len = strlen(argv[i + 1]);
1375 if (interface_len < IFNAMSIZ) {
1376 if (strncmp(argv[i + 1], "lo", (sizeof("lo") - 1)) == 0) {
1377 PRINTK("Illegal operation\n");
1378 goto arp_error;
1379 }
1380 if (strncpy_s(arp_cmd.iface, IFNAMSIZ, argv[i + 1], interface_len) != EOK) {
1381 PRINTK("strncpy_s error\n");
1382 goto arp_error;
1383 }
1384 arp_cmd.iface[interface_len] = '\0';
1385 } else {
1386 PRINTK("Iface name is big \n");
1387 goto arp_error;
1388 }
1389 i += 2;
1390 argc -= 2;
1391 } else if (strcmp("-d", argv[i]) == 0 && (argc > 1)) {
1392 /* arp delete */
1393 arp_cmd.option = ARP_OPTION_DEL;
1394 arp_cmd.ipaddr = inet_addr(argv[i + 1]);
1395
1396 if (arp_cmd.ipaddr == IPADDR_NONE) {
1397 PRINTK("IP address is not correct!\n");
1398 goto arp_error;
1399 }
1400
1401 i += 2;
1402 argc -= 2;
1403 } else if (strcmp("-s", argv[i]) == 0 && (argc > 2)) {
1404 /* arp add */
1405 char *digit = NULL;
1406 u32_t macaddrlen = strlen(argv[i + 2]) + 1;
1407 char tmpStr[MAX_MACADDR_STRING_LENGTH];
1408 char *tmpStr1 = NULL;
1409 char *saveptr1 = NULL;
1410 char *temp = NULL;
1411 int j;
1412
1413 arp_cmd.option = ARP_OPTION_ADD;
1414 arp_cmd.ipaddr = inet_addr(argv[i + 1]);
1415
1416 if (arp_cmd.ipaddr == IPADDR_NONE) {
1417 PRINTK("IP address is not correct!\n");
1418 goto arp_error;
1419 }
1420
1421 /*cannot add an arp entry of 127.*.*.* */
1422 if ((arp_cmd.ipaddr & (u32_t)0x0000007fUL) == (u32_t)0x0000007fUL) {
1423 PRINTK("IP address is not correct!\n");
1424 goto arp_error;
1425 }
1426
1427 if (macaddrlen != MAX_MACADDR_STRING_LENGTH) {
1428 PRINTK("Wrong MAC address length\n");
1429 goto arp_error;
1430 }
1431
1432 if (strncpy_s(tmpStr, MAX_MACADDR_STRING_LENGTH, argv[i + 2], macaddrlen - 1) != 0) {
1433 PRINTK("Wrong MAC address\n");
1434 goto arp_error;
1435 }
1436
1437 for (j = 0, tmpStr1 = tmpStr; j < 6; j++, tmpStr1 = NULL) {
1438 digit = strtok_r(tmpStr1, ":", &saveptr1);
1439 if ((digit == NULL) || (strlen(digit) > 2)) {
1440 PRINTK("MAC address is not correct\n");
1441 goto arp_error;
1442 }
1443
1444 for (temp = digit; *temp != '\0'; temp++) {
1445 if (!isxdigit(*temp)) {
1446 PRINTK("MAC address is not correct\n");
1447 goto arp_error;
1448 }
1449 }
1450
1451 CONVERT_STRING_TO_HEX(digit, arp_cmd.ethaddr[j]);
1452 }
1453
1454 i += 3;
1455 argc -= 3;
1456 } else {
1457 goto arp_error;
1458 }
1459 }
1460
1461 ret = tcpip_callback(lwip_arp_internal, &arp_cmd);
1462 if (ret != ERR_OK) {
1463 PRINTK("%s : tcpip_callback failed in line %d : errnu %d", __FUNCTION__, __LINE__, ret);
1464 sys_sem_free(&arp_cmd.cb_completed);
1465 return 1;
1466 }
1467 (void)sys_arch_sem_wait(&arp_cmd.cb_completed, 0);
1468 sys_sem_free(&arp_cmd.cb_completed);
1469 arp_cmd.cb_print_buf[PRINT_BUF_LEN - 1] = '\0';
1470 PRINTK("%s", arp_cmd.cb_print_buf);
1471 return 0;
1472
1473arp_error:
1474 lwip_arp_usage("arp");
1475 sys_sem_free(&arp_cmd.cb_completed);
1476 return 1;
1477}
LWIP_STATIC void lwip_arp_internal(void *arg)
Definition: api_shell.c:1249
volatile int tcpip_init_finish
Definition: fixme.c:403
LWIP_STATIC void lwip_arp_usage(const char *cmd)
Definition: api_shell.c:1338
in_addr_t inet_addr(const char *cp)
Definition: socket.c:334
void sys_sem_free(sys_sem_t *sem)
Definition: sys_arch.c:288
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeoutMs)
Definition: sys_arch.c:272
err_t sys_sem_new(sys_sem_t *sem, u8_t count)
Definition: sys_arch.c:257
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
函数调用图:

◆ lwip_ifconfig()

u32_t lwip_ifconfig ( int  argc,
const char **  argv 
)

在文件 api_shell.c684 行定义.

685{
686 int i;
687 static struct ifconfig_option ifconfig_cmd;
688 err_t ret;
689
690#if LWIP_STATS
691 u32_t stat_err_cnt;
692 u32_t stat_drop_cnt;
693 u32_t stat_rx_or_tx_cnt;
694 u32_t stat_rx_or_tx_bytes;
695#endif
696
697#if LWIP_ARP
698 u32_t retval;
699 struct netif *netiftmp = NULL;
700#if LWIP_ENABLE_IP_CONFLICT_SIGNAL
701 u32_t old_ip4addr;
702 err_t err;
704 extern u32_t is_ip_conflict_signal;
705#endif /* LWIP_ENABLE_IP_CONFLICT_SIGNAL */
706#endif /* LWIP_ARP */
707#if LWIP_IPV6
709 extern u32_t is_dup_detect_initialized;
710#endif
711 if (!tcpip_init_finish) {
712 PRINTK("%s: tcpip_init have not been called\n", __FUNCTION__);
713 return 2;
714 }
715 /* To support "ifconfig -a" command
716 RX packets:XXXX errors:X dropped:X overruns:X bytes:XXXX (Human readable format)
717 TX packets:XXXX errors:X dropped:X overruns:X bytes:XXXX (Human readable format)
718
719 Below is assumed for 'overrun' stat.
720 Linux Kernel:
721 RX: FIFO overrun
722 Data structure: net_device->stats->rx_fifo_errors
723 Flag which is marked when FIFO overrun: ENRSR_FO
724
725 Function: ei_receive->ENRSR_FO
726
727 TX: A "FIFO underrun" occurred during transmit.
728 Data structure: net_device->stats->tx_fifo_errors
729 Flag which is marked when FIFO underrun: ENTSR_FU
730
731 Function: ei_tx_intr->ENTSR_FU
732
733 LWIP:
734 So in our case,
735 while receiving a packet RX case, if the buffer is full (trypost - it is sys_mbox_trypost)
736 the error will be returned, we can consider that an overflow has happened.
737 So this can be RX overrun.
738
739 But while transmitting a packet TX case, underrun cannot happen because it block on the
740 message Q if it is full (NOT trypost - it is sys_mbox_post). So TX overrun is always 0.
741 */
742 if (argc) {
743 if (strcmp("-a", argv[0]) == 0) {
744#if LWIP_STATS
745 stat_rx_or_tx_cnt = lwip_stats.ip.recv;
746 stat_err_cnt = (u32_t)(lwip_stats.ip.ip_rx_err +
747 lwip_stats.ip.lenerr +
748 lwip_stats.ip.chkerr +
749 lwip_stats.ip.opterr +
750 lwip_stats.ip.proterr);
751 stat_drop_cnt = (u32_t)(lwip_stats.ip.drop + lwip_stats.link.link_rx_drop);
752 stat_rx_or_tx_bytes = lwip_stats.ip.ip_rx_bytes;
753
754 PRINTK("%18s:%u\t errors:%u\t ip dropped:%u\t link dropped:%u\t overrun:%d\t bytes:%u ",
755 "RX packets",
756 stat_rx_or_tx_cnt,
757 stat_err_cnt,
758 stat_drop_cnt,
759 lwip_stats.link.link_rx_drop,
760 lwip_stats.ip.link_rx_overrun,
761 stat_rx_or_tx_bytes);
762
763 /* Print in Human readable format of the incoming bytes */
764 lwip_printsize(lwip_stats.ip.ip_rx_bytes);
765#if IP6_STATS
766 stat_rx_or_tx_cnt = lwip_stats.ip6.recv;
767 stat_err_cnt = (u32_t)(lwip_stats.ip6.ip_rx_err +
768 lwip_stats.ip6.lenerr +
769 lwip_stats.ip6.chkerr +
770 lwip_stats.ip6.opterr +
771 lwip_stats.ip6.proterr);
772 stat_drop_cnt = lwip_stats.ip6.drop;
773 stat_rx_or_tx_bytes = lwip_stats.ip6.ip_rx_bytes;
774
775 PRINTK("%18s:%u\t errors:%u\t dropped:%u\t overrun:%d\t bytes:%u ",
776 "RX packets(ip6)",
777 stat_rx_or_tx_cnt,
778 stat_err_cnt,
779 stat_drop_cnt,
780 lwip_stats.ip.link_rx_overrun,
781 stat_rx_or_tx_bytes);
782
783 /* Print in Human readable format of the incoming bytes */
784 lwip_printsize(lwip_stats.ip6.ip_rx_bytes);
785#endif
786 stat_rx_or_tx_cnt = (u32_t)(lwip_stats.ip.fw + lwip_stats.ip.xmit);
787 stat_err_cnt = (u32_t)(lwip_stats.ip.rterr + lwip_stats.ip.ip_tx_err);
788 /* IP layer drop stat param is not maintained, failure at IP is considered in 'errors' stat */
789 stat_drop_cnt = lwip_stats.link.link_tx_drop;
790 stat_rx_or_tx_bytes = lwip_stats.ip.ip_tx_bytes;
791
792 PRINTK("%18s:%u\t errors:%u\t link dropped:%u\t overrun:0\t bytes:%u",
793 "TX packets",
794 stat_rx_or_tx_cnt,
795 stat_err_cnt,
796 stat_drop_cnt,
797 stat_rx_or_tx_bytes);
798
799 /* Print in Human readable format of the outgoing bytes */
800 lwip_printsize(lwip_stats.ip.ip_tx_bytes);
801
802 stat_rx_or_tx_cnt = (u32_t)(lwip_stats.ip6.fw + lwip_stats.ip6.xmit);
803 stat_err_cnt = (u32_t)(lwip_stats.ip6.rterr + lwip_stats.ip6.ip_tx_err);
804 stat_rx_or_tx_bytes = lwip_stats.ip6.ip_tx_bytes;
805
806 PRINTK("%18s:%u\t errors:%u\t overrun:0\t bytes:%u",
807 "TX packets(ip6)",
808 stat_rx_or_tx_cnt,
809 stat_err_cnt,
810 stat_rx_or_tx_bytes);
811
812 /* Print in Human readable format of the outgoing bytes */
813 lwip_printsize(lwip_stats.ip6.ip_tx_bytes);
814#endif /* LWIP_STATS */
815 return 0;
816 }
817 }
818
819 (void)memset_s(&ifconfig_cmd, sizeof(ifconfig_cmd), 0, sizeof(ifconfig_cmd));
820 if (sys_sem_new(&ifconfig_cmd.cb_completed, 0) != ERR_OK) {
821 PRINTK("%s: sys_sem_new fail\n", __FUNCTION__);
822 return 1;
823 }
824
825 i = 0;
826 /* Get the interface */
827 if (argc > 0) {
828 if (strlen(argv[i]) < IFNAMSIZ) {
829 if (strncpy_s(ifconfig_cmd.iface, IFNAMSIZ, argv[i], (strlen(argv[i]))) != EOK) {
830 sys_sem_free(&ifconfig_cmd.cb_completed);
831 PRINTK("ifconfig : strncpy_s error\n");
832 return 1;
833 }
834 ifconfig_cmd.iface[IFNAMSIZ - 1] = '\0';
835 } else {
836 sys_sem_free(&ifconfig_cmd.cb_completed);
837 PRINTK("ifconfig : interface name is too big\n");
838 return 1;
839 }
840 i++;
841 argc--;
842 if (argc == 0) {
843 /* no more arguments, show the interface state. */
844 ret = tcpip_callback(lwip_ifconfig_show_internal, &ifconfig_cmd);
845 if (ret != ERR_OK) {
846 sys_sem_free(&ifconfig_cmd.cb_completed);
847 PRINTK("ifconfig : internal error, l:%d err:%d\n", __LINE__, ret);
848 return 1;
849 }
850 (void)sys_arch_sem_wait(&ifconfig_cmd.cb_completed, 0);
851 sys_sem_free(&ifconfig_cmd.cb_completed);
852 ifconfig_cmd.cb_print_buf[PRINT_BUF_LEN - 1] = '\0';
853 PRINTK("%s", ifconfig_cmd.cb_print_buf);
854 return 0;
855 }
856 } else {
857 /* no more arguments, show all the interface state. */
858 ret = tcpip_callback(lwip_ifconfig_show_internal, &ifconfig_cmd);
859 if (ret != ERR_OK) {
860 sys_sem_free(&ifconfig_cmd.cb_completed);
861 PRINTK("ifconfig : internal error, l:%d err:%d\n", __LINE__, ret);
862 return 1;
863 }
864 (void)sys_arch_sem_wait(&ifconfig_cmd.cb_completed, 0);
865 sys_sem_free(&ifconfig_cmd.cb_completed);
866 ifconfig_cmd.cb_print_buf[PRINT_BUF_LEN - 1] = '\0';
867 PRINTK("%s", ifconfig_cmd.cb_print_buf);
868
869 return 0;
870 }
871
872 /* ifup/ifdown */
873 if (strcmp("up", argv[i]) == 0) {
874 ifconfig_cmd.option |= IFCONFIG_OPTION_SET_UP;
875 /* setup the interface, other arguments is ignored. */
876 ret = tcpip_callback(lwip_ifconfig_internal, &ifconfig_cmd);
877 if (ret != ERR_OK) {
878 sys_sem_free(&ifconfig_cmd.cb_completed);
879 PRINTK("ifconfig : internal error, l:%d err:%d\n", __LINE__, ret);
880 return 1;
881 }
882 (void)sys_arch_sem_wait(&ifconfig_cmd.cb_completed, 0);
883 sys_sem_free(&ifconfig_cmd.cb_completed);
884 ifconfig_cmd.cb_print_buf[PRINT_BUF_LEN - 1] = '\0';
885 PRINTK("%s", ifconfig_cmd.cb_print_buf);
886 return 0;
887 } else if (strcmp("down", argv[i]) == 0) {
888 ifconfig_cmd.option |= IFCONFIG_OPTION_SET_DOWN;
889 /* setdown the interface, other arguments is ignored. */
890 ret = tcpip_callback(lwip_ifconfig_internal, &ifconfig_cmd);
891 if (ret != ERR_OK) {
892 sys_sem_free(&ifconfig_cmd.cb_completed);
893 PRINTK("ifconfig : internal error, l:%d err:%d\n", __LINE__, ret);
894 return 1;
895 }
896 (void)sys_arch_sem_wait(&ifconfig_cmd.cb_completed, 0);
897 sys_sem_free(&ifconfig_cmd.cb_completed);
898 ifconfig_cmd.cb_print_buf[PRINT_BUF_LEN - 1] = '\0';
899 PRINTK("%s", ifconfig_cmd.cb_print_buf);
900 return 0;
901 }
902 /* check if set the ip address. */
903#if LWIP_ARP
904 netiftmp = netifapi_netif_find_by_name(ifconfig_cmd.iface);
905 if (netiftmp == NULL) {
906 sys_sem_free(&ifconfig_cmd.cb_completed);
907 PRINTK("ifconfig : Interface %s not found\n", ifconfig_cmd.iface);
908 return 1;
909 }
910#if LWIP_ENABLE_IP_CONFLICT_SIGNAL
911 old_ip4addr = ipaddr_addr(ipaddr_ntoa(&netiftmp->ip_addr));
912#endif /* LWIP_ENABLE_IP_CONFLICT_SIGNAL */
913#endif /* LWIP_ARP */
914 if (!strcmp(argv[i], "inet") || ip4addr_aton(argv[i], ip_2_ip4(&ifconfig_cmd.ip_addr))) {
915 if (!strcmp(argv[i], "inet")) {
916 if (argc <= 1) {
917 sys_sem_free(&ifconfig_cmd.cb_completed);
918 goto ifconfig_error;
919 }
920
921 if (!ip4addr_aton(argv[i + 1], ip_2_ip4(&ifconfig_cmd.ip_addr))) {
922 sys_sem_free(&ifconfig_cmd.cb_completed);
923 PRINTK("ifconfig : Invalid IPv4 Address\n");
924 return 1;
925 }
926 argc--;
927 i++;
928 }
929 IP_SET_TYPE_VAL((ifconfig_cmd.ip_addr), IPADDR_TYPE_V4);
930#if LWIP_ARP
931 if (!ip_addr_cmp(&ifconfig_cmd.ip_addr, &netiftmp->ip_addr)) {
932 ifconfig_cmd.option |= IFCONFIG_OPTION_SET_IP;
933 }
934#else
935 ifconfig_cmd.option |= IFCONFIG_OPTION_SET_IP;
936#endif /* LWIP_ARP */
937 argc--;
938 i++;
939 } else if (!strcmp(argv[i], "inet6")) {
940 if (argc < 3) {
941 sys_sem_free(&ifconfig_cmd.cb_completed);
942 goto ifconfig_error;
943 }
944 if (strcmp(argv[i + 1], "add") && strcmp(argv[i + 1], "del")) {
945 sys_sem_free(&ifconfig_cmd.cb_completed);
946 goto ifconfig_error;
947 }
948
949 if (!ip6addr_aton(argv[i + 2], ip_2_ip6(&ifconfig_cmd.ip_addr))) {
950 sys_sem_free(&ifconfig_cmd.cb_completed);
951 PRINTK("ifconfig : Invalid IPv6 Address\n");
952 return 1;
953 }
954
955 IP_SET_TYPE_VAL((ifconfig_cmd.ip_addr), IPADDR_TYPE_V6);
956 ifconfig_cmd.option |= (!strcmp(argv[i + 1], "add") ? IFCONFIG_OPTION_SET_IP : IFCONFIG_OPTION_DEL_IP);
957 argc -= 3;
958 i += 3;
959 }
960
961 if (ifconfig_cmd.option & IFCONFIG_OPTION_DEL_IP) {
962 if (argc != 0) {
963 sys_sem_free(&ifconfig_cmd.cb_completed);
964 goto ifconfig_error;
965 }
966 }
967
968 while (argc > 0) {
969 if (strcmp("netmask", argv[i]) == 0 && (argc > 1) && (ipaddr_addr(argv[i + 1]) != IPADDR_NONE)) {
970 /* if set netmask */
971 ip_addr_set_ip4_u32_val((ifconfig_cmd.netmask), ipaddr_addr(argv[i + 1]));
972 ifconfig_cmd.option |= IFCONFIG_OPTION_SET_NETMASK;
973 i += 2;
974 argc -= 2;
975 } else if (strcmp("gateway", argv[i]) == 0 && (argc > 1) && (ipaddr_addr(argv[i + 1]) != IPADDR_NONE)) {
976 /* if set gateway */
977 ip_addr_set_ip4_u32_val((ifconfig_cmd.gw), ipaddr_addr(argv[i + 1]));
978 ifconfig_cmd.option |= IFCONFIG_OPTION_SET_GW;
979 i += 2;
980 argc -= 2;
981 } else if (strcmp("hw", argv[i]) == 0 && argc > 2 && strcmp("ether", argv[i + 1]) == 0) {
982 /* if set HWaddr */
983 char *digit = NULL;
984 u32_t macaddrlen = strlen(argv[i + 2]) + 1;
985 char tmpStr[MAX_MACADDR_STRING_LENGTH];
986 char *tmpStr1 = NULL;
987 char *saveptr = NULL;
988 int j;
989
990 if (macaddrlen != MAX_MACADDR_STRING_LENGTH) {
991 sys_sem_free(&ifconfig_cmd.cb_completed);
992 PRINTK("ifconfig : wrong MAC address format\n");
993 return 1;
994 }
995
996 if (strncpy_s(tmpStr, MAX_MACADDR_STRING_LENGTH, argv[i + 2], macaddrlen - 1) != 0) {
997 sys_sem_free(&ifconfig_cmd.cb_completed);
998 PRINTK("ifconfig : wrong MAC address\n");
999 return 1;
1000 }
1001 for (j = 0, tmpStr1 = tmpStr; j < 6; j++, tmpStr1 = NULL) {
1002 digit = strtok_r(tmpStr1, ":", &saveptr);
1003 if ((digit == NULL) || (strlen(digit) > 2)) {
1004 sys_sem_free(&ifconfig_cmd.cb_completed);
1005 PRINTK("ifconfig : wrong MAC address format\n");
1006 return 1;
1007 }
1008 CONVERT_STRING_TO_HEX(digit, ifconfig_cmd.ethaddr[j]);
1009 }
1010 ifconfig_cmd.option |= IFCONFIG_OPTION_SET_HW;
1011 i += 3;
1012 argc -= 3;
1013 } else if (!strcmp("mtu", argv[i]) && (argc > 1)) {
1014 /* if set mtu */
1015 if ((atoi(argv[i + 1]) < 0) || (atoi(argv[i + 1]) > 0xFFFF)) {
1016 sys_sem_free(&ifconfig_cmd.cb_completed);
1017 PRINTK("\nifconfig: Invalid argument for mtu\n");
1018 goto ifconfig_error;
1019 }
1020
1021 ifconfig_cmd.mtu = (u16_t)(atoi(argv[i + 1]));
1022 ifconfig_cmd.option |= IFCONFIG_OPTION_SET_MTU;
1023 i += 2;
1024 argc -= 2;
1025 } else {
1026 sys_sem_free(&ifconfig_cmd.cb_completed);
1027 goto ifconfig_error;
1028 }
1029 }
1030
1031#if LWIP_ARP && LWIP_ENABLE_IP_CONFLICT_SIGNAL
1032 if ((ifconfig_cmd.option & IFCONFIG_OPTION_SET_IP) && IP_IS_V4_VAL((ifconfig_cmd.ip_addr))) {
1033 /* Create the semaphore for ip conflict detection. */
1034 if (sys_sem_new(&ip_conflict_detect, 0) != ERR_OK) {
1035 sys_sem_free(&ifconfig_cmd.cb_completed);
1036 PRINTK("ifconfig: internal error\n");
1037 return 1;
1038 }
1040 }
1041#endif /* LWIP_ARP && LWIP_ENABLE_IP_CONFLICT_SIGNAL */
1042
1043#if LWIP_IPV6
1044 if ((ifconfig_cmd.option & IFCONFIG_OPTION_SET_IP) && IP_IS_V6_VAL((ifconfig_cmd.ip_addr))) {
1045 /* Create the semaphore for duplicate address detection. */
1046 if (sys_sem_new(&dup_addr_detect, 0) != ERR_OK) {
1047 sys_sem_free(&ifconfig_cmd.cb_completed);
1048 PRINTK("ifconfig: internal error\n");
1049 return 1;
1050 }
1052 }
1053#endif /* LWIP_IPV6 */
1054
1055 ret = tcpip_callback(lwip_ifconfig_internal, &ifconfig_cmd);
1056 if (ret != ERR_OK) {
1057 sys_sem_free(&ifconfig_cmd.cb_completed);
1058#if LWIP_ARP && LWIP_ENABLE_IP_CONFLICT_SIGNAL
1059 if ((ifconfig_cmd.option & IFCONFIG_OPTION_SET_IP) && IP_IS_V4_VAL((ifconfig_cmd.ip_addr))) {
1062 }
1063#endif /* LWIP_ARP && LWIP_ENABLE_IP_CONFLICT_SIGNAL */
1064
1065#if LWIP_IPV6
1066 if ((ifconfig_cmd.option & IFCONFIG_OPTION_SET_IP) && IP_IS_V6_VAL((ifconfig_cmd.ip_addr))) {
1069 }
1070#endif /* LWIP_IPV6 */
1071
1072 PRINTK("%s : tcpip_callback failed in line %d : errnu %d", __FUNCTION__, __LINE__, ret);
1073 return 1;
1074 }
1075 (void)sys_arch_sem_wait(&ifconfig_cmd.cb_completed, 0);
1076 ifconfig_cmd.cb_print_buf[PRINT_BUF_LEN - 1] = '\0';
1077 PRINTK("%s", ifconfig_cmd.cb_print_buf);
1078#if LWIP_ARP && LWIP_ENABLE_IP_CONFLICT_SIGNAL
1079 /* Pend 2 seconds for waiting the arp reply if the ip is already in use.*/
1080 if ((ifconfig_cmd.option & IFCONFIG_OPTION_SET_IP) && IP_IS_V4_VAL((ifconfig_cmd.ip_addr))) {
1081 err = (err_t)sys_arch_sem_wait(&ip_conflict_detect, DUP_ARP_DETECT_TIME);
1084 if (err < 0) {
1085 /* The result neither conflict nor timeout. */
1086 PRINT_ERR("ifconfig: internal error\n");
1087 sys_sem_free(&ifconfig_cmd.cb_completed);
1088 return 1;
1089 } else if (err < DUP_ARP_DETECT_TIME) {
1090 /* Duplicate use of new ip, restore it to the old one. */
1091 PRINT_ERR("ifconfig: ip conflict!\n");
1092 ip_addr_set_ip4_u32_val(ifconfig_cmd.ip_addr, old_ip4addr);
1093 ret = tcpip_callback(lwip_ifconfig_internal, &ifconfig_cmd);
1094 if (ret != ERR_OK) {
1095 sys_sem_free(&ifconfig_cmd.cb_completed);
1096 PRINTK("%s : tcpip_callback failed in line %d : errnu %d", __FUNCTION__, __LINE__, ret);
1097 return 1;
1098 }
1099 (void)sys_arch_sem_wait(&ifconfig_cmd.cb_completed, 0);
1100 sys_sem_free(&ifconfig_cmd.cb_completed);
1101 ifconfig_cmd.cb_print_buf[PRINT_BUF_LEN - 1] = '\0';
1102 PRINTK("%s", ifconfig_cmd.cb_print_buf);
1103 return 1;
1104 }
1105 }
1106#endif /* LWIP_ARP && LWIP_ENABLE_IP_CONFLICT_SIGNAL */
1107#if LWIP_IPV6
1108 if ((ifconfig_cmd.option & IFCONFIG_OPTION_SET_IP) && IP_IS_V6_VAL(ifconfig_cmd.ip_addr)) {
1109 /* Pend 2 seconds for waiting the arp reply if the ip is already in use.*/
1110 retval = sys_arch_sem_wait(&dup_addr_detect, DUP_ARP_DETECT_TIME);
1113 if (retval == SYS_ARCH_ERROR) {
1114 sys_sem_free(&ifconfig_cmd.cb_completed);
1115 /* The result neither conflict nor timeout. */
1116 PRINT_ERR("ifconfig: internal error\n");
1117 return 1;
1118 } else if (retval < DUP_ARP_DETECT_TIME) {
1119 /* Duplicate use of new ip, restore it to the old one. */
1120 struct netif *netif = NULL;
1121 PRINT_ERR("ifconfig: IP conflict!\n");
1122 netif = netifapi_netif_find_by_name(ifconfig_cmd.iface);
1123 i = netif_get_ip6_addr_match(netif, &ifconfig_cmd.ip_addr.u_addr.ip6);
1124 if (i >= 0) {
1125 netif->ip6_addr_state[i] = IP6_ADDR_INVALID;
1126 }
1127
1128 sys_sem_free(&ifconfig_cmd.cb_completed);
1129 ifconfig_cmd.cb_print_buf[PRINT_BUF_LEN - 1] = '\0';
1130 PRINTK("%s", ifconfig_cmd.cb_print_buf);
1131 return 1;
1132 }
1133 }
1134#endif /* LWIP_IPV6 */
1135 sys_sem_free(&ifconfig_cmd.cb_completed);
1136 return 0;
1137ifconfig_error:
1138 lwip_ifconfig_usage("ifconfig");
1139 return 1;
1140}
LWIP_STATIC void lwip_ifconfig_usage(const char *cmd)
Definition: api_shell.c:671
LWIP_STATIC void lwip_ifconfig_show_internal(void *arg)
Definition: api_shell.c:429
LWIP_STATIC void lwip_ifconfig_internal(void *arg)
Definition: api_shell.c:476
void lwip_printsize(size_t size)
Definition: api_shell.c:656
sys_sem_t ip_conflict_detect
Definition: fixme.c:369
int ip6addr_aton(const char *cp, ip6_addr_t *addr)
Definition: fixme.c:405
sys_sem_t dup_addr_detect
Definition: fixme.c:373
struct netif * netifapi_netif_find_by_name(const char *name)
Definition: fixme.c:241
u32_t is_ip_conflict_signal
Definition: fixme.c:368
u32_t is_dup_detect_initialized
Definition: fixme.c:372
uint32_t sys_sem_t
Definition: sys_arch.h:52
函数调用图:

◆ netstat_internal()

void netstat_internal ( void ctx)
这是这个函数的调用关系图:

◆ osShellDns()

u32_t osShellDns ( int  argc,
const char **  argv 
)

在文件 api_shell.c2635 行定义.

2636{
2637 ip_addr_t dns = {0};
2638 err_t err;
2639 int i;
2640 if (argc < 1 || argv == NULL) {
2641 goto usage;
2642 }
2643
2644 if (tcpip_init_finish == 0) {
2645 PRINTK("%s: tcpip_init have not been called\n", __FUNCTION__);
2646 return LOS_NOK;
2647 }
2648
2649 if (argc == 1 && (strcmp(argv[0], "-a") == 0)) {
2650 for (i = 0; i < DNS_MAX_SERVERS; i++) {
2651 err = lwip_dns_getserver((u8_t)i, &dns);
2652 if (err == ERR_OK) {
2653 PRINTK("dns %d: %s\n", i + 1, ipaddr_ntoa_unsafe(&dns));
2654 } else {
2655 PRINTK("dns: failed\n");
2656 return LOS_NOK;
2657 }
2658 }
2659 return LOS_OK;
2660 } else if (argc == 2) {
2661 i = atoi(argv[0]);
2662 if ((i <= 0) || (i > DNS_MAX_SERVERS))
2663 goto usage;
2664#if LWIP_IPV6
2665 if (ip6addr_aton(argv[1], ((ip6_addr_t *)&dns))) {
2666#if LWIP_IPV4 && LWIP_IPV6
2667 dns.type = IPADDR_TYPE_V6;
2668#endif
2669 if (!ip6_addr_isglobal((ip6_addr_t *)&dns)) {
2670 PRINTK("ip address<%s> is wrong\n", argv[1]);
2671 return LOS_NOK;
2672 }
2673 } else
2674#endif
2675 {
2676#if LWIP_IPV4
2677 ((ip4_addr_t *)&dns)->addr = ipaddr_addr(argv[1]);
2678 if (((ip4_addr_t *)&dns)->addr == IPADDR_NONE) {
2679 PRINTK("ip address<%s> is wrong\n", argv[1]);
2680 return LOS_NOK;
2681 }
2682#if LWIP_IPV4 && LWIP_IPV6
2683 dns.type = IPADDR_TYPE_V4;
2684#endif
2685#endif
2686 }
2687
2688 err = lwip_dns_setserver((u8_t)(i - 1), &dns);
2689 if (err != ERR_OK) {
2690 PRINTK("dns : failed\n");
2691 return LOS_NOK;
2692 }
2693 return LOS_OK;
2694 }
2695usage:
2696 PRINTK("usage:\n");
2697 PRINTK("\tdns <1-%d> <IP>\n", DNS_MAX_SERVERS);
2698 PRINTK("\tdns -a\n");
2699 return LOS_NOK;
2700}
err_t lwip_dns_setserver(u8_t numdns, ip_addr_t *dnsserver)
Definition: fixme.c:345
err_t lwip_dns_getserver(u8_t numdns, ip_addr_t *dnsserver)
Definition: fixme.c:352
函数调用图:

◆ osShellNetIfDown()

u32_t osShellNetIfDown ( int  argc,
const char **  argv 
)

◆ osShellNetIfUp()

u32_t osShellNetIfUp ( int  argc,
const char **  argv 
)

◆ osShellNetstat()

u32_t osShellNetstat ( int  argc,
const char **  argv 
)
这是这个函数的调用关系图:

◆ osShellNtpdate()

u32_t osShellNtpdate ( int  argc,
const char **  argv 
)

在文件 api_shell.c2590 行定义.

2591{
2592 int server_num = 0;
2593 char *ret = NULL;
2594 struct timeval get_time;
2595 char buf[50];
2596
2597 (void)memset_s(&get_time, sizeof(struct timeval), 0, sizeof(struct timeval));
2598
2599 if (!tcpip_init_finish) {
2600 PRINTK("%s: tcpip_init have not been called\n", __FUNCTION__);
2601 return LOS_NOK;
2602 }
2603
2604 if (argc < 1 || argv == NULL) {
2605 goto usage;
2606 }
2607
2608 server_num = lwip_sntp_start(argc, (char **)argv, &get_time);
2609 if (server_num >= 0 && server_num < argc) {
2610 ret = ctime_r((time_t *)&get_time.tv_sec, buf);
2611 if (ret != NULL) {
2612 PRINTK("time server %s: %s\n", argv[server_num], ret);
2613 } else {
2614 PRINTK("ctime return null error\n");
2615 }
2616 } else {
2617 PRINTK("no server suitable for synchronization found\n");
2618 }
2619
2620 return LOS_OK;
2621
2622usage:
2623 PRINTK("\nUsage:\n");
2624 PRINTK("ntpdate [SERVER_IP1] [SERVER_IP2] ...\n");
2625 return LOS_NOK;
2626}
int lwip_sntp_start(int server_num, char **sntp_server, struct timeval *time)
Definition: fixme.c:379
函数调用图:

◆ osShellPing()

u32_t osShellPing ( int  argc,
const char **  argv 
)

在文件 api_shell.c1928 行定义.

1929{
1930 int sfd;
1931 struct sockaddr_in to;
1932 struct icmp_echo_hdr iecho;
1933 struct pbuf *pbuf_resp = NULL;
1934 struct icmp_echo_hdr *iecho_resp = NULL;
1935 struct ip_hdr *iphdr_resp = NULL;
1936 s16_t ip_hlen;
1937 ip_addr_t dst_ipaddr;
1938 fd_set fdReadSet;
1939 struct timeval stTimeVal;
1940 struct timespec start, end;
1941 int ret;
1942 s32_t i;
1943 long rtt;
1944 s32_t pingcount;
1945 char buf[50];
1946
1947 if (!tcpip_init_finish) {
1948 PRINTK("ping: tcpip_init have not been called\n");
1949 return LOS_NOK;
1950 }
1951
1952 if ((argc < 1) || (argv == NULL)) {
1953 PRINTK("ping : invalid arguments, ping command receives ip as command line argument \n");
1954 return LOS_NOK;
1955 }
1956
1957 if (argc == 2) {
1958 pingcount = atoi(argv[1]);
1959 if (pingcount < 1)
1960 pingcount = LWIP_SHELL_CMD_PING_RETRY_TIMES;
1961 } else {
1962 pingcount = LWIP_SHELL_CMD_PING_RETRY_TIMES;
1963 }
1964 PRINTK("ping %u packets start.\n", pingcount);
1965
1966 /* initialize dst IP address */
1967#if LWIP_DNS
1968 ip_2_ip4(&dst_ipaddr)->addr = get_hostip(argv[0]);
1969#else /* LWIP_DNS */
1970 ip_2_ip4(&dst_ipaddr)->addr = inet_addr(argv[0]);
1971#endif /* LWIP_DNS */
1972
1973 to.sin_family = AF_INET;
1974 to.sin_addr.s_addr = ip_2_ip4(&dst_ipaddr)->addr;
1975 to.sin_port = 0;
1976
1977 if (to.sin_addr.s_addr == IPADDR_NONE || to.sin_addr.s_addr == IPADDR_ANY) {
1978 PRINTK("ping : invalid ip address : NONE or ANY\n");
1979 return LOS_NOK;
1980 }
1981
1982 sfd = lwip_socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
1983 if (sfd == -1) {
1984 PRINTK("ping : failed, socket creation failed\n");
1985 return LOS_NOK;
1986 }
1987
1988 pbuf_resp = pbuf_alloc(PBUF_RAW, IP_HLEN + sizeof(struct icmp_echo_hdr), PBUF_RAM);
1989 if (pbuf_resp == NULL) {
1990 PRINTK("ping : memory allocation failed\n");
1991 goto FAILURE;
1992 }
1993
1994 for (i = 0; i < pingcount; i++) {
1995 (void)memset_s(&iecho, sizeof(iecho), 0, sizeof(iecho));
1996 ICMPH_TYPE_SET(&iecho, (u8_t)ICMP_ECHO);
1997 iecho.chksum = inet_chksum(&iecho, sizeof(struct icmp_echo_hdr));
1998
1999 ret = lwip_sendto(sfd, &iecho, sizeof(struct icmp_echo_hdr), 0, (struct sockaddr *)&to, (socklen_t)sizeof(to));
2000 if (ret == -1) {
2001 PRINTK("ping : sending ICMP echo msg failed\n");
2002 goto FAILURE;
2003 }
2004
2005 /* capture the start time to calculate round trip time */
2006 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &start);
2007 /* Wait in select for ICMP response msg */
2008 FD_ZERO(&fdReadSet);
2009 FD_SET(sfd, &fdReadSet);
2010 stTimeVal.tv_sec = LWIP_SHELL_CMD_PING_TIMEOUT / 1000;
2011 stTimeVal.tv_usec = 0;
2012
2013DO_SELECT:
2014 ret = select(sfd + 1, &fdReadSet, 0, 0, &stTimeVal);
2015 if (ret < 0) {
2016 PRINTK("ping : select failure\n");
2017 goto FAILURE;
2018 } else if (ret == 0) {
2019 PRINTK("Request timed out.\n");
2020 continue;
2021 }
2022
2023 /* capture the end time to calculate round trip time */
2024 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &end);
2025 rtt = ((end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000);
2026
2027 ret = lwip_recv(sfd, pbuf_resp->payload, pbuf_resp->len, 0);
2028 if (ret == -1) {
2029 PRINTK("ping : receiving ICMP echo response msg failed\n");
2030 goto FAILURE;
2031 }
2032
2033 /* Accessing ip header and icmp header */
2034 iphdr_resp = (struct ip_hdr *)(pbuf_resp->payload);
2035 ip_hlen = (s16_t)(IPH_HL(iphdr_resp) * 4);
2036 if (pbuf_header(pbuf_resp, (s16_t)(-ip_hlen))) {
2037 /* this failure will never happen, but failure handle is written just to be in safe side */
2038 PRINTK("ping : memory management failure\n");
2039 goto FAILURE;
2040 }
2041 iecho_resp = (struct icmp_echo_hdr *)pbuf_resp->payload;
2042
2043 /* Reverting back pbuf to its original state */
2044 if (pbuf_header(pbuf_resp, ip_hlen)) {
2045 /* this failure will never happen, but failure handle is written just to be in safe side */
2046 PRINTK("ping : memory management failure\n");
2047 goto FAILURE;
2048 }
2049
2050 if (iphdr_resp->src.addr == to.sin_addr.s_addr) {
2051 if (ICMPH_TYPE(iecho_resp) == ICMP_ER) {
2052 PRINTK("[%u]Reply from %s: time=%ims TTL=%u\n", i,
2053 inet_ntoa_r(to.sin_addr.s_addr, buf, sizeof(buf)), rtt, iphdr_resp->_ttl);
2054 } else if (ICMPH_TYPE(iecho_resp) == ICMP_ECHO) {
2055 /* If ping self, stack will receive a ICMP_ECHO request message flowing a ICMP_ER reply message,
2056 and we need reply only, do select again */
2057 goto DO_SELECT;
2058 }
2059 }
2060 }
2061
2062 (void)lwip_close(sfd);
2063 (void)pbuf_free(pbuf_resp);
2064 return LOS_OK;
2065
2066FAILURE:
2067 (void)lwip_close(sfd);
2068 if (pbuf_resp != NULL) {
2069 (void)pbuf_free(pbuf_resp);
2070 }
2071
2072 return LOS_NOK;
2073}
LWIP_STATIC unsigned int get_hostip(const char *hname)
Definition: api_shell.c:1543
ssize_t lwip_sendto(int s, const void *dataptr, size_t size, int flags, const struct sockaddr *to, socklen_t tolen)
Definition: sockets.c:90
int lwip_socket(int domain, int type, int protocol)
Definition: sockets.c:71
if(tv==NULL)
Definition: time.c:430
int clock_gettime(clockid_t clockID, struct timespec *tp)
当用户程序进行特定系统调用时(例如clock_gettime(CLOCK_REALTIME_COARSE, &ts)),VDSO代码页会将其拦截;
Definition: time.c:614
函数调用图:

◆ osShellTftp()

u32_t osShellTftp ( int  argc,
const char **  argv 
)

在文件 main.c80 行定义.

81{
82 u32_t ulRemoteAddr = IPADDR_NONE;
83 const u16_t usTftpServPort = 69;
84 u8_t ucTftpGet = 0;
85 s8_t *szLocalFileName = NULL;
86 s8_t *szRemoteFileName = NULL;
87 u32_t ret;
88
89 int i = 1;
90 if (argc < 1 || argv == NULL) {
91 goto usage;
92 }
93
94 if (!tcpip_init_finish) {
95 PRINTK("%s: tcpip_init have not been called\n", __FUNCTION__);
96 return LOS_NOK;
97 }
98
99 while (i < argc) {
100 if (strcmp(argv[i], "-p") == 0) {
101 ucTftpGet = 0;
102 i++;
103 continue;
104 }
105
106 if (strcmp(argv[i], "-g") == 0) {
107 ucTftpGet = 1;
108 i++;
109 continue;
110 }
111
112 if (strcmp(argv[i], "-l") == 0 && ((i + 1) < argc)) {
113 szLocalFileName = (s8_t *)argv[i + 1];
114 i += 2;
115 continue;
116 }
117
118 if (strcmp(argv[i], "-r") == 0 && ((i + 1) < argc)) {
119 szRemoteFileName = (s8_t *)argv[i + 1];
120 i += 2;
121 continue;
122 }
123
124 if ((i + 1) == argc) {
125 ulRemoteAddr = inet_addr(argv[i]);
126 break;
127 }
128
129 goto usage;
130 }
131
132 if (ulRemoteAddr == IPADDR_NONE || szLocalFileName == NULL || szRemoteFileName == NULL) {
133 goto usage;
134 }
135
136 if (ucTftpGet) {
137 ret = lwip_tftp_get_file_by_filename(ntohl(ulRemoteAddr), usTftpServPort,
138 TRANSFER_MODE_BINARY, szRemoteFileName, szLocalFileName);
139 } else {
140 ret = lwip_tftp_put_file_by_filename(ntohl(ulRemoteAddr), usTftpServPort,
141 TRANSFER_MODE_BINARY, szLocalFileName, szRemoteFileName);
142 }
143
144 LWIP_ASSERT("TFTP UNKNOW ERROR!", ret < ARRAY_SIZE(TftpError));
145 PRINTK("%s", TftpError[ret]);
146 if (ret) {
147 return LOS_NOK;
148 } else {
149 return LOS_OK;
150 }
151usage:
152 PRINTK("usage:\nTransfer a file from/to tftp server\n");
153 PRINTK("tftp <-g/-p> -l FullPathLocalFile -r RemoteFile Host\n");
154 return LOS_NOK;
155}
static char * TftpError[]
Definition: main.c:44
static int tcpip_init_finish
Definition: main.c:43
u32_t lwip_tftp_put_file_by_filename(u32_t ulHostAddr, u16_t usTftpServPort, u8_t ucTftpTransMode, s8_t *szSrcFileName, s8_t *szDestDirPath)
Definition: tftpc.c:918
u32_t lwip_tftp_get_file_by_filename(u32_t ulHostAddr, u16_t usTftpServPort, u8_t ucTftpTransMode, s8_t *szSrcFileName, s8_t *szDestDirPath)
Definition: tftpc.c:508
函数调用图: