I'm having problems with the following code. I receive my data with no problems, however that data is loaded with extraneous bits! For example, the following code grabs all traffic directed to it from nfqueue, and prints out each byte followed by a newline.
At first the data is 开发者_开发百科exactly what I'd expect, but then there are lines that have seeming 4 bytes on them!
int main(int argc, char** argv) {
int fd;
ssize_t rv;
char buf[4096];
struct nfq_handle* h;
struct nfq_q_handle* qh;
h = nfq_open();
if (!h) {
fprintf(stderr, "error during nfq_open()\n");
exit(1);
}
if (nfq_unbind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_unbind_pf()\n");
exit(1);
}
if (nfq_bind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_bind_pf()\n");
exit(1);
}
printf("Binding to queue 0...\n");
qh = nfq_create_queue(h, 0, &cb, NULL);
if (!qh) {
fprintf(stderr, "error during nfq_create_queue()\n");
exit(1);
}
printf("Copying packets...\n");
if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
fprintf(stderr, "error during nfq_set_mode()\n");
exit(1);
}
fd = nfq_fd(h);
memset(buf, 0, 4096);
while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
for (int i = 0; i < rv; i++) printf("%02x\n", *(buf+i));
printf("\n\n");
nfq_handle_packet(h, buf, rv);
}
nfq_destroy_queue(qh);
nfq_close(h);
}
Output:
...
50
10
39
08
48
ffffffa4
00
00
...
I couldn't find anything on the internet, or in bug trackers similar to this problem. Where can I begin to diagnose it? Why are there all these extra bits in my data? How can I fix it?
I did a comparison with the output from tcpdump, and they aren't appearing there either.
It seems that buffer contains char with value exceeding 128 and the signed/unsigned conversion error takes place.
I've calculated the value of your byte - it is 164. Try to convert your bytes into unsigned chars before passing them to printf:
printf("%02x\n", *(unsigned char*)(buf+i));
精彩评论