-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsh_netcmds.c
1398 lines (1156 loc) · 33.3 KB
/
nsh_netcmds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* apps/nshlib/nsh_netcmds.c
*
* Copyright (C) 2007-2012, 2014-2015, 2017 Gregory Nutt.
* All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#ifdef CONFIG_NET
#include <nuttx/compiler.h>
#include <sys/stat.h> /* Needed for open */
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sched.h>
#include <fcntl.h> /* Needed for open */
#include <dirent.h>
#include <libgen.h> /* Needed for basename */
#include <errno.h>
#include <debug.h>
#if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP)
# include <netdb.h>
#endif
#include <net/ethernet.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ether.h>
#include <nuttx/irq.h>
#include <nuttx/clock.h>
#include <nuttx/net/net.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/netstats.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/icmp.h>
#include <nuttx/net/icmpv6.h>
#ifdef CONFIG_NET_6LOWPAN
# include <nuttx/net/sixlowpan.h>
# ifdef CONFIG_WIRELESS_PKTRADIO
# include <nuttx/wireless/pktradio.h>
# endif
#endif
#ifdef CONFIG_NETLINK_ROUTE
# include <nuttx/net/arp.h>
#endif
#ifdef CONFIG_NETUTILS_NETLIB
# include "netutils/netlib.h"
#endif
#ifdef CONFIG_NET_UDP
# include "netutils/netlib.h"
# if !defined(CONFIG_NSH_DISABLE_GET) || !defined(CONFIG_NSH_DISABLE_PUT)
# include "netutils/tftp.h"
# endif
#endif
#ifdef CONFIG_NET_TCP
# ifndef CONFIG_NSH_DISABLE_WGET
# include "netutils/webclient.h"
# endif
#endif
#if defined(CONFIG_NETINIT_DHCPC) || defined(CONFIG_NETINIT_DNS)
# include "netutils/dhcpc.h"
#endif
#include "nsh.h"
#include "nsh_console.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#undef HAVE_HWADDR
#undef HAVE_EADDR
#undef HAVE_RADIOADDR
#if defined(CONFIG_NET_ETHERNET)
# define HAVE_HWADDR 1
#elif defined(CONFIG_NET_6LOWPAN)
# if defined(CONFIG_WIRELESS_IEEE802154)
# define HAVE_HWADDR 1
# define HAVE_EADDR 1
# elif defined(CONFIG_WIRELESS_PKTRADIO)
# define HAVE_HWADDR 1
# define HAVE_RADIOADDR 1
# endif
#endif
/* Get the larger value */
#ifndef MAX
# define MAX(a,b) (a > b ? a : b)
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/* Describes the MAC address of the selected device */
#ifdef HAVE_HWADDR
#if defined(CONFIG_NET_ETHERNET)
typedef uint8_t mac_addr_t[IFHWADDRLEN];
#elif defined(HAVE_EADDR)
typedef uint8_t mac_addr_t[8];
#elif defined(HAVE_RADIOADDR)
typedef struct pktradio_addr_s mac_addr_t;
#endif
#endif
#ifdef CONFIG_NET_UDP
struct tftpc_args_s
{
bool binary; /* true:binary ("octet") false:text ("netascii") */
bool allocated; /* true: destpath is allocated */
FAR char *destpath; /* Path at destination */
FAR const char *srcpath; /* Path at src */
in_addr_t ipaddr; /* Host IP address */
};
#endif
typedef int (*nsh_netdev_callback_t)(FAR struct nsh_vtbl_s *vtbl,
FAR char *devname);
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: net_statistics
****************************************************************************/
#if defined(CONFIG_NET_STATISTICS) && defined(CONFIG_FS_PROCFS) && \
!defined(CONFIG_FS_PROCFS_EXCLUDE_NET) && \
!defined(CONFIG_NSH_DISABLE_IFCONFIG)
static inline void net_statistics(FAR struct nsh_vtbl_s *vtbl)
{
nsh_catfile(vtbl, "ifconfig", CONFIG_NSH_PROC_MOUNTPOINT "/net/stat");
}
#else
# define net_statistics(vtbl)
#endif
/****************************************************************************
* Name: ifconfig_callback
****************************************************************************/
#if !defined(CONFIG_NSH_DISABLE_IFUPDOWN) || !defined(CONFIG_NSH_DISABLE_IFCONFIG)
static int ifconfig_callback(FAR struct nsh_vtbl_s *vtbl, FAR char *devname)
{
char buffer[IFNAMSIZ + 12];
DEBUGASSERT(vtbl != NULL && devname != NULL);
/* Construct the full path to the /proc/net entry for this device */
snprintf(buffer, IFNAMSIZ + 12,
CONFIG_NSH_PROC_MOUNTPOINT "/net/%s", devname);
nsh_catfile(vtbl, "ifconfig", buffer);
return OK;
}
#endif /* !CONFIG_NSH_DISABLE_IFUPDOWN || !CONFIG_NSH_DISABLE_IFCONFIG */
/****************************************************************************
* Name: tftpc_parseargs
****************************************************************************/
#ifdef CONFIG_NET_UDP
int tftpc_parseargs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
struct tftpc_args_s *args)
{
FAR const char *fmt = g_fmtarginvalid;
bool badarg = false;
int option;
/* Get the get/put options */
memset(args, 0, sizeof(struct tftpc_args_s));
while ((option = getopt(argc, argv, ":bnf:h:")) != ERROR)
{
switch (option)
{
case 'b':
args->binary = true;
break;
case 'n':
args->binary = false;
break;
case 'f':
args->destpath = optarg;
break;
case 'h':
if (!netlib_ipv4addrconv(optarg, (FAR uint8_t *)&args->ipaddr))
{
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
badarg = true;
}
break;
case ':':
nsh_error(vtbl, g_fmtargrequired, argv[0]);
badarg = true;
break;
case '?':
default:
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
badarg = true;
break;
}
}
/* If a bad argument was encountered, then return without processing */
if (badarg)
{
return ERROR;
}
/* There should be exactly one parameter left on the command-line */
if (optind == argc - 1)
{
args->srcpath = argv[optind];
}
/* optind == argc means that there is nothing left on the command-line */
else if (optind >= argc)
{
fmt = g_fmtargrequired;
goto errout;
}
/* optind < argc - 1 means that there are too many arguments on the
* command-line
*/
else
{
fmt = g_fmttoomanyargs;
goto errout;
}
/* The HOST IP address is also required */
if (args->ipaddr == 0)
{
fmt = g_fmtargrequired;
goto errout;
}
/* If the destpath was not provided, then we have do a little work. */
if (args->destpath == NULL)
{
FAR char *tmp1;
FAR char *tmp2;
/* Copy the srcpath... baseanme might modify it */
fmt = g_fmtcmdoutofmemory;
tmp1 = strdup(args->srcpath);
if (tmp1 == NULL)
{
goto errout;
}
/* Get the basename of the srcpath */
tmp2 = basename(tmp1);
if (tmp2 == NULL)
{
free(tmp1);
goto errout;
}
/* Use that basename as the destpath */
args->destpath = strdup(tmp2);
free(tmp1);
if (args->destpath == NULL)
{
goto errout;
}
args->allocated = true;
}
return OK;
errout:
nsh_output(vtbl, fmt, argv[0]);
return ERROR;
}
#endif
/****************************************************************************
* Name: wget_callback
****************************************************************************/
#ifdef CONFIG_NET_TCP
#ifndef CONFIG_NSH_DISABLE_WGET
static void wget_callback(FAR char **buffer, int offset, int datend,
FAR int *buflen, FAR void *arg)
{
write((int)((intptr_t)arg), &((*buffer)[offset]), datend - offset);
}
#endif
#endif
/****************************************************************************
* Name: nsh_foreach_netdev
****************************************************************************/
#if !defined(CONFIG_NSH_DISABLE_IFUPDOWN) || !defined(CONFIG_NSH_DISABLE_IFCONFIG)
static int nsh_foreach_netdev(nsh_netdev_callback_t callback,
FAR struct nsh_vtbl_s *vtbl,
FAR char *cmd)
{
FAR struct dirent *entry;
FAR DIR *dir;
int ret = OK;
/* Open the /proc/net directory */
dir = opendir(CONFIG_NSH_PROC_MOUNTPOINT "/net");
if (dir == NULL)
{
nsh_error(vtbl, g_fmtcmdfailed, cmd, "opendir", NSH_ERRNO);
return ERROR;
}
/* Look for device configuration "regular" files */
while ((entry = readdir(dir)) != NULL)
{
/* Skip anything that is not a regular file and skip the file
* /proc/dev/stat which does not correspond to a network driver.
*/
if (entry->d_type == DTYPE_FILE &&
strcmp(entry->d_name, "stat") != 0)
{
/* Performt he callback. It returns any non-zero value, then
* terminate the search.
*/
ret = callback(vtbl, entry->d_name);
if (ret != OK)
{
break;
}
}
}
/* Close the directory */
closedir(dir);
return ret;
}
#endif
/****************************************************************************
* Name: nsh_addrconv
****************************************************************************/
#if !defined(CONFIG_NSH_DISABLE_IFCONFIG) && defined(HAVE_HWADDR)
static inline bool nsh_addrconv(FAR const char *hwstr,
FAR mac_addr_t *macaddr)
{
/* REVISIT: How will we handle Ethernet and SLIP networks together? */
#if defined(CONFIG_NET_ETHERNET)
return !netlib_ethaddrconv(hwstr, *macaddr);
#elif defined(HAVE_EADDR)
return !netlib_eaddrconv(hwstr, *macaddr);
#elif defined(HAVE_RADIOADDR)
return !netlib_nodeaddrconv(hwstr, macaddr);
#else
return -ENOSUPP;
#endif
}
#endif
/****************************************************************************
* Name: nsh_sethwaddr
****************************************************************************/
#if !defined(CONFIG_NSH_DISABLE_IFCONFIG) && defined(HAVE_HWADDR)
static inline void nsh_sethwaddr(FAR const char *ifname,
FAR mac_addr_t *macaddr)
{
#if defined(CONFIG_NET_ETHERNET)
netlib_setmacaddr(ifname, *macaddr);
#elif defined(HAVE_EADDR)
netlib_seteaddr(ifname, *macaddr);
#elif defined(HAVE_RADIOADDR)
netlib_setnodeaddr(ifname, macaddr);
#endif
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: cmd_get
****************************************************************************/
#ifdef CONFIG_NET_UDP
#ifndef CONFIG_NSH_DISABLE_GET
int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
struct tftpc_args_s args;
FAR char *fullpath;
/* Parse the input parameter list */
if (tftpc_parseargs(vtbl, argc, argv, &args) != OK)
{
return ERROR;
}
/* Get the full path to the local file */
fullpath = nsh_getfullpath(vtbl, args.destpath);
/* Then perform the TFTP get operation */
if (tftpget(args.srcpath, fullpath, args.ipaddr, args.binary) != OK)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "tftpget", NSH_ERRNO);
}
/* Release any allocated memory */
if (args.allocated)
{
free(args.destpath);
}
free(fullpath);
return OK;
}
#endif
#endif
/****************************************************************************
* Name: cmd_ifup
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_IFUPDOWN
int cmd_ifup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR char *ifname = NULL;
int ret;
if (argc != 2)
{
nsh_output(vtbl, "Please select ifname:\n");
return nsh_foreach_netdev(ifconfig_callback, vtbl, "ifup");
}
ifname = argv[1];
ret = netlib_ifup(ifname);
nsh_output(vtbl, "ifup %s...%s\n", ifname, (ret == OK) ? "OK" : "Failed");
return ret;
}
#endif
/****************************************************************************
* Name: cmd_ifdown
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_IFUPDOWN
int cmd_ifdown(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR char *ifname = NULL;
int ret;
if (argc != 2)
{
nsh_output(vtbl, "Please select ifname:\n");
return nsh_foreach_netdev(ifconfig_callback, vtbl, "ifdown");
}
ifname = argv[1];
ret = netlib_ifdown(ifname);
nsh_output(vtbl, "ifdown %s...%s\n",
ifname, (ret == OK) ? "OK" : "Failed");
return ret;
}
#endif
/****************************************************************************
* Name: cmd_ifconfig
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_IFCONFIG
int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
#ifdef CONFIG_NET_IPv4
struct in_addr addr;
in_addr_t gip = INADDR_ANY;
#endif
#ifdef CONFIG_NET_IPv6
struct in6_addr addr6;
#endif
int i;
FAR char *ifname = NULL;
FAR char *hostip = NULL;
FAR char *gwip = NULL;
FAR char *mask = NULL;
FAR char *tmp = NULL;
#ifdef HAVE_HWADDR
FAR char *hw = NULL;
#endif
#if defined(CONFIG_NETINIT_DHCPC) || defined(CONFIG_NETINIT_DNS)
FAR char *dns = NULL;
#endif
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
bool inet6 = false;
#endif
bool missingarg = true;
bool badarg = false;
#ifdef HAVE_HWADDR
mac_addr_t macaddr;
#endif
#if defined(CONFIG_NETINIT_DHCPC)
FAR void *handle;
#endif
int ret;
/* With one or no arguments, ifconfig simply shows the status of the
* network device:
*
* ifconfig
* ifconfig [interface]
*/
if (argc <= 2)
{
ret = nsh_foreach_netdev(ifconfig_callback, vtbl, "ifconfig");
if (ret < 0)
{
return ERROR;
}
net_statistics(vtbl);
return OK;
}
/* If both the network interface name and an IP address are supplied as
* arguments, then ifconfig will set the address of the Ethernet device:
*
* ifconfig ifname [ip_address] [named options]
*/
if (argc > 2)
{
for (i = 1; i < argc; i++)
{
if (i == 1)
{
ifname = argv[i];
missingarg = false;
}
else
{
tmp = argv[i];
if (!strcmp(tmp, "dr") || !strcmp(tmp, "gw") ||
!strcmp(tmp, "gateway"))
{
if (argc - 1 >= i + 1)
{
gwip = argv[i + 1];
i++;
}
else
{
badarg = true;
}
}
else if (!strcmp(tmp, "netmask"))
{
if (argc - 1 >= i + 1)
{
mask = argv[i + 1];
i++;
}
else
{
badarg = true;
}
}
else if (!strcmp(tmp, "inet"))
{
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
inet6 = false;
#elif !defined(CONFIG_NET_IPv4)
badarg = true;
#endif
}
else if (!strcmp(tmp, "inet6"))
{
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
inet6 = true;
#elif !defined(CONFIG_NET_IPv6)
badarg = true;
#endif
}
#ifdef HAVE_HWADDR
/* REVISIT: How will we handle Ethernet and SLIP together? */
else if (!strcmp(tmp, "hw"))
{
if (argc - 1 >= i + 1)
{
hw = argv[i + 1];
i++;
badarg = nsh_addrconv(hw, &macaddr);
}
else
{
badarg = true;
}
}
#endif
#if defined(CONFIG_NETINIT_DHCPC) || defined(CONFIG_NETINIT_DNS)
else if (!strcmp(tmp, "dns"))
{
if (argc - 1 >= i + 1)
{
dns = argv[i + 1];
i++;
}
else
{
badarg = true;
}
}
#endif
else if (i == 2)
{
hostip = tmp;
}
else
{
badarg = true;
}
}
}
}
if (missingarg)
{
nsh_error(vtbl, g_fmtargrequired, argv[0]);
return ERROR;
}
if (badarg)
{
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
return ERROR;
}
#ifdef HAVE_HWADDR
/* Set Hardware Ethernet MAC address */
if (hw != NULL)
{
ninfo("HW MAC: %s\n", hw);
nsh_sethwaddr(ifname, &macaddr);
}
#endif
/* Set IP address */
#ifdef CONFIG_NET_IPv6
#ifdef CONFIG_NET_IPv4
if (inet6)
#endif
{
if (hostip != NULL)
{
/* REVISIT: Should DHCPC check be used here too? */
ninfo("Host IP: %s\n", hostip);
inet_pton(AF_INET6, hostip, &addr6);
}
netlib_set_ipv6addr(ifname, &addr6);
}
#endif /* CONFIG_NET_IPv6 */
#ifdef CONFIG_NET_IPv4
#ifdef CONFIG_NET_IPv6
else
#endif
{
if (hostip != NULL)
{
#if defined(CONFIG_NETINIT_DHCPC)
if (strcmp(hostip, "dhcp") == 0)
{
/* Set DHCP addr */
ninfo("DHCPC Mode\n");
addr.s_addr = 0;
gip = 0;
}
else
#endif
{
/* Set host IP address */
ninfo("Host IP: %s\n", hostip);
addr.s_addr = inet_addr(hostip);
gip = addr.s_addr;
}
}
netlib_set_ipv4addr(ifname, &addr);
}
#endif /* CONFIG_NET_IPv4 */
/* Set gateway */
#ifdef CONFIG_NET_IPv6
#ifdef CONFIG_NET_IPv4
if (inet6)
#endif
{
/* Only set the gateway address if it was explicitly provided. */
if (gwip != NULL)
{
ninfo("Gateway: %s\n", gwip);
inet_pton(AF_INET6, gwip, &addr6);
netlib_set_dripv6addr(ifname, &addr6);
}
}
#endif /* CONFIG_NET_IPv6 */
#ifdef CONFIG_NET_IPv4
#ifdef CONFIG_NET_IPv6
else
#endif
{
if (gwip != NULL)
{
ninfo("Gateway: %s\n", gwip);
gip = addr.s_addr = inet_addr(gwip);
}
else
{
if (gip != 0)
{
ninfo("Gateway: default\n");
gip = NTOHL(gip);
gip &= ~0x000000ff;
gip |= 0x00000001;
gip = HTONL(gip);
}
addr.s_addr = gip;
}
netlib_set_dripv4addr(ifname, &addr);
}
#endif /* CONFIG_NET_IPv4 */
/* Set network mask */
#ifdef CONFIG_NET_IPv6
#ifdef CONFIG_NET_IPv4
if (inet6)
#endif
{
if (mask != NULL)
{
ninfo("Netmask: %s\n", mask);
inet_pton(AF_INET6, mask, &addr6);
}
else
{
ninfo("Netmask: Default\n");
inet_pton(AF_INET6, "ffff:ffff:ffff:ffff::", &addr6);
}
netlib_set_ipv6netmask(ifname, &addr6);
}
#endif /* CONFIG_NET_IPv6 */
#ifdef CONFIG_NET_IPv4
#ifdef CONFIG_NET_IPv6
else
#endif
{
if (mask != NULL)
{
ninfo("Netmask: %s\n", mask);
addr.s_addr = inet_addr(mask);
}
else
{
ninfo("Netmask: Default\n");
addr.s_addr = inet_addr("255.255.255.0");
}
netlib_set_ipv4netmask(ifname, &addr);
}
#endif /* CONFIG_NET_IPv4 */
UNUSED(ifname); /* Not used in all configurations */
#if defined(CONFIG_NETINIT_DHCPC) || defined(CONFIG_NETINIT_DNS)
#ifdef CONFIG_NET_IPv6
#ifdef CONFIG_NET_IPv4
if (inet6)
#endif
{
#warning Missing Logic
}
#endif /* CONFIG_NET_IPv6 */
#ifdef CONFIG_NET_IPv4
#ifdef CONFIG_NET_IPv6
else
#endif
{
if (dns != NULL)
{
ninfo("DNS: %s\n", dns);
addr.s_addr = inet_addr(dns);
}
else
{
ninfo("DNS: Default\n");
addr.s_addr = gip;
}
netlib_set_ipv4dnsaddr(&addr);
}
#endif /* CONFIG_NET_IPv4 */
#endif /* CONFIG_NETINIT_DHCPC || CONFIG_NETINIT_DNS */
#if defined(CONFIG_NETINIT_DHCPC)
/* Get the MAC address of the NIC */
if (!gip)
{
netlib_getmacaddr("eth0", macaddr);
/* Set up the DHCPC modules */
handle = dhcpc_open("eth0", &macaddr, IFHWADDRLEN);
/* Get an IP address. Note that there is no logic for renewing the IP
* address in this example. The address should be renewed in
* ds.lease_time/2 seconds.
*/
if (handle != NULL)
{
struct dhcpc_state ds;
dhcpc_request(handle, &ds);
netlib_set_ipv4addr("eth0", &ds.ipaddr);
if (ds.netmask.s_addr != 0)
{
netlib_set_ipv4netmask("eth0", &ds.netmask);
}
if (ds.default_router.s_addr != 0)
{
netlib_set_dripv4addr("eth0", &ds.default_router);
}
if (ds.dnsaddr.s_addr != 0)
{
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
}
dhcpc_close(handle);
}
}
#endif
#if !defined(CONFIG_NET_IPv4) && !defined(CONFIG_NET_IPv6)
UNUSED(hostip);
UNUSED(mask);
UNUSED(gwip);
#endif
#ifdef CONFIG_NET_IPv4
UNUSED(gip);
#endif
return OK;
}
#endif
/****************************************************************************
* Name: cmd_nslookup
****************************************************************************/
#if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP)
int cmd_nslookup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR struct addrinfo *info;
FAR struct addrinfo *next;
char buffer[48];
int ret;
/* We should be guaranteed this by the command line parser */
DEBUGASSERT(argc == 2);
/* Get the matching address + canonical name */
ret = getaddrinfo(argv[1], NULL, NULL, &info);
if (ret != OK)
{
nsh_error(vtbl, g_fmtcmdfailed,
argv[0], "getaddrinfo", NSH_HERRNO_OF(ret));
return ERROR;
}
for (next = info; next != NULL; next = next->ai_next)
{
/* Convert the address to a string */
ret = getnameinfo(next->ai_addr, next->ai_addrlen,
buffer, sizeof(buffer), NULL, 0, NI_NUMERICHOST);
if (ret != OK)
{
freeaddrinfo(info);