annotate toys/pending/ifconfig.c @ 864:764df39f62b4

More ifconfig cleanup.
author Rob Landley <rob@landley.net>
date Tue, 16 Apr 2013 23:49:47 -0500
parents 493f412fc5da
children 0fa773e2a4fe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* ifconfig.c - Configure network interface.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
3 * Copyright 2012 Ranjan Kumar <ranjankumar.bth@gmail.com>
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
4 * Copyright 2012 Kyungwan Han <asura321@gamil.com>
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
5 * Reviewed by Kyungsu Kim <kaspyx@gmail.com>
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * Not in SUSv4.
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
8
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 USE_IFCONFIG(NEWTOY(ifconfig, "?a", TOYFLAG_BIN))
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
10
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config IFCONFIG
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "ifconfig"
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
13 default n
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 usage: ifconfig [-a] interface [address]
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
16
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 Configure network interface.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 [add ADDRESS[/PREFIXLEN]]
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 [del ADDRESS[/PREFIXLEN]]
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 [[-]broadcast [ADDRESS]] [[-]pointopoint [ADDRESS]]
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 [netmask ADDRESS] [dstaddr ADDRESS]
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 [outfill NN] [keepalive NN]
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 [hw ether|infiniband ADDRESS] [metric NN] [mtu NN]
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 [[-]trailers] [[-]arp] [[-]allmulti]
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 [multicast] [[-]promisc] [txqueuelen NN] [[-]dynamic]
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 [mem_start NN] [io_addr NN] [irq NN]
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 [up|down] ...
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 */
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
30
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 #define FOR_ifconfig
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 #include "toys.h"
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 #include "toynet.h"
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
34
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 #include <net/if.h>
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 #include <net/if_arp.h>
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 #include <net/ethernet.h>
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
38
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 typedef struct sockaddr_with_len {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 union {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 struct sockaddr sock;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 struct sockaddr_in sock_in;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 struct sockaddr_in6 sock_in6;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 }sock_u;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 socklen_t socklen;
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
46 } sockaddr_with_len;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
47
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
48 unsigned get_strtou(char *, char **, int);
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
49 char *address_to_name(struct sockaddr *);
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
50 sockaddr_with_len *get_sockaddr(char *, int, sa_family_t);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
51
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 typedef struct _proc_net_dev_info {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 char ifrname[IFNAMSIZ]; //interface name.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 unsigned long long receive_bytes; //total bytes received
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 unsigned long long receive_packets; //total packets received
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 unsigned long receive_errors; //bad packets received
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 unsigned long receive_drop; //no space in linux buffers
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 unsigned long receive_fifo; //receiver fifo overrun
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 unsigned long receive_frame; //received frame alignment error
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 unsigned long receive_compressed;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 unsigned long receive_multicast; //multicast packets received
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
62
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 unsigned long long transmit_bytes; //total bytes transmitted
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 unsigned long long transmit_packets; //total packets transmitted
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 unsigned long transmit_errors; //packet transmit problems
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 unsigned long transmit_drop; //no space available in linux
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 unsigned long transmit_fifo;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 unsigned long transmit_colls;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 unsigned long transmit_carrier;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 unsigned long transmit_compressed; //num_tr_compressed;
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
71 } PROC_NET_DEV_INFO;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
72
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
73 // man netdevice
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 typedef struct _iface_list {
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
75 struct _iface_list *next;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 int hw_type;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 short ifrflags; //used for addr, broadcast, and mask.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 short ifaddr; //if set print ifraddr, irrdstaddr, ifrbroadaddr and ifrnetmask.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 struct sockaddr ifraddr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 struct sockaddr ifrdstaddr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 struct sockaddr ifrbroadaddr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 struct sockaddr ifrnetmask;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 struct sockaddr ifrhwaddr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 int ifrmtu;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 int ifrmetric;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
86 PROC_NET_DEV_INFO dev_info;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 int txqueuelen;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 struct ifmap ifrmap;
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
89 int non_virtual_iface;
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
90 } IFACE_LIST;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
91
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
92
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 #define HW_NAME_LEN 20
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 #define HW_TITLE_LEN 30
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
95
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 typedef struct _hw_info {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 char hw_name[HW_NAME_LEN];
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 char hw_title[HW_TITLE_LEN];
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 int hw_addrlen;
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
100 } HW_INFO;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
101
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
102 static char *field_format[] = {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
103 "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u",
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u"
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 };
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
107
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 #define NO_RANGE -1
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 #define IO_MAP_INDEX 0x100
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
110
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 static int show_iface(char *iface_name);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
112 static void print_ip6_addr(IFACE_LIST *l_ptr);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 static void clear_list(void);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
114
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 //from /net/if.h
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 static char *iface_flags_str[] = {
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
117 "UP", "BROADCAST", "DEBUG", "LOOPBACK", "POINTOPOINT", "NOTRAILERS",
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
118 "RUNNING", "NOARP", "PROMISC", "ALLMULTI", "MASTER", "SLAVE", "MULTICAST",
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
119 "PORTSEL", "AUTOMEDIA", "DYNAMIC", NULL
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 };
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 //from /usr/include/linux/netdevice.h
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 #ifdef IFF_PORTSEL
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 //Media selection options.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
124 # ifndef IF_PORT_UNKNOWN
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 enum {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 IF_PORT_UNKNOWN = 0,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 IF_PORT_10BASE2,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 IF_PORT_10BASET,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 IF_PORT_AUI,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
130 IF_PORT_100BASET,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 IF_PORT_100BASETX,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 IF_PORT_100BASEFX
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 };
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
134 # endif
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 #endif
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
136
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 //from kernel header ipv6.h
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
138 #define IPV6_ADDR_ANY 0x0000U
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
139 #define IPV6_ADDR_LOOPBACK 0x0010U
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 #define IPV6_ADDR_LINKLOCAL 0x0020U
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
141 #define IPV6_ADDR_SITELOCAL 0x0040U
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 #define IPV6_ADDR_COMPATv4 0x0080U
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
143
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
144 //for the param settings.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
145
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
146 //for ipv6 add/del
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
147 struct ifreq_inet6 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 struct in6_addr ifrinte6_addr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
149 uint32_t ifrinet6_prefixlen;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
150 int ifrinet6_ifindex;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
151 };
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
152
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 #ifndef SIOCSKEEPALIVE
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 # define SIOCSKEEPALIVE (SIOCDEVPRIVATE) /* Set keepalive timeout in sec */
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 # define SIOCGKEEPALIVE (SIOCDEVPRIVATE+1) /* Get keepalive timeout */
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
156 #endif
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
157
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 #ifndef SIOCSOUTFILL
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
159 # define SIOCSOUTFILL (SIOCDEVPRIVATE+2) /* Set outfill timeout */
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
160 # define SIOCGOUTFILL (SIOCDEVPRIVATE+3) /* Get outfill timeout */
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 #endif
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
162
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 #ifndef INFINIBAND_ALEN
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
164 # define INFINIBAND_ALEN 20
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
165 #endif
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
166
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 static void set_flags(int sockfd, struct ifreq *ifre, int arg_flag, int flag); //verify
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
168 static void set_mtu(int sockfd, struct ifreq *ifre, char *mtu); //verify
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
169 static void set_metric(int sockfd, struct ifreq *ifre, char *metric); //verify
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
170 static void set_qlen(int sockfd, struct ifreq *ifre, char *qlen); //verify
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
171 static void set_address(int sockfd, char *host_name, struct ifreq *ifre, int request, char *req_name);
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
172 static void set_hw_address(int sockfd, char ***argv, struct ifreq *ifre, int request, char *req_name);
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
173 static void set_ipv6_addr(int sockfd, struct ifreq *ifre, char *ipv6_addr, int request, char *req_name);
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
174 static void set_memstart(int sockfd, struct ifreq *ifre, char *start_addr, int request, char *req_name);
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
175 static void set_ioaddr(int sockfd, struct ifreq *ifre, char *baddr, int request, char *req_name);
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
176 static void set_irq(int sockfd, struct ifreq *ifre, char *irq_val, int request, char *req_name);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
177
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
178 void xioctl(int fd, int request, void *data)
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
179 {
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
180 if (ioctl(fd, request, data) < 0) perror_exit("ioctl %d", request);
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
181 }
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
182
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
183 char *omit_whitespace(char *s)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
184 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 while(*s == ' ' || (unsigned char)(*s - 9) <= (13 - 9)) s++;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
186 return (char *) s;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
187 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
188
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
189 char *safe_strncpy(char *dst, char *src, size_t size)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
190 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 if(!size) return dst;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
192 dst[--size] = '\0';
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 return strncpy(dst, src, size);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
194 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
195
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
196 /*
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
197 * verify the host is local unix path.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 * if so, set the swl input param accordingly.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
199 */
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
200 static int is_host_unix(char *host, sockaddr_with_len **swl)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
201 {
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
202 if (strncmp(host, "local:", 6) == 0) {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
203 struct sockaddr_un *sockun;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
204
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
205 *swl = xzalloc(sizeof(struct sockaddr_with_len));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 (*swl)->socklen = sizeof(struct sockaddr_un);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
207 (*swl)->sock_u.sock.sa_family = AF_UNIX;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
208 sockun = (struct sockaddr_un *)&(*swl)->sock_u.sock;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
209 safe_strncpy(sockun->sun_path, host + 6, sizeof(sockun->sun_path));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
210 return 1;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
211 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
212 return 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
213 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
214
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
215 /*
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
216 * validate the input param (host) for valid ipv6 ip and extract port number (if there).
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
217 */
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
218 static void get_host_and_port(char **host, int *port)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
220 char *ch_ptr;
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
221 char *org_host = *host;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
222 if (*host[0] == '[') {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
223 (*host)++;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 ch_ptr = strchr(*host, ']');
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
225 if (!ch_ptr || (ch_ptr[1] != ':' && ch_ptr[1] != '\0'))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
226 error_exit("bad address '%s'", org_host);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
227 } else {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 ch_ptr = strrchr(*host, ':');
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
229 //There is more than one ':' like "::1"
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
230 if(ch_ptr && strchr(*host, ':') != ch_ptr) ch_ptr = NULL;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
231 }
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
232 if (ch_ptr) { //pointer to ":" or "]:"
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
233 int size = ch_ptr - (*host) + 1;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
234 safe_strncpy(*host, *host, size);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
235 if (*ch_ptr != ':') {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
236 ch_ptr++; //skip ']'
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
237 //[nn] without port
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
238 if (!*ch_ptr) return;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
239 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 ch_ptr++; //skip ':' to get the port number.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
241 *port = get_strtou(ch_ptr, NULL, 10);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
242 if (errno || (unsigned)*port > 65535) error_exit("bad port '%s'", org_host);
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
243 }
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
244 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
245
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
246 /*
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
247 * used to extract the address info from the given host ip
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
248 * and update the swl param accordingly.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
249 */
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
250 static int get_socket_stream(char *host, sa_family_t af, sockaddr_with_len **swl)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
251 {
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
252 struct addrinfo hints, *result, *rp;
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
253 int status;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
254
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
255 memset(&hints, 0 , sizeof(struct addrinfo));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
256 hints.ai_family = af;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
257 hints.ai_socktype = SOCK_STREAM;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
258
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
259 status = getaddrinfo(host, NULL, &hints, &result);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
260 if (status) error_exit("bad address '%s' : %s", host, gai_strerror(status));
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
261
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
262 for (rp = result; rp; rp = rp->ai_next) {
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
263 if (rp->ai_family == AF_INET || rp->ai_family == AF_INET6) {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
264 *swl = xmalloc(sizeof(struct sockaddr_with_len));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
265 (*swl)->socklen = rp->ai_addrlen;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
266 memcpy(&((*swl)->sock_u.sock), rp->ai_addr, rp->ai_addrlen);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
267 break;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
268 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
269 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
270 freeaddrinfo(result);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
271 return rp ? 0 : -1;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
272 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
273
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
274 /*
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
275 * use to get the socket address with the given host ip.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
276 */
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
277 sockaddr_with_len *get_sockaddr(char *host, int port, sa_family_t af)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
278 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
279 sockaddr_with_len *swl = NULL;
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
280 in_port_t port_num = htons(port);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
281
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
282 if(is_host_unix(host, &swl) && swl) return swl;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
283
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
284 //[IPV6_ip]:port_num
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
285 if(host[0] == '[' || strrchr(host, ':')) get_host_and_port((char **)&host, &port);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
286
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
287 if (get_socket_stream(host, af, &swl)) return NULL;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
288
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
289 if(swl->sock_u.sock.sa_family == AF_INET)
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
290 swl->sock_u.sock_in.sin_port = port_num;
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
291 else if(swl->sock_u.sock.sa_family == AF_INET6)
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
292 swl->sock_u.sock_in6.sin6_port = port_num;
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
293
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
294 return swl;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
295 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
296
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
297 /*
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
298 * get the numeric hostname and service name, for a given socket address.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
299 */
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
300 char *address_to_name(struct sockaddr *sock)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
301 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
302 //man page of getnameinfo.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
303 char hbuf[NI_MAXHOST] = {0,}, sbuf[NI_MAXSERV] = {0,};
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
304 int status = 0;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
305
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
306 if(sock->sa_family == AF_INET) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
307 socklen_t len = sizeof(struct sockaddr_in);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
308 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
309 return xmsprintf("%s:%s", hbuf, sbuf);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
310 else {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
311 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
312 return NULL;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
313 }
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
314 } else if(sock->sa_family == AF_INET6) {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
315 socklen_t len = sizeof(struct sockaddr_in6);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
316 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
317 //verification for resolved hostname.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
318 if(strchr(hbuf, ':')) return xmsprintf("[%s]:%s", hbuf, sbuf);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
319 else return xmsprintf("%s:%s", hbuf, sbuf);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
320 } else {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
321 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
322 return NULL;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
323 }
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
324 } else if(sock->sa_family == AF_UNIX) {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
325 struct sockaddr_un *sockun = (void*)sock;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
326 return xmsprintf("local:%.*s", (int) sizeof(sockun->sun_path), sockun->sun_path);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
327 } else return NULL;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
328 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
329
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
330 /*
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
331 * used to converts string into int and validate the input str for invalid int value or out-of-range.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
332 */
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
333 unsigned get_strtou(char *str, char **endp, int base)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
334 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
335 unsigned long uli;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
336 char *endptr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
337
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
338 if(!isalnum(str[0])) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
339 errno = ERANGE;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
340 return UINT_MAX;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
341 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
342 errno = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
343 uli = strtoul(str, &endptr, base);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
344 if(uli > UINT_MAX) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
345 errno = ERANGE;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
346 return UINT_MAX;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
347 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
348
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
349 if(endp) *endp = endptr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
350 if(endptr[0]) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
351 if(isalnum(endptr[0]) || errno) { //"123abc" or out-of-range
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
352 errno = ERANGE;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
353 return UINT_MAX;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
354 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
355 errno = EINVAL;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
356 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
357 return uli;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
358 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
359
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
360
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
361 IFACE_LIST *iface_list_head;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
362
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
363 void ifconfig_main(void)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
364 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
365 char **argv = toys.optargs;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
366
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
367 if(*argv && (strcmp(*argv, "--help") == 0)) show_help();
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
368
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
369 //"ifconfig" / "ifconfig eth0"
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
370 if(!argv[0] || !argv[1]) { //one or no argument
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
371 toys.exitval = show_iface(*argv);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
372 //free allocated memory.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
373 clear_list();
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
374 return;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
375 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
376
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
377 //set ifconfig params.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
378 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
379 struct ifreq ifre;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
380 int sockfd = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
381 //get interface name
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
382 memset(&ifre, 0, sizeof(struct ifreq));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
383 strncpy(ifre.ifr_name, *argv, IFNAMSIZ);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
384 ifre.ifr_name[IFNAMSIZ-1] = 0;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
385 if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) perror_exit("socket");
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
386
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
387 while(*++argv != NULL) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
388 /* flags settings */
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
389 if (!strcmp(*argv, "up"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
390 set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
391 else if (!strcmp(*argv, "down"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
392 set_flags(sockfd, &ifre, 0, IFF_UP);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
393
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
394 else if (!strcmp(*argv, "arp"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
395 set_flags(sockfd, &ifre, 0, IFF_NOARP);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
396 else if (!strcmp(*argv, "-arp"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
397 set_flags(sockfd, &ifre, IFF_NOARP, 0);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
398 else if (!strcmp(*argv, "trailers"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
399 set_flags(sockfd, &ifre, 0, IFF_NOTRAILERS);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
400 else if (!strcmp(*argv, "-trailers"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
401 set_flags(sockfd, &ifre, IFF_NOTRAILERS, 0);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
402
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
403 else if (!strcmp(*argv, "promisc"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
404 set_flags(sockfd, &ifre, IFF_PROMISC, 0);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
405 else if (!strcmp(*argv, "-promisc"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
406 set_flags(sockfd, &ifre, 0, IFF_PROMISC);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
407 else if (!strcmp(*argv, "allmulti"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
408 set_flags(sockfd, &ifre, IFF_ALLMULTI, 0);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
409 else if (!strcmp(*argv, "-allmulti"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
410 set_flags(sockfd, &ifre, 0, IFF_ALLMULTI);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
411 else if (!strcmp(*argv, "multicast"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
412 set_flags(sockfd, &ifre, IFF_MULTICAST, 0);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
413 else if (!strcmp(*argv, "-multicast"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
414 set_flags(sockfd, &ifre, 0, IFF_MULTICAST);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
415 else if (!strcmp(*argv, "dynamic"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
416 set_flags(sockfd, &ifre, IFF_DYNAMIC, 0);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
417 else if (!strcmp(*argv, "-dynamic"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
418 set_flags(sockfd, &ifre, 0, IFF_DYNAMIC);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
419 else if (!strcmp(*argv, "-pointopoint"))
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
420 set_flags(sockfd, &ifre, 0, IFF_POINTOPOINT);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
421 /*value setup */
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
422 else if (!strcmp(*argv, "pointopoint")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
423 if (!*++argv) show_help();
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
424 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
425 set_flags(sockfd, &ifre, IFF_POINTOPOINT, 0);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
426 } else if (!strcmp(*argv, "netmask")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
427 if (!*++argv) show_help();
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
428 set_address(sockfd, *argv, &ifre, SIOCSIFNETMASK, "SIOCSIFNETMASK");
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
429 } else if (!strcmp(*argv, "-broadcast")) {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
430 set_flags(sockfd, &ifre, 0, IFF_BROADCAST);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
431 } else if (!strcmp(*argv, "broadcast")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
432 if (!*++argv) show_help();
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
433 set_address(sockfd, *argv, &ifre, SIOCSIFBRDADDR, "SIOCSIFBRDADDR");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
434 set_flags(sockfd, &ifre, IFF_BROADCAST, 0);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
435 } else if (!strcmp(*argv, "dstaddr")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
436 if (!*++argv) show_help();
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
437 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR");
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
438 } else if (!strcmp(*argv, "hw")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
439 if (!*++argv) show_help();
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
440 set_hw_address(sockfd, &argv, &ifre, SIOCSIFHWADDR, "SIOCSIFHWADDR");
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
441 } else if (!strcmp(*argv, "mtu")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
442 if (!*++argv) show_help();
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
443 set_mtu(sockfd, &ifre, *argv);
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
444 } else if (!strcmp(*argv, "metric")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
445 if (!*++argv) show_help();
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
446 set_metric(sockfd, &ifre, *argv);
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
447 } else if (!strcmp(*argv, "txqueuelen")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
448 if (!*++argv) show_help();
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
449 set_qlen(sockfd, &ifre, *argv);
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
450 } else if (!strcmp(*argv, "keepalive")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
451 if (!*++argv) show_help();
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
452 ifre.ifr_data = (void *)strtoul(*argv, 0, 0);
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
453 xioctl(sockfd, SIOCSKEEPALIVE, &ifre);
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
454 } else if (!strcmp(*argv, "outfill")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
455 if (!*++argv) show_help();
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
456 ifre.ifr_data = (void *)strtoul(*argv, 0, 0);
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
457 xioctl(sockfd, SIOCSOUTFILL, &ifre);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
458 } else if (!strcmp(*argv, "add")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
459 if (!*++argv) show_help();
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
460 set_ipv6_addr(sockfd, &ifre, *argv, SIOCSIFADDR, "SIOCSIFADDR");
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
461 } else if (!strcmp(*argv, "del")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
462 if (!*++argv) show_help();
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
463 set_ipv6_addr(sockfd, &ifre, *argv, SIOCDIFADDR, "SIOCDIFADDR");
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
464 } else if (!strcmp(*argv, "mem_start")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
465 if (!*++argv) show_help();
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
466 set_memstart(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP");
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
467 } else if (!strcmp(*argv, "io_addr")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
468 if (!*++argv) show_help();
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
469 set_ioaddr(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP");
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
470 } else if (!strcmp(*argv, "irq")) {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
471 if (!*++argv) show_help();
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
472 set_irq(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP");
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
473 } else {
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
474 if (isdigit(**argv) || !strcmp(*argv, "default")) {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
475 char *iface_name = ifre.ifr_name;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
476 short int is_colon = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
477 set_address(sockfd, *argv, &ifre, SIOCSIFADDR, "SIOCSIFADDR");
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
478 while (*iface_name) {
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
479 if (*iface_name == ':') {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
480 is_colon = 1;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
481 break;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
482 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
483 iface_name++;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
484 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
485 //if the interface name is not an alias; set the flag and continue.
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
486 if(!is_colon) set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
487 } else if (!strcmp(*argv, "inet") || !strcmp(*argv, "inet6")) continue;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
488 else {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
489 errno = EINVAL;
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
490 toys.exithelp++;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
491 error_exit("bad argument");
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
492 }
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
493 }
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
494
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
495 }
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
496 if(sockfd > 0) close(sockfd);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
497 }
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
498 }
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
499
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
500
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
501 static void set_flags(int sockfd, struct ifreq *ifre, int set_flag, int reset_flag)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
502 {
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
503 xioctl(sockfd, SIOCGIFFLAGS, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
504 ifre->ifr_flags = (ifre->ifr_flags & (~reset_flag)) | set_flag;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
505 xioctl(sockfd, SIOCSIFFLAGS, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
506 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
507
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
508 static void set_mtu(int sockfd, struct ifreq *ifre, char *mtu)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
509 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
510 ifre->ifr_mtu = strtoul(mtu, NULL, 0);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
511 xioctl(sockfd, SIOCSIFMTU, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
512 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
513
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
514 static void set_metric(int sockfd, struct ifreq *ifre, char *metric)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
515 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
516 ifre->ifr_metric = strtoul(metric, NULL, 0);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
517 xioctl(sockfd, SIOCSIFMETRIC, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
518 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
519
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
520 static void set_qlen(int sockfd, struct ifreq *ifre, char *qlen)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
521 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
522 ifre->ifr_qlen = strtoul(qlen, NULL, 0);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
523 xioctl(sockfd, SIOCSIFTXQLEN, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
524 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
525
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
526 static void set_ipv6_addr(int sockfd, struct ifreq *ifre, char *ipv6_addr, int request, char *req_name)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
527 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
528 char *prefix;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
529 int plen = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
530 sockaddr_with_len *swl = NULL;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
531
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
532 prefix = strchr(ipv6_addr, '/');
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
533 if(prefix) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
534 plen = get_int_value(prefix + 1, 0, 128);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
535 *prefix = '\0';
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
536 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
537 swl = get_sockaddr(ipv6_addr, 0, AF_INET6);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
538 if(!swl) error_exit("error in resolving host name");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
539 int sockfd6;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
540 struct ifreq_inet6 ifre6;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
541 memcpy((char *) &ifre6.ifrinte6_addr,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
542 (char *) &(swl->sock_u.sock_in6.sin6_addr),
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
543 sizeof(struct in6_addr));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
544 //Create a channel to the NET kernel.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
545 if( (sockfd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
546 perror_exit("AF_INET6 SOCK_DGRAM", 0);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
547 xioctl(sockfd6, SIOGIFINDEX, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
548 ifre6.ifrinet6_ifindex = ifre->ifr_ifindex;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
549 ifre6.ifrinet6_prefixlen = plen;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
550
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
551 xioctl(sockfd6, request, &ifre6);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
552 if(swl != NULL) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
553 free(swl);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
554 swl = NULL;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
555 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
556 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
557
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
558 static void set_address(int sockfd, char *host_name, struct ifreq *ifre, int request, char *req_name)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
559 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
560 struct sockaddr_in sock_in;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
561 sockaddr_with_len *swl = NULL;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
562 sock_in.sin_family = AF_INET;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
563 sock_in.sin_port = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
564
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
565 //Default 0.0.0.0
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
566 if(strcmp(host_name, "default") == 0) sock_in.sin_addr.s_addr = INADDR_ANY;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
567 else {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
568 swl = get_sockaddr(host_name, 0, AF_INET);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
569 if(!swl) error_exit("error in resolving host name");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
570
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
571 sock_in.sin_addr = swl->sock_u.sock_in.sin_addr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
572 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
573 memcpy((char *)&ifre->ifr_addr, (char *) &sock_in, sizeof(struct sockaddr));
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
574 xioctl(sockfd, request, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
575
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
576 if(swl != NULL) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
577 free(swl);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
578 swl = NULL;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
579 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
580 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
581
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
582 static int hex_to_binary(char *hw_addr, struct sockaddr *sock, int count)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
583 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
584 int i = 0, j = 0;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
585 unsigned char nib_val, ch;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
586
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
587 char *ptr = (char *) sock->sa_data;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
588 if (count == ETH_ALEN) sock->sa_family = ARPHRD_ETHER;
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
589 else if (count == INFINIBAND_ALEN) sock->sa_family = ARPHRD_INFINIBAND;
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
590 else return -1;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
591 //e.g. hw_addr "62:2D:A6:9E:2D:BE"
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
592 for (; *hw_addr && (i < count); i++) {
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
593 if (*hw_addr == ':') hw_addr++;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
594 j = nib_val = 0;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
595 for (;j < 2; j++) {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
596 ch = *hw_addr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
597 //0-9 = 10 chars.
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
598 if (((unsigned char)(ch - '0')) < 10) ch = (ch - '0');
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
599 //a-f = 6 chars.
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
600 else if (((unsigned char)((ch) - 'a')) < 6) ch = (ch - ('a'-10));
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
601 //A-F = 6 chars.
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
602 else if (((unsigned char)((ch) - 'A')) < 6) ch = (ch - ('A'-10));
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
603 else if (j && (ch == ':' || ch == 0)) break;
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
604 else return -1;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
605 hw_addr++;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
606 nib_val <<= 4;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
607 nib_val += ch;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
608 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
609 *ptr++ = nib_val;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
610 }
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
611 if (*hw_addr) return -1;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
612 return 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
613 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
614
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
615 static void set_hw_address(int sockfd, char ***argv, struct ifreq *ifre, int request, char *req_name)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
616 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
617 int hw_class = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
618 char *hw_addr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
619 struct sockaddr sock;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
620 char *ptr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
621 char *hw_class_strings[] = {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
622 "ether",
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
623 "infiniband",
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
624 NULL
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
625 };
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
626
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
627 if(strcmp(hw_class_strings[0], **argv) == 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
628 hw_class = 1;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
629 else if(strcmp(hw_class_strings[1], **argv) == 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
630 hw_class = 2;
861
35b059e1654b Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents: 856
diff changeset
631 if(!hw_class || !(*argv += 1)) {
35b059e1654b Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents: 856
diff changeset
632 errno = EINVAL;
35b059e1654b Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents: 856
diff changeset
633 toys.exithelp++;
35b059e1654b Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents: 856
diff changeset
634 error_exit("bad hardware class");
35b059e1654b Isaac Dunham's help string cleanup.
Rob Landley <rob@landley.net>
parents: 856
diff changeset
635 }
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
636
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
637 memset(&sock, 0, sizeof(struct sockaddr));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
638 hw_addr = **argv;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
639 if(hw_class == 1) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
640 if(hex_to_binary(hw_addr, &sock, ETH_ALEN))
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
641 error_exit("invalid hw-addr %s", hw_addr);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
642 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
643 else {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
644 if(hex_to_binary(hw_addr, &sock, INFINIBAND_ALEN))
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
645 error_exit("invalid hw-addr %s", hw_addr);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
646 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
647 ptr = (char *)&sock;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
648 memcpy( ((char *) ifre) + offsetof(struct ifreq, ifr_hwaddr), ptr, sizeof(struct sockaddr));
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
649 xioctl(sockfd, request, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
650 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
651
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
652 static void set_memstart(int sockfd, struct ifreq *ifre, char *start_addr, int request, char *req_name)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
653 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
654 unsigned long mem_start = strtoul(start_addr, NULL, 0);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
655
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
656 xioctl(sockfd, SIOCGIFMAP, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
657 ifre->ifr_map.mem_start = mem_start;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
658 xioctl(sockfd, request, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
659 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
660
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
661 static void set_ioaddr(int sockfd, struct ifreq *ifre, char *baddr, int request, char *req_name)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
662 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
663 unsigned short int base_addr = strtoul(baddr, NULL, 0);
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
664 xioctl(sockfd, SIOCGIFMAP, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
665 ifre->ifr_map.base_addr = base_addr;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
666 xioctl(sockfd, request, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
667 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
668
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
669 static void set_irq(int sockfd, struct ifreq *ifre, char *irq_val, int request, char *req_name)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
670 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
671 unsigned short int irq = strtoul(irq_val, NULL, 0);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
672 char *ptr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
673 struct ifmap *map;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
674
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
675 xioctl(sockfd, SIOCGIFMAP, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
676
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
677 ptr = ((char *) ifre) + offsetof(struct ifreq, ifr_map);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
678 map = (struct ifmap *)ptr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
679 map->irq = irq;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
680 xioctl(sockfd, request, ifre);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
681 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
682
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
683 /* Display ifconfig info. */
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
684 static void get_proc_info(char *buff, IFACE_LIST *l_ptr, int version)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
685 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
686 char *name;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
687 memset(&l_ptr->dev_info, 0, sizeof(PROC_NET_DEV_INFO));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
688
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
689 buff = omit_whitespace(buff);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
690 name = strsep(&buff, ":");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
691 if(!buff)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
692 error_exit("error in getting the device name:");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
693
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
694 if(strlen(name) < (IFNAMSIZ)) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
695 strncpy(l_ptr->dev_info.ifrname, name, IFNAMSIZ-1);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
696 l_ptr->dev_info.ifrname[IFNAMSIZ-1] = '\0';
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
697 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
698 else {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
699 l_ptr->dev_info.ifrname[0] = '\0';
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
700 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
701
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
702 sscanf(buff, field_format[version],
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
703 &l_ptr->dev_info.receive_bytes,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
704 &l_ptr->dev_info.receive_packets,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
705 &l_ptr->dev_info.receive_errors,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
706 &l_ptr->dev_info.receive_drop,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
707 &l_ptr->dev_info.receive_fifo,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
708 &l_ptr->dev_info.receive_frame,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
709 &l_ptr->dev_info.receive_compressed,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
710 &l_ptr->dev_info.receive_multicast,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
711 &l_ptr->dev_info.transmit_bytes,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
712 &l_ptr->dev_info.transmit_packets,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
713 &l_ptr->dev_info.transmit_errors,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
714 &l_ptr->dev_info.transmit_drop,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
715 &l_ptr->dev_info.transmit_fifo,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
716 &l_ptr->dev_info.transmit_colls,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
717 &l_ptr->dev_info.transmit_carrier,
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
718 &l_ptr->dev_info.transmit_compressed
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
719 );
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
720
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
721 if(version == 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
722 l_ptr->dev_info.receive_bytes = l_ptr->dev_info.transmit_bytes = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
723 if(version == 1)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
724 l_ptr->dev_info.receive_multicast = l_ptr->dev_info.receive_compressed = l_ptr->dev_info.transmit_compressed = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
725 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
726
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
727 static void add_iface_to_list(IFACE_LIST *newnode)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
728 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
729 IFACE_LIST *head_ref = iface_list_head;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
730
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
731 if((head_ref == NULL) || strcmp(newnode->dev_info.ifrname, head_ref->dev_info.ifrname) < 0) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
732 newnode->next = head_ref;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
733 head_ref = newnode;
862
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
734 } else {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
735 IFACE_LIST *current = head_ref;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
736 while(current->next != NULL && (strcmp(current->next->dev_info.ifrname, newnode->dev_info.ifrname)) < 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
737 current = current->next;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
738 newnode->next = current->next;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
739 current->next = newnode;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
740 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
741 iface_list_head = head_ref;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
742 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
743
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
744 static int get_device_info(IFACE_LIST *l_ptr)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
745 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
746 struct ifreq ifre;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
747 char *ifrname = l_ptr->dev_info.ifrname;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
748 int sokfd;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
749
862
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
750 if ((sokfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return sokfd;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
751 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
752 if(ioctl(sokfd, SIOCGIFFLAGS, &ifre) < 0) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
753 close(sokfd);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
754 return NO_RANGE;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
755 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
756 l_ptr->ifrflags = ifre.ifr_flags;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
757
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
758 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
759 if(ioctl(sokfd, SIOCGIFHWADDR, &ifre) >= 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
760 memcpy(l_ptr->ifrhwaddr.sa_data, ifre.ifr_hwaddr.sa_data, sizeof(l_ptr->ifrhwaddr.sa_data));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
761
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
762 l_ptr->hw_type = ifre.ifr_hwaddr.sa_family;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
763
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
764 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
765 if(ioctl(sokfd, SIOCGIFMETRIC, &ifre) >= 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
766 l_ptr->ifrmetric = ifre.ifr_metric;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
767
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
768 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
769 if(ioctl(sokfd, SIOCGIFMTU, &ifre) >= 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
770 l_ptr->ifrmtu = ifre.ifr_mtu;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
771
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
772 #ifdef SIOCGIFMAP
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
773 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
774 if(ioctl(sokfd, SIOCGIFMAP, &ifre) == 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
775 l_ptr->ifrmap = ifre.ifr_map;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
776 #endif
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
777
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
778 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
779 l_ptr->txqueuelen = NO_RANGE;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
780 if(ioctl(sokfd, SIOCGIFTXQLEN, &ifre) >= 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
781 l_ptr->txqueuelen = ifre.ifr_qlen;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
782
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
783 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
784 ifre.ifr_addr.sa_family = AF_INET;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
785
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
786 if(ioctl(sokfd, SIOCGIFADDR, &ifre) == 0) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
787 l_ptr->ifaddr = 1;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
788 l_ptr->ifraddr = ifre.ifr_addr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
789 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
790 if(ioctl(sokfd, SIOCGIFDSTADDR, &ifre) >= 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
791 l_ptr->ifrdstaddr = ifre.ifr_dstaddr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
792
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
793 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
794 if(ioctl(sokfd, SIOCGIFBRDADDR, &ifre) >= 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
795 l_ptr->ifrbroadaddr = ifre.ifr_broadaddr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
796
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
797 strncpy(ifre.ifr_name, ifrname, IFNAMSIZ);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
798 if(ioctl(sokfd, SIOCGIFNETMASK, &ifre) >= 0)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
799 l_ptr->ifrnetmask = ifre.ifr_netmask;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
800 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
801 close(sokfd);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
802 return 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
803 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
804
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
805 static void get_ifconfig_info(void)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
806 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
807 IFACE_LIST *l_ptr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
808 int version_num = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
809
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
810 FILE *fp = fopen("/proc/net/dev", "r");
862
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
811 if (!fp) return;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
812
862
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
813 fgets(toybuf, sizeof(toybuf), fp); //skip 1st header line.
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
814 fgets(toybuf, sizeof(toybuf), fp); //skip 2nd header line.
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
815
862
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
816 if(strstr(toybuf, "compressed")) version_num = 2;
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
817 else if(strstr(toybuf, "bytes")) version_num = 1;
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
818 else version_num = 0;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
819
862
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
820 while(fgets(toybuf, sizeof(toybuf), fp)) {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
821 l_ptr = xzalloc(sizeof(IFACE_LIST));
862
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
822 get_proc_info(toybuf, l_ptr, version_num);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
823 add_iface_to_list(l_ptr);
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
824 l_ptr->non_virtual_iface = 1;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
825 errno = 0;
862
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
826 if(get_device_info(l_ptr) < 0) perror_exit("%s", l_ptr->dev_info.ifrname);
53cee02f1811 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 861
diff changeset
827 }
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
828 fclose(fp);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
829 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
830
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
831 static void get_hw_info(int hw_type, HW_INFO *hw_info)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
832 {
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
833 memset(hw_info, 0, sizeof(HW_INFO));
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
834
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
835 switch(hw_type) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
836 case ARPHRD_LOOPBACK: //Loopback device.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
837 strncpy(hw_info->hw_name, "loop", HW_NAME_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
838 strncpy(hw_info->hw_title, "Local Loopback", HW_TITLE_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
839 hw_info->hw_addrlen = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
840 break;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
841 case ARPHRD_ETHER: //Ethernet
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
842 strncpy(hw_info->hw_name, "ether", HW_NAME_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
843 strncpy(hw_info->hw_title, "Ethernet", HW_TITLE_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
844 hw_info->hw_addrlen = ETH_ALEN;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
845 break;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
846 case ARPHRD_PPP: //ARPHRD_PPP
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
847 strncpy(hw_info->hw_name, "ppp", HW_NAME_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
848 strncpy(hw_info->hw_title, "Point-to-Point Protocol", HW_TITLE_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
849 hw_info->hw_addrlen = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
850 break;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
851 case ARPHRD_INFINIBAND: //InfiniBand
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
852 strncpy(hw_info->hw_name, "infiniband", HW_NAME_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
853 strncpy(hw_info->hw_title, "InfiniBand", HW_TITLE_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
854 hw_info->hw_addrlen = 20;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
855 break;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
856 case ARPHRD_SIT: //sit0 device - IPv6-in-IPv4
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
857 strncpy(hw_info->hw_name, "sit", HW_NAME_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
858 strncpy(hw_info->hw_title, "IPv6-in-IPv4", HW_TITLE_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
859 hw_info->hw_addrlen = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
860 break;
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
861 case -1:
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
862 strncpy(hw_info->hw_name, "unspec", HW_NAME_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
863 strncpy(hw_info->hw_title, "UNSPEC", HW_TITLE_LEN);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
864 hw_info->hw_addrlen = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
865 break;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
866 default:
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
867 break;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
868 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
869 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
870
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
871 static void print_hw_addr(int hw_type, HW_INFO hw_info, IFACE_LIST *l_ptr)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
872 {
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
873 unsigned char *address = (unsigned char *)l_ptr->ifrhwaddr.sa_data;
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
874
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
875 if(!address || !hw_info.hw_addrlen) return;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
876 xprintf("HWaddr ");
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
877 if(hw_type == ARPHRD_ETHER) {
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
878 int i;
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
879
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
880 for (i=0; i<6; i++) xprintf(":%02X"+!i, address[i]);
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
881 }
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
882 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
883
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
884 static char *get_ip_addr(struct sockaddr *skaddr)
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
885 {
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
886 struct sockaddr_in *sin = (struct sockaddr_in *)skaddr;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
887
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
888 if(skaddr->sa_family == 0xFFFF || !skaddr->sa_family) return "[NOT SET]";
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
889 if(sin->sin_family != AF_INET) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
890 errno = EAFNOSUPPORT;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
891 return NULL;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
892 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
893
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
894 return inet_ntoa(sin->sin_addr);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
895 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
896
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
897 static void print_ip_addr(IFACE_LIST *l_ptr)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
898 {
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
899 char *af_name;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
900 int af = l_ptr->ifraddr.sa_family;
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
901
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
902 if (af == AF_INET) af_name = "inet";
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
903 else if (af == AF_INET6) af_name = "inet6";
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
904 else if (af == AF_UNSPEC) af_name = "unspec";
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
905
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
906 xprintf("%10s%s addr:%s ", " ", af_name, get_ip_addr(&l_ptr->ifraddr));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
907 if(l_ptr->ifrflags & IFF_POINTOPOINT)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
908 xprintf(" P-t-P:%s ", get_ip_addr(&l_ptr->ifrdstaddr));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
909 if(l_ptr->ifrflags & IFF_BROADCAST)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
910 xprintf(" Bcast:%s ", get_ip_addr(&l_ptr->ifrbroadaddr));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
911 xprintf(" Mask:%s\n", get_ip_addr(&l_ptr->ifrnetmask));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
912 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
913
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
914 static void print_iface_flags(IFACE_LIST *l_ptr)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
915 {
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
916 if (l_ptr->ifrflags != 0) {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
917 unsigned short mask = 1;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
918 char **str = iface_flags_str;
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
919
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
920 for(; *str != NULL; str++) {
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
921 if(l_ptr->ifrflags & mask) xprintf("%s ", *str);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
922 mask = mask << 1;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
923 }
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
924 } else xprintf("[NO FLAGS] ");
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
925 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
926
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
927 static void print_media(IFACE_LIST *l_ptr)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
928 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
929 #ifdef IFF_PORTSEL
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
930 if(l_ptr->ifrflags & IFF_PORTSEL) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
931 xprintf("Media:");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
932 if(l_ptr->ifrmap.port == IF_PORT_UNKNOWN)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
933 xprintf("%s", "unknown");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
934 else if(l_ptr->ifrmap.port == IF_PORT_10BASE2)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
935 xprintf("%s", "10base2");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
936 else if(l_ptr->ifrmap.port == IF_PORT_10BASET)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
937 xprintf("%s", "10baseT");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
938 else if(l_ptr->ifrmap.port == IF_PORT_AUI)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
939 xprintf("%s", "AUI");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
940 else if(l_ptr->ifrmap.port == IF_PORT_100BASET)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
941 xprintf("%s", "100baseT");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
942 else if(l_ptr->ifrmap.port == IF_PORT_100BASETX)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
943 xprintf("%s", "100baseTX");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
944 else if(l_ptr->ifrmap.port == IF_PORT_100BASEFX)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
945 xprintf("%s", "100baseFX");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
946 if(l_ptr->ifrflags & IFF_AUTOMEDIA)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
947 xprintf("(auto)");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
948 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
949 #endif
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
950 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
951
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
952 static void print_ip6_addr(IFACE_LIST *l_ptr)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
953 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
954 char iface_name[IFNAMSIZ] = {0,};
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
955 char buf[BUFSIZ] = {0,};
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
956 int plen, scope;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
957
844
f8250900f94b More ifconfig cleanups, mostly removing unused code.
Rob Landley <rob@landley.net>
parents: 843
diff changeset
958 FILE *fp = fopen("/proc/net/if_inet6", "r");
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
959 if(!fp) return;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
960
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
961 while(fgets(buf, BUFSIZ, fp)) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
962 int nitems = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
963 char ipv6_addr[40] = {0,};
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
964 nitems = sscanf(buf, "%32s %*08x %02x %02x %*02x %15s\n",
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
965 ipv6_addr+7, &plen, &scope, iface_name);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
966 if(nitems != 4) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
967 if((nitems < 0) && feof(fp))
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
968 break;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
969 perror_exit("sscanf");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
970 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
971 if(strcmp(l_ptr->dev_info.ifrname,iface_name) == 0) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
972 int i = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
973 struct sockaddr_in6 sock_in6;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
974 int len = sizeof(ipv6_addr) / (sizeof ipv6_addr[0]);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
975 char *ptr = ipv6_addr+7;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
976 while((i < len-2) && (*ptr)) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
977 ipv6_addr[i++] = *ptr++;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
978 //put ':' after 4th bit
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
979 if(!((i+1) % 5))
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
980 ipv6_addr[i++] = ':';
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
981 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
982 ipv6_addr[i+1] = '\0';
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
983 if(inet_pton(AF_INET6, ipv6_addr, (struct sockaddr *) &sock_in6.sin6_addr) > 0) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
984 sock_in6.sin6_family = AF_INET6;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
985 memset(buf, 0, (sizeof(buf) /sizeof(buf[0])));
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
986 if(inet_ntop(AF_INET6, &sock_in6.sin6_addr, buf, BUFSIZ) > 0) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
987 xprintf("%10sinet6 addr: %s/%d", " ", buf, plen);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
988 xprintf(" Scope:");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
989 if(scope == IPV6_ADDR_ANY) xprintf(" Global");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
990 else if(scope == IPV6_ADDR_LOOPBACK) xprintf(" Host");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
991 else if(scope == IPV6_ADDR_LINKLOCAL) xprintf(" Link");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
992 else if(scope == IPV6_ADDR_SITELOCAL) xprintf(" Site");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
993 else if(scope == IPV6_ADDR_COMPATv4) xprintf(" Compat");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
994 else xprintf("Unknown");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
995 xprintf("\n");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
996 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
997 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
998 }
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
999 }
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1000 fclose(fp);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1001 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1002
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1003 static void display_ifconfig(IFACE_LIST *l_ptr)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1004 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1005 HW_INFO hw_info;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1006 int hw_type = l_ptr->hw_type;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1007
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1008 get_hw_info(hw_type, &hw_info);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1009 xprintf("%-9s Link encap:%s ", l_ptr->dev_info.ifrname, hw_info.hw_title);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1010 print_hw_addr(hw_type, hw_info, l_ptr);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1011
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1012 print_media(l_ptr);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1013
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
1014 xputc('\n');
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1015 if(l_ptr->ifaddr)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1016 print_ip_addr(l_ptr); //print addr, p-p addr, broadcast addr and mask addr.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1017
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1018 //for ipv6 to do.
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1019 print_ip6_addr(l_ptr);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1020 xprintf("%10s", " ");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1021 //print flags
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1022 print_iface_flags(l_ptr);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
1023 if(!l_ptr->ifrmetric) l_ptr->ifrmetric = 1;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1024 xprintf(" MTU:%d Metric:%d", l_ptr->ifrmtu, l_ptr->ifrmetric);
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1025 xprintf("\n");
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1026 if(l_ptr->non_virtual_iface) {
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1027 xprintf("%10s", " ");
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1028 xprintf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1029 l_ptr->dev_info.receive_packets, l_ptr->dev_info.receive_errors,
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1030 l_ptr->dev_info.receive_drop, l_ptr->dev_info.receive_fifo,
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1031 l_ptr->dev_info.receive_frame);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1032 //Dummy types for non ARP hardware.
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1033 if((hw_type == ARPHRD_CSLIP) || (hw_type == ARPHRD_CSLIP6))
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1034 xprintf("%10scompressed:%lu\n", " ", l_ptr->dev_info.receive_compressed);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1035 xprintf("%10sTX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n", " ",
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1036 l_ptr->dev_info.transmit_packets, l_ptr->dev_info.transmit_errors,
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1037 l_ptr->dev_info.transmit_drop, l_ptr->dev_info.transmit_fifo,
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1038 l_ptr->dev_info.transmit_carrier);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1039 xprintf("%10scollisions:%lu ", " ", l_ptr->dev_info.transmit_colls);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1040 //Dummy types for non ARP hardware.
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1041 if((hw_type == ARPHRD_CSLIP) || (hw_type == ARPHRD_CSLIP6))
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1042 xprintf("compressed:%lu ", l_ptr->dev_info.transmit_compressed);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1043 if(l_ptr->txqueuelen != NO_RANGE)
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1044 xprintf("txqueuelen:%d ", l_ptr->txqueuelen);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1045
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1046 xprintf("\n%10s", " ");
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1047 xprintf("RX bytes:%llu ", l_ptr->dev_info.receive_bytes);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1048 xprintf("TX bytes:%llu\n", l_ptr->dev_info.transmit_bytes);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1049 }
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1050 if(l_ptr->ifrmap.irq || l_ptr->ifrmap.mem_start || l_ptr->ifrmap.dma || l_ptr->ifrmap.base_addr) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1051 xprintf("%10s", " ");
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1052 if(l_ptr->ifrmap.irq)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1053 xprintf("Interrupt:%d ", l_ptr->ifrmap.irq);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1054 if(l_ptr->ifrmap.base_addr >= IO_MAP_INDEX)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1055 xprintf("Base address:0x%lx ", l_ptr->ifrmap.base_addr);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1056 if(l_ptr->ifrmap.mem_start)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1057 xprintf("Memory:%lx-%lx ", l_ptr->ifrmap.mem_start, l_ptr->ifrmap.mem_end);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1058 if(l_ptr->ifrmap.dma)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1059 xprintf("DMA chan:%x ", l_ptr->ifrmap.dma);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
1060 xputc('\n');
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1061 }
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
1062 xputc('\n');
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1063 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1064
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1065 static int readconf(void)
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1066 {
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1067 int num_of_req = 30;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1068 struct ifconf ifcon;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1069 struct ifreq *ifre;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1070 int num, status = -1, sokfd;
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1071
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1072 ifcon.ifc_buf = NULL;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1073 sokfd = socket(AF_INET, SOCK_DGRAM, 0);
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1074 if(sokfd < 0) {
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1075 perror_msg("error: no inet socket available");
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1076 return -1;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1077 }
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1078 for (;;) {
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1079 ifcon.ifc_len = sizeof(struct ifreq) * num_of_req; //Size of buffer.
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1080 ifcon.ifc_buf = xrealloc(ifcon.ifc_buf, ifcon.ifc_len);
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1081
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1082 if((status = ioctl(sokfd, SIOCGIFCONF, &ifcon)) == -1) {
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1083 perror_msg("ioctl %#x failed", SIOCGIFCONF);
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1084 goto LOOP_BREAK;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1085 }
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1086 //in case of overflow, increase number of requests and retry.
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1087 if (ifcon.ifc_len == (int)(sizeof(struct ifreq) * num_of_req)) {
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1088 num_of_req += 10;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1089 continue;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1090 }
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1091 break;
864
764df39f62b4 More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 863
diff changeset
1092 }
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1093
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1094 ifre = ifcon.ifc_req;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1095 for(num = 0; num < ifcon.ifc_len && ifre; num += sizeof(struct ifreq), ifre++) {
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1096 //Escape duplicate values from the list.
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1097 IFACE_LIST *list_ptr;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1098 int match_found = 0;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1099 for(list_ptr = iface_list_head; list_ptr != NULL; list_ptr = list_ptr->next) {
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1100 //if interface already in the list then donot add it in the list.
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1101 if(!strcmp(ifre->ifr_name, list_ptr->dev_info.ifrname)) {
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1102 match_found = 1;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1103 break;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1104 }
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1105 }
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1106 if(!match_found) {
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1107 IFACE_LIST *l_ptr = xzalloc(sizeof(IFACE_LIST));
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1108 safe_strncpy(l_ptr->dev_info.ifrname, ifre->ifr_name, IFNAMSIZ);
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1109 add_iface_to_list(l_ptr);
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1110 errno = 0;
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1111 if(get_device_info(l_ptr) < 0)
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1112 perror_exit("%s", l_ptr->dev_info.ifrname);
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1113 }
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1114 }
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1115
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1116 LOOP_BREAK:
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1117 close(sokfd);
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1118 free(ifcon.ifc_buf);
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
1119
863
493f412fc5da Fix the help commit I screwed up, and replace leading tabs with spaces.
Rob Landley <rob@landley.net>
parents: 862
diff changeset
1120 return status;
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1121 }
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1122
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1123 static int show_iface(char *iface_name)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1124 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1125 get_ifconfig_info();
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1126
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1127 if(iface_name) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1128 IFACE_LIST *l_ptr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1129 int is_dev_found = 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1130 for(l_ptr = iface_list_head; l_ptr; l_ptr = l_ptr->next) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1131 if(strcmp(l_ptr->dev_info.ifrname, iface_name) == 0) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1132 is_dev_found = 1;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1133 display_ifconfig(l_ptr);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1134 break;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1135 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1136 }
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1137 //if the given interface is not in the list.
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1138 if(!is_dev_found) {
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1139 IFACE_LIST *l_ptr = xzalloc(sizeof(IFACE_LIST));
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1140 safe_strncpy(l_ptr->dev_info.ifrname, iface_name, IFNAMSIZ);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1141 errno = 0;
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1142 if(get_device_info(l_ptr) < 0) {
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
1143 char *errmsg;
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1144 if(errno == ENODEV) errmsg = "Device not found";
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1145 else errmsg = strerror(errno);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1146 error_msg("%s: error getting interface info: %s", iface_name, errmsg);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1147 free(l_ptr);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1148 return 1;
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1149 }
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1150 else display_ifconfig(l_ptr);
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1151 free(l_ptr);
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1152 }
852
6be04ec7b7ac More ifconfig cleanup.
Rob Landley <rob@landley.net>
parents: 844
diff changeset
1153 } else {
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1154 IFACE_LIST *l_ptr;
842
af5aab6e6678 An ifconfig bugfix from the original submitter.
Rob Landley <rob@landley.net>
parents: 841
diff changeset
1155 if(readconf() < 0) return 1;
841
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1156 for(l_ptr = iface_list_head; l_ptr; l_ptr = l_ptr->next) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1157 if((l_ptr->ifrflags & IFF_UP) || (toys.optflags & FLAG_a))
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1158 display_ifconfig(l_ptr);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1159 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1160 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1161 return 0;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1162 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1163
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1164 static void clear_list(void)
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1165 {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1166 IFACE_LIST *temp_ptr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1167 while(iface_list_head != NULL) {
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1168 temp_ptr = iface_list_head->next;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1169 free(iface_list_head);
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1170 iface_list_head = temp_ptr;
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1171 }
cb0c3a773437 Kyungwan Han submitted ifconfig.
Rob Landley <rob@landley.net>
parents:
diff changeset
1172 }