1. 程式人生 > >Centos7 多網卡抓包可以抓到UDP但程序recvfrom不到

Centos7 多網卡抓包可以抓到UDP但程序recvfrom不到

centos7 客戶 正常 any opened 在服務器 2.x type AD

問題:

Centos7多網卡,抓包時發現某網卡上有UDP包,但是用程序recvfrom無法接收到消息。

解決步驟:

1.確認防火墻是否關閉;

已關閉

2.確認網卡是否開啟過濾:cat /proc/sys/net/ipv4/conf/ethxxx/rp_filter (ethxxx是網卡名稱)

結果:已關閉(當然此配置項只影響組播,這是病急亂投醫)

3.隨便找一個服務端和客戶端的代碼,測試是否能正常進行udp通信;

結果發現完全正常。

server.cpp

技術分享圖片
#include <sys/types.h>
#include <sys/socket.h>
#include 
<pthread.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <arpa/inet.h> int main(int argc, char **argv) { if (argc != 2) { printf("Usage: %s port\n", argv[0]); exit(
1); } printf("Welcome! This is a UDP server, I can only received message from client and reply with same message\n"); struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(atoi(argv[1])); addr.sin_addr.s_addr = htonl(INADDR_ANY); int sock;
if ( (sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("socket"); exit(1); } if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); exit(1); } char buff[409600]; struct sockaddr_in clientAddr; int n; unsigned int len = sizeof(clientAddr); while (1) { n = recvfrom(sock, buff, 409600, 0, (struct sockaddr*)&clientAddr, &len); if (n>0) { buff[n] = 0; printf("%s %u says: %s\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), buff); n = sendto(sock, buff, n, 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr)); if (n < 0) { perror("sendto"); break; } } else { perror("recv"); break; } } return 0; }
View Code

client.cpp

技術分享圖片
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>


int main(int argc, char **argv)
{
    if (argc != 3)
    {
        printf("Usage: %s ip port", argv[0]);
        exit(1);
    }
    printf("This is a UDP client\n");
    struct sockaddr_in addr;
    int sock;

    if ( (sock=socket(AF_INET, SOCK_DGRAM, 0)) <0)
    {
        perror("socket");
        exit(1);
    }
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    if (addr.sin_addr.s_addr == INADDR_NONE)
    {
        printf("Incorrect ip address!");
        close(sock);
        exit(1);
    }

    char buff[512]="aaaaaaaaaaaaaaa";
    unsigned int len = sizeof(addr);
    while (1)
    {
        gets(buff);
        int n;
        n = sendto(sock, buff, strlen(buff), 0, (struct sockaddr *)&addr, sizeof(addr));
        if (n < 0)
        {
            perror("sendto");
            close(sock);
            break;
        }
        n = recvfrom(sock, buff, 512, 0, (struct sockaddr *)&addr, &len);
        if (n>0)
        {
            buff[n] = 0;
            printf("received:");
            puts(buff);
        }
        else if (n==0)
        {
            printf("server closed\n");
            close(sock);
            break;
        }
        else if (n == -1)
        {
            perror("recvfrom");
            close(sock);
            break;
        }
    }
    
    return 0;
}
View Code

4.再次抓包查看

此次抓包發現,雙網口的Android設備用兩根獨立的網線連到服務器不同的網卡,設置兩個網段:192.168.1.xx和192.168.2.xx,其中192.168.1.xx是Android設備的默認路由網段。從Android設備發送UDP消息到192.168.2.xx網段,結果服務器端對應的網口抓包顯示的源地址為192.168.1.xx,將UDP發送消息改為192.168.1.xx後,在服務器的相應網卡地址接收,問題解決。

問題原因:

Android設備的多網卡不共用同一路由表,因此造成路由失效;當路由生效時,又把默認的網卡地址組裝到UDP中,造成此問題。

Centos7 多網卡抓包可以抓到UDP但程序recvfrom不到