Annotation of embedaddon/arping/src/findif_linux.c, revision 1.1.1.4

1.1       misho       1: /* arping/src/findif_linux.c
                      2:  *
1.1.1.3   misho       3:  *  Copyright (C) 2000-2014 Thomas Habets <thomas@habets.se>
1.1       misho       4:  *
1.1.1.3   misho       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.
1.1       misho       9:  *
1.1.1.3   misho      10:  *  This program is distributed in the hope that it will be useful,
1.1       misho      11:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1.1.1.3   misho      12:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     13:  *  GNU General Public License for more details.
1.1       misho      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:  */
1.1.1.2   misho      19: /**
                     20:  * Fallback to ugly solution. This should not actually be used, as
                     21:  * modern systems have getifaddrs().
                     22:  */
1.1       misho      23: #if HAVE_CONFIG_H
                     24: #include "config.h"
                     25: #endif
                     26: 
                     27: #include <stdio.h>
                     28: #include <string.h>
                     29: 
1.1.1.3   misho      30: #if HAVE_LIBNET_H
                     31: #include <libnet.h>
                     32: #endif
                     33: 
1.1       misho      34: #include "arping.h"
                     35: 
1.1.1.4 ! misho      36: #error "This code should never be chosen. If you just want to use it then uncomment this error line"
        !            37: 
1.1       misho      38: /**
                     39:  * WARNING: non-reentrant
                     40:  */
                     41: const char *
1.1.1.2   misho      42: arping_lookupdev(uint32_t srcip,
1.1       misho      43:                  uint32_t dstip,
                     44:                  char *ebuf)
                     45: {
1.1.1.3   misho      46:         FILE *f = NULL;
1.1       misho      47:        static char buf[1024];
                     48:        char buf1[1024];
                     49:        char buf2[1024];
                     50:        char *p,*p2;
                     51:        int n;
                     52: 
1.1.1.3   misho      53:         *ebuf = 0;
                     54: 
                     55:         do_libnet_init(NULL, 0);
1.1       misho      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"))) {
1.1.1.3   misho      65:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
                     66:                          "popen(/sbin/ip): %s", strerror(errno));
1.1       misho      67:                goto failed;
                     68:        }
                     69:        if (0>(n = fread(buf, 1, sizeof(buf)-1, f))) {
1.1.1.3   misho      70:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
                     71:                          "fread(/sbin/ip): %s", strerror(errno));
1.1       misho      72:                goto failed;
                     73:        }
                     74:        buf[n] = 0;
                     75:        if (-1 == pclose(f)) {
1.1.1.3   misho      76:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
                     77:                          "pclose(/sbin/ip): %s", strerror(errno));
1.1       misho      78:                goto failed;
                     79:        }
1.1.1.3   misho      80:         f = NULL;
1.1       misho      81: 
                     82:        /*
                     83:         * Parse interface name
                     84:         */
1.1.1.3   misho      85:         const char* head = "dev ";
                     86:         p = strstr(buf, head);
1.1       misho      87:        if (!p) {
1.1.1.3   misho      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.");
1.1       misho      93:                goto failed;
                     94:        }
                     95: 
1.1.1.3   misho      96:         p += strlen(head);
1.1       misho      97: 
                     98:        p2 = strchr(p, ' ');
                     99:        if (!p2) {
1.1.1.3   misho     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.");
1.1       misho     105:                goto failed;
                    106:        }
                    107:        *p2 = 0;
                    108:        return p;
                    109:  failed:
1.1.1.3   misho     110:         if (f) {
                    111:                 pclose(f);
                    112:         }
1.1.1.2   misho     113:        return NULL;
1.1       misho     114: }
1.1.1.2   misho     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>