1: /* arping/src/unix.c
2: *
3: * Copyright (C) 2000-2011 Thomas Habets <thomas@habets.se>
4: *
5: * This program is free software; you can redistribute it and/or modify
6: * it under the terms of the GNU General Public License as published by
7: * the Free Software Foundation; either version 2 of the License, or
8: * (at your option) any later version.
9: *
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU General Public License for more details.
14: *
15: * You should have received a copy of the GNU General Public License along
16: * with this program; if not, write to the Free Software Foundation, Inc.,
17: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18: */
19: #if HAVE_CONFIG_H
20: #include "config.h"
21: #endif
22:
23: #include <errno.h>
24: #include <signal.h>
25: #include <string.h>
26:
27: #if HAVE_SYS_SOCKET_H
28: #include <sys/socket.h>
29: #endif
30:
31: #if HAVE_NETINET_IN_H
32: #include <netinet/in.h>
33: #endif
34:
35: #include <pcap.h>
36:
37: #include "arping.h"
38:
39: #define UNUSED(x) (void)(x)
40:
41: /**
42: * Fall back on getting device name from pcap.
43: */
44: const char *
45: arping_lookupdev_default(uint32_t srcip, uint32_t dstip, char *ebuf)
46: {
47: #ifdef HAVE_PCAP_FINDALLDEVS
48: UNUSED(srcip);
49: pcap_if_t *ifs = NULL;
50: int rc = pcap_findalldevs(&ifs, ebuf);
51: if (rc) {
52: return NULL;
53: }
54:
55: pcap_if_t *t;
56: char* ifname = NULL;
57: for (t = ifs; !ifname && t; t = t->next) {
58: #ifdef PCAP_IF_LOOPBACK
59: if (t->flags & PCAP_IF_LOOPBACK) {
60: continue;
61: }
62: #endif
63: #ifdef PCAP_IF_UP
64: if (!(t->flags & PCAP_IF_UP)) {
65: continue;
66: }
67: #endif
68:
69: // This code is only called when using -F, which is "don't try
70: // to be smart". If we wanted to be smart we would have used
71: // findif_*.c.
72: if (1) {
73: ifname = strdup(t->name); // Memory leak.
74: break;
75: }
76:
77: // UNREACHABLE
78: pcap_addr_t *a;
79: for (a = t->addresses; !ifname && a; a = a->next) {
80: if (a->addr->sa_family != AF_INET) {
81: continue;
82: }
83: const struct sockaddr_in* sa = (struct sockaddr_in*)a->addr;
84: const struct sockaddr_in* smask = (struct sockaddr_in*)a->netmask;
85: const uint32_t addr = sa->sin_addr.s_addr;
86: const uint32_t mask = smask->sin_addr.s_addr;
87: if ((addr & mask) != (dstip & mask)) {
88: // Not optimal: memory leak.
89: ifname = strdup(t->name);
90: }
91: }
92: }
93: pcap_freealldevs(ifs);
94: return ifname;
95: #else
96: UNUSED(srcip);
97: UNUSED(dstip);
98: return pcap_lookupdev(ebuf);
99: #endif
100: }
101:
102: /**
103: *
104: */
105: void
106: do_signal_init()
107: {
108: if (SIG_ERR == signal(SIGINT, sigint)) {
109: fprintf(stderr, "arping: failed to set SIGINT handler: %s\n",
110: strerror(errno));
111: }
112: }
113: /* ---- Emacs Variables ----
114: * Local Variables:
115: * c-basic-offset: 8
116: * indent-tabs-mode: nil
117: * End:
118: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>