coppino/ipv6.h

99 lines
2.6 KiB
C
Raw Normal View History

2019-12-23 13:13:13 +00:00
#ifndef COPPINO_IPV6_H
#define COPPINO_IPV6_H
namespace ipv6 {
2019-12-24 22:41:31 +00:00
/* maximum length for an IPv6 packet */
2019-12-23 13:13:13 +00:00
const uint8_t LEN = 128;
2019-12-24 22:41:31 +00:00
/* ICMP Types */
2019-12-23 17:57:21 +00:00
const uint8_t ICMP_ECHO_REQUEST_TYPE = 128;
const uint8_t ICMP_ECHO_REPLY_TYPE = 129;
const uint8_t ICMP_RADV_TYPE = 134;
2019-12-23 17:57:21 +00:00
2019-12-24 22:41:31 +00:00
/* Next-Header Types */
2019-12-23 17:57:21 +00:00
const uint8_t NH_ICMP = 58;
const uint8_t NH_UDP = 17;
2019-12-23 13:13:13 +00:00
class IPv6Addr {
private:
char address[16];
public:
IPv6Addr();
IPv6Addr(const char* address);
};
2019-12-23 17:57:21 +00:00
extern const IPv6Addr ALL_NODES_ADDRESS;
2019-12-23 13:13:13 +00:00
class IPv6Packet {
private:
char packet[LEN];
public:
IPv6Packet();
IPv6Packet(const char* buffer, int len);
void setLen(int len);
int getLen();
void setSrcAddress(const IPv6Addr* address);
const IPv6Addr* getSrcAddress();
void setDstAddress(const IPv6Addr* address);
const IPv6Addr* getDstAddress();
2019-12-23 13:13:13 +00:00
void setNextHeader(uint8_t next_header);
void setFlow(const char* flow);
2019-12-23 18:02:05 +00:00
void setPayload(char* payload, int len);
void send();
2019-12-23 13:13:13 +00:00
void doAction();
};
2019-12-24 22:41:31 +00:00
/* incoming and outgoing packets buffer */
extern IPv6Packet packetin;
extern IPv6Packet packetout;
2019-12-23 13:13:13 +00:00
/* Note: content of message may be changed */
int handleICMP(char* output_buffer, const char* input_buffer, int len);
2019-12-23 17:57:21 +00:00
/* Compute internet checksum. Actually it is not so straightforward: */
uint16_t compute_checksum(
const IPv6Addr* src_addr, /* source IPv6 address */
const IPv6Addr* dst_addr, /* destination IPv6 address */
uint8_t next_header, /* IPv6 next header number */
int upper_len, /* length as found in the upper layer protocol */
const char* payload, /* IPv6 payload */
int len /* IPv6 payload length */
);
2019-12-24 22:41:31 +00:00
/* RFC 4861 -- begin */
struct ICMPv6_RADV {
uint8_t type;
uint8_t code;
char checksum[2];
uint8_t cur_hop_limit;
uint8_t mo_flags;
uint16_t router_lifetime;
uint32_t reachable_time;
uint32_t retrans_time;
};
struct ICMPv6_RADV_Option {
uint8_t type;
uint8_t len;
};
struct ICMPv6_RADV_PrefixInformation {
uint8_t type;
uint8_t len;
uint8_t prefix_length;
uint8_t la_flags;
uint32_t valid_lifetime;
uint32_t preferred_lifetime;
uint32_t reserved2;
char prefix[16];
};
2019-12-24 22:41:31 +00:00
/* RFC 4861 -- end */
void setEUI(const char* eui); /* 64bit Extended Unique Identifier */
void SLAAC(bool enable); /* enable IPv6 automatic prefix configuration (listens for RADV) */
void setGlobalAddress(const char* address); /* global IPv6 address, manually configured */
2019-12-23 13:13:13 +00:00
}
#endif