Accessing/manipulating networkdevices in C

Howdy how

BenBE asked me to give him some advice how to open and manipulate network devices from user space.
Although it’s not really a kernel hacking issue, as his way of asking proposed I’ll post the respective link after all, because I’ve promised.

Usually you use APIs for such a purpose.

In this case its the library called Libdnet and the project page can be found under the following URL:
http://libdnet.sourceforge.net/

I just have taken a look into Debian’s package viewer and somehow the relevant header ( /usr/include/dnet/tun.h )
is missing ( packages broken/outdated? O.o)

Anyway, the header file would only tell us that much (Thx to benny for the tip with the highlighter! 😉 ):

/*
 * tun.h
 *
 * Network tunnel device.
 *
 * Copyright (c) 2001 Dug Song 
 *
 * $Id: tun.h,v 1.2 2005/01/25 21:29:12 dugsong Exp $
 */

#ifndef DNET_TUN_H
#define DNET_TUN_H

typedef struct tun      tun_t;

__BEGIN_DECLS
tun_t      *tun_open(struct addr *src, struct addr *dst, int mtu);
int         tun_fileno(tun_t *tun);
const char *tun_name(tun_t *tun);
ssize_t     tun_send(tun_t *tun, const void *buf, size_t size);
ssize_t     tun_recv(tun_t *tun, void *buf, size_t size);
tun_t      *tun_close(tun_t *tun);
__END_DECLS

#endif /* DNET_TUN_H */

An example can be found a little more deeper on the following site:
http://csfacwiki.cslabs.ewu.edu/wiki/securitylab/index.php/Libdnet

By using libudev for dynamic file checking and path allocation
you could also make it a little more dynamic and flexible.
(Using hardcoded strings in order to allocate files is usually a very bad idea)

Leave a Reply

Your email address will not be published. Required fields are marked *