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