69,371
社区成员
发帖
与我相关
我的任务
分享
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <sys/time.h>
#include <string.h>
#include <iostream>
using namespace std;
const int LISTEN_PORT = 7800;
const int PACK_LEN = 8192;
struct psdhdr
{
u_int32_t saddr;
u_int32_t daddr;
u_int8_t zero;
u_int8_t protocol;
u_int16_t len;
};
u_int16_t checksum(u_int16_t *buffer, int size);
int main(int argc, char *argv[])
{
char payload[] = "hello";
struct sockaddr_in sin, din;
memset(&sin, 0, sizeof(struct sockaddr_in));
memset(&din, 0, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
din.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr("19.9.0.95");
din.sin_addr.s_addr = inet_addr("18.8.6.66");
sin.sin_port = htons(LISTEN_PORT);
din.sin_port = htons(LISTEN_PORT);
const int listen_fd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
if (listen_fd < 0)
{
cout << "system error [ " << errno << ":" << strerror(errno) << "]"<< endl;
return 1;
}
if (bind(listen_fd, (const struct sockaddr *)&sin, sizeof(sin)) < 0)
{
close(listen_fd);
cout << "system error [ " << errno << ":" << strerror(errno) << "]"<< endl;
return 1;
}
int on = 1;
if (setsockopt(listen_fd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0)
{
cout << "system error [ " << errno << ":" << strerror(errno) << "]" << endl;
return 1;
}
char buf[PACK_LEN] = {0};
struct iphdr *iphdr = (struct iphdr *) buf;
struct psdhdr psd;
struct udphdr *pudphdr = (struct udphdr *) (buf+sizeof(struct iphdr));
memcpy(buf+sizeof(struct iphdr)+sizeof(struct udphdr), payload, strlen(payload));
const int data_len = sizeof(struct iphdr)+sizeof(struct udphdr)+strlen(payload);
iphdr->ihl = 5;
iphdr->version = 4;
iphdr->tos = 0;
iphdr->tot_len = data_len;
iphdr->id = htons(random());
iphdr->frag_off = 0;
iphdr->ttl = 0xff;
iphdr->protocol = IPPROTO_UDP;
iphdr->check = 0;
iphdr->saddr = sin.sin_addr.s_addr;
iphdr->daddr = din.sin_addr.s_addr;
iphdr->check = checksum((u_int16_t *)buf, sizeof(struct iphdr));
pudphdr->source = sin.sin_port;
pudphdr->dest = din.sin_port;
pudphdr->len = htons(sizeof(struct udphdr)+strlen(payload));
pudphdr->check = 0;
psd.saddr = iphdr->saddr;
psd.daddr = iphdr->daddr;
psd.zero = 0;
psd.protocol = IPPROTO_UDP;
psd.len = pudphdr->len;
char tmp[sizeof(struct psdhdr) + ntohs(pudphdr->len)];
struct psdhdr *ppsdhdr = (struct psdhdr *) tmp;
ppsdhdr->saddr = iphdr->saddr;
ppsdhdr->daddr = iphdr->daddr;
ppsdhdr->zero = 0;
ppsdhdr->protocol = IPPROTO_UDP;
ppsdhdr->len = pudphdr->len;
memcpy(tmp+sizeof(struct psdhdr), buf+sizeof(struct iphdr), ntohs(pudphdr->len));
pudphdr->check = checksum((u_int16_t *)tmp, sizeof(tmp));
for (int cnt = 1; cnt < 100; ++cnt)
{
if (sendto(listen_fd, buf, data_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
cout << "system error [ " << errno << ":" << strerror(errno) << "]" << endl;
cout << cnt << " failed" << endl;
}
else
{
cout << cnt << " success" << endl;
}
sleep(2);
}
cout << "Hello world!" << endl;
return 0;
}
u_int16_t checksum(u_int16_t *buffer, int size)
{
u_int64_t ret = 0;
while (size > 1)
{
ret += *buffer++;
size -= 2;
}
if (size == 1)
{
ret += *((u_int8_t *)buffer);
}
ret = (ret >> 16) + (ret & 0xffff);
ret += (ret >> 16);
return (u_int16_t)(~ret);
}