Annotation of embedaddon/arping/src/findif_bsdroute.c, revision 1.1.1.2

1.1       misho       1: /* arping/src/findif_bsdroute.c
                      2:  *
1.1.1.2 ! misho       3:  *  Copyright (C) 2000-2014 Thomas Habets <thomas@habets.se>
1.1       misho       4:  *
1.1.1.2 ! 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.2 ! 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.2 ! 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:  */
                     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: 
1.1.1.2 ! misho      30: #if HAVE_LIBNET_H
        !            31: #include <libnet.h>
        !            32: #endif
        !            33: 
1.1       misho      34: #include "arping.h"
                     35: 
                     36: /**
                     37:  *
                     38:  */
                     39: const char *
                     40: arping_lookupdev(uint32_t srcip,
                     41:                  uint32_t dstip,
                     42:                  char *ebuf)
                     43: {
1.1.1.2 ! misho      44:         FILE *f = NULL;
1.1       misho      45:        static char buf[10240];
                     46:        char buf1[1024];
                     47:        char *p,*p2;
                     48:        int n;
                     49: 
1.1.1.2 ! misho      50:         *ebuf = 0;
        !            51: 
        !            52:         do_libnet_init(NULL, 0);
1.1       misho      53:        libnet_addr2name4_r(dstip,0,buf1, 1024);
                     54: 
                     55:        /*
                     56:         * Construct and run command
                     57:         */
                     58:        snprintf(buf, 1023, "/sbin/route -n get %s 2>&1",
                     59:                 buf1);
                     60:        if (!(f = popen(buf, "r"))) {
1.1.1.2 ! misho      61:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
        !            62:                          "popen(/sbin/route): %s", strerror(errno));
1.1       misho      63:                goto failed;
                     64:        }
                     65:        if (0 > (n = fread(buf, 1, sizeof(buf)-1, f))) {
1.1.1.2 ! misho      66:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
        !            67:                          "fread(/sbin/route): %s", strerror(errno));
1.1       misho      68:                goto failed;
                     69:        }
                     70:        buf[n] = 0;
                     71:        if (-1 == pclose(f)) {
1.1.1.2 ! misho      72:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
        !            73:                          "pclose(/sbin/route): %s", strerror(errno));
1.1       misho      74:                goto failed;
                     75:        }
1.1.1.2 ! misho      76:         f = NULL;
1.1       misho      77: 
                     78:        /*
                     79:         * Parse interface name
                     80:         */
1.1.1.2 ! misho      81:         const char* head = "interface: ";
        !            82:         p = strstr(buf, head);
1.1       misho      83:        if (!p) {
1.1.1.2 ! misho      84:                 if (verbose) {
        !            85:                         printf("arping: /sbin/route output: %s\n", buf);
        !            86:                 }
        !            87:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
        !            88:                          "\"interface:\" not found in /sbin/route output.");
1.1       misho      89:                goto failed;
                     90:        }
                     91: 
1.1.1.2 ! misho      92:         p += strlen(head);
1.1       misho      93: 
                     94:        p2 = strchr(p, '\n');
                     95:        if (!p2) {
1.1.1.2 ! misho      96:                 if (verbose) {
        !            97:                         printf("arping: /sbin/route output: %s\n", buf);
        !            98:                 }
        !            99:                 snprintf(ebuf, LIBNET_ERRBUF_SIZE,
        !           100:                          "interface not found in /sbin/route output.");
1.1       misho     101:                goto failed;
                    102:        }
                    103:        *p2 = 0;
                    104:        return p;
                    105:  failed:
1.1.1.2 ! misho     106:         if (f) {
        !           107:                 pclose(f);
        !           108:         }
1.1       misho     109:        return NULL;
                    110: }
                    111: /* ---- Emacs Variables ----
                    112:  * Local Variables:
                    113:  * c-basic-offset: 8
                    114:  * indent-tabs-mode: nil
                    115:  * End:
                    116:  */

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