This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlanscan.c
170 lines (151 loc) · 4.38 KB
/
lanscan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Written by Oron Peled <[email protected]>
*
* Modified by Dominique Domet de Mont, 2007, [email protected]
* Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
*
* Some code snippets adapted from spak
* (http://www.xenos.net/software/spak/)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include "config.h"
#include "linkloop.h"
/* Linkloop configuration */
static struct lanscan {
char displayHeader; /* display header before listing interfaces */
char displayMac; /* display the MAC address */
char displayNames; /* display interface names */
char displayState; /* display interface states */
char displayOneline; /* display one line summary */
} ls = {
.displayHeader = 1,
.displayMac = 1,
.displayNames = 1,
.displayState = 1,
.displayOneline = 0
};
static struct if_flag {
short int flag;
char *flag_name;
} if_flags[] = {
{ IFF_UP, "UP" },
{ IFF_BROADCAST, "BROADCAST" },
{ IFF_DEBUG, "DEBUG" },
{ IFF_LOOPBACK, "LOOPBACK" },
{ IFF_POINTOPOINT, "POINTOPOINT" },
{ IFF_NOTRAILERS, "NOTRAILERS" },
{ IFF_RUNNING, "RUNNING" },
{ IFF_NOARP, "NOARP" },
{ IFF_PROMISC, "PROMISC" },
{ IFF_ALLMULTI, "ALLMULTI" },
{ IFF_MASTER, "MASTER" },
{ IFF_SLAVE, "SLAVE" },
{ IFF_MULTICAST, "MULTICAST" },
{ IFF_PORTSEL, "PORTSEL" },
{ IFF_AUTOMEDIA, "AUTOMEDIA" },
};
static short int get_ifflags(int sock, const char name[]) {
struct ifreq ifr;
strncpy(ifr.ifr_name, name, IFNAMSIZ);
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
perror("ioctl(SIOCGIFFLAGS)");
exit(1);
}
return ifr.ifr_flags;
}
static void usage(const char *program) {
fprintf(stderr, "Usage: %s -[option...]\n"
"\t-h Print this help text\n"
"\t-? Print this help text\n"
"\t-i<face> Print network interface names\n"
"\t-a<mAc> Print network interface MAC address\n"
"\t-s<tate> Print network interface state\n"
"\t-o<ne> Print one line summary, interface names only\n"
, program
);
exit(1);
}
int main(int argc, char *argv[]) {
struct ifconf ifc;
struct ifreq ifr_x[MAX_IFACES];
int sock, err, ifindex, i;
u_int8_t myMAC[IFHWADDRLEN];
short int flags;
/* check options if any */
if(argc > 1) {
/* unsupported options or help */
if(strpbrk(argv[1], "aiso") == NULL) {
usage(argv[0]);
}
/* supported options provided: turn the header display off */
ls.displayHeader = 0;
if (strchr(argv[1], 'i') == NULL)
ls.displayNames = 0;
if (strchr(argv[1], 'a') == NULL)
ls.displayMac = 0;
if (strchr(argv[1], 's') == NULL)
ls.displayState = 0;
if (strchr(argv[1], 'o')) {
ls.displayOneline = 1;
ls.displayState = ls.displayMac = ls.displayNames = 0;
}
}
if ((sock = socket(PF_PACKET, SOCK_PACKET, 0)) < 0) {
perror("socket");
exit(1);
}
ifc.ifc_len = MAX_IFACES * sizeof(struct ifreq);
ifc.ifc_req = ifr_x;
if ((err = ioctl(sock, SIOCGIFCONF, &ifc)) < 0) {
perror("ioctl");
exit(1);
}
if (ls.displayHeader)
printf("retrieved info for %i interface(s)\n", ifc.ifc_len / sizeof(struct ifreq));
for (err = 0; err < ifc.ifc_len / sizeof(struct ifreq); err++) {
flags = get_ifflags(sock, ifr_x[err].ifr_name);
get_hwaddr(sock, ifr_x[err].ifr_name, &ifindex, myMAC);
/* Discard local loopback port */
if (strcmp(ifr_x[err].ifr_name, "lo") == 0)
continue;
/* Discard alias ports, ie name including a column ':' */
if (strchr(ifr_x[err].ifr_name, ':'))
continue;
if (ls.displayOneline) {
printf("%s\t", ifr_x[err].ifr_name);
continue;
}
if (ls.displayMac)
printf("%s\t", mac2str(myMAC));
/* here, in order to mimic the HP UX output, we print the interface name,
* interface physical point of attachement, physical point ID
* as the same "interface name"
*/
if (ls.displayNames) {
printf("%s %s %s\t", ifr_x[err].ifr_name, ifr_x[err].ifr_name,
ifr_x[err].ifr_name);
}
if (ls.displayState) {
putchar('<');
for (i = 0; i < sizeof(if_flags)/sizeof(if_flags[0]); i++) {
if (flags & if_flags[i].flag)
printf("%s ", if_flags[i].flag_name);
}
putchar('>');
}
putchar('\n');
}
return EXIT_SUCCESS;
}