[TriLUG] netlink sockets

John F Davis johndavi at us.ibm.com
Mon May 13 16:15:18 EDT 2002


Hello

I finally checked out the source you mentioned below.  It unfortunately
does ioctls as well in a polling loop with sleeps.  (It does have
maketone.c however, which is interesting in its self.)

For what its worth, here is a code fragment I wrote (way back when) which
also does ioctls.  This simple code will just print each interface name and
the words up (if the interface is up) and loopback (if the interface is
loopback).
ie. its the same thing, but brain dead simple and in one file.  The error
routines are in another library and you can always roll your own in that
regard.

Here is some sample output.

davis at tadpole:~/progs/c/linktest3$ ./driver
interface[0] name=lo up loopback
interface[1] name=tr0 up

<in another window I did a ifconfig lo down>

davis at tadpole:~/progs/c/linktest3$ ./driver
interface[0] name=lo loopback
interface[1] name=tr0 up


#include <net/if.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "utils.h"

#define MaxInterfaces 8

int stRouterSocket;

//proto's
void getFlags(char *pchIfName);

int main(int argc, char* argv[]) {
    int i,ret;
    struct ifconf ifc;
    struct ifreq ifreqs[ MaxInterfaces ];   // ioctl interface

    stRouterSocket = socket(PF_INET,SOCK_STREAM,0);
    if (-1 == stRouterSocket) {
        err_sys("Couldn't get router socket.");
    }

    ifc.ifc_len = MaxInterfaces * sizeof(struct ifreq);
    ifc.ifc_ifcu.ifcu_req = ifreqs;

    ret = ioctl(stRouterSocket, SIOCGIFCONF,&ifc);
    if (-1 == ret) {
        err_sys("Couldn't get interface configurations.");
    }

    for(i=0; i<ifc.ifc_len/sizeof(struct ifreq); i++) {
        printf("interface[%d] name=%s ",i, ifreqs[i].ifr_ifrn.ifrn_name);
        getFlags(ifreqs[i].ifr_ifrn.ifrn_name);
        printf("\n");
    }


    close(stRouterSocket);

    return (0);
}
void getFlags(char *pchIfName) {
    int ret;
    struct ifreq ifreq;

    strcpy(ifreq.ifr_ifrn.ifrn_name, pchIfName);
    ret = ioctl(stRouterSocket, SIOCGIFFLAGS,&ifreq);
    if (-1 == ret) {
        err_sys("Couldn't get interface flags.");
    }
    if (ifreq.ifr_ifru.ifru_flags & IFF_UP ) { printf("up "); }
    if (ifreq.ifr_ifru.ifru_flags & IFF_LOOPBACK ) { printf("loopback "); }

}




JD

Michael Alan Dorman <mdorman at debian.org>@trilug.org on 05/08/2002 09:27:10
AM

Please respond to trilug at trilug.org

Sent by:    trilug-admin at trilug.org


To:    trilug at trilug.org
cc:
Subject:    Re: [TriLUG] netlink sockets



"John F Davis" <johndavi at us.ibm.com> writes:
> Does anybody know anything about netlink sockets?  I'm a little confused.
> I understand netlink sockets can be used to update the routing table and
to
> get routing table updates,
> but can netlink sockets be used to get interface link messages?  How
about
> network interface ifconfig up and down notifications?  Also, I want to
get
> these notifications in user space.
> Any idea?

Nothing direct, but the laptop-net package in Debian has a daemon
called ifd that watches a port and initiates various actions when it
connects or disconnects.  You could probably crib some of what you're
looking for from that:

http://http.us.debian.org/debian/pool/main/l/laptop-net/laptop-net_2.7.orig.tar.gz


Mike.
_______________________________________________
TriLUG mailing list
    http://www.trilug.org/mailman/listinfo/trilug
TriLUG Organizational FAQ:
    http://www.trilug.org/~lovelace/faq/TriLUG-faq.html





More information about the TriLUG mailing list