简体   繁体   English

C ++ recvfrom不接收所有(二进制)数据

[英]C++ recvfrom doesn't receive all (binary) data

I'm trying to recieve a DNS request (DNS content with DNS header) in C++ on Linux 2.6.32. 我正在尝试在Linux 2.6.32上用C ++接收DNS请求(带有DNS标头的DNS内容)。 The code works when I write a string over UDP with netcat. 当我使用netcat在UDP上写一个字符串时,代码有效。 But if I do a "nslookup google.com", it recieves only this: 但如果我做“nslookup google.com”,它只收到这个:

root@EliteBook:/home/.../dns# xxd request.bin  
0000000: 8a2b 01

And if I use "tcpdump -x 'udp port 53' it shows me many bytes more. 如果我使用“tcpdump -x'udp port 53',它会向我显示更多字节。

My C++ code: 我的C ++代码:

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

#define BUFLEN 1024*6
#define NPACK 10
#define PORT 53

using namespace std;

void diep(const string &s)
{
        perror(s.c_str());
        exit(1);
}

int main()
{
        struct sockaddr_in si_me;
        struct sockaddr si_other;
        int slen=sizeof(si_other);
        int s;                                          // UDP Socket
        char buf[BUFLEN];
        buf[0] = 'X';
        buf[1] = 'Y';
        buf[2] = 'Z';

        if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
                diep("Failed to create socket!");

        memset((char *) &si_me, 0, sizeof(si_me));
        si_me.sin_family = AF_INET;
        si_me.sin_port = htons(PORT);
        si_me.sin_addr.s_addr = htonl(INADDR_ANY);
        if (bind(s, (const sockaddr*) &si_me, sizeof(si_me))==-1)
                diep("bind");

        for (int i=0; i<NPACK; i++) {
                if (recvfrom(s, buf, BUFLEN, 0, &si_other, (socklen_t*) &slen)==-1)
                        diep("recvfrom()");

                struct sockaddr_in *si_other_in = (struct sockaddr_in *)&si_other;

                cout << "Pkg arrived: " << buf << " " << inet_ntoa(si_other_in->sin_addr) << ":" << ntohs(si_other_in->sin_port) << endl;

                FILE * pFile;
                pFile = fopen ("request.bin","wb");
                if (pFile!=NULL)
                {
                        fputs (buf, pFile);
                        fclose (pFile);
                }
        }
        close(s);

        return 0;
}

Compile this with 用这个编译

g++ -Wall main.cpp -o dns

So, what is my fault, did I forget something or must I use an other call to read binary data? 那么,我的错是什么,我忘记了什么或者我是否必须使用其他调用来读取二进制数据?

PS: Localhost is the first nameserver in /etc/resolv.conf PS:Localhost是/etc/resolv.conf中的第一个名称服务器

EDIT: 编辑:

I looket at the return value from recvfrom, and it shows me: 我看了一下recvfrom的返回值,它告诉我:

size: **27**
Pkg arrived: �. 127.0.0.1:36686
size: **45**
Pkg arrived: l 127.0.0.1:38952

That should be OK. 那应该没问题。 Why does my buffer doesn't hold all bytes? 为什么我的缓冲区不能保存所有字节?

You are mostly ignoring the return value of recvfrom() . 你大多忽略了recvfrom()的返回值。 That return value tells you how many bytes you received. 该返回值告诉您收到了多少字节。

From the man page : 手册页

Upon successful completion, recvfrom() returns the length of the message in bytes 成功完成后, recvfrom()以字节为单位返回消息的长度

Without that information, you cannot know that you didn't receive all of the bytes. 没有这些信息,您无法知道您没有收到所有字节。

So, what is my fault, did I forget something or must I use an other call to read binary data? 那么,我的错是什么,我忘记了什么或者我是否必须使用其他调用来读取二进制数据?

You are reading the binary data just fine, but you are writing it incorrectly. 您正在阅读二进制数据,但您写的不正确。 Your call to std::cout << buf and your call to fputs(buf, pFile) each operate in character strings, not on binary data. 你对std::cout << buf的调用以及你对fputs(buf, pFile)调用都是用字符串操作的,而不是二进制数据。 Specifically, they each write the bytes up to, but not including, the first zero-valued byte. 具体地说,它们每个都写入字节,但不包括第一个零值字节。

To fix your program, record the answer from recvfrom, skip the call to cout << buf , and replace your fputs() with an fwrite . 要修复你的程序,记录来自recvfrom的答案,跳过对cout << buf的调用,并用fwrite替换你的fputs()

// UNTESTED
for (int i=0; i<NPACK; i++) {
    int bufsize;
    if ( (bufsize = recvfrom(s, buf, BUFLEN, 0, &si_other, (socklen_t*) &slen))==-1)
        diep("recvfrom()");

        struct sockaddr_in *si_other_in = (struct sockaddr_in *)&si_other;

        cout << "Pkg arrived: " << bufsize << " " << inet_ntoa(si_other_in->sin_addr) << ":" << ntohs(si_other_in->sin_port) << endl;

        FILE * pFile;
        pFile = fopen ("request.bin","wb");
        if (pFile!=NULL)
        {
            fwrite(buf, 1, bufsize, pFile);
            fclose (pFile);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM