Giter VIP home page Giter VIP logo

Comments (3)

guyharris avatar guyharris commented on June 15, 2024

Submitted by guy_harris

Logged In: YES
user_id=541179

If RH 9 includes "getifaddrs()", you won't necessarily have
all addresses in the interface list be IPv4 addresses;
perhaps if it doesn't include "getifaddrs()", they all are,
and perhaps if it does, the first one is, but it's still
probably unwise to assume the first address is an IPv4
address.

Try the attached version of iip.c instead.

from libpcap.

infrastation avatar infrastation commented on June 15, 2024

Cleaner version of iip.c

/*
 * x was here
 * cc iip.c -lpcap -o iip
 */

#define _GNU_SOURCE     /* needed by vasprintf */
#define __USE_BSD

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <pcap.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>



char            errbuf[PCAP_ERRBUF_SIZE];


char           *ipfromlong(unsigned long s_addr);
u_int32_t       get_interface_addr(char *interface);
void            fatal(char *pattern, ...);


int
main(int argc, char **argv)
{
    if (argc != 2) {
    printf("usage: %s interface\n\n", argv[0]);
    exit(0);
    }

    printf("device: %s address: %s\n", argv[1],
       ipfromlong(get_interface_addr(argv[1])));

    return 0;
}



void
fatal(char *pattern, ...)
{
    va_list         ap;
    int             len;
    char           *p;

    va_start(ap, pattern);
    len = vasprintf(&p, pattern, ap);
    va_end(ap);

    if (len > 0) {
        printf("%s; exit forced.\n", p);
        free(p);
    }

    exit(1);
}


char
               *
ipfromlong(unsigned long s_addr)
{
    struct in_addr  myaddr;

    myaddr.s_addr = s_addr;
    return inet_ntoa(myaddr);
}


u_int32_t
get_interface_addr(char *interface)
{
    pcap_if_t      *dev,
                   *alldevs;
    u_int32_t       ret;
    struct pcap_addr *addr;

    if (strcmp("lo", interface) == 0)
    return (htonl(INADDR_LOOPBACK));

    if (pcap_findalldevs(&alldevs, errbuf) < 0)
    fatal("pcap_findalldevs(): %s", errbuf);

    ret = INADDR_NONE;

    for (dev = alldevs; dev; dev = dev->next) {
    if (strcmp(dev->name, interface) == 0) {
        for (addr = dev->addresses; addr; addr = addr->next) {
        if (addr->addr && addr->addr->sa_family == AF_INET) {
            ret =
            ((struct sockaddr_in *) (addr->addr))->sin_addr.s_addr;
            break;
        }
        }
    }
    }

    pcap_freealldevs(alldevs);

    if (ret == INADDR_NONE)
    fatal("get_interface_addr(): address not found");

    return ret;
}

from libpcap.

infrastation avatar infrastation commented on June 15, 2024

Addressed, closing.

from libpcap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.