Моя цель заключается в успешной отправке пакета IPv4 с использованием реализации сокета SOCK_RAW/IPPROTO_RAW на C++ и проверке того, что пакет был отправлен (и имеет ожидаемую структуру) с помощью WireShark.
Вот моя программа уровня 3:
Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEBUG true;
#define FIXED_IPV4_HEADER_BYTE_LENGTH 20
template
void print(type message) {
std::cout 8) & 0xff);
}
void set_identification(uint16_t identification) {
this->identification = identification;
uint16_t identification_n = htons(identification);
octets[4] = octets[4] | ((identification_n >> 0) & 0xff);
octets[5] = octets[5] | ((identification_n >> 8) & 0xff);
}
void set_flags(uint8_t flags) {
this->flags = flags;
octets[6] = octets[6] | (flags fragment_offset = fragment_offset;
uint16_t fragment_offset_n = htons(fragment_offset);
octets[6] = octets[6] | ((fragment_offset_n >> 0) & 0xff);
octets[7] = octets[7] | ((fragment_offset_n >> 8) & 0xff);
}
void set_time_to_live(uint8_t ttl) {
this->time_to_live = ttl;
octets[8] = ttl;
}
void set_protocol(uint8_t protocol) {
this->protocol = protocol;
octets[9] = protocol;
}
void set_header_checksum(uint16_t checksum) {
this->header_checksum = checksum;
uint16_t checksum_n = htons(checksum);
octets[10] = octets[10] | ((checksum_n >> 0) & 0xff);
octets[11] = octets[11] | ((checksum_n >> 8) & 0xff);
}
void set_source_address(uint32_t address) {
this->source_address = address;
uint32_t address_n = htonl(address);
octets[12] = octets[12] | ((address_n >> 0) & 0xff);
octets[13] = octets[13] | ((address_n >> 8) & 0xff);
octets[14] = octets[14] | ((address_n >> 16) & 0xff);
octets[15] = octets[15] | ((address_n >> 24) & 0xff);
}
void set_destination_address(uint32_t address) {
this->destination_address = address;
uint32_t address_n = htonl(address);
octets[16] = octets[16] | ((address_n >> 0) & 0xff);
octets[17] = octets[17] | ((address_n >> 8) & 0xff);
octets[18] = octets[18] | ((address_n >> 16) & 0xff);
octets[19] = octets[19] | ((address_n >> 24) & 0xff);
}
// num_options_specified is the number of 32 bit data structures specified in the options,
// even if there are more than one options specified per 4-octet (32 bits).
void set_options(uint32_t *options, size_t num_options_specified) {
this->options = new uint32_t[num_options_specified];
memcpy(this->options, options, num_options_specified * 4);
this->num_options_specified = num_options_specified;
}
void set_contents(uint8_t *contents, size_t num_content_bytes) {
this->contents = new uint8_t[num_content_bytes];
memcpy(this->contents, contents, num_content_bytes);
this->num_content_bytes = num_content_bytes;
}
void calculate_and_set_derived_values() {
set_ihl(FIXED_IPV4_HEADER_BYTE_LENGTH / 4 + num_options_specified);
set_length(FIXED_IPV4_HEADER_BYTE_LENGTH + num_options_specified * 4 + num_content_bytes);
int checksum = 0;
for (int i = 0; i < 20; i += 2) {
checksum += (((int) octets[i])
Подробнее здесь: [url]https://stackoverflow.com/questions/79238935/unable-to-send-packet-using-ipproto-raw-socket-in-c-macos-14-7-sonoma[/url]