Я пытаюсь запрограммировать код, который может создавать и отправлять необработанные сокеты с возможностью заполнения всех полей в заголовке IP и заголовке TCP.
Я могу фактически установить все поля для заголовка tcp.Если я установлю Флаги IP
short int ip_moreFrag :1, ip_doNotFrag:1, ip_reserved : 1;
на 0
и последую за пакетом в Wireshark, я вижу, что все поля, которые я установил, тоже установлены. введите описание изображения здесь
Но если я установлю флаги Ip на 1
и последую за пакетом на Wireshark, я могу видеть, что Protokol моего пакета изменился на Ipv4 и флагине установлен в Wireshark. Wireshark IPv4 Protokol Почему?
Я не смог найти причину, по которой Программа не отправляет tcp-пакеты, если я установил один из флагов ip на 1. Не могли бы вы мне помочь?
Вот мой код:
// Run as root or SUID 0,
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
// Packet length
#define PCKT_LEN 8192
struct ipheader {
unsigned char ip_hl :4, ip_v :4;
unsigned char ip_tos :8;
unsigned short int ip_len :16;
unsigned short int ip_id :16;
unsigned short int ip_off :13;
unsigned short int ip_moreFrag :1, ip_doNotFrag:1, ip_reserved : 1;
unsigned char ip_ttl :8;
unsigned char ip_p :8;
unsigned short int ip_sum :16;
unsigned int iph_sourceip :32;
unsigned int iph_destip :32;
};
/* Structure of a TCP header */
struct tcpheader {
unsigned short int th_sport :16;
unsigned short int th_dport :16;
unsigned int th_seq :32;
unsigned int th_acknum :32;
unsigned char th_reserved :4, th_off :4;
unsigned short int th_fin :1, th_syn :1, th_rst :1, th_psh :1, th_ack :1,
th_urg :1, th_cwr :1, th_ece :1;
unsigned short int th_win :16;
unsigned short int th_sum :16;
unsigned short int th_urp :16;
};
unsigned short csum(unsigned short *buf, int len) {
unsigned long sum;
for (sum = 0; len > 0; len--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return (unsigned short) (~sum);
}
int main(int argc, char *argv[]) {
int sd;
// No data, just datagram
char buffer[PCKT_LEN];
// The size of the headers
struct ipheader *ip = (struct ipheader *) buffer;
struct tcpheader *tcp = (struct tcpheader *) (buffer
+ sizeof(struct ipheader));
struct sockaddr_in sin, din;
int one = 1;
const int *val = &one;
memset(buffer, 0, PCKT_LEN);
sd = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
if (sd < 0) {
perror("socket() error");
exit(-1);
} else
printf("socket()-SOCK_RAW and tcp protocol is OK.\n");
// The source is redundant, may be used later if needed
// Address family
sin.sin_family = AF_INET;
din.sin_family = AF_INET;
// Source port, can be any, modify as needed
sin.sin_port = htons(atoi("1000"));
din.sin_port = htons(atoi("1000"));
// Source IP, can be any, modify as needed
sin.sin_addr.s_addr = inet_addr("1.2.3.4");
din.sin_addr.s_addr = inet_addr("127.0.0.1");
// IP structure
ip->ip_v = 4;
ip->ip_hl = 5;
ip->ip_tos = 16;
ip->ip_off = 0;
ip->ip_reserved = 0;
ip->ip_doNotFrag = 0;
ip->ip_moreFrag = 0;
ip->ip_len = htons(sizeof(struct ipheader) + sizeof(struct tcpheader));
ip->ip_id = htons(54321);
ip->ip_ttl = 255;
ip->ip_p = 6; // TCP
ip->ip_sum = 0; // Done by kernel
ip->iph_sourceip = inet_addr("1.2.3.4");
ip->iph_destip = inet_addr("127.0.0.1");
tcp->th_sport = htons(atoi("1000"));
tcp->th_dport = htons(atoi("1000"));
tcp->th_seq = htonl(0);
tcp->th_acknum = 0;
// tcp->th_hlen = 4;
tcp->th_reserved = 0;
tcp->th_off = 5;
tcp->th_fin = 1;
tcp->th_syn = 1;
tcp->th_rst = 1;
tcp->th_psh = 1;
tcp->th_ack = 1;
tcp->th_urg = 1;
tcp->th_cwr = 1;
tcp->th_ece = 1;
tcp->th_win = htons(32767);
tcp->th_sum = 0; // Done by kernel
tcp->th_urp = 0;
// IP checksum calculation
ip->ip_sum = htons(
csum((unsigned short *) buffer,
(sizeof(struct ipheader) + sizeof(struct tcpheader))));
// Inform the kernel do not fill up the headers' structure, we fabricated our own
if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0) {
perror("setsockopt() error");
exit(-1);
} else
printf("setsockopt() is OK\n");
unsigned int count;
if (sendto(sd, buffer, ip->ip_len, 0, (struct sockaddr *) &din, sizeof(din))
< 0)
// Verify
{
perror("sendto() error");
exit(-1);
} else
printf("Count #%u - sendto() is OK\n", count);
close(sd);
return 0;
}