0

我正在尝试使用套接字来嗅探 OpenFlow 数据包。我有一些代码试图收集 OF 数据包并打印标题信息,但它似乎没有打印正确的信息。我已经在 mininet 中设置了一个环境并将其指向一个遥控器。在wireshark中,我看到标准的回显请求/回复正在进行,在openflow中是在标题部分定义的类型2和3。当我查看我在程序中收集的数据包数据时,我似乎打印了奇数的 2 类数据包,没有其他任何东西让我认为我的代码不正确,并且打印的 2 类数据包不是 2 类数据包。

我一直在玩这个,所以如果它有点混乱/不完美,我很抱歉。我试图让它对每个人都可读:

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <openflow/openflow.h>
#include "openflow/lib/ofpbuf.h"

int sock_raw;
void DieWithError(char *errorMessage);
void handlePacket(unsigned char *, int);
void printOFHeader(struct ofp_header *);

int main()
{
    int saddr_size , data_size;
    struct sockaddr saddr;
    unsigned char *buffer = (unsigned char *)malloc(65536); //Its Big! -- Malloc allocates a block of size bytes of memory, returning a pointer to the beginning of the block
    printf("Starting...\n");

    //Create a raw socket that shall sniff
    sock_raw = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
    printf("socket: %d\n",sock_raw);
    if(sock_raw < 0)
    {
        printf("Socket Error\n");
        return 1;
    }


    while(1)
    {

        saddr_size = sizeof saddr;
        //Receive a packet

        data_size = recvfrom(sock_raw, buffer, 65536, 0 , &saddr , (socklen_t*)&saddr_size);
        if(data_size <0 )
        {
            printf("Recvfrom error , failed to get packets\n");
            return 1;
        }

        //Now process the packet
        handlePacket(buffer, data_size);
    }
    close(sock_raw);
    printf("Finished");
    return 0;
}

void DieWithError(char *errorMessage)
{
    perror(errorMessage);
    exit(1);
}

void handlePacket(unsigned char *buff, int data_size)
{

    //first get IP header length
    struct iphdr *iph = (struct iphdr *)buff;
    unsigned short iphdrlen = iph->ihl*4;

    //get tcp header length
    struct tcphdr *tcph = (struct tcphdr*)(buff + iphdrlen);
    unsigned int ofph_offset = (iphdrlen+(unsigned int)tcph->doff*4);

    //add total length of ip and tcp headers to the buffer to get the start of the OF Header
     struct ofp_header *oh = (struct ofp_header*)(buff + ofph_offset);

     //Check it is an OF Header -- Not sure this is right
    if(oh->version == OFP_VERSION)
    {
        printOFHeader(oh);
    }
    else
    {
       printf("This is not an openflow packet\n");
    }
}

void printOFHeader(struct ofp_header * oh)
{
    printf("OPENFLOW PACKET HEADER\n");
    printf("    |-OF Version         %d\n", oh->version);
    printf("    |-Type:              %d\n", oh->type);
    printf("    |-Transaction ID:              %d\n", oh->xid);
    printf("    |-Length:              %d\n", oh->length);
    printf("----------------------------------------------------");
    printf("\n");
}

我已经测试了以下数据:

    //first get IP header length
    struct iphdr *iph = (struct iphdr *)buff;
    unsigned short iphdrlen = iph->ihl*4;

    //get tcp header length
    struct tcphdr *tcph = (struct tcphdr*)(buff + iphdrlen);

这些打印数据包的正确端口地址和 IP 地址,所以我认为我的缺陷在于我如何分配 ofp_header 结构。这个问题最好放在 openflow.org 网站上,但我找不到邮件组。

4

1 回答 1

0

尝试订阅此邮件列表: https ://mailman.stanford.edu/pipermail/mininet-discuss/

于 2014-02-10T21:45:18.593 回答