多个以太网接口 - 如何创建从C code单独的网络和访问 [英] Multiple Ethernet Interfaces - How to create a separate network and access from C code

查看:516
本文介绍了多个以太网接口 - 如何创建从C code单独的网络和访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Linux设备(实际上是一个的BeagleBoard为原型)有两个以太网适配器。我想是这样的:

I have a Linux device (actually a BeagleBoard for the prototype) with two Ethernet adapters. What I want is this:


  • 主以太网接口(eth0的)连接到客户端的网络(可能是DHCP或分配一个静态IP)。

  • Primary ethernet interface (eth0) connects to a client's network (may be DHCP or assigned a static IP).

第二个以太网接口(eth1的)直接连接到另一台Linux板。

Second ethernet interface (eth1) connects directly to another Linux board.

在比格尔用户级C程序可以监听来自客户端的网络(在eth0)传入连接,根据需要作出反应,并有可能连接到其他设备上的eth1

User-level C programs on the Beagle can listen for incoming connections from the client's network (on eth0), respond as required, and possibly connect to the other device on eth1

我们希望第二设备从网络的其余部分被完全隐藏,因此,只有在小猎犬的程序可以连接到它。

We want the second device to be completely hidden from the rest of the network, so that only programs on the Beagle can connect to it.

我希望这两个接口可能是完全分开的,我的code可以选择它想监听/打开的连接的接口。典型的code:

I was hoping that the two interfaces could be completely separate, and my code could choose which interface it wanted to listen / open connections on. Typical code:

socket_desc = socket(AF_INET, SOCK_STREAM, 0);

memset(&client_addr, 0, sizeof(client_addr)); 
client_addr.sin_family = AF_INET;
inet_pton(  AF_INET, address, (void *)(&(client_addr.sin_addr.s_addr))  );
client_addr.sin_port = htons(port);   

/* Connect to remote on TCP port: */
connect(*socket_desc, (struct sockaddr*) &client_addr, sizeof(client_addr) );
...

我们可以设置的地址和端口进行连接,但不是以太网接口。我发现<一个href=\"http://stackoverflow.com/questions/16988688/linux-tcp-socket-programming-over-multiple-ethernet-ports\">this回答。这是否意味着什么,我想实现必须由内核路由?

We can set the address and port to connect, but not the ethernet interface. I found this answer. Does this mean that what I am trying to achieve MUST be left to the kernel routing?

我知道我可以设置的eth1是对自己的子网,但我有这样一个问题:既然我们不知道客户端的网络什么,我怎么知道,无论我子网使用不会结束与客户端的网络冲突的?例如,我在这里使用192.168.1.0,这样我就可以将eth1上192.168.0.0 ......但是如果客户端使用的范围是多少?我们不希望重新配置设置ETH1和每个部署其连接的设备,虽然我们可能必须配置为eth0。

I know I can set up eth1 to be on its own subnet, but I have a problem with this: Given that we don't know anything about the client's network, how do I know that whatever subnet I use won't end up conflicting with the client's network? For example, I use 192.168.1.0 here, so I could put eth1 on 192.168.0.0... but what if the client uses that range? We don't want to reconfigure the settings for eth1 and its attached device for every deployment, although we may have to configure eth0.

那么,有没有更好的方式来做到这一点?或者保留的IP地址范围,我可以用它为无法保证与客户的网络冲突eth1上的子网(例如169.254.1.x)?

So is there a better way to do this? Or perhaps a reserved IP address range I could use for the subnet on eth1 which is guaranteed not to conflict with the client's network (e.g. 169.254.1.x)?

对不起,这是一个有点模糊,但我一直在谷歌搜索这几天,可能已经变得更加糊涂了,而不是减少。

Sorry this is a bit vague, but I have been Googling this for days and have probably become more confused rather than less.

编辑 - 2013年8月29日:

我只是发现这个问题提供部分答案:你可以使用的setsockopt(...) SO_BINDTODEVICE 绑定到一个特定的设备/接口。这似乎工作,虽然我仍然无法弄清楚如何设置路由表在内部网络碰巧有相同的IP地址范围内连接到eth0的外部网络的情况可靠地工作。

I've just found this question which provides a partial answer: You can use setsockopt(...) with SO_BINDTODEVICE to bind to a specific device / interface. This seems to work, although I still can't figure out how to set up routing tables to work reliably in the situation where the "internal" network happens to have the same IP address range as the "external" network connected to eth0.

推荐答案

我有一个项目,同样的要求,我们结束了使用LXC,在那里我们可以在一个物理接口分配到一个容器中,所以我们可以有容器同一IP地址的子网,例如。

I had the same requirement for a project and we ended up using LXC, where we could assign a physical interface to a container, and so we could have containers with same ip address subnets, for example.

检查它在 http://lxc.sourceforge.net/

这篇关于多个以太网接口 - 如何创建从C code单独的网络和访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆