Files
opencoreC/include/abi/net.h
T

83 lines
1.5 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;
#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);
uint32_t parse_ip(const char *str);
void format_ip(uint32_t ip, char *buf);
#ifdef __cplusplus
}
#endif