更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
api_shell.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "../core/ipv4/etharp.c" /* for arp_table */
33#define icmp6_hdr netinet_icmp6_hdr
34#include <netinet/icmp6.h>
35#undef icmp6_hdr
36#include "lwip/fixme.h"
37#include "lwip/opt.h"
38
39#if LWIP_ENABLE_LOS_SHELL_CMD
40#include "lwip/api.h"
41#include "lwip/tcpip.h"
42#include "lwip/netif.h"
43#include "lwip/netdb.h"
44#include "lwip/stats.h"
45#include "lwip/err.h"
46#include "lwip/inet.h"
47#include "netif/etharp.h"
48#include "lwip/ip_addr.h"
49#include "lwip/ip6_addr.h"
50#include "lwip/icmp.h"
51#include "lwip/priv/nd6_priv.h"
52#include "lwip/sockets.h"
53#include "lwip/inet_chksum.h"
54#include "lwip/raw.h"
55#include "los_config.h"
56#include <string.h>
57#include "limits.h"
58#include <stdlib.h>
59#include <stdio.h>
60#include <errno.h>
61#include <time.h>
62#include <ctype.h>
63#include <poll.h>
64
65#include "lwip/api_shell.h"
66
67#include "lwip/dns.h"
68#include "lwip/udp.h"
69#include "lwip/priv/tcp_priv.h"
70
71#include "lwip/dhcp.h"
72#include "lwip/netifapi.h"
73#include "los_strnlen_user.h"
74#include "linux/kernel.h"
75#ifdef LOSCFG_SHELL
76#include "shcmd.h"
77#include "shell.h"
78#endif
79
80#define LWIP_STATIC static
81
82#if LWIP_ARP
84#endif
85extern volatile int tcpip_init_finish;
86extern const char *const tcp_state_str[];
87extern int get_unused_socket_num(void);
88
89#if LWIP_IPV6
90#define LWIP_MAX_PING6_ARG_COUNT 64
91#define LWIP_PING6_STANDARD_PKT_SIZE 56
92#define LWIP_PING6_STARTING_SEQ_NUM 0x2255
93#define LWIP_PING6_OUT_OF_ORDER_MAGNITUDE 1
94
95#define LWIP_PING6_COUNT_ARG 1
96#define LWIP_PING6_SOURCE_ADDRESS_ARG 2
97#define LWIP_PING6_INTERFACE_ARG 4
98#define LWIP_PING6_HOSTNAME_ARG 8
99#define LWIP_PING6_DEFAULT_SOCKET 16
100#endif
101
102/* Forward Declarations [START] */
103#ifndef LWIP_TESTBED
104LWIP_STATIC
105#endif
106int print_netif(struct netif *netif, char *print_buf, unsigned int buf_len);
107
108#ifndef LWIP_TESTBED
109LWIP_STATIC
110#endif
111void lwip_ifconfig_show_internal(void *arg);
112
113#ifndef LWIP_TESTBED
114LWIP_STATIC
115#endif
116void lwip_ifconfig_internal(void *arg);
117
118void lwip_printsize(size_t size);
119LWIP_STATIC void lwip_ifconfig_usage(const char *cmd);
120
121#ifndef LWIP_TESTBED
122LWIP_STATIC
123#endif
124void lwip_arp_show_internal(struct netif *netif, char *printf_buf, unsigned int buf_len);
125
126#ifndef LWIP_TESTBED
127LWIP_STATIC
128#endif
129void lwip_arp_internal(void *arg);
130
131LWIP_STATIC void lwip_arp_usage(const char *cmd);
132void ifup_internal(void *arg);
133void ifdown_internal(void *arg);
134
135#if LWIP_DNS
136LWIP_STATIC unsigned int get_hostip(const char *hname);
137
138#ifndef LWIP_TESTBED
139LWIP_STATIC
140#endif
141struct hostent *gethostnameinfo(const char *host);
142
143#endif /* LWIP_DNS */
144
145#ifdef LWIP_DEBUG_INFO
146LWIP_STATIC u32_t netdebug_memp(int argc, const char **argv);
147LWIP_STATIC u32_t netdebug_sock(int argc, const char **argv);
148u32_t osShellNetDebug(int argc, const char **argv);
149u32_t osShellIpDebug(int argc, const char **argv);
150#endif /* LWIP_DEBUG_INFO */
151#if LWIP_IPV6
152/* Holds params for ping6 task */
153typedef struct ping6_args {
157 u8_t pad;
159 ip6_addr_t src_addr;
160 ip6_addr_t dst_addr;
162
163/* Holds stats for ongoing ping6 task */
164typedef struct ping6_stats {
165 u32_t flag;
166 u32_t min_rtt;
167 u32_t max_rtt;
168 float avg_rtt;
170LWIP_STATIC void update_ping6_stats(ping6_stats_t *ping6_stats, u32_t rtt, u32_t nreceived);
171LWIP_STATIC int parse_args_ping6(int argc, const char **argv, ping6_args_t *ping6_params);
172
173u32_t osShellPing6(int argc, const char **argv);
174LWIP_STATIC int create_ping6_socket(u8_t type, const void *param);
175LWIP_STATIC const char *convert_icmpv6_err_to_string(u8_t err_type);
176#endif /* LWIP_IPV6 */
177u32_t osTcpserver(int argc, const char **argv);
178void udpserver(int argc, const char **argv);
179void tcp_access(int sockfd);
180#if LWIP_IPV6
181int netstat_get_udp_sendQLen6(struct udp_pcb *udppcb, struct pbuf *udpbuf);
182int netstat_udp_sendq6(struct udp_pcb *upcb);
183#endif
184#if LWIP_IPV4
185int netstat_get_udp_sendQLen(struct udp_pcb *udppcb, struct pbuf *udpbuf);
186#endif
187int netstat_tcp_sendq(struct tcp_pcb *tpcb);
188int netstat_tcp_recvq(struct tcp_pcb *tpcb);
189int netstat_netconn_recvq(const struct netconn *conn);
190
191int netstat_udp_sendq(struct udp_pcb *upcb);
192int netstat_netconn_sendq(struct netconn *conn);
193/* Forward Declarations [END] */
194
195#define IFCONFIG_OPTION_SET_IP (1)
196#define IFCONFIG_OPTION_SET_NETMASK (1 << 1)
197#define IFCONFIG_OPTION_SET_GW (1 << 2)
198#define IFCONFIG_OPTION_SET_HW (1 << 3)
199#define IFCONFIG_OPTION_SET_UP (1 << 4)
200#define IFCONFIG_OPTION_SET_DOWN (1 << 5)
201#define IFCONFIG_OPTION_SET_MTU (1 << 6)
202#define IFCONFIG_OPTION_DEL_IP (1 << 7)
203
204#define NETSTAT_ENTRY_SIZE 120
205#define MAX_NETSTAT_ENTRY (NETSTAT_ENTRY_SIZE * (MEMP_NUM_TCP_PCB + MEMP_NUM_UDP_PCB + MEMP_NUM_TCP_PCB_LISTEN + 1))
206
207#define PRINT_BUF_LEN 1024
208#define MAX_MACADDR_STRING_LENGTH 18 /* including NULL */
209
210#define CONVERT_STRING_TO_HEX(_src, _dest) \
211{ \
212 const char *_srcString = (char *)_src; \
213 _dest = 0; \
214 while (*_srcString) { \
215 _dest = (unsigned char)((_dest << 4) & 0xFF); \
216 if ((*_srcString >= 48) && (*_srcString <= 57)) /* between 0 to 9 */ \
217 _dest |= (unsigned char)(*_srcString - 48); \
218 else if ((*_srcString >= 65 && *_srcString <= 70)) /* between A to F */ \
219 _dest |= (unsigned char)((*_srcString - 65) + 10); \
220 else if ((*_srcString >= 97 && *_srcString <= 102)) /* between a to f */ \
221 _dest |= (unsigned char)((*_srcString - 97) + 10); \
222 else break; \
223 ++_srcString; \
224 } \
225}
226
227#define ERR_IFCONFIG_STRING_PUT(ret, str) \
228 do { \
229 (ret) = snprintf_s(ifconfig_cmd->cb_print_buf + ifconfig_cmd->print_len, \
230 PRINT_BUF_LEN - ifconfig_cmd->print_len, \
231 ((PRINT_BUF_LEN - ifconfig_cmd->print_len) - 1), (str)); \
232 if (((ret) > 0) && ((unsigned int)(ret) < (PRINT_BUF_LEN - ifconfig_cmd->print_len))) \
233 ifconfig_cmd->print_len += (unsigned int)(ret); \
234 } while (0) \
235
236#define LWIP_MSECS_TO_SECS(time_in_msecs) (time_in_msecs / 1000)
238 char iface[IFNAMSIZ];
239 unsigned int option;
240 ip_addr_t ip_addr;
241 ip_addr_t netmask;
242 ip_addr_t gw;
243 unsigned char ethaddr[6];
244 u16_t mtu;
245 /* when using telnet, print to the telnet socket will result in system */
246 /* deadlock. So we cahe the prinf data to a buf, and when the tcpip */
247 /* callback returns, then print the data out to the telnet socket */
249 char cb_print_buf[PRINT_BUF_LEN];
250 unsigned int print_len;
251};
252
258};
259
261 char *if_name;
262 err_t err;
264};
265
266#ifndef LWIP_TESTBED
267LWIP_STATIC
268#endif
269int print_netif(struct netif *netif, char *print_buf, unsigned int buf_len)
270{
271 int i, ret;
272 char *tmp = print_buf;
273#if LWIP_IPV6
274 char *addr = NULL;
275#endif
276
277 if (buf_len < 1) {
278 goto out;
279 }
280 if (netif->link_layer_type == LOOPBACK_IF) {
281 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "%.2s\t", netif->name);
282 } else {
283 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "%s\t", netif_get_name(netif));
284 }
285 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
286 goto out;
287 tmp += ret;
288 buf_len -= (unsigned int)ret;
289#if LWIP_IPV4
290 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "ip:%s ", ipaddr_ntoa(&netif->ip_addr));
291 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
292 goto out;
293 tmp += ret;
294 buf_len -= (unsigned int)ret;
295
296 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "netmask:%s ", ipaddr_ntoa(&netif->netmask));
297 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
298 goto out;
299 tmp += ret;
300 buf_len -= (unsigned int)ret;
301
302 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "gateway:%s\n", ipaddr_ntoa(&netif->gw));
303 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
304 goto out;
305 tmp += ret;
306 buf_len -= (unsigned int)ret;
307#endif
308
309#if LWIP_IPV6
310 for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
311 /* only PREFERRED addresses are displyaed */
312 if (!ip6_addr_isvalid(netif->ip6_addr_state[i])) {
313 continue;
314 }
315 addr = ip6addr_ntoa((const ip6_addr_t *)&netif->ip6_addr[i]);
316 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "\tip6: %s/64\n", addr ? addr : "::");
317 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
318 goto out;
319 tmp += ret;
320 buf_len -= (unsigned int)ret;
321 }
322#endif
323
324 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "\tHWaddr ");
325 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
326 goto out;
327 tmp += ret;
328 buf_len -= (unsigned int)ret;
329
330 for (i = 0; i < netif->hwaddr_len - 1; i++) {
331 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "%02x:", netif->hwaddr[i]);
332 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
333 goto out;
334 tmp += ret;
335 buf_len -= (unsigned int)ret;
336 }
337
338 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "%02x", netif->hwaddr[i]);
339 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
340 goto out;
341 tmp += ret;
342 buf_len -= (unsigned int)ret;
343
344 ret = snprintf_s(tmp, buf_len, (buf_len - 1), " MTU:%d %s", netif->mtu,
345 netif->flags & NETIF_FLAG_UP ? "Running" : "Stop");
346 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
347 goto out;
348 tmp += ret;
349 buf_len -= (unsigned int)ret;
350
351 if (netif_default == netif && netif_is_up(netif)) {
352 ret = snprintf_s(tmp, buf_len, (buf_len - 1), " %s", "Default");
353 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
354 goto out;
355 tmp += ret;
356 buf_len -= (unsigned int)ret;
357 }
358
359 ret = snprintf_s(tmp, buf_len, (buf_len - 1), " %s\n", netif->flags & NETIF_FLAG_LINK_UP ? "Link UP" : "Link Down");
360 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
361 goto out;
362 tmp += ret;
363
364#if MIB2_STATS
365 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "\tRX packets:%u ",
366 netif->mib2_counters.ifinucastpkts + netif->mib2_counters.ifinnucastpkts);
367 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
368 goto out;
369 tmp += ret;
370 buf_len -= (unsigned int)ret;
371
372 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "errors:%u ", netif->mib2_counters.ifinerrors);
373 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
374 goto out;
375 tmp += ret;
376 buf_len -= (unsigned int)ret;
377
378 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "dropped:%u ", netif->mib2_counters.ifindiscards);
379 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
380 goto out;
381 tmp += ret;
382 buf_len -= (unsigned int)ret;
383
384 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "overruns:%u\n", netif->mib2_counters.ifinoverruns);
385 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
386 goto out;
387 tmp += ret;
388 buf_len -= (unsigned int)ret;
389
390 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "\tTX packets:%u ",
391 netif->mib2_counters.ifoutucastpkts + netif->mib2_counters.ifoutnucastpkts);
392 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
393 goto out;
394 tmp += ret;
395 buf_len -= (unsigned int)ret;
396
397 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "errors:%u ", netif->mib2_counters.ifouterrors);
398 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
399 goto out;
400 tmp += ret;
401 buf_len -= (unsigned int)ret;
402
403 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "dropped:%u\n", netif->mib2_counters.ifoutdiscards);
404 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
405 goto out;
406 tmp += ret;
407 buf_len -= (unsigned int)ret;
408
409 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "\tRX bytes:%u ", netif->mib2_counters.ifinoctets);
410 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
411 goto out;
412 tmp += ret;
413 buf_len -= (unsigned int)ret;
414
415 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "TX bytes:%u\n", netif->mib2_counters.ifoutoctets);
416 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
417 goto out;
418 tmp += ret;
419 buf_len -= (unsigned int)ret;
420#endif
421
422out:
423 return (int)(tmp - print_buf);
424}
425
426#ifndef LWIP_TESTBED
427LWIP_STATIC
428#endif
430{
431 struct netif *netif = NULL;
432 struct ifconfig_option *ifconfig_cmd = (struct ifconfig_option *)arg;
433 int ret;
434
435 if (netif_list == NULL) {
436 ret = snprintf_s(ifconfig_cmd->cb_print_buf, PRINT_BUF_LEN - ifconfig_cmd->print_len,
437 ((PRINT_BUF_LEN - ifconfig_cmd->print_len) - 1), "Device not init\n");
438 if ((ret > 0) && ((unsigned int)ret < (PRINT_BUF_LEN - ifconfig_cmd->print_len))) {
439 ifconfig_cmd->print_len += (unsigned int)ret;
440 }
441 sys_sem_signal(&ifconfig_cmd->cb_completed);
442 return;
443 }
444
445 if (ifconfig_cmd->iface[0] == '\0') {
446 /* display all netif */
447 for (netif = netif_list; netif != NULL; netif = netif->next) {
448 ret = print_netif(netif, ifconfig_cmd->cb_print_buf + ifconfig_cmd->print_len,
449 PRINT_BUF_LEN - ifconfig_cmd->print_len);
450 ifconfig_cmd->print_len += (unsigned int)ret;
451 }
452 } else {
453 netif = netif_find(ifconfig_cmd->iface);
454 if (netif == NULL) {
455 ret = snprintf_s(ifconfig_cmd->cb_print_buf + ifconfig_cmd->print_len,
456 (PRINT_BUF_LEN - ifconfig_cmd->print_len),
457 ((PRINT_BUF_LEN - ifconfig_cmd->print_len) - 1), "Device not found\n");
458 if ((ret > 0) && ((unsigned int)ret < (PRINT_BUF_LEN - ifconfig_cmd->print_len))) {
459 ifconfig_cmd->print_len += (unsigned int)ret;
460 }
461
462 sys_sem_signal(&ifconfig_cmd->cb_completed);
463 return;
464 }
465
466 ret = print_netif(netif, ifconfig_cmd->cb_print_buf + ifconfig_cmd->print_len,
467 PRINT_BUF_LEN - ifconfig_cmd->print_len);
468 ifconfig_cmd->print_len += (unsigned int)ret;
469 }
470 sys_sem_signal(&ifconfig_cmd->cb_completed);
471}
472
473#ifndef LWIP_TESTBED
474LWIP_STATIC
475#endif
477{
478 struct ifconfig_option *ifconfig_cmd = NULL;
479 struct netif *netif = NULL;
480 ip_addr_t ip_addr;
481 ip_addr_t netmask;
482 ip_addr_t gw;
483 unsigned short mtu;
484 struct netif *loc_netif = NULL;
485 int ret;
486 s8_t idx;
487 err_t err;
488
489 ifconfig_cmd = (struct ifconfig_option *)arg;
490 netif = netif_find(ifconfig_cmd->iface);
491 if (netif == NULL) {
492 ERR_IFCONFIG_STRING_PUT(ret, "Device not found\n");
493 goto out;
494 }
495
496 if (ifconfig_cmd->option & IFCONFIG_OPTION_SET_UP) {
497 (void)netif_set_up(netif);
498 goto out;
499 } else if (ifconfig_cmd->option & IFCONFIG_OPTION_SET_DOWN) {
500 (void)netif_set_down(netif);
501 goto out;
502 }
503
504 if ((ifconfig_cmd->option & IFCONFIG_OPTION_SET_IP) ||
505 (ifconfig_cmd->option & IFCONFIG_OPTION_SET_NETMASK) ||
506 (ifconfig_cmd->option & IFCONFIG_OPTION_SET_HW)) {
507 (void)netif_set_down(netif);
508 }
509
510 if (ifconfig_cmd->option & IFCONFIG_OPTION_SET_IP) {
511 ip_addr_set_val(&ip_addr, &(ifconfig_cmd->ip_addr));
512 if (IP_IS_V4_VAL(ifconfig_cmd->ip_addr)) {
513 /* check the address is not multicast/broadcast/0/loopback */
514 if (ip_addr_ismulticast_val(&ip_addr) || ip_addr_isbroadcast_val(&ip_addr, netif) ||
515 ip_addr_isany(&ip_addr) || ip_addr_isloopback(&ip_addr)) {
516 ERR_IFCONFIG_STRING_PUT(ret, "Don't set ip as a multicast/broadcast/0/loopback address!\n");
517 goto out;
518 }
519
520 /* reset gateway if new and previous ipaddr not in same net */
521 if (!ip_addr_netcmp_val(&ip_addr, &netif->ip_addr, ip_2_ip4(&netif->netmask))) {
522 ip_addr_set_zero(&netif->gw);
523 if (netif == netif_default) {
524 (void)netif_set_default(NULL);
525 }
526 }
527
528 /* lwip disallow two netif sit in same net at the same time */
529 loc_netif = netif_list;
530 while (loc_netif != NULL) {
531 if (loc_netif == netif) {
532 loc_netif = loc_netif->next;
533 continue;
534 }
535 if (IP_IS_V4_VAL(ifconfig_cmd->ip_addr) && ip_addr_cmp(&netif->netmask, &loc_netif->netmask) &&
536 ip_addr_netcmp_val(&loc_netif->ip_addr, &ip_addr, ip_2_ip4(&netif->netmask))) {
537 ERR_IFCONFIG_STRING_PUT(ret, "Duplicate network!\n");
538 goto out;
539 }
540 loc_netif = loc_netif->next;
541 }
542
543#if LWIP_DHCP
544 if (netif_dhcp_data(netif) && netif_dhcp_data(netif)->state != DHCP_STATE_OFF) {
545 (void)netif_dhcp_off(netif);
546 }
547#endif
548 netif_set_ipaddr(netif, ip_2_ip4(&ip_addr));
549 } else if (IP_IS_V6_VAL(ifconfig_cmd->ip_addr)) {
550 idx = -1;
551 err = netif_add_ip6_address(netif, ip_2_ip6(&ip_addr), &idx);
552 if (err != ERR_OK || idx == -1) {
553 ERR_IFCONFIG_STRING_PUT(ret, "The IPv6 has reached the Global address limit, "
554 "you should delete one address before add!\n");
555 goto out;
556 }
557 }
558 }
559
560 if (ifconfig_cmd->option & IFCONFIG_OPTION_DEL_IP) {
561 (void)netif_do_rmv_ipv6_addr(netif, &ifconfig_cmd->ip_addr);
562 }
563
564 if (ifconfig_cmd->option & IFCONFIG_OPTION_SET_NETMASK) {
565 ip_addr_set_val(&netmask, &(ifconfig_cmd->netmask));
566 /* check data valid */
567 if (!ip_addr_netmask_valid(ip_2_ip4(&netmask))) {
568 ERR_IFCONFIG_STRING_PUT(ret, "ifconfig: netmask is invalid!\n");
569 goto out;
570 }
571
572#if LWIP_DHCP
573 if (netif_dhcp_data(netif) && netif_dhcp_data(netif)->state != DHCP_STATE_OFF) {
574 (void)netif_dhcp_off(netif);
575 }
576#endif
577 if (netif_ip4_netmask(netif)->addr != ip_2_ip4(&netmask)->addr) {
578 /* lwip disallow two netif sit in same net at the same time */
579 loc_netif = netif_list;
580 while (loc_netif != NULL) {
581 if (loc_netif == netif) {
582 loc_netif = loc_netif->next;
583 continue;
584 }
585 if (ip_addr_cmp(&loc_netif->netmask, &netmask) &&
586 ip_addr_netcmp(&loc_netif->ip_addr, &netif->ip_addr, ip_2_ip4(&netmask))) {
587 ERR_IFCONFIG_STRING_PUT(ret, "Duplicate network!\n");
588 goto out;
589 }
590 loc_netif = loc_netif->next;
591 }
592 netif_set_netmask(netif, ip_2_ip4(&netmask));
593 /* check if gateway still reachable */
594 if (!ip_addr_netcmp(&netif->gw, &netif->ip_addr, ip_2_ip4(&netmask))) {
595 ip_addr_set_zero(&(netif->gw));
596 if (netif == netif_default) {
597 (void)netif_set_default(NULL);
598 }
599 }
600 }
601 }
602
603 if ((ifconfig_cmd->option & IFCONFIG_OPTION_SET_HW) &&
604 netif_set_hwaddr(netif, ifconfig_cmd->ethaddr, NETIF_MAX_HWADDR_LEN) != ERR_OK) {
605 ERR_IFCONFIG_STRING_PUT(ret, "Failed to update the hwaddr of the device!\n");
606 (void)netif_set_up(netif);
607 goto out;
608 }
609
610 if ((ifconfig_cmd->option & IFCONFIG_OPTION_SET_IP) ||
611 (ifconfig_cmd->option & IFCONFIG_OPTION_SET_NETMASK) ||
612 (ifconfig_cmd->option & IFCONFIG_OPTION_SET_HW)) {
613 (void)netif_set_up(netif);
614 }
615
616 if (ifconfig_cmd->option & IFCONFIG_OPTION_SET_GW) {
617 ip_addr_set_val(&gw, &ifconfig_cmd->gw);
618
619 /* check the address multicast/0/loopback */
620 if (ip_addr_ismulticast_val(&gw) || ip_addr_isbroadcast_val(&gw, netif) ||
621 ip_addr_isany(&gw) || ip_addr_isloopback(&gw)) {
622 ERR_IFCONFIG_STRING_PUT(ret, "Don't set gateway as a multicast/broadcast/0/loopback address!\n");
623 goto out;
624 }
625
626 /* check if reachable */
627 if (!ip_addr_netcmp_val(&gw, &netif->ip_addr, ip_2_ip4(&netif->netmask))) {
628 ERR_IFCONFIG_STRING_PUT(ret, "The address is unreachable!\n");
629 goto out;
630 }
631
632 if (netif_default != netif) {
633 ip_addr_set_zero(&netif->gw);
634 (void)netif_set_default(netif);
635 }
636
637#if LWIP_DHCP
638 if (netif_dhcp_data(netif) && netif_dhcp_data(netif)->state != DHCP_STATE_OFF) {
639 (void)netif_dhcp_off(netif);
640 }
641#endif
642 netif_set_gw(netif, ip_2_ip4(&gw));
643 }
644
645 if (ifconfig_cmd->option & IFCONFIG_OPTION_SET_MTU) {
646 mtu = ifconfig_cmd->mtu;
647 if (netif_set_mtu(netif, mtu) != ERR_OK) {
648 ERR_IFCONFIG_STRING_PUT(ret, "Invalid MTU\n");
649 }
650 }
651out:
652 sys_sem_signal(&ifconfig_cmd->cb_completed);
653}
654
655
656void lwip_printsize(size_t size)
657{
658 static const char *SIZES[] = {"B", "KB", "MB", "GB"};
659 size_t divis = 0;
660 size_t rem = 0;
661
662 while ((size >= 1024) && (divis < ((sizeof(SIZES) / sizeof(char *)) - 1))) {
663 rem = (size % 1024);
664 divis++;
665 size /= 1024;
666 }
667
668 PRINTK("(%.1f %s) \r\n", (float)size + (float)rem / 1024.0, SIZES[divis]);
669}
670
671LWIP_STATIC void lwip_ifconfig_usage(const char *cmd)
672{
673 PRINTK("Usage:"\
674 "\n%s [-a] "\
675 "\n[interface]"\
676 "\n[interface ipaddr] <netmask mask> <gateway gw>"\
677 "\n[interface inet6 add|del ipaddr]"\
678 "\n[interface hw ether MAC]"\
679 "\n[interface mtu NN]"\
680 "\n[interface up|down]\n",
681 cmd);
682}
683
684u32_t lwip_ifconfig(int argc, const char **argv)
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}
1141
1142#ifdef LOSCFG_SHELL
1143SHELLCMD_ENTRY(ifconfig_shellcmd, CMD_TYPE_EX, "ifconfig", XARGS, (CmdCallBackFunc)lwip_ifconfig);
1144#endif /* LOSCFG_SHELL */
1145/* add arp entry to arp cache */
1146#define ARP_OPTION_ADD 1
1147/* delete arp entry to arp cache */
1148#define ARP_OPTION_DEL 2
1149/* print all arp entry in arp cache */
1150#define ARP_OPTION_SHOW 3
1151
1153 /* see the ARP_OPTION_ above */
1155 /* descriptive abbreviation of network interface */
1156 char iface[IFNAMSIZ];
1157 /* ip addr */
1158 unsigned int ipaddr;
1159 /* hw addr */
1160 unsigned char ethaddr[6];
1161 /* when using telnet, printf to the telnet socket will result in system */
1162 /* deadlock.so don't do it.cahe the data to prinf to a buf, and when */
1163 /* callback returns, then printf the data out to the telnet socket */
1165 char cb_print_buf[PRINT_BUF_LEN];
1167};
1168
1169#ifndef LWIP_TESTBED
1170LWIP_STATIC
1171#endif
1172void lwip_arp_show_internal(struct netif *netif, char *printf_buf, unsigned int buf_len)
1173{
1174 u8_t state, i;
1175 int ret;
1176 char *tmp = printf_buf;
1177 if (buf_len < 1) {
1178 return;
1179 }
1180 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "%-24s%-24s%-12s%-12s\n", "Address", "HWaddress", "Iface", "Type");
1181 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
1182 return;
1183 tmp += ret;
1184 buf_len -= (unsigned int)ret;
1185
1186 if (netif != NULL) {
1187 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
1188 state = arp_table[i].state;
1189 if (((state == ETHARP_STATE_STABLE)
1190 #if ETHARP_SUPPORT_STATIC_ENTRIES
1191 || (state == ETHARP_STATE_STATIC)
1192#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
1193 ) && arp_table[i].netif) {
1194
1195 if (strcmp(netif_get_name(netif), netif_get_name(arp_table[i].netif)) != 0) {
1196 continue;
1197 }
1198
1199 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "%-24s%02X:%02X:%02X:%02X:%02X:%02X %s %s\n",
1200 ip4addr_ntoa(&arp_table[i].ipaddr),
1201 arp_table[i].ethaddr.addr[0], arp_table[i].ethaddr.addr[1],
1202 arp_table[i].ethaddr.addr[2], arp_table[i].ethaddr.addr[3],
1203 arp_table[i].ethaddr.addr[4], arp_table[i].ethaddr.addr[5],
1204 netif_get_name(netif),
1205#if ETHARP_SUPPORT_STATIC_ENTRIES
1206 ((state == ETHARP_STATE_STATIC) ? "static" : "dynamic")
1207#else
1208 "dynamic"
1209#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
1210 );
1211 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
1212 return;
1213 tmp += ret;
1214 buf_len -= (unsigned int)ret;
1215 }
1216 }
1217 } else {
1218 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
1219 state = arp_table[i].state;
1220 if (((state == ETHARP_STATE_STABLE)
1221 #if ETHARP_SUPPORT_STATIC_ENTRIES
1222 || (state == ETHARP_STATE_STATIC)
1223#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
1224 ) && arp_table[i].netif) {
1225 ret = snprintf_s(tmp, buf_len, (buf_len - 1), "%-24s%02X:%02X:%02X:%02X:%02X:%02X %s %s\n",
1226 ip4addr_ntoa(&arp_table[i].ipaddr),
1227 arp_table[i].ethaddr.addr[0], arp_table[i].ethaddr.addr[1],
1228 arp_table[i].ethaddr.addr[2], arp_table[i].ethaddr.addr[3],
1229 arp_table[i].ethaddr.addr[4], arp_table[i].ethaddr.addr[5],
1230 netif_get_name(arp_table[i].netif),
1231#if ETHARP_SUPPORT_STATIC_ENTRIES
1232 ((state == ETHARP_STATE_STATIC) ? "static" : "dynamic")
1233#else
1234 "dynamic"
1235#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
1236 );
1237 if ((ret <= 0) || ((unsigned int)ret >= buf_len))
1238 return;
1239 tmp += ret;
1240 buf_len -= (unsigned int)ret;
1241 }
1242 }
1243 }
1244}
1245
1246#ifndef LWIP_TESTBED
1247LWIP_STATIC
1248#endif
1249void lwip_arp_internal(void *arg)
1250{
1251#if LWIP_IPV4
1252 struct arp_option *arp_cmd = (struct arp_option *)arg;
1253 struct netif *netif = NULL;
1254 struct eth_addr ethaddr;
1255 ip4_addr_t ipaddr;
1256 err_t ret = 0;
1257 int type = 0;
1258
1259 if (arp_cmd->iface[0] == 'd' && arp_cmd->iface[1] == 'e') {
1260 netif = NULL;
1261 } else {
1262 /* find the specified netif by it's name */
1263 netif = netif_find(arp_cmd->iface);
1264 if (netif == NULL) {
1265 (void)snprintf_s(arp_cmd->cb_print_buf, PRINT_BUF_LEN, (PRINT_BUF_LEN - 1), "No such device\n");
1266 goto out;
1267 }
1268 }
1269
1270 type = arp_cmd->option;
1271 switch (type) {
1272 case ARP_OPTION_SHOW:
1273 if (netif != NULL) {
1274 lwip_arp_show_internal(netif, arp_cmd->cb_print_buf, PRINT_BUF_LEN);
1275 } else {
1276 lwip_arp_show_internal(NULL, arp_cmd->cb_print_buf, PRINT_BUF_LEN);
1277 }
1278 break;
1279
1280 case ARP_OPTION_ADD:
1281#if ETHARP_SUPPORT_STATIC_ENTRIES
1282 ipaddr.addr = arp_cmd->ipaddr;
1283 (void)memcpy_s(ethaddr.addr, sizeof(ethaddr.addr), arp_cmd->ethaddr, 6);
1284 if (netif != NULL) {
1285 if (ip4_addr_netcmp(&ipaddr, ip_2_ip4(&(netif->ip_addr)), ip_2_ip4(&(netif->netmask)))) {
1286 ret = etharp_update_arp_entry(netif, &ipaddr, &ethaddr,
1287 ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY);
1288 } else {
1289 ret = ERR_RTE;
1290 }
1291 } else {
1292 ret = etharp_add_static_entry(&ipaddr, &ethaddr);
1293 }
1294#else
1295 ret = ERR_ARG;
1296#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
1297 break;
1298
1299 case ARP_OPTION_DEL:
1300 ipaddr.addr = arp_cmd->ipaddr;
1301 (void)memcpy_s(ethaddr.addr, sizeof(ethaddr.addr), arp_cmd->ethaddr, 6);
1302 if (netif != NULL) {
1303 ret = etharp_delete_arp_entry(netif, &ipaddr);
1304 } else {
1305 for (netif = netif_list; netif != NULL; netif = netif->next) {
1306 ret = etharp_delete_arp_entry(netif, &ipaddr);
1307 if (ret == ERR_OK) {
1308 /*only can del success one time*/
1309 break;
1310 }
1311 }
1312 }
1313 break;
1314
1315 default:
1316 (void)snprintf_s(arp_cmd->cb_print_buf, PRINT_BUF_LEN, (PRINT_BUF_LEN - 1), "Error\n");
1317 goto out;
1318 }
1319
1320out:
1321 if (type == ARP_OPTION_ADD || type == ARP_OPTION_DEL) {
1322 if (ret == ERR_MEM) {
1323 (void)snprintf_s(arp_cmd->cb_print_buf, PRINT_BUF_LEN, (PRINT_BUF_LEN - 1), "Out of memory error\n");
1324 } else if (ret == ERR_ARG) {
1325 (void)snprintf_s(arp_cmd->cb_print_buf, PRINT_BUF_LEN, (PRINT_BUF_LEN - 1), "Illegal argument\n");
1326 } else if (ret == ERR_RTE) {
1327 (void)snprintf_s(arp_cmd->cb_print_buf, PRINT_BUF_LEN, (PRINT_BUF_LEN - 1), "Network is unreachable\n");
1328 } else {
1329 (void)snprintf_s(arp_cmd->cb_print_buf, PRINT_BUF_LEN, (PRINT_BUF_LEN - 1), "Successed\n");
1330 }
1331 }
1332#endif
1333
1334 sys_sem_signal(&arp_cmd->cb_completed);
1335}
1336
1337
1338LWIP_STATIC void lwip_arp_usage(const char *cmd)
1339{
1340 PRINTK("Usage:"
1341 "\n%s"
1342 "\n%s [-i IF] -s IPADDR HWADDR"
1343 "\n%s [-i IF] -d IPADDR\n",
1344 cmd, cmd, cmd);
1345}
1346
1347u32_t lwip_arp(int argc, const char **argv)
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}
1478
1479#ifdef LOSCFG_SHELL_CMD_DEBUG
1481#endif /* LOSCFG_SHELL_CMD_DEBUG */
1482
1483void ifup_internal(void *arg)
1484{
1485 struct netif *netif = NULL;
1486 struct if_cmd_data *ifcmd_data;
1487
1488 ifcmd_data = (struct if_cmd_data *)arg;
1489 if (ifcmd_data == NULL) {
1490 return;
1491 }
1492 netif = netif_find(ifcmd_data->if_name);
1493 if (netif == NULL) {
1494 ifcmd_data->err = ERR_VAL;
1495 } else {
1496 (void)netif_set_up(netif);
1497 ifcmd_data->err = ERR_OK;
1498 }
1499
1500 sys_sem_signal(&ifcmd_data->cb_completed);
1501}
1502
1503void ifdown_internal(void *arg)
1504{
1505 struct netif *netif = NULL;
1506 struct if_cmd_data *ifcmd_data = NULL;
1507
1508 ifcmd_data = (struct if_cmd_data *)arg;
1509 if (ifcmd_data == NULL) {
1510 return;
1511 }
1512 netif = netif_find(ifcmd_data->if_name);
1513 if (netif == NULL) {
1514 ifcmd_data->err = ERR_VAL;
1515 } else {
1516 (void)netif_set_down(netif);
1517 ifcmd_data->err = ERR_OK;
1518 }
1519
1520 sys_sem_signal(&ifcmd_data->cb_completed);
1521}
1522
1523#if LWIP_DNS
1524#ifndef LWIP_TESTBED
1525LWIP_STATIC
1526#endif
1527struct hostent *gethostnameinfo(const char *host)
1528{
1529 static struct hostent hostbuf;
1530 struct hostent *hp = NULL;
1531 const size_t hstbuflen = 1024;
1532 char tmphstbuf[1024];
1533 int res;
1534 int herr;
1535
1536 res = lwip_gethostbyname_r(host, &hostbuf, tmphstbuf, hstbuflen, &hp, &herr);
1537 /* Check for errors. */
1538 if (res || hp == NULL)
1539 return NULL;
1540 return hp;
1541}
1542
1543LWIP_STATIC unsigned int get_hostip(const char *hname)
1544{
1545 unsigned int ip = 0;
1546 int ret;
1547
1548 struct hostent *pent = gethostnameinfo(hname);
1549 if (pent == NULL || pent->h_addr == NULL)
1550 return 0;
1551 ret = memcpy_s(&ip, sizeof(ip), pent->h_addr, 4);
1552 if (ret != 0) {
1553 return 0;
1554 }
1555 return ip;
1556}
1557#endif
1558
1559#if LWIP_EXT_POLL_SUPPORT
1560static int ping_taskid = -1;
1561static int ping_kill = 0;
1562#define PING_ZERO_DATA_LEN 8
1563static void lwip_ping_usage(void)
1564{
1565 PRINTK("Usage:"\
1566 "\n ping"
1567 "\n ping [-n cnt] [-w interval] [-l data_len] destination"
1568 "\n ping [-t] [-w interval] destination"
1569 "\n ping -k");
1570 PRINTK("\n -t means ping forever, user can use -k to stop the forever ping\n");
1571}
1572
1573LWIP_STATIC int osPingFunc(u32_t destip, u32_t cnt, u32_t interval, u32_t data_len)
1574{
1575 int sfd;
1576 struct sockaddr_in to;
1577 struct pbuf *pbuf_resp = NULL;
1578 struct icmp_echo_hdr *iecho = NULL;
1579 struct icmp_echo_hdr *iecho_resp = NULL;
1580 struct ip_hdr *iphdr_resp = NULL;
1581 u32_t iecho_len;
1582 s16_t ip_hlen;
1583 u32_t forever;
1584 u32_t i = 0;
1585 u32_t succ_cnt = 0;
1586 u32_t failed_cnt = 0;
1587 struct timespec start, end;
1588 long timout_ms = 0;
1589 struct pollfd pfd;
1590 long rtt;
1591 int ret = 0;
1592 u32_t intrvl = 0;
1593 char *data_buf = NULL;
1594 BOOL timeout_flag = false;
1595 char buf[50];
1596
1597 iecho_len = sizeof(struct icmp_echo_hdr) + data_len;
1598 sfd = lwip_socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
1599 if (sfd < 0) {
1600 perror("Ping socket");
1601 return -1;
1602 }
1603 pbuf_resp = pbuf_alloc(PBUF_RAW, IP_HLEN + sizeof(struct icmp_echo_hdr), PBUF_RAM);
1604 if (pbuf_resp == NULL) {
1605 PRINTK("Ping: pbuf_resp malloc failed\n");
1606 ret = -1;
1607 goto FAILURE;
1608 }
1609 iecho = (struct icmp_echo_hdr *)mem_malloc(iecho_len);
1610 if (iecho == NULL) {
1611 PRINTK("Ping: echo request malloc failed\n");
1612 ret = -1;
1613 goto FAILURE;
1614 }
1615
1616 to.sin_family = AF_INET;
1617 to.sin_addr.s_addr = destip; /* already in network order */
1618 to.sin_port = 0;
1619
1620 if (data_len > PING_ZERO_DATA_LEN) {
1621 (void)memset_s(iecho, sizeof(struct icmp_echo_hdr) + PING_ZERO_DATA_LEN,
1622 0, sizeof(struct icmp_echo_hdr) + PING_ZERO_DATA_LEN);
1623 data_buf = (char *)iecho + sizeof(struct icmp_echo_hdr) + PING_ZERO_DATA_LEN;
1624 for (i = 0; i < data_len - PING_ZERO_DATA_LEN; i++) {
1625 *(data_buf + i) = i + 0x10;
1626 }
1627 } else {
1628 (void)memset_s(iecho, sizeof(struct icmp_echo_hdr) + data_len, 0, sizeof(struct icmp_echo_hdr) + data_len);
1629 }
1630 iecho->id = htons((u16_t)LOS_CurTaskIDGet());
1631 ICMPH_TYPE_SET(iecho, (u8_t)ICMP_ECHO);
1632 forever = (cnt ? 0 : 1);
1633 i = 0;
1634 while (!ping_kill && (forever || (i < cnt))) {
1635 iecho->seqno = htons((u16_t)i);
1636 iecho->chksum = 0;
1637 iecho->chksum = inet_chksum((void *)iecho, iecho_len);
1638
1639 ret = sendto(sfd, iecho, iecho_len, 0, (struct sockaddr *)&to, (socklen_t)sizeof(to));
1640 if (ret < 0) {
1641 perror("Ping: sending ICMP echo request failed\n");
1642 goto FAILURE;
1643 }
1644
1645 /* capture the start time to calculate RTT */
1646 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &start);
1647
1648 /* poll for ICMP echo response msg */
1649 pfd.fd = sfd;
1650
1651 do {
1652 pfd.events = POLLIN;
1653 pfd.revents = 0;
1654 timeout_flag = false;
1655 ret = poll(&pfd, 1, LWIP_SHELL_CMD_PING_TIMEOUT);
1656 if (ret < 0) {
1657 perror("Ping: poll\n");
1658 goto FAILURE;
1659 } else if (ret == 0) {
1660 /* first type timeout event */
1661 timeout_flag = true;
1662 break;
1663 }
1664
1665 ret = recv(sfd, pbuf_resp->payload, pbuf_resp->len, MSG_DONTWAIT);
1666 if (ret < 0) {
1667 perror("Ping: recv echo reply failed\n");
1668 goto FAILURE;
1669 }
1670
1671 /* Accessing ip header and icmp header */
1672 iphdr_resp = pbuf_resp->payload;
1673
1674 ip_hlen = (IPH_HL(iphdr_resp) << 2);
1675 if (pbuf_header(pbuf_resp, -ip_hlen)) {
1676 /* this failure will never happen, but failure handle is written just to be in safe side */
1677 PRINTK("Ping : memory management failure\n");
1678 goto FAILURE;
1679 }
1680 iecho_resp = (struct icmp_echo_hdr *)pbuf_resp->payload;
1681 /* Reverting back pbuf to its original state */
1682 if (pbuf_header(pbuf_resp, ip_hlen)) {
1683 /* this failure will never happen, but failure handle is written just to be in safe side */
1684 PRINTK("ping : memory management failure\n");
1685 goto FAILURE;
1686 }
1687
1688 if ((iphdr_resp->src.addr != to.sin_addr.s_addr) ||
1689 ((ICMPH_TYPE(iecho_resp) == ICMP_ECHO) && (iphdr_resp->src.addr == to.sin_addr.s_addr))) {
1690 /* second type timeout event */
1691 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &end);
1692 timout_ms = ((end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000);
1693 timout_ms = LWIP_SHELL_CMD_PING_TIMEOUT - timout_ms;
1694 } else {
1695 timout_ms = 0;
1696 break;
1697 }
1698 } while (timout_ms >= 0);
1699
1700 /* all timeout events are true timeout */
1701 if ((timout_ms < 0) || (timeout_flag == true)) {
1702 failed_cnt++;
1703 i++;
1704 PRINTK("\nPing: destination unreachable ...");
1705 continue;
1706 }
1707 /* capture the end time to calculate round trip time */
1708 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &end);
1709 rtt = ((end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000);
1710
1711 if (iphdr_resp->src.addr == to.sin_addr.s_addr) {
1712 switch (ICMPH_TYPE(iecho_resp)) {
1713 case ICMP_ER:
1714 PRINTK("\n[%u]Reply from %s: ", i, inet_ntop(AF_INET, &to.sin_addr, buf, sizeof(buf)));
1715 if (rtt > 0) {
1716 PRINTK("time=%ims ", rtt);
1717 } else {
1718 PRINTK("time<1ms ");
1719 }
1720 PRINTK("TTL=%u", iphdr_resp->_ttl);
1721
1722 /* delay 1s for every successful ping */
1723 intrvl = interval;
1724 do {
1725 if (intrvl < 1000) {
1726 sys_msleep(intrvl);
1727 break;
1728 }
1729 intrvl -= 1000;
1730 sys_msleep(1000);
1731 if (ping_kill == 1)
1732 break;
1733 } while (intrvl > 0);
1734 succ_cnt++;
1735 break;
1736 case ICMP_DUR:
1737 PRINTK("\nPing: destination host unreachable ...");
1738 break;
1739 case ICMP_SQ:
1740 PRINTK("\nPing: source quench ...");
1741 break;
1742 case ICMP_RD:
1743 PRINTK("\nPing: redirect ...");
1744 break;
1745 case ICMP_TE:
1746 PRINTK("\nPing: time exceeded ...");
1747 break;
1748 case ICMP_PP:
1749 PRINTK("\nPing: parameter problem ...");
1750 break;
1751 default :
1752 PRINTK("\nPing: unknown error ...");
1753 break;
1754 }
1755 i++;
1756 }
1757 }
1758
1759 PRINTK("\n--- %s ping statistics ---\n", inet_ntop(AF_INET, &to.sin_addr, buf, sizeof(buf)));
1760 PRINTK("%u packets transmitted, %u received, %u loss\n", i, succ_cnt, failed_cnt);
1761
1762FAILURE:
1763 ping_kill = 0;
1764 (void)lwip_close(sfd);
1765 if (pbuf_resp != NULL) {
1766 (void)pbuf_free(pbuf_resp);
1767 }
1768 if (iecho != NULL) {
1769 mem_free(iecho);
1770 }
1771 return ret;
1772}
1773
1774static void ping_cmd(unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3)
1775{
1776 u32_t destip = p0;
1777 u32_t count = p1;
1778 u32_t interval = p2;
1779 u32_t data_len = p3;
1780 int ret;
1781
1782 ret = osPingFunc(destip, count, interval, data_len);
1783 if (ret < 0) {
1784 PRINTK("Ping cmd failed due to some errors\n");
1785 }
1786
1787 ping_taskid = -1;
1788}
1789
1790u32_t osShellPing(int argc, const char **argv)
1791{
1792 int ret;
1793 u32_t i = 0;
1794 u32_t count = 0;
1795 int count_set = 0;
1796 u32_t interval = 1000; /* default ping interval */
1797 u32_t data_len = 48; /* default data length */
1798 ip4_addr_t dst_ipaddr;
1799 TSK_INIT_PARAM_S stPingTask;
1800
1801 if (!tcpip_init_finish) {
1802 PRINTK("Ping: tcpip_init have not been called\n");
1803 return LOS_NOK;
1804 }
1805
1806 if ((argc < 1) || (argv == NULL)) {
1807 PRINTK("Ping: require dest ipaddr at least\n");
1808 goto ping_error;
1809 }
1810
1811 /* could add more param support */
1812 while (argc > 0) {
1813 if (strcmp("-n", argv[i]) == 0 && (argc > 1)) {
1814 ret = atoi(argv[i + 1]);
1815 if (ret <= 0) {
1816 PRINTK("Ping count should be greater than 0 \n");
1817 goto ping_error;
1818 }
1819 count = ret;
1820 count_set = 1;
1821 i += 2;
1822 argc -= 2;
1823 } else if (strcmp("-t", argv[i]) == 0) {
1824 count = 0; /* ping forerver */
1825 count_set = 1;
1826 i++;
1827 argc--;
1828 } else if (strcmp("-w", argv[i]) == 0 && (argc > 1)) {
1829 ret = atoi(argv[i + 1]);
1830 if (ret <= 0) {
1831 PRINTK("Ping interval should be greater than 0 \n");
1832 goto ping_error;
1833 }
1834
1835 interval = ret;
1836 i += 2;
1837 argc -= 2;
1838 } else if (strcmp("-l", argv[i]) == 0 && (argc > 1)) {
1839 ret = atoi(argv[i + 1]);
1840 if (ret < 0 || ret > (int)(LWIP_MAX_UDP_RAW_SEND_SIZE - sizeof(struct icmp_echo_hdr))) {
1841 PRINTK("Ping data length error, should be in range of [0, %d] \n",
1842 LWIP_MAX_UDP_RAW_SEND_SIZE - sizeof(struct icmp_echo_hdr));
1843 goto ping_error;
1844 }
1845 data_len = ret;
1846 i += 2;
1847 argc -= 2;
1848 } else if (strcmp("-k", argv[i]) == 0) {
1849 if (ping_taskid > 0) {
1850 ping_kill = 1; /* stop the current ping task */
1851 return LOS_OK;
1852 } else {
1853 PRINTK("No ping task running...\n");
1854 return LOS_NOK;
1855 }
1856 } else {
1857 if (argc == 1) {
1858 break;
1859 } else {
1860 PRINTK("Invalid Ping param\n");
1861 goto ping_error;
1862 }
1863 }
1864 }
1865
1866 if (!count_set) {
1867 count = LWIP_SHELL_CMD_PING_RETRY_TIMES;
1868 }
1869
1870 /* initialize dst IP address */
1871 if (argc <= 0) {
1872 goto ping_error;
1873 }
1874#if LWIP_DNS
1875 dst_ipaddr.addr = get_hostip(argv[i]);
1876#else /* LWIP_DNS */
1877 dst_ipaddr.addr = inet_addr(argv[i]);
1878#endif /* LWIP_DNS */
1879
1880 if (dst_ipaddr.addr == IPADDR_NONE || dst_ipaddr.addr == IPADDR_ANY) {
1881 PRINTK("Invalid dest ipaddr: NONE or ANY\n");
1882 return LOS_NOK;
1883 }
1884
1885 /* start one task if ping forever or ping count greater than 60 */
1886 if (count == 0 || count > LWIP_SHELL_CMD_PING_RETRY_TIMES) {
1887 if (ping_taskid > 0) {
1888 PRINTK("Ping task already running and only support one now\n");
1889 return LOS_NOK;
1890 }
1891 stPingTask.pfnTaskEntry = (TSK_ENTRY_FUNC)ping_cmd;
1892 stPingTask.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
1893 stPingTask.pcName = "ping_task";
1894 stPingTask.usTaskPrio = 8; /* higher than shell */
1895 stPingTask.uwResved = LOS_TASK_STATUS_DETACHED;
1896 stPingTask.auwArgs[0] = dst_ipaddr.addr; /* network order */
1897 stPingTask.auwArgs[1] = count;
1898 stPingTask.auwArgs[2] = interval;
1899 stPingTask.auwArgs[3] = data_len;
1900 ret = LOS_TaskCreate((UINT32 *)(&ping_taskid), &stPingTask);
1901 if (ret != LOS_OK) {
1902 PRINTK("ping_task create failed 0x%08x.\n", ret);
1903 count = LWIP_SHELL_CMD_PING_RETRY_TIMES;
1904 } else {
1905 return LOS_OK;
1906 }
1907 }
1908
1909 /* two cases:
1910 1, ping cout less than LWIP_SHELL_CMD_PING_RETRY_TIMES;
1911 2, ping task create failed;
1912 */
1913 if (osPingFunc(dst_ipaddr.addr, count, interval, data_len) < 0) {
1914 PRINTK("Ping cmd failed due some errors\n");
1915 }
1916
1917 return LOS_OK;
1918ping_error:
1920 return LOS_NOK;
1921}
1922#ifdef LOSCFG_SHELL
1924#endif /* LOSCFG_SHELL */
1925
1926#else /* LWIP_EXT_POLL_SUPPORT*/
1927
1928u32_t osShellPing(int argc, const char **argv)
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}
2074
2075#ifdef LOSCFG_SHELL
2076SHELLCMD_ENTRY(ping_shellcmd, CMD_TYPE_EX, "ping", XARGS, (CmdCallBackFunc)osShellPing);
2077#endif /* LOSCFG_SHELL */
2078
2079#endif /* LWIP_EXT_POLL_SUPPORT*/
2080
2081#if LWIP_IPV6
2082u32_t osShellPing6(int argc, const char **argv)
2083{
2084 u16_t icmpv6_id;
2085 u16_t icmpv6_seq;
2086 u32_t nsent;
2087 u32_t nrecieve;
2088 u32_t last_received;
2089 struct timespec start, end, start_in_reply;
2090 struct timespec first, last;
2091 long rtt;
2092 int ret;
2093 fd_set fdReadSet;
2094 void *param = NULL;
2095 ping6_args_t ping6_params;
2097 struct sockaddr_in6 to;
2098 struct icmp6_echo_hdr *iecho_resp = NULL;
2099 struct icmp6_echo_hdr *iecho = NULL;
2100 struct timeval stTimeVal;
2101 struct timeval deltaTimeVal;
2102 struct pbuf *pbuf_resp = NULL;
2103 struct pbuf *pbuf_req = NULL;
2104 int sfd = -1;
2105 u8_t type;
2106 u8_t select_on_socket = 0;
2107 char buf[INET6_ADDRSTRLEN];
2108
2109 if (!tcpip_init_finish) {
2110 PRINTK("%s: tcpip_init have not been called\n", __FUNCTION__);
2111 ret = -1;
2112 goto exit;
2113 }
2114
2115 if ((argc < 1) || (argc > LWIP_MAX_PING6_ARG_COUNT) || (argv == NULL)) {
2116 goto usage;
2117 }
2118
2119 ret = parse_args_ping6(argc, argv, &ping6_params);
2120 if (ret == -1) {
2121 goto exit;
2122 }
2123
2124 if (ping6_params.args_found & LWIP_PING6_SOURCE_ADDRESS_ARG) {
2125 type = LWIP_PING6_SOURCE_ADDRESS_ARG;
2126 param = (void *)(&ping6_params.src_addr);
2127 } else if (ping6_params.args_found & LWIP_PING6_INTERFACE_ARG) {
2128 type = LWIP_PING6_INTERFACE_ARG;
2129 param = (void *)(argv[ping6_params.interface_index]);
2130 } else {
2131 type = LWIP_PING6_DEFAULT_SOCKET;
2132 param = NULL;
2133 }
2134
2135 /* Create a socket for sending and receiving pings with appropriate bindings */
2136 sfd = create_ping6_socket(type, param);
2137 if (sfd == -1) {
2138 ret = -1;
2139 goto exit;
2140 }
2141
2142 pbuf_req = pbuf_alloc(PBUF_RAW, LWIP_PING6_STANDARD_PKT_SIZE, PBUF_RAM);
2143 pbuf_resp = pbuf_alloc(PBUF_RAW, LWIP_PING6_STANDARD_PKT_SIZE, PBUF_RAM);
2144 if ((pbuf_resp == NULL) || (pbuf_req == NULL)) {
2145 PRINTK("ping6 : Memory Allocation Failed\n");
2146 ret = -1;
2147 goto exit;
2148 }
2149
2150 to.sin6_family = AF_INET6;
2151 inet6_addr_from_ip6addr(&to.sin6_addr, &(ping6_params.dst_addr));
2152 to.sin6_port = htons(IPPROTO_ICMPV6);
2153
2154#if LWIP_DNS
2155 if (lwip_strnicmp(inet_ntop(AF_INET6, &to.sin6_addr, buf, sizeof(buf)), argv[ping6_params.host_index],
2156 sizeof(to.sin6_addr))) {
2157 /* If There Was A DNS Resolution */
2158 PRINTK("PING %s (%s) with %d bytes of data.\n",
2159 argv[ping6_params.host_index], buf, LWIP_PING6_STANDARD_PKT_SIZE);
2160 } else {
2161 PRINTK("PING %s with %d bytes of data.\n", buf, LWIP_PING6_STANDARD_PKT_SIZE);
2162 }
2163#else
2164 PRINTK("PING %s with %d bytes of data\n", inet_ntop(AF_INET6, &to.sin6_addr, buf, sizeof(buf)), LWIP_PING6_STANDARD_PKT_SIZE);
2165#endif /* LWIP_DNS */
2166
2167 nrecieve = 0;
2168 ping6_stats.flag = 0;
2169 ping6_stats.avg_rtt = 0;
2170 ping6_stats.max_rtt = 0;
2171 ping6_stats.min_rtt = 0;
2172 last_received = LWIP_PING6_STARTING_SEQ_NUM + LWIP_PING6_OUT_OF_ORDER_MAGNITUDE + 1;
2173 icmpv6_id = (u16_t)LWIP_RAND();
2174 icmpv6_seq = LWIP_PING6_STARTING_SEQ_NUM;
2175 /* Setting the start time of the entire ping task for statistics */
2176 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &first);
2177
2178 nsent = 0;
2179
2180 for (nsent = 0; nsent < ping6_params.pingcount; nsent++) {
2181 /* capture the start tick to calculate rtt */
2182 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &start);
2183 /* Initialize Memory To Prevent Unintialized Memory Read Write Issues */
2184 if (memset_s(pbuf_req->payload, pbuf_req->len, (int)(start.tv_nsec), pbuf_req->len) != 0) {
2185 goto exit;
2186 }
2187
2188 iecho = (struct icmp6_echo_hdr *)pbuf_req->payload;
2189 iecho->type = ICMP6_TYPE_EREQ;
2190 iecho->id = icmpv6_id;
2191 icmpv6_seq++;
2192 iecho->seqno = icmpv6_seq;
2193 iecho->code = 0;
2194
2195 /* Embedding the start_tick as data into the icmp_payload */
2196 (void)pbuf_header(pbuf_req, (s16_t)(-(s16_t)(sizeof(struct icmp6_echo_hdr))));
2197 if (memcpy_s(pbuf_req->payload, pbuf_req->len, (void *)&start, sizeof(start)) != 0) {
2198 goto exit;
2199 }
2200 (void)pbuf_header(pbuf_req, (s16_t)((s16_t)(sizeof(struct icmp6_echo_hdr))));
2201
2202 ret = lwip_sendto(sfd, iecho, pbuf_req->len, 0, (struct sockaddr *)&to, (socklen_t)sizeof(to));
2203 if (ret == -1) {
2204 PRINTK("ping6 : Sending ICMP Echo message failed\n");
2205 goto exit;
2206 }
2207
2208 /* Wait in select for ICMP response msg */
2209 FD_ZERO(&fdReadSet);
2210 FD_SET(sfd, &fdReadSet);
2211 stTimeVal.tv_sec = LWIP_MSECS_TO_SECS(LWIP_SHELL_CMD_PING_TIMEOUT);
2212 stTimeVal.tv_usec = 0;
2213 select_on_socket = 1;
2214
2215 while (select_on_socket) {
2216 select_on_socket = 0;
2217 ret = select(sfd + 1, &fdReadSet, 0, 0, &stTimeVal);
2218 if (ret < 0) {
2219 PRINTK("ping6 : select failure\n");
2220 goto exit;
2221 } else if (ret == 0) {
2222 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &end);
2223 PRINTK("Request timed out\n");
2224 continue;
2225 }
2226
2227 /* capture the end time to calculate round trip time */
2228 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &end);
2229
2230 ret = lwip_recv(sfd, pbuf_resp->payload, pbuf_resp->len, 0);
2231 if (ret == -1) {
2232 PRINTK("ping6 : receiving ICMP echo response msg failed\n");
2233 goto exit;
2234 }
2235
2236 if ((u32_t)ret < sizeof(struct icmp6_echo_hdr *)) {
2237 /* Drop the packet if its too short [Doesnot contain even the header !!] */
2238 PRINTK("ping6 : received ICMP echo response too short\n");
2239 goto REDUCE_SELECT_TIME;
2240 }
2241
2242 /* Acceping the ICMPv6 payload. */
2243 /* Here, pbuf_resp->payload wont contain IPv6 Header since its an AF_INET6 RAW Socket */
2244 iecho_resp = (struct icmp6_echo_hdr *)pbuf_resp->payload;
2245
2246 if (iecho_resp->id == icmpv6_id) {
2247 if (iecho_resp->type == ICMP6_TYPE_EREP) {
2248 if (ret < LWIP_PING6_STANDARD_PKT_SIZE) {
2249 /* Drop the packet if its too short */
2250 PRINTK("ping6 : received ICMP echo response too short\n");
2251 goto REDUCE_SELECT_TIME;
2252 }
2253 /* Accept and process only those delayed EREP only if its sequence num is within out-of-order magnitude */
2254 if (nsent && iecho_resp->seqno != icmpv6_seq &&
2255 (iecho_resp->seqno<(u16_t)(last_received - LWIP_PING6_OUT_OF_ORDER_MAGNITUDE) ||
2256 iecho_resp->seqno>(u16_t)(
2257 last_received + LWIP_PING6_OUT_OF_ORDER_MAGNITUDE))) {
2258 /* Otherwise drop it and wait for more packets */
2259 goto REDUCE_SELECT_TIME;
2260 }
2261 ++nrecieve;
2262 last_received = iecho_resp->seqno;
2263 /* Retrieving the start_tick from the packet which was embedded when the request was transmitted */
2264 (void)pbuf_header(pbuf_resp, (s16_t)(-(s16_t)(sizeof(struct icmp6_echo_hdr))));
2265 if (memcpy_s((void *)&start_in_reply, sizeof(start_in_reply),
2266 pbuf_resp->payload, sizeof(start_in_reply)) != EOK) {
2267 goto REDUCE_SELECT_TIME;
2268 }
2269 (void)pbuf_header(pbuf_resp, (s16_t)((s16_t)(sizeof(struct icmp6_echo_hdr))));
2270
2271 rtt = ((end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000);
2272
2273 PRINTK("%d bytes from %s : icmp_seq=%d time", ret,
2274 inet_ntop(AF_INET6, &to.sin6_addr, buf, sizeof(buf)),
2275 ((iecho_resp->seqno) - LWIP_PING6_STARTING_SEQ_NUM));
2276 if (rtt < 1) {
2277 PRINTK("<1 ms\n");
2278 } else {
2279 PRINTK("=%i ms\n", rtt);
2280 }
2281
2282 update_ping6_stats(&ping6_stats, (u32_t)(rtt), nrecieve);
2283
2284 /* Checking if its a delayed packet */
2285 if ((iecho_resp->seqno != icmpv6_seq) && (nsent < ping6_params.pingcount)) {
2286 /* Incase of delayed packet wait on socket for other response before sending a new PING */
2287 /* We have to reduce the timeout value now when selecting on socket */
2288 goto REDUCE_SELECT_TIME;
2289 }
2290 } else {
2291 PRINTK("[%u]ping6 : %s\n", nsent, convert_icmpv6_err_to_string(iecho_resp->type));
2292 }
2293 } else {
2294 /* If incoming packet does not matches with icmp_id, it should be ignored */
2295 /* Reduce the timeout for select on socket */
2296REDUCE_SELECT_TIME:
2297 deltaTimeVal.tv_sec = (long)(end.tv_sec - start.tv_sec);
2298 deltaTimeVal.tv_usec = (long)((end.tv_nsec - start.tv_nsec) / 1000);
2299 /* Subtract deltaTime from stTime and store in stTime */
2300 /* This will reduce the select time on the socket */
2301 timersub(&stTimeVal, &deltaTimeVal, &stTimeVal);
2302 select_on_socket = 1;
2303 }
2304 }
2305 }
2306
2307 ret = ERR_OK;
2308
2309 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &last);
2310 /* Display ping stats */
2311 PRINTK("--- %s ping statistics ---\n", argv[ping6_params.host_index]);
2312 PRINTK("%d packets transmitted, %d received, %.2f%% packet loss, time %dms\n",
2313 nsent, nrecieve, (float)(((float)(nsent - nrecieve)) * ((float)(100)) / ((float)(nsent))),
2314 ((last.tv_sec - first.tv_sec) * 1000 + (last.tv_nsec - first.tv_nsec) / 1000000));
2315 if (nrecieve) {
2316 /* Display rtt stats only if atleast one packet is recieved */
2317 PRINTK("rtt min/avg/max = %u/%.2f/%u ms\n", ping6_stats.min_rtt, ping6_stats.avg_rtt, ping6_stats.max_rtt);
2318 }
2319
2320exit:
2321 if (sfd != -1) {
2322 (void)lwip_close(sfd);
2323 }
2324
2325 if (pbuf_resp != NULL) {
2326 (void)pbuf_free(pbuf_resp);
2327 }
2328
2329 if (pbuf_req != NULL) {
2330 (void)pbuf_free(pbuf_req);
2331 }
2332 return (u32_t)((ret == (int)ERR_OK) ? LOS_OK : LOS_NOK);
2333
2334usage:
2335 PRINTK("Usage:\n");
2336 PRINTK("\tping6 [-c count] [-I interface/sourceAddress] destination\n");
2337 return LOS_NOK;
2338}
2339
2340LWIP_STATIC int create_ping6_socket(u8_t type, const void *param)
2341{
2342 int sfd;
2343 int ret;
2344 struct sockaddr_in6 stHostAddr6;
2345 struct icmp6_filter icmp6_sock_filter;
2346
2347 sfd = lwip_socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
2348 if (sfd == -1) {
2349 PRINTK("ping6 : Failed, socket creation failed\n");
2350 return -1;
2351 }
2352
2353 if (param != NULL) {
2354 if (type == LWIP_PING6_SOURCE_ADDRESS_ARG) {
2355 /* Binding socket to the provided source address */
2356 (void)memset_s(&stHostAddr6, sizeof(stHostAddr6), 0, sizeof(stHostAddr6));
2357 stHostAddr6.sin6_family = AF_INET6;
2358 inet6_addr_from_ip6addr(&stHostAddr6.sin6_addr, (ip6_addr_t *)param);
2359 stHostAddr6.sin6_port = htons(IPPROTO_ICMPV6);
2360 stHostAddr6.sin6_scope_id = 0;
2361
2362 ret = lwip_bind(sfd, (struct sockaddr *)&stHostAddr6, sizeof(stHostAddr6));
2363 if (ret == -1) {
2364 (void)lwip_close(sfd);
2365 PRINTK("ping6 : bind icmp socket: cannot assign requested address\n");
2366 return ret;
2367 }
2368 } else if (type == LWIP_PING6_INTERFACE_ARG) {
2369 /* Binding socket to the provided netif */
2370 ret = lwip_setsockopt(sfd, SOL_SOCKET, SO_BINDTODEVICE, (char *)(param), strlen((char *)(param)));
2371 if (ret == -1) {
2372 (void)lwip_close(sfd);
2373 PRINTK("ping6: unknownn iface %s\n", (char *)(param));
2374 return ret;
2375 }
2376 }
2377 }
2378
2379 /* Setting socket filter since we are interested only in ECHO REPLY and ERROR messages */
2380 ICMP6_FILTER_SETBLOCKALL (&icmp6_sock_filter);
2381 ICMP6_FILTER_SETPASS (ICMP6_TYPE_EREP, &icmp6_sock_filter);
2382 ICMP6_FILTER_SETPASS (ICMP6_TYPE_DUR, &icmp6_sock_filter);
2383 ICMP6_FILTER_SETPASS (ICMP6_TYPE_PTB, &icmp6_sock_filter);
2384 ICMP6_FILTER_SETPASS (ICMP6_TYPE_TE, &icmp6_sock_filter);
2385
2386 ret = lwip_setsockopt(sfd, IPPROTO_ICMPV6, ICMP6_FILTER, &icmp6_sock_filter, sizeof(struct icmp6_filter));
2387 if (ret == -1) {
2388 (void)lwip_close(sfd);
2389 PRINTK("ping6 : setsockopt: Invalid Argument\n");
2390 return -1;
2391 }
2392
2393 return sfd;
2394}
2395
2396/*
2397 * Function to parse the command line args for ping6 shell utility
2398 * @return:
2399 * Success: ERR_OK
2400 * Failure: -1
2401 */
2402LWIP_STATIC int parse_args_ping6(int argc, const char **argv, ping6_args_t *ping6_params)
2403{
2404 int pingcount;
2405 int ret = -1;
2406#if LWIP_DNS
2407 struct addrinfo *res = NULL;
2408 struct addrinfo hints_structure;
2409#endif
2410 u8_t i = 0;
2411
2412 IP6_ADDR(&(ping6_params->dst_addr), 0, 0, 0, 0);
2413 IP6_ADDR(&(ping6_params->src_addr), 0, 0, 0, 0);
2414
2415 ping6_params->pingcount = LWIP_SHELL_CMD_PING_RETRY_TIMES;
2416 ping6_params->host_index = 0;
2417 ping6_params->args_found = 0;
2418
2419 while (i < argc) {
2420 if (strcmp("-c", argv[i]) == 0) {
2421 /* Handle number of ICMP packets to transmit :: -c [number_of_packets] */
2422 if (ping6_params->args_found & LWIP_PING6_COUNT_ARG) {
2423 PRINTK("ping6: -c option present multiple times \n");
2424 ret = -1;
2425 goto exit;
2426 }
2427
2428 if (i + 2 > argc) {
2429 PRINTK("ping6: ping count(-c) should require an argument \n");
2430 ret = -1;
2431 goto exit;
2432 }
2433
2434 pingcount = atoi(argv[i + 1]);
2435 if (pingcount <= 0) {
2436 PRINTK("ping6: bad number of packets to transmit \n");
2437 ret = -1;
2438 goto exit;
2439 }
2440
2441 ping6_params->args_found |= LWIP_PING6_COUNT_ARG;
2442 ping6_params->pingcount = (u32_t)pingcount;
2443 i = (u8_t)(i + 2);
2444 } else if (strcmp("-I", argv[i]) == 0) {
2445 /* Handle interface ID / sourceAddress using which the ICMP Packets has to be transmitted :: -I [interface_id/source_address] */
2446
2447 if ((ping6_params->args_found & LWIP_PING6_SOURCE_ADDRESS_ARG) ||
2448 (ping6_params->args_found & LWIP_PING6_INTERFACE_ARG)) {
2449 PRINTK("ping6: -I option present multiple times \n");
2450 ret = -1;
2451 goto exit;
2452 }
2453
2454 if (i + 2 > argc) {
2455 PRINTK("ping6: interface/source address(-I) should require an argument \n");
2456 ret = -1;
2457 goto exit;
2458 }
2459
2460 /* Check whether the given argument to -I is source address */
2461 if (ip6addr_aton(argv[i + 1], &(ping6_params->src_addr))) {
2462 ping6_params->args_found |= LWIP_PING6_SOURCE_ADDRESS_ARG;
2463 i = (u8_t)(i + 2);
2464 continue;
2465 }
2466
2467 /* Storing the index where interface name is found */
2468 /* If this name is not valid, then it will fail later in setsockopt(BIND_TO_DEVICE) */
2469 ping6_params->interface_index = (u8_t)(i + 1);
2470 ping6_params->args_found |= LWIP_PING6_INTERFACE_ARG;
2471
2472 i = (u8_t)(i + 2);
2473 } else {
2474 if (argv[i][0] == '-') {
2475 /* Check whether its a bad option */
2476 PRINTK("ping6: bad option %s\n", argv[i]);
2477 ret = -1;
2478 goto exit;
2479 } else if (ping6_params->args_found & LWIP_PING6_HOSTNAME_ARG) {
2480 /* Check whether hostname is already found and there are extra arguments */
2481 PRINTK("ping6: bad parameter %s\n", argv[i]);
2482 ret = -1;
2483 goto exit;
2484 }
2485
2486#if LWIP_DNS
2487 /* Resolve the given hostname */
2488 hints_structure.ai_family = AF_INET6;
2489 hints_structure.ai_flags = 0;
2490 ret = lwip_getaddrinfo(argv[i], NULL, &hints_structure, &res);
2491 if (ret != ERR_OK) {
2492 PRINTK("ping6 : Host : %s can't be resolved to IPv6 address\n", argv[i]);
2493 ret = -1;
2494 goto exit;
2495 }
2496
2497 inet6_addr_to_ip6addr(&(ping6_params->dst_addr),
2498 &(((const struct sockaddr_in6 *)(res->ai_addr))->sin6_addr));
2499#else
2500 /* Convert the string representation to network form */
2501 if (!ip6addr_aton(argv[i], &(ping6_params->dst_addr))) {
2502 PRINTK("ping6 : Invalid IPv6 Address : %s\n", argv[i]);
2503 ret = -1;
2504 goto exit;
2505 }
2506#endif /* LWIP_DNS */
2507 if (ip6_addr_isany(&(ping6_params->dst_addr)) || ip6_addr_isnone(&(ping6_params->dst_addr))) {
2508#if LWIP_DNS
2509 PRINTK("ping6 : IPv6 address of host : %s (%s) is invalid\n", argv[i],
2510 ip6addr_ntoa((const ip6_addr_t *)&(ping6_params->dst_addr)));
2511#else
2512 PRINTK("ping6 : Invalid IPv6 address : %s\n", argv[i]);
2513#endif /* LWIP_DNS */
2514 goto exit;
2515 }
2516 /* Setting host_index to the index of argv[] where the host/IP is present */
2517 ping6_params->args_found |= LWIP_PING6_HOSTNAME_ARG;
2518 ping6_params->host_index = (u8_t)(i);
2519 i = (u8_t)(i + 1);
2520 }
2521 }
2522
2523 if (!(ping6_params->args_found & LWIP_PING6_HOSTNAME_ARG)) {
2524 /* Hostname/IPv6 address not found */
2525 PRINTK("ping6 : Hostname/IPv6 address to ping is not specified\n");
2526 ret = -1;
2527 goto exit;
2528 }
2529
2530 if (ip6_addr_islinklocal(&(ping6_params->dst_addr)) &&
2531 !(ping6_params->args_found & (LWIP_PING6_INTERFACE_ARG | LWIP_PING6_SOURCE_ADDRESS_ARG))) {
2532 /* For link-local addresses, -I is mandatory */
2533 PRINTK("ping6 : Interface specification is mandatory for link-local addresses\n");
2534 ret = -1;
2535 goto exit;
2536 }
2537
2538 ret = ERR_OK;
2539
2540exit:
2541#if LWIP_DNS
2542 lwip_freeaddrinfo(res);
2543#endif /* LWIP_DNS */
2544
2545 return ret;
2546}
2547
2548/*
2549 * Function to update ping6_stats
2550 * stats is maintained in ping6_stats structure
2551 */
2552LWIP_STATIC void update_ping6_stats(ping6_stats_t *ping6_stats, u32_t rtt, u32_t nreceived)
2553{
2554 if (rtt > ping6_stats->max_rtt) {
2555 ping6_stats->max_rtt = rtt;
2556 }
2557
2558 if (ping6_stats->flag == 0 || rtt < ping6_stats->min_rtt) {
2559 ping6_stats->min_rtt = rtt;
2560 ping6_stats->flag = 1;
2561 }
2562
2564 (float)((float)((float)rtt - ping6_stats->avg_rtt) / (float)(nreceived)));
2565}
2566
2567LWIP_STATIC const char *convert_icmpv6_err_to_string(u8_t err_type)
2568{
2569 switch (err_type) {
2570 case ICMP6_TYPE_DUR:
2571 return "Destination Unreachable";
2572 case ICMP6_TYPE_PTB:
2573 return "Packet too big";
2574 case ICMP6_TYPE_TE:
2575 return "Time Exceeded";
2576 case ICMP6_TYPE_PP:
2577 return "Parameter Problem";
2578 default:
2579 break;
2580 }
2581 return "Unknown Error";
2582}
2583
2584#ifdef LOSCFG_SHELL_CMD_DEBUG
2585SHELLCMD_ENTRY(ping6_shellcmd, CMD_TYPE_EX, "ping6", XARGS, (CmdCallBackFunc)osShellPing6);
2586#endif /* LOSCFG_SHELL_CMD_DEBUG */
2587#endif /* LWIP_IPV6 */
2588
2589#if LWIP_SNTP
2590u32_t osShellNtpdate(int argc, const char **argv)
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}
2627
2628#ifdef LOSCFG_SHELL_CMD_DEBUG
2629SHELLCMD_ENTRY(ntpdate_shellcmd, CMD_TYPE_EX, "ntpdate", XARGS, (CmdCallBackFunc)osShellNtpdate);
2630#endif /* LOSCFG_SHELL_CMD_DEBUG */
2631
2632#endif /* LWIP_SNTP*/
2633
2634#if LWIP_DNS
2635u32_t osShellDns(int argc, const char **argv)
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}
2701
2702#ifdef LOSCFG_SHELL_CMD_DEBUG
2704#endif /* LOSCFG_SHELL_CMD_DEBUG */
2705#endif /* LWIP_DNS */
2706#if LWIP_IPV6
2707extern struct nd6_neighbor_cache_entry neighbor_cache[LWIP_ND6_NUM_NEIGHBORS];
2708#endif
2709#if LWIP_IPV6
2710int netstat_get_udp_sendQLen6(struct udp_pcb *udppcb, struct pbuf *udpbuf)
2711{
2712 int sendLen = -1;
2713 u16_t offset = 0, len;
2714 struct ip6_hdr *iphdr = NULL;
2715 struct udp_hdr *udphdr = NULL;
2716 struct ip6_dest_hdr *dest_hdr = NULL;
2717 struct ip6_frag_hdr *frag_hdr = NULL;
2718 u8_t nexth;
2719 u16_t hlen = 0;
2720
2721 LWIP_ERROR("netstat_get_udp6_sendQLen: NULL pcb received\n", (udppcb != NULL), return -1);
2722 LWIP_ERROR("netstat_get_udp6_sendQLen: NULL pbuf received\n", (udpbuf != NULL), return -1);
2723
2724 iphdr = (struct ip6_hdr *)udpbuf->payload;
2725
2726 if (!(ip6_addr_cmp(&iphdr->dest, ip_2_ip6(&udppcb->remote_ip)) &&
2727 (ip_addr_isany(&udppcb->local_ip) ||
2728 ip6_addr_cmp(&iphdr->src, ip_2_ip6(&udppcb->local_ip))))) {
2729 goto FUNC_OUT;
2730 }
2731
2732 len = IP6_HLEN;
2733 if (pbuf_header(udpbuf, (s16_t)(-(s16_t)(len)))) {
2734 goto FUNC_OUT;
2735 }
2736
2737 offset = len;
2738
2739 nexth = IP6H_NEXTH(iphdr);
2740 while (offset < udpbuf->tot_len) {
2741 if (nexth == IP6_NEXTH_NONE || nexth == IP6_NEXTH_UDP || nexth == IP6_NEXTH_UDPLITE) {
2742 break;
2743 }
2744 switch (nexth) {
2745 case IP6_NEXTH_HOPBYHOP:
2746 case IP6_NEXTH_ROUTING:
2747 nexth = *((u8_t *)udpbuf->payload);
2748 hlen = (u16_t)(8 * (1 + *((u8_t *)udpbuf->payload + 1)));
2749 break;
2750 case IP6_NEXTH_DESTOPTS:
2751 nexth = *((u8_t *)udpbuf->payload);
2752 dest_hdr = (struct ip6_dest_hdr *)udpbuf->payload;
2753 hlen = (u16_t)(8 * (1 + dest_hdr->_hlen));
2754 break;
2755 case IP6_NEXTH_FRAGMENT:
2756 frag_hdr = (struct ip6_frag_hdr *)udpbuf->payload;
2757 nexth = frag_hdr->_nexth;
2758 hlen = IP6_FRAG_HLEN;
2759 break;
2760 default:
2761 /* Unknown next_header */
2762 goto FUNC_OUT;
2763 }
2764
2765 (void)pbuf_header(udpbuf, (s16_t)(-(s16_t)hlen));
2766 offset = (u16_t)(offset + hlen);
2767 }
2768
2769 /* If the while loop test condition failed , then revert the last offset change */
2770 if (offset >= udpbuf->tot_len) {
2771 offset = (u16_t)(offset - hlen);
2772 goto FUNC_OUT;
2773 }
2774
2775 LWIP_ERROR("Transport option should be UDP", (nexth == IP6_NEXTH_UDP || nexth == IP6_NEXTH_UDPLITE), goto FUNC_OUT);
2776
2777 if (offset > iphdr->_plen) {
2778 goto FUNC_OUT;
2779 }
2780
2781 /* check if there is enough space for atleast udp header available */
2782 if (udpbuf->tot_len < UDP_HLEN) {
2783 goto FUNC_OUT;
2784 }
2785
2786 udphdr = (struct udp_hdr *)udpbuf->payload;
2787 if ((ntohs(udphdr->dest) == udppcb->remote_port) && (ntohs(udphdr->src) == udppcb->local_port)) {
2788 if (ntohs(udphdr->len) > UDP_HLEN) {
2789 sendLen = ntohs(udphdr->len) - UDP_HLEN;
2790 } else {
2791 sendLen = udpbuf->tot_len - UDP_HLEN;
2792 }
2793 }
2794
2795FUNC_OUT:
2796 (void)pbuf_header(udpbuf, (s16_t)offset); // can not cross max limit of s16_t
2797 return sendLen;
2798}
2799#endif
2800
2801#if LWIP_IPV4
2802int netstat_get_udp_sendQLen(struct udp_pcb *udppcb, struct pbuf *udpbuf)
2803{
2804 int sendLen = -1;
2805 u16_t offset = 0, len;
2806 struct ip_hdr *iphdr = NULL;
2807 struct udp_hdr *udphdr = NULL;
2808
2809 LWIP_ERROR("netstat_get_udp_sendQLen: NULL pcb received\n", (udppcb != NULL), return -1);
2810 LWIP_ERROR("netstat_get_udp_sendQLen: NULL pbuf received\n", (udpbuf != NULL), return -1);
2811
2812 iphdr = (struct ip_hdr *)udpbuf->payload;
2813
2814 if (!(ip4_addr_cmp(&iphdr->dest, ip_2_ip4(&udppcb->remote_ip)) &&
2815 (ip_addr_isany(&udppcb->local_ip) ||
2816 ip4_addr_cmp(&iphdr->src, ip_2_ip4(&udppcb->local_ip))))) {
2817 goto FUNC_OUT;
2818 }
2819#if LWIP_UDPLITE
2820 if ((IPH_PROTO(iphdr) != IP_PROTO_UDP) && (IPH_PROTO(iphdr) != IP_PROTO_UDPLITE)) {
2821#else
2822 if (IPH_PROTO(iphdr) != IP_PROTO_UDP) {
2823#endif
2824 goto FUNC_OUT;
2825 }
2826
2827 if ((ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK) != 0) {
2828 goto FUNC_OUT;
2829 }
2830
2831 len = (u16_t)(IPH_HL(iphdr) * 4);
2832 if (pbuf_header(udpbuf, (s16_t)(-len))) {
2833 goto FUNC_OUT;
2834 }
2835
2836 offset = (u16_t)(offset + len);
2837
2838 udphdr = (struct udp_hdr *)udpbuf->payload;
2839 if ((ntohs(udphdr->dest) == udppcb->remote_port) && (ntohs(udphdr->src) == udppcb->local_port)) {
2840 sendLen = ntohs(udphdr->len) - UDP_HLEN;
2841 }
2842
2843FUNC_OUT:
2844 (void)pbuf_header(udpbuf, (s16_t)offset);
2845 return sendLen;
2846}
2847#endif
2848
2849int netstat_tcp_recvq(struct tcp_pcb *tpcb)
2850{
2851 unsigned int retVal = 0;
2852#if LWIP_SO_RCVBUF
2853 struct netconn *conn = NULL;
2854#endif
2855
2856 LWIP_ERROR("netstat_tcp_recvq: Received NULL pcb\n", (tpcb != NULL), return 0);
2857
2858#if LWIP_SO_RCVBUF
2859 conn = (struct netconn *)tpcb->callback_arg;
2860 if (conn != NULL) {
2861 switch (conn->type) {
2862 case NETCONN_TCP:
2863 case NETCONN_RAW:
2864#if LWIP_IPV6
2865 case NETCONN_RAW_IPV6:
2866 case NETCONN_UDP_IPV6:
2867#endif
2868 case NETCONN_UDP:
2869 SYS_ARCH_GET(((unsigned int)conn->recv_avail /*+ conn->lrcv_left*/), retVal);
2870 break;
2871 default:
2872 retVal = 0; /* ur... very ugly, damn DHCP DNS and SNTP */
2873 }
2874 }
2875#endif
2876
2877 return (int)retVal;
2878}
2879
2880int netstat_tcp_sendq(struct tcp_pcb *tpcb)
2881{
2882 int retVal = 0;
2883 struct tcp_seg *useg = NULL;
2884
2885 LWIP_ERROR("netstat_tcp_sendq: Received NULL pcb\n", (tpcb != NULL), return 0);
2886
2887 for (useg = tpcb->unacked; useg != NULL; useg = useg->next) {
2888 retVal = retVal + useg->len;
2889 }
2890
2891 return retVal;
2892}
2893
2894#if LWIP_IPV6
2895int netstat_udp_sendq6(struct udp_pcb *upcb)
2896{
2897 int retLen = 0;
2898 int ret;
2899 int idx = 0;
2900 int i;
2901#if LWIP_ND6_QUEUEING
2902 struct nd6_q_entry *neibq = NULL;
2903#endif
2904
2905 LWIP_ERROR("netstat_udp_sendq6: Received NULL pcb\n", (upcb != NULL), return 0);
2906
2907 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
2908 if (neighbor_cache[i].state != ND6_NO_ENTRY) {
2909 if (ip6_addr_cmp(&upcb->remote_ip.u_addr.ip6, &neighbor_cache[i].next_hop_address)) {
2910 idx = i;
2911 break;
2912 }
2913 }
2914 }
2915#if LWIP_ND6_QUEUEING
2916 for (neibq = neighbor_cache[idx].q; neibq != NULL; neibq = neibq->next) {
2917 ret = netstat_get_udp_sendQLen6(upcb, neibq->p);
2918 if (ret >= 0) {
2919 retLen += ret;
2920 }
2921 }
2922#else
2923 ret = netstat_get_udp_sendQLen6(upcb, neighbor_cache[idx].q);
2924 if (ret >= 0) {
2925 retLen += ret;
2926 }
2927#endif
2928 return retLen;
2929}
2930#endif
2931
2932#if LWIP_IPV4
2933int netstat_udp_sendq(struct udp_pcb *upcb)
2934{
2935 int retLen = 0;
2936 int ret;
2937 int arpidx = -1;
2938 int i;
2939#if ARP_QUEUEING
2940 struct etharp_q_entry *arpq = NULL;
2941#endif
2942
2943 LWIP_ERROR("netstat_udp_sendq: Received NULL pcb\n", (upcb != NULL), return 0);
2944
2945 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
2946 if (arp_table[i].state != ETHARP_STATE_EMPTY) {
2947 if (ip4_addr_cmp(ip_2_ip4(&upcb->remote_ip), &arp_table[i].ipaddr)) {
2948 arpidx = i;
2949 break;
2950 }
2951 }
2952 }
2953
2954 if (arpidx >= 0) {
2955#if ARP_QUEUEING
2956 for (arpq = arp_table[arpidx].q; arpq != NULL; arpq = arpq->next) {
2957 ret = netstat_get_udp_sendQLen(upcb, arpq->p);
2958 if (ret > 0) {
2959 retLen += ret;
2960 if (retLen <= 0) { // overflow, set rteLen = -1 to indicate
2961 retLen = -1;
2962 break;
2963 }
2964 }
2965 }
2966#else
2967 ret = netstat_get_udp_sendQLen(upcb, arp_table[arpidx].q);
2968 if (ret > 0) {
2969 retLen += ret;
2970 if (retLen <= 0) { // overflow, set rteLen = -1 to indicate
2971 retLen = -1;
2972 }
2973 }
2974#endif
2975 }
2976 return retLen;
2977}
2978#endif
2979int netstat_netconn_recvq(const struct netconn *conn)
2980{
2981 unsigned int retVal = 0;
2982
2983#if LWIP_SO_RCVBUF
2984 if (conn == NULL) {
2985 return 0;
2986 }
2987
2988 switch (NETCONNTYPE_GROUP((unsigned int)(conn->type))) {
2989 case NETCONN_TCP:
2990 case NETCONN_RAW:
2991#if PF_PKT_SUPPORT
2992 case NETCONN_PKT_RAW:
2993#endif
2994 case NETCONN_UDP:
2995 SYS_ARCH_GET(((unsigned int)conn->recv_avail /*+ conn->lrcv_left*/), retVal);
2996 break;
2997 default:
2998 retVal = 0; /* ur... very ugly, damn DHCP DNS and SNTP */
2999 }
3000#endif
3001 return (int)retVal;
3002}
3003
3004int netstat_netconn_sendq(struct netconn *conn)
3005{
3006 int retVal = 0;
3007
3008 if (conn == NULL) {
3009 return 0;
3010 }
3011
3012 switch (NETCONNTYPE_GROUP((unsigned int)(conn->type))) {
3013 case NETCONN_TCP:
3014 retVal = netstat_tcp_sendq(conn->pcb.tcp);
3015 break;
3016 case NETCONN_RAW:
3017 retVal = 0;
3018 break;
3019#if PF_PKT_SUPPORT
3020 case NETCONN_PKT_RAW:
3021 retVal = 0; /* always be 0 as frame send to driver directly */
3022#endif
3023 break;
3024 case NETCONN_UDP:
3025 retVal = netstat_udp_sendq(conn->pcb.udp);
3026 break;
3027 default:
3028 retVal = 0; /* ur... very ugly, damn DHCP DNS and SNTP */
3029 }
3030
3031 return retVal;
3032}
3033void netstat_internal(void *ctx)
3034{
3035 s8_t local_ip_port[64] = {0};
3036 s8_t remote_ip_port[64] = {0};
3037 struct tcp_pcb *tpcb = NULL;
3038 struct tcp_pcb_listen *lpcb = NULL;
3039 struct udp_pcb *upcb = NULL;
3040 struct raw_pcb *rpcb = NULL;
3041 s8_t *entry_buf = NULL;
3042 u32_t entry_buf_len;
3043 u32_t entry_buf_offset;
3044 struct netstat_data *ndata = (struct netstat_data *)ctx;
3045 int iRet;
3046 int recvQlen = 0;
3047 int sendQlen = 0;
3048 u_int proto;
3049#if PF_PKT_SUPPORT
3050 u8_t netif_name[IFNAMSIZ];
3051 struct netif *netif = NULL;
3052#endif
3053
3054 if (ndata == NULL) {
3055 return;
3056 }
3057 entry_buf = ndata->netstat_out_buf;
3058 entry_buf_len = ndata->netstat_out_buf_len;
3059 entry_buf_offset = 0;
3060
3061 if (entry_buf == NULL) {
3062 goto out;
3063 }
3064 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3065 "========== total sockets %d ====== unused sockets %d ==========\n",
3066 LWIP_CONFIG_NUM_SOCKETS, get_unused_socket_num());
3067 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3068 goto out;
3069 }
3070 entry_buf_len -= (u32_t)(iRet);
3071 entry_buf_offset += (u32_t)(iRet);
3072
3073#if LWIP_TCP
3074 if (tcp_active_pcbs != NULL || tcp_bound_pcbs != NULL || tcp_tw_pcbs != NULL || tcp_listen_pcbs.pcbs != NULL) {
3075 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3076 "%-8s%-12s%-12s%-24s%-24s%-16s\n",
3077 "Proto", "Recv-Q", "Send-Q", "Local Address", "Foreign Address", "State");
3078 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3079 goto out;
3080 }
3081 entry_buf_len -= (u32_t)(iRet);
3082 entry_buf_offset += (u32_t)(iRet);
3083
3084 for (tpcb = tcp_active_pcbs; tpcb != NULL; tpcb = tpcb->next) {
3085 iRet = snprintf_s((char *)local_ip_port, sizeof(local_ip_port), (sizeof(local_ip_port) - 1),
3086 "%s:%d", ipaddr_ntoa(&tpcb->local_ip), tpcb->local_port);
3087 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(local_ip_port))) {
3088 goto out;
3089 }
3090
3091 iRet = snprintf_s((char *)remote_ip_port, sizeof(remote_ip_port), (sizeof(remote_ip_port) - 1),
3092 "%s:%d", ipaddr_ntoa(&tpcb->remote_ip), tpcb->remote_port);
3093 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(remote_ip_port))) {
3094 goto out;
3095 }
3096 if (tpcb->state == SYN_RCVD) {
3097 recvQlen = 0;
3098 sendQlen = 0;
3099 } else {
3100 recvQlen = netstat_netconn_recvq(tpcb->callback_arg);
3101 sendQlen = netstat_netconn_sendq(tpcb->callback_arg);
3102 }
3103 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3104 IP_IS_V6(&tpcb->local_ip) ? "%-8s%-12d%-12d%-39s%-39s%-16s\n" :
3105 "%-8s%-12d%-12d%-24s%-24s%-16s\n",
3106 IP_IS_V6(&tpcb->local_ip) ? "tcp-ip6" : "tcp",
3107 recvQlen, sendQlen, local_ip_port, remote_ip_port, tcp_state_str[tpcb->state]);
3108 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3109 goto out;
3110 }
3111 entry_buf_len -= (u32_t)(iRet);
3112 entry_buf_offset += (u32_t)(iRet);
3113 }
3114
3115 /* For bound PCBs */
3116 sendQlen = 0;
3117 recvQlen = 0;
3118
3119 for (tpcb = tcp_bound_pcbs; tpcb != NULL; tpcb = tpcb->next) {
3120 iRet = snprintf_s((char *)local_ip_port, sizeof(local_ip_port), (sizeof(local_ip_port) - 1),
3121 "%s:%d", ipaddr_ntoa(&tpcb->local_ip), tpcb->local_port);
3122 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(local_ip_port))) {
3123 goto out;
3124 }
3125
3126 iRet = snprintf_s((char *)remote_ip_port, sizeof(remote_ip_port), (sizeof(remote_ip_port) - 1),
3127 "%s:%d", ipaddr_ntoa(&tpcb->remote_ip), tpcb->remote_port);
3128 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(remote_ip_port))) {
3129 goto out;
3130 }
3131
3132 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3133 IP_IS_V6(&tpcb->local_ip) ? "%-8s%-12d%-12d%-39s%-39s%-16s\n" :
3134 "%-8s%-12d%-12d%-24s%-24s%-16s\n",
3135 IP_IS_V6(&tpcb->local_ip) ? "tcp-ip6" : "tcp",
3136 recvQlen, sendQlen, local_ip_port, remote_ip_port, tcp_state_str[tpcb->state]);
3137 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3138 goto out;
3139 }
3140 entry_buf_len -= (u32_t)(iRet);
3141 entry_buf_offset += (u32_t)(iRet);
3142 }
3143
3144 for (tpcb = tcp_tw_pcbs; tpcb != NULL; tpcb = tpcb->next) {
3145 iRet = snprintf_s((char *)local_ip_port, sizeof(local_ip_port), (sizeof(local_ip_port) - 1),
3146 "%s:%d", ipaddr_ntoa(&tpcb->local_ip), tpcb->local_port);
3147 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(local_ip_port))) {
3148 goto out;
3149 }
3150
3151 iRet = snprintf_s((char *)remote_ip_port, sizeof(remote_ip_port), (sizeof(remote_ip_port) - 1),
3152 "%s:%d", ipaddr_ntoa(&tpcb->remote_ip), tpcb->remote_port);
3153 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(remote_ip_port))) {
3154 goto out;
3155 }
3156
3157 recvQlen = netstat_netconn_recvq(tpcb->callback_arg);
3158 sendQlen = netstat_netconn_sendq(tpcb->callback_arg);
3159 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3160 IP_IS_V6(&tpcb->local_ip) ? "%-8s%-12d%-12d%-39s%-39s%-16s\n" :
3161 "%-8s%-12d%-12d%-24s%-24s%-16s\n",
3162 IP_IS_V6(&tpcb->local_ip) ? "tcp-ip6" : "tcp",
3163 recvQlen, sendQlen, local_ip_port, remote_ip_port, tcp_state_str[tpcb->state]);
3164 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3165 goto out;
3166 }
3167 entry_buf_len -= (u32_t)(iRet);
3168 entry_buf_offset += (u32_t)(iRet);
3169 }
3170
3171 /* For listen PCBs */
3172 recvQlen = 0;
3173 sendQlen = 0;
3174
3175 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
3176 iRet = snprintf_s((char *)local_ip_port, sizeof(local_ip_port), (sizeof(local_ip_port) - 1),
3177 "%s:%d", ipaddr_ntoa(&lpcb->local_ip), lpcb->local_port);
3178 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(local_ip_port))) {
3179 goto out;
3180 }
3181
3182 iRet = snprintf_s((char *)remote_ip_port, sizeof(remote_ip_port), (sizeof(remote_ip_port) - 1),
3183 "%s:%d", ipaddr_ntoa(&lpcb->remote_ip), 0);
3184 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(remote_ip_port))) {
3185 goto out;
3186 }
3187
3188 recvQlen = netstat_netconn_recvq(lpcb->callback_arg);
3189
3190 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3191 IP_IS_V6(&lpcb->local_ip) ? "%-8s%-12d%-12d%-39s%-39s%-16s\n" :
3192 "%-8s%-12d%-12d%-24s%-24s%-16s\n",
3193 IP_IS_V6(&lpcb->local_ip) ? "tcp-ip6" : "tcp",
3194 recvQlen, sendQlen, local_ip_port, remote_ip_port, tcp_state_str[lpcb->state]);
3195 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3196 goto out;
3197 }
3198 entry_buf_len -= (u32_t)(iRet);
3199 entry_buf_offset += (u32_t)(iRet);
3200 }
3201 }
3202#endif
3203#if LWIP_UDP
3204 if (udp_pcbs != NULL) {
3205 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3206 "\n%-8s%-12s%-12s%-24s%-24s\n",
3207 "Proto", "Recv-Q", "Send-Q", "Local Address", "Foreign Address");
3208 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3209 goto out;
3210 }
3211 entry_buf_len -= (u32_t)(iRet);
3212 entry_buf_offset += (u32_t)(iRet);
3213
3214 for (upcb = udp_pcbs; upcb != NULL; upcb = upcb->next) {
3215 iRet = snprintf_s((char *)local_ip_port, sizeof(local_ip_port), (sizeof(local_ip_port) - 1),
3216 "%s:%d", ipaddr_ntoa(&upcb->local_ip), upcb->local_port);
3217 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(local_ip_port))) {
3218 goto out;
3219 }
3220
3221 iRet = snprintf_s((char *)remote_ip_port, sizeof(remote_ip_port), (sizeof(remote_ip_port) - 1),
3222 "%s:%d", ipaddr_ntoa(&upcb->remote_ip), upcb->remote_port);
3223 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(remote_ip_port))) {
3224 goto out;
3225 }
3226
3227 recvQlen = netstat_netconn_recvq(upcb->recv_arg);
3228#if LWIP_IPV6
3229 sendQlen = IP_IS_V6(&upcb->local_ip) ? netstat_udp_sendq6(upcb) : netstat_netconn_sendq(upcb->recv_arg);
3230#else
3231 sendQlen = netstat_netconn_sendq(upcb->recv_arg);
3232#endif
3233 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3234 IP_IS_V6(&upcb->local_ip) ? "%-8s%-12d%-12d%-39s%-39s%-16s\n" :
3235 "%-8s%-12d%-12d%-24s%-24s%-16s\n",
3236 IP_IS_V6(&upcb->local_ip) ? "udp-ip6" : "udp",
3237 recvQlen, sendQlen, local_ip_port, remote_ip_port, " ");
3238 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3239 goto out;
3240 }
3241 entry_buf_len -= (u32_t)(iRet);
3242 entry_buf_offset += (u32_t)(iRet);
3243 }
3244 }
3245#endif
3246
3247#if LWIP_RAW
3248 if (raw_pcbs != NULL) {
3249 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3250 "\n%-8s%-12s%-12s%-20s%-20s%-16s%-16s\n",
3251 "Type", "Recv-Q", "Send-Q", "Local Address", "Foreign Address", "Protocol", "HDRINCL");
3252 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3253 goto out;
3254 }
3255 entry_buf_len -= (u32_t)(iRet);
3256 entry_buf_offset += (u32_t)(iRet);
3257
3258 for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) {
3259 iRet = snprintf_s((char *)local_ip_port, sizeof(local_ip_port), (sizeof(local_ip_port) - 1),
3260 "%s", ipaddr_ntoa(&rpcb->local_ip));
3261 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(local_ip_port))) {
3262 goto out;
3263 }
3264
3265 iRet = snprintf_s((char *)remote_ip_port, sizeof(remote_ip_port), (sizeof(remote_ip_port) - 1),
3266 "%s", ipaddr_ntoa(&rpcb->remote_ip));
3267 if ((iRet <= 0) || ((u32_t)(iRet) >= sizeof(remote_ip_port))) {
3268 goto out;
3269 }
3270
3271 recvQlen = netstat_netconn_recvq(rpcb->recv_arg);
3272 sendQlen = netstat_netconn_sendq(rpcb->recv_arg);
3273
3274 proto = rpcb->protocol;//raw_proto;
3275 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3276 "%-8s%-12d%-12d%-20s%-20s%-16u%-16d\n",
3277 "raw", recvQlen, sendQlen, local_ip_port, remote_ip_port, proto, /*rpcb->hdrincl*/0);
3278 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3279 goto out;
3280 }
3281 entry_buf_len -= (u32_t)(iRet);
3282 entry_buf_offset += (u32_t)(iRet);
3283 }
3284 }
3285#if PF_PKT_SUPPORT
3286 if (pkt_raw_pcbs != NULL) {
3287 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3288 "\n%-12s%-12s%-12s%-16s%-12s\n", "Type", "Recv-Q", "Send-Q", "Protocol", "netif");
3289 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3290 goto out;
3291 }
3292 entry_buf_len -= (u32_t)(iRet);
3293 entry_buf_offset += (u32_t)(iRet);
3294
3295 for (rpcb = pkt_raw_pcbs; rpcb != NULL; rpcb = rpcb->next) {
3296 recvQlen = netstat_netconn_recvq(rpcb->recv_arg);
3297 sendQlen = netstat_netconn_sendq(rpcb->recv_arg);
3298
3299 for (netif = netif_list; netif != NULL; netif = netif->next) {
3300 if (netif_get_index(netif)/*netif->ifindex*/ == rpcb->netif_idx/*index*/) {
3301 (void)snprintf_s((char *)netif_name, IFNAMSIZ, IFNAMSIZ - 1, "%s", netif_get_name(netif));
3302 break;
3303 }
3304 }
3305
3306 if (netif == NULL) {
3307 (void)snprintf_s((char *)netif_name, IFNAMSIZ, IFNAMSIZ - 1, "%s", "None");
3308 }
3309
3310 proto = rpcb->protocol;//ntohs(rpcb->proto.eth_proto);
3311
3312 iRet = snprintf_s((char *)(entry_buf + entry_buf_offset), entry_buf_len, entry_buf_len - 1,
3313 "%-12s%-12d%-12d%-16x%-12s\n", "pkt-raw", recvQlen, sendQlen, proto, netif_name);
3314 if ((iRet <= 0) || ((u32_t)(iRet) >= entry_buf_len)) {
3315 goto out;
3316 }
3317 entry_buf_len -= (u32_t)(iRet);
3318 entry_buf_offset += (u32_t)(iRet);
3319 }
3320 }
3321#endif
3322#endif
3323
3324out:
3325 ndata->netstat_out_buf_updated_len = entry_buf_offset;
3327 return;
3328}
3329
3330u32_t osShellNetstat(int argc, const char **argv)
3331{
3332 struct netstat_data ndata;
3333 err_t err;
3334
3335 if (argc > 0) {
3336 PRINTK("\nUsage: netstat\n");
3337 return LOS_NOK;
3338 }
3339
3340 if (tcpip_init_finish == 0) {
3341 PRINTK("%s: tcpip_init have not been called\n", __FUNCTION__);
3342 return LOS_NOK;
3343 }
3344
3345 ndata.netstat_out_buf = mem_malloc(MAX_NETSTAT_ENTRY);
3346 if (ndata.netstat_out_buf == NULL) {
3347 PRINTK("%s: no free mem\n", __FUNCTION__);
3348 return LOS_NOK;
3349 }
3350 ndata.netstat_out_buf_len = MAX_NETSTAT_ENTRY;
3352
3353 if (sys_sem_new(&ndata.cb_completed, 0) != ERR_OK) {
3354 goto err_hand;
3355 }
3356
3357 err = tcpip_callback(netstat_internal, &ndata);
3358 if (err != ERR_OK) {
3359 sys_sem_free(&ndata.cb_completed);
3360 goto err_hand;
3361 }
3362
3364 sys_sem_free(&ndata.cb_completed);
3365 if ((ndata.netstat_out_buf_updated_len > 0) && (ndata.netstat_out_buf_updated_len < MAX_NETSTAT_ENTRY)) {
3366 PRINTK("%s\n", (char *)(ndata.netstat_out_buf));
3367 mem_free(ndata.netstat_out_buf);
3368 return LOS_OK;
3369 }
3370
3371err_hand:
3372 mem_free(ndata.netstat_out_buf);
3373 ndata.netstat_out_buf = NULL;
3374 (void)(argv);
3375 return LOS_NOK;
3376}
3377#ifdef LOSCFG_SHELL
3378SHELLCMD_ENTRY(netstat_shellcmd, CMD_TYPE_EX, "netstat", XARGS, (CmdCallBackFunc)osShellNetstat);
3379#endif /* LOSCFG_SHELL */
3380
3381#define NETIF_NAME_LEN 10
3382STATIC VOID OsShellDhclientUsage(VOID)
3383{
3384 PRINTK("<netif name>, start dhcp for netif name\n"
3385 "-x <netif name>, stop dhcp for netif name\n"
3386 "-h | --help, print dhclient command usage\n");
3387}
3388
3389u32_t OsShellDhclient(int argc, const char **argv)
3390{
3391 struct netif *netif = NULL;
3392
3393 if (argc == 0) {
3394 OsShellDhclientUsage();
3395 } else if (argc == 1) {
3396 if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) {
3397 OsShellDhclientUsage();
3398 } else {
3399 netif = netif_find(argv[0]);
3400 if (netif != NULL) {
3401 (VOID)netifapi_dhcp_start(netif);
3402 } else {
3403 PRINTK("dhclient: invalid option: %s\n", argv[0]);
3404 OsShellDhclientUsage();
3405 }
3406 }
3407 } else if (argc == 2) {
3408 if (strcmp(argv[0], "-x") == 0) {
3409 netif = netif_find(argv[1]);
3410 if (netif != NULL) {
3411 (VOID)netifapi_dhcp_stop(netif);
3412 } else {
3413 PRINTK("dhclient: invalid option: %s\n", argv[1]);
3414 OsShellDhclientUsage();
3415 }
3416 } else {
3417 PRINTK("dhclient: invalid option: %s\n", argv[0]);
3418 OsShellDhclientUsage();
3419 }
3420 } else {
3421 OsShellDhclientUsage();
3422 }
3423
3424 return 0;
3425}
3426
3427#ifdef LOSCFG_SHELL
3428SHELLCMD_ENTRY(dhclient_shellcmd, CMD_TYPE_EX, "dhclient", XARGS, (CmdCallBackFunc)OsShellDhclient);
3429#endif /* LOSCFG_SHELL */
3430
3431#ifdef LWIP_DEBUG_TCPSERVER
3432
3433#define MAX_SIZE 1024
3434void tcp_access(int sockfd)
3435{
3436 size_t n, i;
3437 ssize_t ret;
3438 char msg[MAX_SIZE] = {0};
3439 while (1) {
3440 PRINTK("waiting for recv\n");
3441 (void)memset_s(msg, MAX_SIZE, 0, MAX_SIZE);
3442 ret = recv(sockfd, msg, MAX_SIZE - 1, 0);
3443 if (ret < 0) {
3444 PRINTK("recv failed, %d.\n", (u32_t)ret);
3445 (void)closesocket(sockfd);
3446 return;
3447 } else if (ret == 0) {
3448 (void)closesocket(sockfd);
3449 PRINTK("client disconnect.\n");
3450 return;
3451 }
3452
3453 n = strlen(msg);
3454 for (i = 0; i < n; ++i) {
3455 if (msg[i] >= 'a' && msg[i] <= 'z') {
3456 msg[i] = (char)(msg[i] + ('A' - 'a'));
3457 } else if (msg[i] >= 'A' && msg[i] <= 'Z') {
3458 msg[i] = (char)(msg[i] + ('a' - 'A'));
3459 }
3460 }
3461
3462 if (send(sockfd, msg, n, 0) < 0) {
3463 PRINTK("send failed!\r\n");
3464 continue;
3465 }
3466 }
3467}
3468
3469u32_t osTcpserver(int argc, const char **argv)
3470{
3471 uint16_t port;
3472 int sockfd = -1;
3473 int ret;
3474 struct sockaddr_in seraddr;
3475 struct sockaddr_in cliaddr;
3476 u32_t cliaddr_size = (u32_t)sizeof(cliaddr);
3477 int reuse, iPortVal;
3478
3479 if (tcpip_init_finish == 0) {
3480 PRINTK("tcpip_init have not been called\n");
3481 return LOS_NOK;
3482 }
3483
3484 if (argc < 1 || argv == NULL) {
3485 PRINTK("\nUsage: tcpserver <port>\n");
3486 return LOS_NOK;
3487 }
3488
3489 iPortVal = atoi(argv[0]);
3490 /* Port 0 not supported , negative values not supported , max port limit is 65535 */
3491 if (iPortVal <= 0 || iPortVal > 65535) {
3492 PRINTK("\nUsage: Invalid port\n");
3493 return LOS_NOK;
3494 }
3495
3496 port = (uint16_t)iPortVal;
3497
3498 /* removed the print of argv[1] as its accessing argv[1] without verifying argc and
3499 * argv[1] not used anywhere else */
3500 PRINTK("argv[0]:%s, argc:%d\r\n", argv[0], argc);
3501 sockfd = socket(AF_INET, SOCK_STREAM, 0);
3502 if (sockfd < 0) {
3503 PRINTK("\nUsage: create socket fail!\n");
3504 return LOS_NOK;
3505 }
3506 reuse = 1;
3507 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse, sizeof(reuse)) != 0) {
3508 (void)closesocket(sockfd);
3509 PRINTK("set SO_REUSEADDR failed\n");
3510 return LOS_NOK;
3511 }
3512
3513 (void)memset_s(&seraddr, sizeof(seraddr), 0, sizeof(seraddr));
3514 seraddr.sin_family = AF_INET;
3515 seraddr.sin_addr.s_addr = htonl(INADDR_ANY);
3516 seraddr.sin_port = htons(port);
3517
3518 ret = bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
3519 if (ret < 0) {
3520 PRINTK("bind ip and port failed");
3521 (void)closesocket(sockfd);
3522 return LOS_NOK;
3523 }
3524
3525 ret = listen(sockfd, 5);
3526 if (ret < 0) {
3527 (void)closesocket(sockfd);
3528 PRINTK("listen failed\n");
3529 return LOS_NOK;
3530 }
3531 while (1) {
3532 PRINTK("waiting for accept\n");
3533 (void)memset_s(&cliaddr, sizeof(struct sockaddr_in), 0, sizeof(struct sockaddr_in));
3534 ret = (int)accept(sockfd, (struct sockaddr *)&cliaddr, &cliaddr_size);
3535 if (ret < 0) {
3536 (void)closesocket(sockfd);
3537 PRINTK("Accept failed, %d\n", ret);
3538 break;
3539 }
3540 tcp_access(ret);
3541 }
3542 return LOS_NOK; // Hits Only If Accept Fails
3543}
3544
3545#ifdef LOSCFG_SHELL_CMD_DEBUG
3546SHELLCMD_ENTRY(tcpserver_shellcmd, CMD_TYPE_EX, "tcpserver", XARGS, (CmdCallBackFunc)osTcpserver);
3547#endif /* LOSCFG_SHELL_CMD_DEBUG */
3548#endif /* LWIP_DEBUG_TCPSERVER */
3549
3550#ifdef LWIP_DEBUG_UDPSERVER
3551void udpserver(int argc, const char **argv)
3552{
3553 int sockfd, fromlen;
3554 int ret, iPortVal;
3555 struct sockaddr_in seraddr;
3556 struct sockaddr_in cliaddr;
3557 size_t n, i;
3558
3559 char msg[MAX_SIZE] = {0};
3560 uint16_t port;
3561
3562 if (argc < 1) {
3563 PRINTK("\nUsage: udpserver <port>\n");
3564 return;
3565 }
3566
3567 iPortVal = atoi(argv[0]);
3568 /* Port 0 not supported , negative values not supported , max port limit is 65535 */
3569 if (iPortVal <= 0 || iPortVal > 65535) {
3570 PRINTK("\nUsage: Invalid Port\n");
3571 return;
3572 }
3573
3574 port = (uint16_t)iPortVal;
3575
3576 PRINTK("port:%d\r\n", port);
3577
3578 sockfd = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
3579 if (sockfd == -1) {
3580 PRINTK("\ncreate socket fail\n");
3581 return;
3582 }
3583
3584 (void)memset_s(&seraddr, sizeof(seraddr), 0, sizeof(seraddr));
3585 (void)memset_s(&cliaddr, sizeof(cliaddr), 0, sizeof(cliaddr));
3586 seraddr.sin_family = AF_INET;
3587 seraddr.sin_addr.s_addr = htonl(INADDR_ANY);
3588 seraddr.sin_port = htons(port);
3589 ret = lwip_bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
3590 if (ret < 0) {
3591 PRINTK("bind ip and port failed:%d\n", errno);
3592 (void)closesocket(sockfd);
3593 return;
3594 }
3595
3596 while (1) {
3597 ret = recvfrom(sockfd, msg, MAX_SIZE - 1, 0, (struct sockaddr *)&cliaddr, (socklen_t *)&fromlen);
3598 if (ret >= 0) {
3599 n = strlen(msg);
3600 for (i = 0; i < n; ++i) {
3601 if (msg[i] >= 'a' && msg[i] <= 'z') {
3602 msg[i] = (char)(msg[i] + 'A' - 'a');
3603 } else if (msg[i] >= 'A' && msg[i] <= 'Z') {
3604 msg[i] = (char)(msg[i] + 'a' - 'A');
3605 }
3606 }
3607 ret = sendto(sockfd, msg, n + 1, 0, (struct sockaddr *)&cliaddr, (socklen_t)fromlen);
3608 if (ret <= 0 && errno == EPIPE) {
3609 break;
3610 }
3611 } else {
3612 break;
3613 }
3614 }
3615
3616 (void)closesocket(sockfd);
3617 return;
3618}
3619
3620#ifdef LOSCFG_SHELL_CMD_DEBUG
3621SHELLCMD_ENTRY(udpserver_shellcmd, CMD_TYPE_EX, "udpserver", XARGS, (CmdCallBackFunc)udpserver);
3622#endif /* LOSCFG_SHELL_CMD_DEBUG */
3623#endif /* LWIP_DEBUG_UDPSERVER */
3624
3625#ifdef LWIP_DEBUG_INFO
3626LWIP_STATIC
3627u32_t netdebug_memp(int argc, const char **argv)
3628{
3629 u32_t ret = LOS_OK;
3630 int type;
3631
3632 if (argc == 2) {
3633 if (!strcmp("-i", argv[1])) {
3634 debug_memp_info();
3635 } else if (!strcmp("-udp", argv[1])) {
3636 debug_memp_type_info(MEMP_UDP_PCB);
3637 } else if (!strcmp("-tcp", argv[1])) {
3638 debug_memp_type_info(MEMP_TCP_PCB);
3639 } else if (!strcmp("-raw", argv[1])) {
3640 debug_memp_type_info(MEMP_RAW_PCB);
3641 } else if (!strcmp("-conn", argv[1])) {
3642 debug_memp_type_info(MEMP_NETCONN);
3643 } else {
3644 ret = LOS_NOK;
3645 }
3646 } else if (argc == 3) {
3647 if (!strcmp("-d", argv[1])) {
3648 type = atoi(argv[2]);
3649 if (type >= 0) {
3650 debug_memp_detail(type);
3651 } else {
3652 PRINTK("Error: type < 0\n");
3653 ret = LOS_NOK;
3654 }
3655 } else {
3656 ret = LOS_NOK;
3657 }
3658 } else {
3659 ret = LOS_NOK;
3660 }
3661
3662 return ret;
3663}
3664
3665LWIP_STATIC
3666u32_t netdebug_sock(int argc, const char **argv)
3667{
3668 int idx;
3669 u32_t ret = LOS_NOK;
3670
3671 if (argc == 2) {
3672 if (!strcmp("-i", argv[1])) {
3673 /* netdebug sock -i */
3674 for (idx = 0; idx < (int)LWIP_CONFIG_NUM_SOCKETS; idx++) {
3675 debug_socket_info(idx, 1, 0);
3676 }
3677 ret = LOS_OK;
3678 }
3679 } else if (argc == 3) {
3680 if (!strcmp("-d", argv[1])) {
3681 /* netdebug sock -d <idx> */
3682 idx = atoi(argv[2]);
3683 if (idx >= 0) {
3684 debug_socket_info(idx, 1, 1);
3685 ret = LOS_OK;
3686 } else {
3687 PRINTK("Error: idx < 0\n");
3688 }
3689 }
3690 }
3691
3692 return ret;
3693}
3694
3695
3696u32_t osShellNetDebug(int argc, const char **argv)
3697{
3698 u32_t ret = LOS_NOK;
3699
3700 if (argc < 1 || argv == NULL) {
3701 goto usage;
3702 }
3703
3704 if (!strcmp("memp", argv[0])) {
3705 ret = netdebug_memp(argc, argv);
3706 if (ret != LOS_OK) {
3707 goto usage_memp;
3708 }
3709 } else if (!strcmp("sock", argv[0])) {
3710 /* netdebug sock {-i | -d <idx>} */
3711 ret = netdebug_sock(argc, argv);
3712 if (ret != LOS_OK) {
3713 goto usage_sock;
3714 }
3715 } else {
3716 goto usage;
3717 }
3718 return ret;
3719
3720usage:
3721 /* Cmd help */
3722 PRINTK("\nUsage:\n");
3723 PRINTK("netdebug memp {-i | -d <type> | -udp | -tcp | -raw |-conn}\n");
3724 PRINTK("netdebug sock {-i | -d <idx>}\n");
3725 return LOS_NOK;
3726
3727usage_memp:
3728 /* netdebug memp help */
3729 PRINTK("\nUsage:\n");
3730 PRINTK("netdebug memp {-i | -d <type> | -udp | -tcp | -raw |-conn}\n");
3731 return LOS_NOK;
3732
3733usage_sock:
3734 /* netdebug sock help */
3735 PRINTK("\nUsage:\n");
3736 PRINTK("netdebug sock {-i | -d <idx>}\n");
3737 return LOS_NOK;
3738}
3739#endif /* LWIP_DEBUG_INFO */
3740
3741#if defined(LOSCFG_SHELL_CMD_DEBUG) && defined(LWIP_DEBUG_INFO)
3742SHELLCMD_ENTRY(netdebug_shellcmd, CMD_TYPE_EX, "netdebug", XARGS, (CmdCallBackFunc)osShellNetDebug);
3743#endif /* LOSCFG_SHELL_CMD_DEBUG && LWIP_DEBUG_INFO */
3744
3745u32_t osShellIpDebug(int argc, const char **argv)
3746{
3747 u8_t i = 0;
3748 char acIPv6Addr[IP6ADDR_STRLEN_MAX + 1] = {0};
3749 char aclladdr[20] = {0};
3750 const char *acStates[] = {"NO_ENTRY", "INCOMPLETE", "REACHABLE", "STALE", "DELAY", "PROBE"};
3751 u8_t atleastOneEntry = 0;
3752
3753 if (!tcpip_init_finish) {
3754 PRINTK("%s: tcpip_init have not been called\n", __FUNCTION__);
3755 goto exit;
3756 }
3757
3758 /* Display prefix */
3759 PRINTK("=================\n");
3760 PRINTK("|| Prefix List ||\n");
3761 PRINTK("=================\n");
3762 PRINTK("%-50s %-16s %-20s\n", "Prefix", "netif", "validLifetime");
3763 PRINTK("---------------------------------------------------------------------------------\n");
3764 /* Display neighbour Cache Entry */
3765 for (i = 0; i < LWIP_ND6_NUM_PREFIXES; i++) {
3766 if (prefix_list[i].netif != NULL && prefix_list[i].invalidation_timer > 0) {
3767 atleastOneEntry = 1;
3768 (void)ip6addr_ntoa_r((const ip6_addr_t *)(prefix_list[i].prefix.addr), (acIPv6Addr), sizeof(acIPv6Addr));
3769 PRINTK("%-50s ", acIPv6Addr);
3770 PRINTK("%-16s ", netif_get_name(prefix_list[i].netif));
3771 PRINTK("%-20u\n", prefix_list[i].invalidation_timer);
3772 }
3773 }
3774
3775 if (!atleastOneEntry) {
3776 PRINTK("**** NO VALID PREFIXES FOUND CONFIGURED ****\n");
3777 }
3778 PRINTK("---------------------------------------------------------------------------------\n");
3779
3780 atleastOneEntry = 0;
3781
3782 PRINTK("\n\n");
3783 PRINTK("============================\n");
3784 PRINTK("|| Neighbor Cache Entries ||\n");
3785 PRINTK("============================\n");
3786 PRINTK("%-50s %-25s %-16s %-15s %-10s\n", "Neighbor", "MAC", "netif", "state", "IsRouter");
3787 PRINTK("------------------------------------------------------------"
3788 "----------------------------------------------------------\n");
3789
3790 /* Display neighbour Cache Entry */
3791 for (i = 0; i < LWIP_ND6_NUM_NEIGHBORS; i++) {
3792 if (neighbor_cache[i].state != ND6_NO_ENTRY) {
3793 atleastOneEntry = 1;
3794 (void)ip6addr_ntoa_r((const ip6_addr_t *)(neighbor_cache[i].next_hop_address.addr), (acIPv6Addr),
3795 sizeof(acIPv6Addr));
3796 PRINTK("%-50s ", acIPv6Addr);
3797
3798 if (snprintf_s(aclladdr, sizeof(aclladdr), sizeof(aclladdr) - 1, "%02X:%02X:%02X:%02X:%02X:%02X",
3799 neighbor_cache[i].lladdr[0], neighbor_cache[i].lladdr[1], neighbor_cache[i].lladdr[2],
3800 neighbor_cache[i].lladdr[3], neighbor_cache[i].lladdr[4], neighbor_cache[i].lladdr[5]) < 0) {
3801 return LOS_NOK;
3802 }
3803 PRINTK("%-25s ", aclladdr);
3804 PRINTK("%-16s ", netif_get_name(neighbor_cache[i].netif));
3805 PRINTK("%-15s ", acStates[neighbor_cache[i].state]);
3806 PRINTK("%-10s\n", (neighbor_cache[i].isrouter ? "Yes" : "No"));
3807 }
3808 }
3809 if (!atleastOneEntry) {
3810 PRINTK("**** NO NEIGHBOURS FOUND ****\n");
3811 }
3812 PRINTK("------------------------------------------------------------"
3813 "----------------------------------------------------------\n");
3814
3815 atleastOneEntry = 0;
3816
3817 PRINTK("\n\n");
3818 PRINTK("===============================\n");
3819 PRINTK("|| Destination Cache Entries ||\n");
3820 PRINTK("===============================\n");
3821 PRINTK("%-50s %-50s %-10s %-10s\n", "Destination", "NextHop", "PMTU", "age");
3822 PRINTK("------------------------------------------------------------"
3823 "--------------------------------------------------------\n");
3824 /* Display destination Cache Entry */
3825 for (i = 0; i < LWIP_ND6_NUM_DESTINATIONS; i++) {
3826 if (!ip6_addr_isany(&(destination_cache[i].destination_addr))) {
3827 atleastOneEntry = 1;
3828 (void)ip6addr_ntoa_r((const ip6_addr_t *)(destination_cache[i].destination_addr.addr), (acIPv6Addr),
3829 sizeof(acIPv6Addr));
3830 PRINTK("%-50s ", acIPv6Addr);
3831 (void)ip6addr_ntoa_r((const ip6_addr_t *)(destination_cache[i].next_hop_addr.addr), (acIPv6Addr),
3832 sizeof(acIPv6Addr));
3833 PRINTK("%-50s ", acIPv6Addr);
3834 PRINTK("%-10u ", destination_cache[i].pmtu);
3835 PRINTK("%-10u\n", destination_cache[i].age);
3836 }
3837 }
3838 if (!atleastOneEntry) {
3839 PRINTK("**** NO DESTINATION CACHE FOUND ****\n");
3840 }
3841 PRINTK("------------------------------------------------------------"
3842 "--------------------------------------------------------\n");
3843
3844 atleastOneEntry = 0;
3845 PRINTK("\n\n");
3846 PRINTK("============================\n");
3847 PRINTK("|| Default Router Entries ||\n");
3848 PRINTK("============================\n");
3849 PRINTK("%-50s %-20s %-10s\n", "Router", "invalidation_timer", "flags");
3850 PRINTK("-----------------------------------------------------------------------------\n");
3851 /* Display Default Router Cache Entry */
3852 for (i = 0; i < LWIP_ND6_NUM_ROUTERS; i++) {
3853 if (default_router_list[i].neighbor_entry) {
3854 atleastOneEntry = 1;
3855 (void)ip6addr_ntoa_r((const ip6_addr_t *)((default_router_list[i].neighbor_entry)->next_hop_address.addr),
3856 (acIPv6Addr), sizeof(acIPv6Addr));
3857 PRINTK("%-50s ", acIPv6Addr);
3858 PRINTK("%-20u ", default_router_list[i].invalidation_timer);
3859 PRINTK("%-10u\n", default_router_list[i].flags);
3860 }
3861 }
3862 if (!atleastOneEntry) {
3863 PRINTK("**** NO DEFAULT ROUTERS FOUND ****\n");
3864 }
3865 PRINTK("-----------------------------------------------------------------------------\n");
3866
3867exit:
3868 return LOS_OK;
3869}
3870
3871#ifdef LOSCFG_SHELL_CMD_DEBUG
3872SHELLCMD_ENTRY(ipdebug_shellcmd, CMD_TYPE_EX, "ipdebug", XARGS, (CmdCallBackFunc)osShellIpDebug);
3873#endif
3874#ifdef LWIP_TESTBED
3875extern void cmd_reset(void);
3876
3877void osShellReboot(int argc, const char **argv)
3878{
3879 cmd_reset();
3880}
3881
3882#ifdef LOSCFG_SHELL_CMD_DEBUG
3883SHELLCMD_ENTRY(reboot_shellcmd, CMD_TYPE_EX, "reboot", XARGS, (CmdCallBackFunc)osShellReboot);
3884#endif /* LOSCFG_SHELL_CMD_DEBUG */
3885#endif
3886
3887#endif //LWIP_ENABLE_LOS_SHELL_CMD
LWIP_STATIC void lwip_arp_internal(void *arg)
Definition: api_shell.c:1249
u32_t osShellPing(int argc, const char **argv)
Definition: api_shell.c:1790
LWIP_STATIC void lwip_ifconfig_usage(const char *cmd)
Definition: api_shell.c:671
u32_t osShellPing6(int argc, const char **argv)
Definition: api_shell.c:2082
int netstat_udp_sendq(struct udp_pcb *upcb)
u32_t osTcpserver(int argc, const char **argv)
int netstat_netconn_recvq(const struct netconn *conn)
LWIP_STATIC void lwip_ifconfig_show_internal(void *arg)
Definition: api_shell.c:429
int netstat_get_udp_sendQLen6(struct udp_pcb *udppcb, struct pbuf *udpbuf)
Definition: api_shell.c:2710
LWIP_STATIC void lwip_ifconfig_internal(void *arg)
Definition: api_shell.c:476
int get_unused_socket_num(void)
Definition: sockets.c:343
volatile int tcpip_init_finish
Definition: fixme.c:403
int netstat_netconn_sendq(struct netconn *conn)
static void lwip_ping_usage(void)
Definition: api_shell.c:1563
void lwip_printsize(size_t size)
Definition: api_shell.c:656
struct nd6_neighbor_cache_entry neighbor_cache[LWIP_ND6_NUM_NEIGHBORS]
u32_t lwip_ifconfig(int argc, const char **argv)
Definition: api_shell.c:684
int netstat_tcp_sendq(struct tcp_pcb *tpcb)
const char *const tcp_state_str[]
Definition: fixme.c:389
void tcp_access(int sockfd)
u32_t osShellIpDebug(int argc, const char **argv)
static int ping_kill
Definition: api_shell.c:1561
struct ping6_stats ping6_stats_t
static void ping_cmd(unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3)
Definition: api_shell.c:1774
LWIP_STATIC const char * convert_icmpv6_err_to_string(u8_t err_type)
Definition: api_shell.c:2567
SHELLCMD_ENTRY(ifconfig_shellcmd, CMD_TYPE_EX, "ifconfig", XARGS,(CmdCallBackFunc) lwip_ifconfig)
void udpserver(int argc, const char **argv)
LWIP_STATIC int print_netif(struct netif *netif, char *print_buf, unsigned int buf_len)
Definition: api_shell.c:269
u32_t osShellDns(int argc, const char **argv)
Definition: api_shell.c:2635
void ifup_internal(void *arg)
Definition: api_shell.c:1483
static int ping_taskid
Definition: api_shell.c:1560
void ifdown_internal(void *arg)
Definition: api_shell.c:1503
u32_t osShellNetDebug(int argc, const char **argv)
LWIP_STATIC void lwip_arp_show_internal(struct netif *netif, char *printf_buf, unsigned int buf_len)
Definition: api_shell.c:1172
LWIP_STATIC void update_ping6_stats(ping6_stats_t *ping6_stats, u32_t rtt, u32_t nreceived)
Definition: api_shell.c:2552
LWIP_STATIC int osPingFunc(u32_t destip, u32_t cnt, u32_t interval, u32_t data_len)
Definition: api_shell.c:1573
LWIP_STATIC unsigned int get_hostip(const char *hname)
Definition: api_shell.c:1543
sys_sem_t ip_conflict_detect
Definition: fixme.c:369
struct ping6_args ping6_args_t
int netstat_tcp_recvq(struct tcp_pcb *tpcb)
LWIP_STATIC u32_t netdebug_memp(int argc, const char **argv)
int netstat_udp_sendq6(struct udp_pcb *upcb)
LWIP_STATIC struct hostent * gethostnameinfo(const char *host)
Definition: api_shell.c:1527
LWIP_STATIC int create_ping6_socket(u8_t type, const void *param)
Definition: api_shell.c:2340
u32_t lwip_arp(int argc, const char **argv)
Definition: api_shell.c:1347
u32_t osShellNtpdate(int argc, const char **argv)
Definition: api_shell.c:2590
LWIP_STATIC int parse_args_ping6(int argc, const char **argv, ping6_args_t *ping6_params)
Definition: api_shell.c:2402
LWIP_STATIC u32_t netdebug_sock(int argc, const char **argv)
int netstat_get_udp_sendQLen(struct udp_pcb *udppcb, struct pbuf *udpbuf)
Definition: api_shell.c:2802
LWIP_STATIC void lwip_arp_usage(const char *cmd)
Definition: api_shell.c:1338
u32_t osShellNetstat(int argc, const char **argv)
void netstat_internal(void *ctx)
@ CMD_TYPE_EX
不支持标准命令参数输入,会把用户填写的命令关键字屏蔽掉,例如:输入ls /ramfs,传入给注册函数的参数只有/ramfs,而ls命令关键字并不会被传入。
Definition: shell.h:91
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
int lwip_sntp_start(int server_num, char **sntp_server, struct timeval *time)
Definition: fixme.c:379
u32_t is_dup_detect_initialized
Definition: fixme.c:372
err_t etharp_delete_arp_entry(struct netif *netif, ip4_addr_t *ipaddr)
Definition: fixme.c:337
err_t lwip_dns_setserver(u8_t numdns, ip_addr_t *dnsserver)
Definition: fixme.c:345
struct raw_pcb * raw_pcbs
Definition: fixme.c:364
err_t netif_set_hwaddr(struct netif *netif, const unsigned char *hw_addr, int hw_len)
Definition: fixme.c:297
err_t netif_dhcp_off(struct netif *netif)
Definition: fixme.c:54
err_t netif_do_rmv_ipv6_addr(struct netif *netif, void *arguments)
Definition: fixme.c:165
err_t etharp_update_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
Definition: fixme.c:328
err_t netif_set_mtu(struct netif *netif, u16_t netif_mtu)
Definition: fixme.c:270
struct raw_pcb * pkt_raw_pcbs
Definition: fixme.c:360
err_t lwip_dns_getserver(u8_t numdns, ip_addr_t *dnsserver)
Definition: fixme.c:352
LITE_OS_SEC_TEXT UINT32 LOS_CurTaskIDGet(VOID)
Obtain current running task ID.
Definition: los_task.c:331
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskCreate(UINT32 *taskID, TSK_INIT_PARAM_S *initParam)
创建任务,并使该任务进入ready状态,如果就绪队列中没有更高优先级的任务,则运行该任务
Definition: los_task.c:718
VOID *(* TSK_ENTRY_FUNC)(UINTPTR param1, UINTPTR param2, UINTPTR param3, UINTPTR param4)
Define the type of a task entrance function.
Definition: los_task.h:480
INT64 ssize_t
Definition: los_typedef.h:79
unsigned int UINT32
Definition: los_typedef.h:57
size_t BOOL
Definition: los_typedef.h:88
int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
设置socket 配置项
Definition: socket.c:148
in_addr_t inet_addr(const char *cp)
Definition: socket.c:334
ssize_t recv(int s, void *mem, size_t len, int flags)
Definition: socket.c:234
ssize_t sendto(int s, const void *dataptr, size_t size, int flags, const struct sockaddr *to, socklen_t tolen)
区别是送达地址,意思是这些数据要发给哪个地址的
Definition: socket.c:268
int bind(int s, const struct sockaddr *name, socklen_t namelen)
Definition: socket.c:94
int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
Definition: socket.c:70
const char * inet_ntop(int af, const void *src, char *dst, socklen_t size)
Definition: socket.c:323
int closesocket(int s)
Definition: socket.c:166
ssize_t recvfrom(int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
区别是返回源地址,意思是这些数据是从哪个地址过来的
Definition: socket.c:240
int listen(int s, int backlog)
Definition: socket.c:209
ssize_t send(int s, const void *dataptr, size_t size, int flags)
相当于文件操作的 write 功能,区别是第四个参数 同 recv
Definition: socket.c:257
int socket(int domain, int type, int protocol)
Definition: socket.c:314
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_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
Definition: sockets.c:77
int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen)
Definition: sockets.c:83
int lwip_socket(int domain, int type, int protocol)
Definition: sockets.c:71
void exit(int status)
Definition: stdlib.c:60
unsigned char ethaddr[6]
Definition: api_shell.c:1160
unsigned int ipaddr
Definition: api_shell.c:1158
char iface[IFNAMSIZ]
Definition: api_shell.c:1156
sys_sem_t cb_completed
Definition: api_shell.c:1164
char cb_print_buf[PRINT_BUF_LEN]
Definition: api_shell.c:1165
int print_buf_len
Definition: api_shell.c:1166
sys_sem_t cb_completed
Definition: api_shell.c:263
char * if_name
Definition: api_shell.c:261
err_t err
Definition: api_shell.c:262
char iface[IFNAMSIZ]
Definition: api_shell.c:238
unsigned int print_len
Definition: api_shell.c:250
ip_addr_t gw
Definition: api_shell.c:242
ip_addr_t netmask
Definition: api_shell.c:241
sys_sem_t cb_completed
Definition: api_shell.c:248
char cb_print_buf[PRINT_BUF_LEN]
Definition: api_shell.c:249
unsigned int option
Definition: api_shell.c:239
ip_addr_t ip_addr
Definition: api_shell.c:240
unsigned char ethaddr[6]
Definition: api_shell.c:243
s8_t * netstat_out_buf
Definition: api_shell.c:254
u32_t netstat_out_buf_len
Definition: api_shell.c:255
sys_sem_t cb_completed
Definition: api_shell.c:257
u32_t netstat_out_buf_updated_len
Definition: api_shell.c:256
u8_t args_found
Definition: api_shell.c:154
ip6_addr_t src_addr
Definition: api_shell.c:159
u8_t pad
Definition: api_shell.c:157
u8_t host_index
Definition: api_shell.c:156
u32_t pingcount
Definition: api_shell.c:158
ip6_addr_t dst_addr
Definition: api_shell.c:160
u8_t interface_index
Definition: api_shell.c:155
float avg_rtt
Definition: api_shell.c:168
u32_t min_rtt
Definition: api_shell.c:166
u32_t flag
Definition: api_shell.c:165
u32_t max_rtt
Definition: api_shell.c:167
UINT16 usTaskPrio
Definition: los_task.h:505
UINTPTR auwArgs[4]
Definition: los_task.h:507
UINT32 uwStackSize
Definition: los_task.h:508
CHAR * pcName
Definition: los_task.h:509
TSK_ENTRY_FUNC pfnTaskEntry
Definition: los_task.h:504
UINT32 uwResved
Definition: los_task.h:513
void sys_sem_free(sys_sem_t *sem)
Definition: sys_arch.c:288
void sys_sem_signal(sys_sem_t *sem)
Definition: sys_arch.c:267
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
uint32_t sys_sem_t
Definition: sys_arch.h:52
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 ARG_NUM_0 ARG_NUM_2 ARG_NUM_1 ARG_NUM_2 ARG_NUM_3 ARG_NUM_7 ARG_NUM_2 ARG_NUM_3 ARG_NUM_2 ARG_NUM_4 ARG_NUM_5 ARG_NUM_6 ARG_NUM_3 ARG_NUM_5 ARG_NUM_7 ARG_NUM_1 ARG_NUM_4 ARG_NUM_5 ARG_NUM_4 ARG_NUM_7 ARG_NUM_2 ARG_NUM_3 ARG_NUM_7 ARG_NUM_7 ARG_NUM_3 ARG_NUM_3 ARG_NUM_3 ARG_NUM_7 ARG_NUM_3 ARG_NUM_2 char ARG_NUM_2 ARG_NUM_1 ARG_NUM_0 ARG_NUM_0 ARG_NUM_3 void ARG_NUM_1 ARG_NUM_0 unsigned ARG_NUM_0 ARG_NUM_2 ARG_NUM_3 ARG_NUM_2 ARG_NUM_5 ARG_NUM_3 ARG_NUM_3 ARG_NUM_4 ARG_NUM_1 ARG_NUM_1 ARG_NUM_3 ARG_NUM_2 ARG_NUM_1 ARG_NUM_4 ARG_NUM_4 ARG_NUM_5 ARG_NUM_3 ARG_NUM_2 void ARG_NUM_6 unsigned ARG_NUM_0 unsigned ARG_NUM_0 ARG_NUM_3 ARG_NUM_3 ARG_NUM_2 ARG_NUM_2 ARG_NUM_1 ARG_NUM_2 ARG_NUM_1 char ARG_NUM_0 ARG_NUM_4 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_4 ARG_NUM_5 ARG_NUM_2 ARG_NUM_3 ARG_NUM_3 ARG_NUM_3 ARG_NUM_3 ARG_NUM_6 ARG_NUM_6 ARG_NUM_5 ARG_NUM_3 void ARG_NUM_3 ARG_NUM_3 ARG_NUM_5 ARG_NUM_1 unsigned ARG_NUM_3 long
ARG_NUM_3 int
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
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
u32_t(* CmdCallBackFunc)(u32_t argc, const char **argv)
Definition: types_adapt.h:86