Blame view

Network/libpcap-1.9.0/testprogs/findalldevstest.c 7.59 KB
1f9d795a   Speedclocker   Sniffer et sender
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  #ifdef HAVE_CONFIG_H
  #include <config.h>
  #endif
  
  #include <stdlib.h>
  #include <string.h>
  #include <sys/types.h>
  #ifdef _WIN32
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <windows.h>
  #else
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <unistd.h>
  #endif
  
  #include <pcap.h>
  
  #include "pcap/funcattrs.h"
  
  static int ifprint(pcap_if_t *d);
  static char *iptos(bpf_u_int32 in);
  
  #ifdef _WIN32
  #include "portability.h"
  
  /*
   * Generate a string for a Win32-specific error (i.e. an error generated when
   * calling a Win32 API).
   * For errors occurred during standard C calls, we still use pcap_strerror()
   */
  #define ERRBUF_SIZE	1024
  static const char *
  win32_strerror(DWORD error)
  {
    static char errbuf[ERRBUF_SIZE+1];
    size_t errlen;
  
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
                  ERRBUF_SIZE, NULL);
  
    /*
     * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
     * message.  Get rid of it.
     */
    errlen = strlen(errbuf);
    if (errlen >= 2) {
      errbuf[errlen - 1] = '\0';
      errbuf[errlen - 2] = '\0';
      errlen -= 2;
    }
    return errbuf;
  }
  
  static char *
  getpass(const char *prompt)
  {
    HANDLE console_handle = GetStdHandle(STD_INPUT_HANDLE);
    DWORD console_mode, save_console_mode;
    static char password[128+1];
    char *p;
  
    fprintf(stderr, "%s", prompt);
  
    /*
     * Turn off echoing.
     */
    if (!GetConsoleMode(console_handle, &console_mode)) {
      fprintf(stderr, "Can't get console mode: %s\n",
              win32_strerror(GetLastError()));
      exit(1);
    }
    save_console_mode = console_mode;
    console_mode &= ~ENABLE_ECHO_INPUT;
    if (!SetConsoleMode(console_handle, console_mode)) {
      fprintf(stderr, "Can't set console mode: %s\n",
              win32_strerror(GetLastError()));
      exit(1);
    }
    if (fgets(password, sizeof password, stdin) == NULL) {
      fprintf(stderr, "\n");
      SetConsoleMode(console_handle, save_console_mode);
      exit(1);
    }
    fprintf(stderr, "\n");
    SetConsoleMode(console_handle, save_console_mode);
    p = strchr(password, '\n');
    if (p != NULL)
      *p = '\0';
   return password;
  }
  #endif
  
  int main(int argc, char **argv)
  {
    pcap_if_t *alldevs;
    pcap_if_t *d;
    bpf_u_int32 net, mask;
    int exit_status = 0;
    char errbuf[PCAP_ERRBUF_SIZE+1];
  #ifdef ENABLE_REMOTE
    struct pcap_rmtauth auth;
    char username[128+1];
    char *p;
    char *password;
  #endif
  
  #ifdef ENABLE_REMOTE
    if (argc >= 2)
    {
      if (pcap_findalldevs_ex(argv[1], NULL, &alldevs, errbuf) == -1)
      {
        /*
         * OK, try it with a user name and password.
         */
        fprintf(stderr, "User name: ");
        if (fgets(username, sizeof username, stdin) == NULL)
          exit(1);
        p = strchr(username, '\n');
        if (p != NULL)
          *p = '\0';
        password = getpass("Password: ");
        auth.type = RPCAP_RMTAUTH_PWD;
        auth.username = username;
        auth.password = password;
        if (pcap_findalldevs_ex(argv[1], &auth, &alldevs, errbuf) == -1)
        {
          fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
          exit(1);
        }
      }
    }
    else
  #endif
    {
      if (pcap_findalldevs(&alldevs, errbuf) == -1)
      {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
        exit(1);
      }
    }
    for(d=alldevs;d;d=d->next)
    {
      if (!ifprint(d))
        exit_status = 2;
    }
  
    if (alldevs != NULL)
    {
      if (pcap_lookupnet(alldevs->name, &net, &mask, errbuf) < 0)
      {
        fprintf(stderr,"Error in pcap_lookupnet: %s\n",errbuf);
        exit_status = 2;
      }
      else
      {
        printf("Preferred device is on network: %s/%s\n",iptos(net), iptos(mask));
      }
    }
  
    pcap_freealldevs(alldevs);
    exit(exit_status);
  }
  
  static int ifprint(pcap_if_t *d)
  {
    pcap_addr_t *a;
    char ipv4_buf[INET_ADDRSTRLEN];
    char ipv6_buf[INET6_ADDRSTRLEN];
    const char *sep;
    int status = 1; /* success */
  
    printf("%s\n",d->name);
    if (d->description)
      printf("\tDescription: %s\n",d->description);
    printf("\tFlags: ");
    sep = "";
    if (d->flags & PCAP_IF_UP) {
      printf("%sUP", sep);
      sep = ", ";
    }
    if (d->flags & PCAP_IF_RUNNING) {
      printf("%sRUNNING", sep);
      sep = ", ";
    }
    if (d->flags & PCAP_IF_LOOPBACK) {
      printf("%sLOOPBACK", sep);
      sep = ", ";
    }
    if (d->flags & PCAP_IF_WIRELESS) {
      printf("%sWIRELESS", sep);
      switch (d->flags & PCAP_IF_CONNECTION_STATUS) {
  
      case PCAP_IF_CONNECTION_STATUS_UNKNOWN:
        printf(" (association status unknown)");
        break;
  
      case PCAP_IF_CONNECTION_STATUS_CONNECTED:
        printf(" (associated)");
        break;
  
      case PCAP_IF_CONNECTION_STATUS_DISCONNECTED:
        printf(" (not associated)");
        break;
  
      case PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE:
        break;
      }
    } else {
      switch (d->flags & PCAP_IF_CONNECTION_STATUS) {
  
      case PCAP_IF_CONNECTION_STATUS_UNKNOWN:
        printf(" (connection status unknown)");
        break;
  
      case PCAP_IF_CONNECTION_STATUS_CONNECTED:
        printf(" (connected)");
        break;
  
      case PCAP_IF_CONNECTION_STATUS_DISCONNECTED:
        printf(" (disconnected)");
        break;
  
      case PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE:
        break;
      }
    }
    sep = ", ";
    printf("\n");
  
    for(a=d->addresses;a;a=a->next) {
      if (a->addr != NULL)
        switch(a->addr->sa_family) {
        case AF_INET:
          printf("\tAddress Family: AF_INET\n");
          if (a->addr)
            printf("\t\tAddress: %s\n",
              inet_ntop(AF_INET,
                 &((struct sockaddr_in *)(a->addr))->sin_addr,
                 ipv4_buf, sizeof ipv4_buf));
          if (a->netmask)
            printf("\t\tNetmask: %s\n",
              inet_ntop(AF_INET,
                 &((struct sockaddr_in *)(a->netmask))->sin_addr,
                 ipv4_buf, sizeof ipv4_buf));
          if (a->broadaddr)
            printf("\t\tBroadcast Address: %s\n",
              inet_ntop(AF_INET,
                 &((struct sockaddr_in *)(a->broadaddr))->sin_addr,
                 ipv4_buf, sizeof ipv4_buf));
          if (a->dstaddr)
            printf("\t\tDestination Address: %s\n",
              inet_ntop(AF_INET,
                 &((struct sockaddr_in *)(a->dstaddr))->sin_addr,
                 ipv4_buf, sizeof ipv4_buf));
          break;
  #ifdef INET6
        case AF_INET6:
          printf("\tAddress Family: AF_INET6\n");
          if (a->addr)
            printf("\t\tAddress: %s\n",
              inet_ntop(AF_INET6,
                 ((struct sockaddr_in6 *)(a->addr))->sin6_addr.s6_addr,
                 ipv6_buf, sizeof ipv6_buf));
          if (a->netmask)
            printf("\t\tNetmask: %s\n",
              inet_ntop(AF_INET6,
                ((struct sockaddr_in6 *)(a->netmask))->sin6_addr.s6_addr,
                 ipv6_buf, sizeof ipv6_buf));
          if (a->broadaddr)
            printf("\t\tBroadcast Address: %s\n",
              inet_ntop(AF_INET6,
                ((struct sockaddr_in6 *)(a->broadaddr))->sin6_addr.s6_addr,
                 ipv6_buf, sizeof ipv6_buf));
          if (a->dstaddr)
            printf("\t\tDestination Address: %s\n",
              inet_ntop(AF_INET6,
                ((struct sockaddr_in6 *)(a->dstaddr))->sin6_addr.s6_addr,
                 ipv6_buf, sizeof ipv6_buf));
          break;
  #endif
        default:
          printf("\tAddress Family: Unknown (%d)\n", a->addr->sa_family);
          break;
        }
      else
      {
        fprintf(stderr, "\tWarning: a->addr is NULL, skipping this address.\n");
        status = 0;
      }
    }
    printf("\n");
    return status;
  }
  
  /* From tcptraceroute */
  #define IPTOSBUFFERS	12
  static char *iptos(bpf_u_int32 in)
  {
  	static char output[IPTOSBUFFERS][3*4+3+1];
  	static short which;
  	u_char *p;
  
  	p = (u_char *)&in;
  	which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
  	sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
  	return output[which];
  }