Problem: I created a raw socket with the UDP protocol, sent a packet consisting of a UDP header and data, everything is fine in the PP, the packet is correct, I also created the same socket in another program, and it accepted the data that I sent, but along with the IP header. How so? Why isn’t this written anywhere?
That is, we send DATA+UDP, and receive from the same socket DATA+UDP+IP.
The second program that receives data only works if you add 20 to the pointer, that is, the size of the IP header.
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <linux/udp.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/ether.h>
#include <sys/types.h>
#include <sys/socket.h>
#define __USE_XOPEN2K
#include <netdb.h>
#define AF_INETv4 0x02
#define AF_INETv6 0x0A
int main(int argc, char **argv)
{
int m_socket = socket(AF_INETv4, SOCK_RAW, IPPROTO_UDP);
struct sockaddr_in bnd;
bnd.sin_family = AF_INET;
bnd.sin_port = htons(55555);
bnd.sin_addr.s_addr = inet_addr("127.0.0.1");
bind(m_socket, &bnd, sizeof(bnd));
perror("bind: ");
struct iovec iov;
char buffer[1500 - 14 - 20] = {0};
iov.iov_base = &buffer;
iov.iov_len = sizeof(buffer);
struct udphdr *udp = iov.iov_base;
udp->source = htons(55555);
udp->dest = htons(22222);
udp->len = htons(8);
udp->check = 0;
struct msghdr msg = {0};
msg.msg_flags = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
struct sockaddr_in in;
in.sin_family = AF_INETv4;
in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
in.sin_port = htons(22222);
msg.msg_name = ∈
msg.msg_namelen = sizeof(struct sockaddr_in);
for(int i = 0; i < 10; i++)
{
sendmsg(m_socket, &msg, 0);
perror("");
}
return 0;
}
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <linux/udp.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/ether.h>
#include <sys/types.h>
#include <sys/socket.h>
#define __USE_XOPEN2K
#include <netdb.h>
#define AF_INETv4 0x02
#define AF_INETv6 0x0A
int main(int argc, char **argv)
{
int m_socket = socket(AF_INETv4, SOCK_RAW, IPPROTO_UDP);
struct sockaddr_in bnd;
bnd.sin_family = AF_INET;
bnd.sin_port = htons(22222);
bnd.sin_addr.s_addr = inet_addr("127.0.0.1");
bind(m_socket, &bnd, sizeof(bnd));
perror("bind: ");
struct iovec iov;
char buffer[1500 - 14 - 20] = {0};
iov.iov_base = &buffer;
iov.iov_len = sizeof(buffer);
struct msghdr msg = {0};
msg.msg_flags = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
struct sockaddr_in in;
in.sin_family = AF_INETv4;
in.sin_addr.s_addr = 0;
in.sin_port = 0;
msg.msg_name = ∈
msg.msg_namelen = sizeof(struct sockaddr_in);
for(int i = 0; i < 10; i++)
{
recvmsg(m_socket, &msg, MSG_ZEROCOPY);
perror("");
struct udphdr *udp = (uint8_t*)iov.iov_base + 20;
uint16_t src = htons(udp->source );
uint16_t dst = htons(udp->dest);
uint16_t len = htons(udp->len);
uint16_t chk = htons(udp->check);
fprintf(stderr, "src: %hu dst: %hu len: %hu chk: %hun", src, dst, len, chk);
}
return 0;
}