--- embedaddon/arping/src/findif_sysctl.c 2014/06/15 16:26:43 1.1.1.1 +++ embedaddon/arping/src/findif_sysctl.c 2016/10/18 13:16:10 1.1.1.2 @@ -1,16 +1,16 @@ /* arping/src/findif_sysctl.c * - * Copyright (C) 2000-2011 Thomas Habets + * Copyright (C) 2000-2014 Thomas Habets * - * This library 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. + * 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. * - * This library is distributed in the hope that it will be useful, + * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., @@ -40,6 +40,10 @@ #include #include +#if HAVE_LIBNET_H +#include +#endif + #include "arping.h" #ifndef SALIGN @@ -72,7 +76,8 @@ arping_lookupdev(uint32_t srcip, int c; /* buffer */ - char *buf, *lim; + char *buf_memory = NULL; + char *lim; size_t bufsize; /* Matching interfaces */ @@ -84,28 +89,38 @@ arping_lookupdev(uint32_t srcip, /* Results */ static char ifName[IFNAMSIZ]; + *ebuf = 0; /* Allocate buffer and retrieve data. */ for (c = 0;;) { if (sysctl(mib, 6, NULL, &bufsize, NULL, 0) < 0) { - strcpy(ebuf, "sysctl: get buffer size error"); + snprintf(ebuf, LIBNET_ERRBUF_SIZE, + "sysctl: get buffer size error: %s", + strerror(errno)); goto failed; } - if ((buf = malloc(bufsize)) == NULL) { - strcpy(ebuf, "malloc: error"); + if ((buf_memory = malloc(bufsize)) == NULL) { + snprintf(ebuf, LIBNET_ERRBUF_SIZE, + "malloc: error: %s", strerror(errno)); goto failed; } - if (sysctl(mib, 6, buf, &bufsize, NULL, 0) == 0) { + if (sysctl(mib, 6, buf_memory, &bufsize, NULL, 0) == 0) { break; } if (errno != ENOMEM || ++c >= 10 ) { - strcpy(ebuf, "sysctl: get ifaces error"); + snprintf(ebuf, LIBNET_ERRBUF_SIZE, + "sysctl: get ifaces error: %s", + strerror(errno)); goto failed; } - fprintf(stderr, "sysctl: buffer size changed"); - free(buf); + if (verbose > 2) { + printf("sysctl: buffer size changed."); + } + free(buf_memory); + buf_memory = NULL; } + const char* buf = buf_memory; lim = buf + bufsize; /* Loop through all interfaces */ @@ -116,7 +131,8 @@ arping_lookupdev(uint32_t srcip, struct if_msghdr *ifh = (struct if_msghdr *)buf; if (ifh->ifm_type != RTM_IFINFO) { - strcpy(ebuf, "Wrong data in NET_RT_IFLIST"); + snprintf(ebuf, LIBNET_ERRBUF_SIZE, + "Wrong data in NET_RT_IFLIST."); return NULL; } sdl = (struct sockaddr_dl *)(buf + @@ -189,7 +205,7 @@ arping_lookupdev(uint32_t srcip, match_count++; - if (verbose) { + if (verbose > 1) { printf("Specified addr matches " "interface '%s':\n", tmpIfName); printf(" IP addr %s, ", @@ -213,24 +229,27 @@ arping_lookupdev(uint32_t srcip, if (match_count == 0 ) { if (verbose) { - strcpy(ebuf, - "No interface found that matches specified IP"); + snprintf(ebuf, LIBNET_ERRBUF_SIZE, + "No interface found that matches" + " specified IP."); } goto failed; } if (verbose && match_count > 1) { - printf("Using interface '%s' with src IP %s due to longer " - "mask.\n", ifName, inet_ntoa(best_addr)); + printf("arping: Using interface '%s' with src IP %s due " + "to longer mask.\n", ifName, inet_ntoa(best_addr)); } #if 0 if (ifce_ip != 0) { *ifce_ip = best_addr.s_addr; } #endif + free(buf_memory); return ifName; failed: + free(buf_memory); return NULL; }