Re: Emulating multiple nics with one
- From: Pascal Hambourg <boite-a-spam@xxxxxxxxxxxxxxx>
- Date: Wed, 07 Nov 2007 17:55:53 +0100
Mambazo a écrit :
If you could please forward you test code to me,
See attached files.
DISCLAIMER : I am not a software developper. I just made this up from some sample code found here and there. /* taplink.c : direct link between multiple TAP interfaces (~hub) */
/* 20/10/2007 */
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <errno.h>
#include <unistd.h>
/* uncomment to print debug messages */
//#define DEBUG
#define max(a, b) ((a)>(b) ? (a):(b))
/* nomber of TAP interfaces to create (minimum 2) */
#define NBIF 3
int main()
{
struct ifreq ifr[NBIF];
int fd[NBIF], fm, len, err, i, j;
char buf[2048];
char *dev="";
fd_set fds;
FD_ZERO(&fds);
fm = 0;
/* create the TAP interfaces */
for (i=0; i<NBIF; i++)
{
fd[i] = open("/dev/net/tun", O_RDWR);
if (fd[i] < 0)
{
printf("cannot open /dev/net/tun for interface #%d\n", i);
return -1;
}
fm = max(fm, fd[i]);
FD_SET(fd[i], &fds);
memset(&ifr[i], 0, sizeof(struct ifreq));
/* Flags: IFF_TUN - TUN device (no Ethernet headers)
* IFF_TAP - TAP device
*
* IFF_NO_PI - Do not provide packet information
*/
ifr[i].ifr_flags = IFF_TAP;
if (*dev)
{
strncpy(ifr[i].ifr_name, dev, IFNAMSIZ);
}
err = ioctl(fd[i], TUNSETIFF, (void *)&(ifr[i]));
if (err < 0)
{
printf("ioctl SETIFF error on interface #%d\n", i);
return -1;
}
#ifdef DEBUG
printf("file descriptor : %d, ", fd[i]);
#endif
printf("interface #%d is : %s\n", i, ifr[i].ifr_name);
err = ioctl(fd[i], TUNSETNOCSUM, 1);
if (err < 0)
{
printf("ioctl SETNOCSUM error on interface #%d\n", i);
return -1;
}
}
fm++;
#ifdef DEBUG
printf("fm = %d\n",fm);
#endif
/* transmit frames from one interface to the others */
while (1)
{
select(fm, &fds, NULL, NULL, NULL);
#ifdef DEBUG
printf("select :");
#endif
for (i=0 ; i<NBIF; i++)
{
if (FD_ISSET(fd[i], &fds))
{
#ifdef DEBUG
printf(" %s", ifr[i].ifr_name);
#endif
len = read(fd[i], buf, sizeof(buf));
for (j=0; j<NBIF; j++)
{
if (j != i)
{
write(fd[j], buf, len);
}
}
}
else
{
FD_SET(fd[i], &fds);
}
}
#ifdef DEBUG
printf("\n");
#endif
}
}
# Bridge a group of TAP interfaces with an ethernet interface
# assumptions :
# the ethernet interface is eth0
# the TAP interfaces are tap0, tap1 & tap2
# the bridge interface is br0
# create a bridge
brctl addbr br0
# add the ethernet interface to the bridge
# the ethernet interface must not have any IP address (nor aliases eth0:Y)
# run "ifconfig eth0 0.0.0.0" to remove the primary IP address if necessary
# it must not be bound to a DHCP client either
# kill the DHCP client or disable the interface with the high level command
# (e.g. ifdown eth0 on Debian-like systems) if necessary
brctl addif br0 eth0
# create the TAP interface group (virtual hub)
/path/to/taplink &
# add one TAP interface to the bridge
brctl addif br0 tap0
# enable all the interfaces
# and if required, change the TAP interfaces MAC addresses
ifconfig eth0 up
ifconfig br0 up
ifconfig tap0 up
ifconfig tap1 up hw ether 00:11:11:11:11:11
ifconfig tap2 up hw ether 00:22:22:22:22:22
# wait a few seconds until the bridge enters forwarding state
sleep 5
# configure the bridge interface with DHCP
dhclient br0
# configure the TAP interface(s) not in the bridge with DHCP
dhclient tap1
dhclient tap2
- References:
- Re: Emulating multiple nics with one
- From: Mambazo
- Re: Emulating multiple nics with one
- Prev by Date: private APN (in GSM network)
- Next by Date: Re: Is there any point to full host names in /etc/hosts ?
- Previous by thread: Re: Emulating multiple nics with one
- Next by thread: Re: Emulating multiple nics with one
- Index(es):
Relevant Pages
|