103 lines
2.0 KiB
C
103 lines
2.0 KiB
C
#pragma once
|
|
#include "stdint.h"
|
|
#include "stddef.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#define ETHERTYPE_IPv4 0x0800
|
|
#define ETHERTYPE_ARP 0x0806
|
|
|
|
#define IP_PROTO_ICMP 1
|
|
#define IP_PROTO_TCP 6
|
|
#define IP_PROTO_UDP 17
|
|
|
|
#define ICMP_ECHO_REPLY 0
|
|
#define ICMP_ECHO_REQUEST 8
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
typedef struct {
|
|
uint8_t dest_mac[6];
|
|
uint8_t src_mac[6];
|
|
uint16_t ethertype;
|
|
} eth_hdr_t;
|
|
|
|
typedef struct {
|
|
uint16_t hw_type;
|
|
uint16_t proto_type;
|
|
uint8_t hw_len;
|
|
uint8_t proto_len;
|
|
uint16_t opcode;
|
|
uint8_t sender_mac[6];
|
|
uint32_t sender_ip;
|
|
uint8_t target_mac[6];
|
|
uint32_t target_ip;
|
|
} arp_hdr_t;
|
|
|
|
typedef struct {
|
|
uint8_t version_ihl;
|
|
uint8_t tos;
|
|
uint16_t total_length;
|
|
uint16_t id;
|
|
uint16_t flags_fragment;
|
|
uint8_t ttl;
|
|
uint8_t protocol;
|
|
uint16_t checksum;
|
|
uint32_t src_ip;
|
|
uint32_t dst_ip;
|
|
} ipv4_hdr_t;
|
|
|
|
typedef struct {
|
|
uint8_t type;
|
|
uint8_t code;
|
|
uint16_t checksum;
|
|
uint16_t id;
|
|
uint16_t sequence;
|
|
} icmp_hdr_t;
|
|
|
|
typedef struct {
|
|
uint16_t src_port;
|
|
uint16_t dst_port;
|
|
uint32_t seq_num;
|
|
uint32_t ack_num;
|
|
uint8_t data_offset_reserved;
|
|
uint8_t flags;
|
|
uint16_t window_size;
|
|
uint16_t checksum;
|
|
uint16_t urgent_ptr;
|
|
} tcp_hdr_t;
|
|
|
|
#define TCP_FLAG_FIN 0x01
|
|
#define TCP_FLAG_SYN 0x02
|
|
#define TCP_FLAG_RST 0x04
|
|
#define TCP_FLAG_PSH 0x08
|
|
#define TCP_FLAG_ACK 0x10
|
|
#define TCP_FLAG_URG 0x20
|
|
|
|
#pragma pack(pop)
|
|
|
|
typedef struct {
|
|
uint32_t ip;
|
|
uint32_t netmask;
|
|
uint32_t gateway;
|
|
uint8_t mac[6];
|
|
} net_config_t;
|
|
|
|
extern net_config_t g_net_cfg;
|
|
|
|
// Net API
|
|
void net_init(void);
|
|
uint16_t net_checksum(const void *buf, size_t len);
|
|
int net_send_arp_request(uint32_t target_ip);
|
|
int net_send_icmp_ping(uint32_t target_ip, uint16_t seq);
|
|
int net_poll_icmp_reply(uint32_t *from_ip, uint16_t *seq);
|
|
int net_http_get(uint32_t target_ip, uint16_t port, const char *path, char *out_body, size_t max_body_len);
|
|
uint32_t parse_ip(const char *str);
|
|
void format_ip(uint32_t ip, char *buf);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|