summaryrefslogtreecommitdiff
path: root/ifconfig.c
blob: 609e84908154cea2b93a3093a1d33f94409cfbde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* See LICENSE file for copyright and license details. */
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#include "util.h"

static void
usage(void)
{
	eprintf("usage: %s [-a] [<interface>] [up|down]\n", argv0);
}

#ifndef IFF_LOWER_UP
#define IFF_LOWER_UP 1<<16
#endif
#ifndef IFF_DORMANT
#define IFF_DORMANT 1<<17
#endif
#ifndef IFF_ECHO
#define IFF_ECHO 1<<18
#endif

static void
print_interface(char *name)
{
	int fd;
	struct ifreq ifr;
	int first;
	
	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd < 0) {
		weprintf("error in socket(AF_INTER, SOCK_DGRAM, 0) on interface '%s': %s\n", name, strerror(errno));
		return;
	}

	strncpy(ifr.ifr_name, name, IF_NAMESIZE);
	if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
		close(fd);
		weprintf("error in ioctl(SIOCGIFFLAGS) on interface '%s': %s\n", name, strerror(errno));
		return;
	}

	printf("%s: flags=%d<", name, ifr.ifr_flags);
	
	if (ifr.ifr_flags == 0) {
		printf(">");
	} else {
		first = 1;		
		if (ifr.ifr_flags & IFF_UP) {
			printf("%sUP", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_BROADCAST) {
			printf("%sBROADCAST", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_DEBUG) {
			printf("%sDEBUG", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_LOOPBACK) {
			printf("%sLOOPBACK", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_POINTOPOINT) {
			printf("%sPOINTOPOINT", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_RUNNING) {
			printf("%sRUNNING", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_NOARP) {
			printf("%sNOARP", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_PROMISC) {
			printf("%sPROMISC", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_NOTRAILERS) {
			printf("%sNOTRAILERS", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_ALLMULTI) {
			printf("%sALLMULTI", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_MASTER) {
			printf("%sMASTER", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_SLAVE) {
			printf("%sSLAVE", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_MULTICAST) {
			printf("%sMULTICAST", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_PORTSEL) {
			printf("%sPORTSEL", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_AUTOMEDIA) {
			printf("%sAUTOMEDIA", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_DYNAMIC) {
			printf("%sDYNAMIC", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_LOWER_UP) {
			printf("%sLOWER_UP", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_DORMANT) {
			printf("%sDORMANT", !first ? "," : ""); first = 0;
		}
		if (ifr.ifr_flags & IFF_ECHO) {
			printf("%sECHO", !first ? "," : ""); first = 0;
		}
		printf(">");
	}

	if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
		close(fd);
		weprintf("error in ioctl(SIOCGIFMTU) on interface '%s': %s\n", name, strerror(errno));
		return;
	}
 	printf("  mtu %d\n", ifr.ifr_mtu);

	char ip_addr[INET_ADDRSTRLEN];
	if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
		struct sockaddr_in* addr = (struct sockaddr_in*)&ifr.ifr_addr;
		inet_ntop(AF_INET, &addr->sin_addr, ip_addr, INET_ADDRSTRLEN);
	} else {
		ip_addr[0] = '\0';
	}
	
	char ip_netmask[INET_ADDRSTRLEN];
	if (ioctl(fd, SIOCGIFNETMASK, &ifr) == 0) {
		struct sockaddr_in* netmask = (struct sockaddr_in*)&ifr.ifr_netmask;
		inet_ntop(AF_INET, &netmask->sin_addr, ip_netmask, INET_ADDRSTRLEN);
	} else {
		ip_netmask[0] = '\0';
	}

	char ip_broadcast[INET_ADDRSTRLEN];
	if (ioctl(fd, SIOCGIFBRDADDR, &ifr) == 0) {
		struct sockaddr_in* broadcast = (struct sockaddr_in*)&ifr.ifr_broadaddr;
		inet_ntop(AF_INET, &broadcast->sin_addr, ip_broadcast, INET_ADDRSTRLEN);
	} else {
		ip_broadcast[0] = '\0';
	}
	
	char ip_hwaddr[32];
	if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
		memcpy(ip_hwaddr, ifr.ifr_hwaddr.sa_data, 8);
	} else {
		memset(ip_hwaddr, 0, 32);
	}

	printf("        inet %s  netmask %s  broadcast %s\n",
	       ip_addr, ip_netmask, ip_broadcast);
	printf("        ether ");
	first = 1;
	for (int i = 0; i <6; i++) {
		printf("%s%02x", ( !first ) ? ":" : "", ip_hwaddr[i] & 0xFF); first = 0;
	}
	puts("");

	close(fd);
}

static void
set_up_down(char *name, int up)
{
	int fd;
	struct ifreq ifr;
	
	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd < 0) {
		weprintf("error in socket(AF_INTER, SOCK_DGRAM, 0) on interface '%s': %s\n", name, strerror(errno));
		return;
	}

	strncpy(ifr.ifr_name, name, IF_NAMESIZE);
	if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
		close(fd);
		weprintf("error in ioctl(SIOCGIFFLAGS) on interface '%s': %s\n", name, strerror(errno));
		return;
	}
	
	if (up) {
		ifr.ifr_flags |= IFF_UP;
	} else  {
		ifr.ifr_flags &= ~IFF_UP;
	}

	if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
		close(fd);
		weprintf("error in ioctl(SIOCGIFFLAGS) on interface '%s': %s\n", name, strerror(errno));
		return;
	}
}

int
main(int argc, char *argv[])
{
	int show_all = 0;
	
	ARGBEGIN {
	case 'a':
		show_all = 1;
		break;
	default:
		usage();
	} ARGEND
	
	if (show_all) {
		struct if_nameindex *interfaces, *interface;
		
		interfaces = if_nameindex();
		if (interfaces != NULL) {
			for (interface = interfaces; (interface->if_index != 0 || interface->if_name != NULL); interface++) {
				print_interface(interface->if_name);
			}
			if_freenameindex(interfaces);
		}
	} else {
		if (argc == 1) {
			print_interface(argv[0]);
		} else if (argc == 2) {
			int up;
			if (strcmp(argv[1], "up") == 0) {
				up = 1;
			} else if (strcmp(argv[1], "down") == 0) {
				up = 0;
			} else {
				weprintf("Expecting 'up' or 'down'\n");
				usage();
			}
			set_up_down(argv[0], up);
		} else {
			usage();
		}
	}
	
	return 0;
}