|
| 1 | + |
| 2 | +// ip-sockets-cpp-lite - RAW socket example (send only, requires administrator/root privileges) |
| 3 | +// |
| 4 | +// This example demonstrates how to send a hand-crafted UDP packet using a RAW socket. |
| 5 | +// The sender builds an IP header + UDP header + payload manually and sends it via sendto(). |
| 6 | +// The receiver is a normal UDP server socket that receives the packet as regular UDP. |
| 7 | +// |
| 8 | +// Usage: run as administrator (Windows) or root (Linux). |
| 9 | +// |
| 10 | +// [raw client] --( IP + UDP + payload )--> [regular udp server] |
| 11 | +// |
| 12 | + |
| 13 | +#include "udp_socket.h" |
| 14 | + |
| 15 | +#include <thread> |
| 16 | +#include <chrono> |
| 17 | +#include <cstdio> |
| 18 | +#include <cstring> |
| 19 | + |
| 20 | +using namespace ipsockets; |
| 21 | + |
| 22 | +static const addr4_t server_addr = "127.0.0.1:9000"; |
| 23 | +static const addr4_t sender_addr = "127.0.0.1:8000"; // spoofed source address (can be anything for raw) |
| 24 | + |
| 25 | +// ============================================================ |
| 26 | +// IP + UDP header structures (packed, network byte order) |
| 27 | +// ============================================================ |
| 28 | + |
| 29 | +#pragma pack(push, 1) |
| 30 | +struct ip_header_t { |
| 31 | + uint8_t ver_ihl; // version (4) + IHL (5) = 0x45 |
| 32 | + uint8_t tos; // type of service |
| 33 | + uint16_t total_len; // total length (IP header + UDP header + payload) |
| 34 | + uint16_t id; // identification |
| 35 | + uint16_t flags_frag; // flags + fragment offset |
| 36 | + uint8_t ttl; // time to live |
| 37 | + uint8_t protocol; // protocol (17 = UDP) |
| 38 | + uint16_t checksum; // header checksum (kernel fills this when IP_HDRINCL is set) |
| 39 | + ip4_t src_ip; // source IP address |
| 40 | + ip4_t dst_ip; // destination IP address |
| 41 | +}; |
| 42 | + |
| 43 | +struct udp_header_t { |
| 44 | + uint16_t src_port; // source port |
| 45 | + uint16_t dst_port; // destination port |
| 46 | + uint16_t length; // UDP header + payload length |
| 47 | + uint16_t checksum; // UDP checksum (0 = disabled) |
| 48 | +}; |
| 49 | +#pragma pack(pop) |
| 50 | + |
| 51 | +// ============================================================ |
| 52 | +// build_raw_packet - constructs IP + UDP + payload in buffer |
| 53 | +// ============================================================ |
| 54 | + |
| 55 | +int build_raw_packet (char* buf, int buf_size, const addr4_t& src, const addr4_t& dst, const char* payload, int payload_len) { |
| 56 | + |
| 57 | + int ip_hdr_len = sizeof (ip_header_t); |
| 58 | + int udp_hdr_len = sizeof (udp_header_t); |
| 59 | + int total_len = ip_hdr_len + udp_hdr_len + payload_len; |
| 60 | + |
| 61 | + if (total_len > buf_size) |
| 62 | + return -1; |
| 63 | + |
| 64 | + memset (buf, 0, total_len); |
| 65 | + |
| 66 | + // fill IP header |
| 67 | + ip_header_t* ip = (ip_header_t*)buf; |
| 68 | + ip->ver_ihl = 0x45; // IPv4, IHL = 5 (20 bytes, no options) |
| 69 | + ip->tos = 0; |
| 70 | + ip->total_len = orders::htonT<uint16_t> ((uint16_t)total_len); |
| 71 | + ip->id = orders::htonT<uint16_t> (0x1234); |
| 72 | + ip->ttl = 64; |
| 73 | + ip->protocol = IPPROTO_UDP; |
| 74 | + ip->checksum = 0; // kernel calculates this when IP_HDRINCL is set |
| 75 | + ip->src_ip = src.ip; |
| 76 | + ip->dst_ip = dst.ip; |
| 77 | + |
| 78 | + // fill UDP header |
| 79 | + udp_header_t* udp = (udp_header_t*)(buf + ip_hdr_len); |
| 80 | + udp->src_port = orders::htonT (src.port); |
| 81 | + udp->dst_port = orders::htonT (dst.port); |
| 82 | + udp->length = orders::htonT<uint16_t> ((uint16_t)(udp_hdr_len + payload_len)); |
| 83 | + udp->checksum = 0; // 0 = checksum disabled (valid for UDP) |
| 84 | + |
| 85 | + // copy payload |
| 86 | + memcpy (buf + ip_hdr_len + udp_hdr_len, payload, payload_len); |
| 87 | + |
| 88 | + return total_len; |
| 89 | +} |
| 90 | + |
| 91 | +// ============================================================ |
| 92 | +// receiver - regular UDP server (receives normal UDP payload) |
| 93 | +// ============================================================ |
| 94 | + |
| 95 | +bool shutdown_receiver = false; |
| 96 | + |
| 97 | +void receiver_func () { |
| 98 | + |
| 99 | + udp_socket_t<v4, socket_type_e::server> sock (log_e::debug); |
| 100 | + if (sock.open (server_addr) != no_error) { |
| 101 | + printf ("receiver: failed to open server socket\n"); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + char buf[1500]; |
| 106 | + addr4_t from; |
| 107 | + while (!shutdown_receiver) { |
| 108 | + int res = sock.recvfrom (buf, sizeof (buf), from); |
| 109 | + if (res > 0) { |
| 110 | + buf[res] = '\0'; |
| 111 | + printf ("receiver: got %d bytes from %s: \"%s\"\n", res, from.to_str().c_str(), buf); |
| 112 | + } |
| 113 | + else if (res == error_timeout) { |
| 114 | + // normal timeout, keep waiting |
| 115 | + } |
| 116 | + else |
| 117 | + printf ("receiver: error %d\n", res); |
| 118 | + } |
| 119 | + printf ("receiver: shutdown\n"); |
| 120 | +} |
| 121 | + |
| 122 | +// ============================================================ |
| 123 | +// sender - RAW socket client (sends hand-crafted IP+UDP packet) |
| 124 | +// ============================================================ |
| 125 | + |
| 126 | +void sender_func () { |
| 127 | + udp_socket_t<v4, socket_type_e::client> sock (log_e::debug, udp_type_e::raw); |
| 128 | + if (sock.open (server_addr) != no_error) { |
| 129 | + printf ("sender: failed to open raw socket (need admin/root privileges)\n"); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + std::this_thread::sleep_for (std::chrono::milliseconds (500)); // wait for receiver to start |
| 134 | + |
| 135 | + const char* messages[] = { "Hello from RAW socket!", "Second RAW packet", "Goodbye!" }; |
| 136 | + |
| 137 | + for (int i = 0; i < 3; i++) { |
| 138 | + char packet[1500]; |
| 139 | + int payload_len = (int)strlen (messages[i]) + 1; // include null terminator |
| 140 | + int packet_len = build_raw_packet (packet, sizeof (packet), sender_addr, server_addr, messages[i], payload_len); |
| 141 | + addr4_t dst = server_addr; |
| 142 | + |
| 143 | + if (packet_len > 0) { |
| 144 | + int res = sock.sendto (packet, packet_len, dst); |
| 145 | + if (res > 0) printf ("sender: sent %d bytes (payload: \"%s\")\n", res, messages[i]); |
| 146 | + else printf ("sender: sendto failed with error %d\n", res); |
| 147 | + } |
| 148 | + |
| 149 | + std::this_thread::sleep_for (std::chrono::seconds (1)); |
| 150 | + } |
| 151 | + |
| 152 | + printf ("sender: done\n"); |
| 153 | +} |
| 154 | + |
| 155 | +// ============================================================ |
| 156 | +// main |
| 157 | +// ============================================================ |
| 158 | + |
| 159 | +int main () { |
| 160 | + |
| 161 | + printf ("=== RAW socket example ===\n"); |
| 162 | + printf ("NOTE: requires administrator (Windows) or root (Linux) privileges.\n\n"); |
| 163 | + |
| 164 | + std::thread receiver (receiver_func); |
| 165 | + std::thread sender (sender_func); |
| 166 | + |
| 167 | + sender.join (); |
| 168 | + |
| 169 | + // give receiver time to process last packet |
| 170 | + std::this_thread::sleep_for (std::chrono::seconds (1)); |
| 171 | + shutdown_receiver = true; |
| 172 | + receiver.join (); |
| 173 | + |
| 174 | + printf ("\ndemo app shutdown\n"); |
| 175 | + |
| 176 | + return 0; |
| 177 | +} |
0 commit comments