| 1 | /* arping/src/findif_getifaddrs.c |
| 2 | * |
| 3 | * Copyright (C) 2000-2014 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 | /** |
| 20 | * Most modern systems should have getifaddrs(). |
| 21 | */ |
| 22 | #define _GNU_SOURCEcxx . . $ `src/findif_getifaddrs.c:22:9`! |
| 23 | #if HAVE_CONFIG_Hcxx . . $ `<built-in>:411:9`! |
| 24 | #include "config.h"cxx . . $ `<file>/config.h`/ |
| 25 | #endif |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <ifaddrs.h> |
| 34 | #include <net/if.h> |
| 35 | #include <netinet/in.h> |
| 36 | |
| 37 | #if HAVE_LIBNET_Hcxx . . $ `config.h:56:9`! |
| 38 | #include <libnet.h> |
| 39 | #endif |
| 40 | |
| 41 | #include "arping.h"cxx . . $ `<file>/src/arping.h`/ |
| 42 | |
| 43 | #define UNUSEDcxx . . $ `src/findif_getifaddrs.c:43:9`!(x) (void)(x) |
| 44 | |
| 45 | const char * |
| 46 | arping_lookupdevcxx . . $ arping_lookupdev(ace80522166ec2ef).(uint32_tcxx . . $ uint32_t# srcip, |
| 47 | uint32_tcxx . . $ uint32_t# dstip, |
| 48 | char *ebuf) |
| 49 | { |
| 50 | struct ifaddrscxx . . $ ifaddrs# *ifa = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 51 | struct ifaddrscxx . . $ ifaddrs# *cur; |
| 52 | const char *ret = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 53 | int match_count = 0; /* Matching interfaces */ |
| 54 | UNUSEDcxx . . $ `src/findif_getifaddrs.c:43:9`!(srcip); |
| 55 | |
| 56 | /* best match */ |
| 57 | in_addr_tcxx . . $ in_addr_t# best_mask = 0; |
| 58 | |
| 59 | /* Results */ |
| 60 | static char ifname[IFNAMSIZcxx . . $ `<external>/2638090476884991293/if.h:129:10`!]; |
| 61 | |
| 62 | *ebuf = 0; |
| 63 | |
| 64 | if (getifaddrscxx . . $ getifaddrs(1d1e0a5ba40b9a63).(&ifa)) { |
| 65 | if (verbosecxx . . $ verbose.) { |
| 66 | printfcxx . . $ printf(581fddd72a8db8fc).("arping: getifaddrs(): %s\n", strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 67 | } |
| 68 | xsnprintfcxx . . $ xsnprintf(ecdd28f83a80e0a0).(ebuf, LIBNET_ERRBUF_SIZEcxx . . $ `<external>/9617454641823861997/libnet-macros.h:112:9`!, |
| 69 | "getifaddrs(): %s", strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 70 | goto out; |
| 71 | } |
| 72 | for (cur = ifa; cur; cur = cur->ifa_nextcxx . . $ ifaddrs#ifa_next.) { |
| 73 | in_addr_tcxx . . $ in_addr_t# addr, mask; |
| 74 | |
| 75 | if (!(cur->ifa_flagscxx . . $ ifaddrs#ifa_flags. & IFF_UPcxx . . $ `<external>/2638090476884991293/if.h:45:10`!)) { |
| 76 | continue; |
| 77 | } |
| 78 | if (!cur->ifa_addrcxx . . $ ifaddrs#ifa_addr. |
| 79 | || !cur->ifa_netmaskcxx . . $ ifaddrs#ifa_netmask. |
| 80 | || !cur->ifa_namecxx . . $ ifaddrs#ifa_name.) { |
| 81 | continue; |
| 82 | } |
| 83 | if (cur->ifa_addrcxx . . $ ifaddrs#ifa_addr.->sa_familycxx . . $ sockaddr#sa_family. != AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!) { |
| 84 | continue; |
| 85 | } |
| 86 | if (cur->ifa_flagscxx . . $ ifaddrs#ifa_flags. & (IFF_LOOPBACKcxx . . $ `<external>/2638090476884991293/if.h:51:10`!|IFF_POINTOPOINTcxx . . $ `<external>/2638090476884991293/if.h:53:10`!)) { |
| 87 | continue; |
| 88 | } |
| 89 | addr =((struct sockaddr_incxx . . $ sockaddr_in#*)cur->ifa_addrcxx . . $ ifaddrs#ifa_addr.)->sin_addrcxx . . $ sockaddr_in#sin_addr..s_addrcxx . . $ in_addr#s_addr.; |
| 90 | mask =((struct sockaddr_incxx . . $ sockaddr_in#*)cur->ifa_netmaskcxx . . $ ifaddrs#ifa_netmask.)->sin_addrcxx . . $ sockaddr_in#sin_addr..s_addrcxx . . $ in_addr#s_addr.; |
| 91 | if ((addr & mask) != (dstip & mask)) { |
| 92 | continue; |
| 93 | } |
| 94 | match_count++; |
| 95 | if (ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mask) > ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(best_mask)) { |
| 96 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(ifname, 0, sizeof(ifname)); |
| 97 | strncpycxx . . $ strncpy(255158a415b673b0).(ifname, cur->ifa_namecxx . . $ ifaddrs#ifa_name., sizeof(ifname)-1); |
| 98 | best_mask = mask; |
| 99 | } |
| 100 | } |
| 101 | if (match_count) { |
| 102 | ret = ifname; |
| 103 | if (verbosecxx . . $ verbose.) { |
| 104 | printfcxx . . $ printf(581fddd72a8db8fc).("arping: Autodetected interface %s\n", ret); |
| 105 | } |
| 106 | } else { |
| 107 | if (verbosecxx . . $ verbose. > 1) { |
| 108 | printfcxx . . $ printf(581fddd72a8db8fc).("arping: Failed to find iface using" |
| 109 | " getifaddrs().\n"); |
| 110 | } |
| 111 | xsnprintfcxx . . $ xsnprintf(ecdd28f83a80e0a0).(ebuf, LIBNET_ERRBUF_SIZEcxx . . $ `<external>/9617454641823861997/libnet-macros.h:112:9`!, |
| 112 | "No matching interface found using getifaddrs()."); |
| 113 | } |
| 114 | out: |
| 115 | if (ifa) { |
| 116 | freeifaddrscxx . . $ freeifaddrs(195192b4a808fd42).(ifa); |
| 117 | } |
| 118 | return ret; |
| 119 | } |
| 120 | /* ---- Emacs Variables ---- |
| 121 | * Local Variables: |
| 122 | * c-basic-offset: 8 |
| 123 | * indent-tabs-mode: nil |
| 124 | * End: |
| 125 | */ |