| 1 | /* arping/src/unix.c |
| 2 | * |
| 3 | * Copyright (C) 2000-2011 Thomas Habets <[email protected]> |
| 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 | #define _GNU_SOURCE |
| 20 | #if HAVE_CONFIG_H |
| 21 | #include "config.h" |
| 22 | #endif |
| 23 | |
| 24 | #include <errno.h> |
| 25 | #include <signal.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | #if HAVE_SYS_SOCKET_H |
| 29 | #include <sys/socket.h> |
| 30 | #endif |
| 31 | |
| 32 | #if HAVE_NETINET_IN_H |
| 33 | #include <netinet/in.h> |
| 34 | #endif |
| 35 | |
| 36 | #include <pcap.h> |
| 37 | |
| 38 | #include "arping.h" |
| 39 | |
| 40 | #define UNUSED(x) (void)(x) |
| 41 | |
| 42 | /** |
| 43 | * Fall back on getting device name from pcap. |
| 44 | */ |
| 45 | const char * |
| 46 | arping_lookupdev_default(uint32_t srcip, uint32_t dstip, char *ebuf) |
| 47 | { |
| 48 | #ifdef HAVE_PCAP_FINDALLDEVS |
| 49 | UNUSED(srcip); |
| 50 | pcap_if_t *ifs = NULL; |
| 51 | int rc = pcap_findalldevs(&ifs, ebuf); |
| 52 | if (rc) { |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | pcap_if_t *t; |
| 57 | char* ifname = NULL; |
| 58 | for (t = ifs; !ifname && t; t = t->next) { |
| 59 | #ifdef PCAP_IF_LOOPBACK |
| 60 | if (t->flags & PCAP_IF_LOOPBACK) { |
| 61 | continue; |
| 62 | } |
| 63 | #endif |
| 64 | #ifdef PCAP_IF_UP |
| 65 | if (!(t->flags & PCAP_IF_UP)) { |
| 66 | continue; |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | // This code is only called when using -F, which is "don't try |
| 71 | // to be smart". If we wanted to be smart we would have used |
| 72 | // findif_*.c. |
| 73 | if (1) { |
| 74 | // Intentional memory leak. Caller may edit the string, |
| 75 | // so do not fix this memory leak. |
| 76 | ifname = strdup(t->name); |
| 77 | break; |
| 78 | } |
| 79 | |
| 80 | // UNREACHABLE |
| 81 | pcap_addr_t *a; |
| 82 | for (a = t->addresses; !ifname && a; a = a->next) { |
| 83 | if (a->addr->sa_family != AF_INET) { |
| 84 | continue; |
| 85 | } |
| 86 | const struct sockaddr_in* sa = (struct sockaddr_in*)a->addr; |
| 87 | const struct sockaddr_in* smask = (struct sockaddr_in*)a->netmask; |
| 88 | const uint32_t addr = sa->sin_addr.s_addr; |
| 89 | const uint32_t mask = smask->sin_addr.s_addr; |
| 90 | if ((addr & mask) != (dstip & mask)) { |
| 91 | // Not optimal: memory leak. |
| 92 | ifname = strdup(t->name); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | pcap_freealldevs(ifs); |
| 97 | return ifname; |
| 98 | #else |
| 99 | UNUSED(srcip); |
| 100 | UNUSED(dstip); |
| 101 | return pcap_lookupdev(ebuf); |
| 102 | #endif |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * |
| 107 | */ |
| 108 | void |
| 109 | do_signal_init() |
| 110 | { |
| 111 | if (SIG_ERR == signal(SIGINT, sigint)) { |
| 112 | fprintf(stderr, "arping: failed to set SIGINT handler: %s\n", |
| 113 | strerror(errno)); |
| 114 | } |
| 115 | } |
| 116 | /* ---- Emacs Variables ---- |
| 117 | * Local Variables: |
| 118 | * c-basic-offset: 8 |
| 119 | * indent-tabs-mode: nil |
| 120 | * End: |
| 121 | */ |