避免通过不匹配地址的接口发送数据

时间:2014-10-03 17:54:35

标签: c linux macos sockets freebsd

想象一下这样的网络设置:

+---------+               +---------+                     
|      (1)+---------------+(2)      |    
|  host1  |               |  host2  |
|         |               |         |
|      (3)+---------------+(4)      |
+---------+               +---------+

其中1,2,3,4是网络接口。两个接口之间的线路表示存在(路由)连接。现在,当我设置客户端和服务器时,将服务器绑定到IP地址4,客户端绑定到1我仍然可以建立连接,因为host1的传出流量将通过接口3路由。

以下代码不是我的实际程序,而是来自a tutorial的示例代码。我稍微修改了一下。它仅用于显示一些不像我希望它表现的工作代码。

server.c

/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     inet_aton(argv[2], &serv_addr.sin_addr);
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
     bzero(buffer,256);
     n = read(newsockfd,buffer,255);
     if (n < 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n",buffer);
     n = write(newsockfd,"I got your message",18);
     if (n < 0) error("ERROR writing to socket");
     close(newsockfd);
     close(sockfd);
     return 0; 
}

client.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <arpa/inet.h>

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr, client_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);

    client_addr.sin_family = AF_INET;
    inet_aton(argv[3], &client_addr.sin_addr);
    client_addr.sin_port = htons(portno);
    if (bind(sockfd, (struct sockaddr *) &client_addr,
              sizeof(client_addr)) < 0) 
              error("ERROR on binding");

    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");
    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    close(sockfd);
    return 0;
}

我想要的是流量仅从1流向2(反之亦然),从3流向4流(或反之亦然)。理想地尝试从例如建立连接1到4应该失败,因为没有路线。

我无法修改任何主机的路由表(或者只能为我的进程更改它?)。 我已经尝试SO_BINDTODEVICE了。一般来说,这就是我想要的,即流量然后流过1和4.然而,它确实不会避免建立1和4之间的连接。后者不是一个要求,但会很高兴。不幸的是,SO_BINDTODEVICE在OSX上不可用,我找不到任何类似的套接字选项。

int bind_to_if(int fd, const char * const if_name)
{
  struct ifreq interface;

  memset(&interface, 0, sizeof(interface));
  strncpy(interface.ifr_name, if_name, IFNAMSIZ);
  if(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &interface, sizeof(struct ifreq))) {
    return -1;
  }

  return0;
}

我的代码需要在Linux,FreeBSD和OSX下运行。如果无法避免,有些ifdef是可以接受的。

0 个答案:

没有答案