-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnet.h
More file actions
65 lines (53 loc) · 2.89 KB
/
net.h
File metadata and controls
65 lines (53 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef NET_H
#define NET_H
#include "config.h"
/* ── Network interface helpers ───────────────────────────────── */
int get_default_iface (char *iface_out); /* buf >= IF_NAMESIZE */
int get_ifdetails (const char *iface,
int *ifindex, uint8_t *mac);
int get_gateway_mac (uint8_t *mac_out);
uint32_t get_local_ip (const char *iface); /* host byte order */
/* ── Packet construction ─────────────────────────────────────── */
uint16_t checksum_ip (struct iphdr *iph);
uint16_t checksum_tcp(struct tcphdr *tcph,
uint32_t src_ip, uint32_t dst_ip);
uint16_t checksum_udp(struct udphdr *udph,
uint32_t src_ip, uint32_t dst_ip,
size_t payload_len);
void create_syn_packet(packet_t *out,
uint32_t src_ip, uint32_t dst_ip,
uint16_t src_port, uint16_t dst_port,
const uint8_t *src_mac,
const uint8_t *dst_mac);
void create_udp_packet(packet_t *out,
uint32_t src_ip, uint32_t dst_ip,
uint16_t src_port, uint16_t dst_port,
const uint8_t *src_mac,
const uint8_t *dst_mac,
const uint8_t *payload, size_t plen);
/* ── Raw HTTP client ─────────────────────────────────────────── */
/*
* Connects to ip:port, sends an HTTP/1.0 request, reads response.
* resp_buf is filled with raw response (headers + body).
* Returns HTTP status code, 0 on connection error.
*/
int raw_http(const char *ip, uint16_t port,
const char *method, const char *path,
const char *extra_headers, /* "Key: val\r\n..." or NULL */
const char *body, /* POST body or NULL */
char *resp_buf, size_t resp_sz,
int timeout_sec);
/* Convenience: extract value of a header from raw response */
int http_get_header(const char *resp, const char *hdr_name,
char *out, size_t out_sz);
/* Returns pointer to body start in resp, or NULL */
const char *http_body(const char *resp);
/* ── String helpers ──────────────────────────────────────────── */
void url_encode (const char *src, char *dst, size_t dst_sz);
void base64_encode(const uint8_t *src, size_t slen,
char *dst, size_t dst_sz);
/* connect() with timeout — returns fd or -1 */
int tcp_connect(const char *ip, uint16_t port, int timeout_sec);
/* xorshift32 PRNG */
uint32_t xorshift32(uint32_t *state);
#endif /* NET_H */