File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / arping / src / findif_linux.c
Revision 1.1.1.4 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Mar 16 23:40:57 2021 UTC (3 years, 3 months ago) by misho
Branches: arping, MAIN
CVS tags: v2_21, HEAD
arping 2.21

    1: /* arping/src/findif_linux.c
    2:  *
    3:  *  Copyright (C) 2000-2014 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: /**
   20:  * Fallback to ugly solution. This should not actually be used, as
   21:  * modern systems have getifaddrs().
   22:  */
   23: #if HAVE_CONFIG_H
   24: #include "config.h"
   25: #endif
   26: 
   27: #include <stdio.h>
   28: #include <string.h>
   29: 
   30: #if HAVE_LIBNET_H
   31: #include <libnet.h>
   32: #endif
   33: 
   34: #include "arping.h"
   35: 
   36: #error "This code should never be chosen. If you just want to use it then uncomment this error line"
   37: 
   38: /**
   39:  * WARNING: non-reentrant
   40:  */
   41: const char *
   42: arping_lookupdev(uint32_t srcip,
   43:                  uint32_t dstip,
   44:                  char *ebuf)
   45: {
   46:         FILE *f = NULL;
   47: 	static char buf[1024];
   48: 	char buf1[1024];
   49: 	char buf2[1024];
   50: 	char *p,*p2;
   51: 	int n;
   52: 
   53:         *ebuf = 0;
   54: 
   55:         do_libnet_init(NULL, 0);
   56: 	libnet_addr2name4_r(dstip,0,buf2,1024);
   57: 	libnet_addr2name4_r(srcip,0,buf1,1024);
   58: 
   59: 	/*
   60: 	 * Construct and run command
   61: 	 */
   62: 	snprintf(buf, 1023, "/sbin/ip route get %s from %s 2>&1",
   63: 		 buf2, buf1);
   64: 	if (!(f = popen(buf, "r"))) {
   65:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
   66:                          "popen(/sbin/ip): %s", strerror(errno));
   67: 		goto failed;
   68: 	}
   69: 	if (0>(n = fread(buf, 1, sizeof(buf)-1, f))) {
   70:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
   71:                          "fread(/sbin/ip): %s", strerror(errno));
   72: 		goto failed;
   73: 	}
   74: 	buf[n] = 0;
   75: 	if (-1 == pclose(f)) {
   76:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
   77:                          "pclose(/sbin/ip): %s", strerror(errno));
   78: 		goto failed;
   79: 	}
   80:         f = NULL;
   81: 
   82: 	/*
   83: 	 * Parse interface name
   84: 	 */
   85:         const char* head = "dev ";
   86:         p = strstr(buf, head);
   87: 	if (!p) {
   88:                 if (verbose) {
   89:                         printf("arping: /sbin/ip output: %s\n", buf);
   90:                 }
   91:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
   92:                          "\"dev \" not found in /sbin/ip output.");
   93: 		goto failed;
   94: 	}
   95: 
   96:         p += strlen(head);
   97: 
   98: 	p2 = strchr(p, ' ');
   99: 	if (!p2) {
  100:                 if (verbose) {
  101:                         printf("arping: /sbin/ip output: %s\n", buf);
  102:                 }
  103:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
  104:                          "interface not found in /sbin/ip output.");
  105: 		goto failed;
  106: 	}
  107: 	*p2 = 0;
  108: 	return p;
  109:  failed:
  110:         if (f) {
  111:                 pclose(f);
  112:         }
  113: 	return NULL;
  114: }
  115: /* ---- Emacs Variables ----
  116:  * Local Variables:
  117:  * c-basic-offset: 8
  118:  * indent-tabs-mode: nil
  119:  * End:
  120:  */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>