more consistent usage of data structures in the code

This commit is contained in:
giomba 2019-12-24 16:00:09 +01:00
parent b21625cd27
commit fabd0886b6
5 changed files with 26 additions and 48 deletions

View File

@ -5,30 +5,11 @@
void setup() { void setup() {
pinMode(13, OUTPUT); pinMode(13, OUTPUT);
Serial.begin(9600); Serial.begin(19200);
memcpy(&coppino::address, "\x20\x01\x04\x70\xc8\x44\x00\x31\x00\x00\x00\x00\x00\x00\xca\xfe", 16); memcpy(&coppino::address, "\x20\x01\x04\x70\xc8\x44\x00\x31\x00\x00\x00\x00\x00\x00\xca\xfe", 16);
} }
void loop() { void loop() {
/* network service routing */
/* some UDP stuff, just to test */
char udp_packet[64];
memcpy(udp_packet, "\x00\x00\x77\x77\x00\x12\x00\x00helloworld", 18);
ipv6::IPv6Packet ip_packet;
ip_packet.setPayload(udp_packet, 18);
ip_packet.setSrcAddress(coppino::address);
// ipv6::IPv6Addr dst_addr("\x20\x01\x04\x70\xc8\x44\x00\x30\x53\x47\xde\x2d\x41\xb1\x5b\x8c");
ipv6::IPv6Addr dst_addr("\x20\x01\x04\x70\xc8\x44\x00\x31\x00\x00\x00\x00\x00\x00\x00\x01");
ip_packet.setDstAddress(dst_addr);
ip_packet.setNextHeader(17); // UDP
// slip::send(&ip_packet, 18 + 40); // ??
coppino::handler(); coppino::handler();
//delay(5000);
} }

10
ipv6.h
View File

@ -35,8 +35,8 @@ namespace ipv6 {
public: public:
IPv6Packet(); IPv6Packet();
IPv6Packet(const char* buffer, int len); IPv6Packet(const char* buffer, int len);
void setSrcAddress(IPv6Addr& address); void setSrcAddress(const IPv6Addr* address);
void setDstAddress(IPv6Addr& address); void setDstAddress(const IPv6Addr* address);
void setNextHeader(uint8_t next_header); void setNextHeader(uint8_t next_header);
void setFlow(const char* flow); void setFlow(const char* flow);
void setPayload(char* payload, int len); void setPayload(char* payload, int len);
@ -45,12 +45,12 @@ namespace ipv6 {
}; };
/* Note: content of message may be changed */ /* Note: content of message may be changed */
void handleICMP(const char* src, const char* dst, const char* flow, char* message, int len); void handleICMP(const IPv6Addr* src, const IPv6Addr* dst, const char* flow, char* message, int len);
/* Compute internet checksum. Actually it is not so straightforward: */ /* Compute internet checksum. Actually it is not so straightforward: */
uint16_t compute_checksum( uint16_t compute_checksum(
const char* src_addr, /* source IPv6 address */ const IPv6Addr* src_addr, /* source IPv6 address */
const char* dst_addr, /* destination IPv6 address */ const IPv6Addr* dst_addr, /* destination IPv6 address */
uint8_t next_header, /* IPv6 next header number */ uint8_t next_header, /* IPv6 next header number */
int upper_len, /* length as found in the upper layer protocol */ int upper_len, /* length as found in the upper layer protocol */
const char* payload, /* IPv6 payload */ const char* payload, /* IPv6 payload */

View File

@ -6,7 +6,7 @@ namespace ipv6 {
const IPv6Addr ALL_NODES_ADDRESS("\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"); /* ff02::1 */ const IPv6Addr ALL_NODES_ADDRESS("\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"); /* ff02::1 */
/******** FUNCTIONS *******/ /******** FUNCTIONS *******/
uint16_t compute_checksum(const char* src_addr, const char* dst_addr, uint8_t next_header, int upper_len, const char* payload, int len) { uint16_t compute_checksum(const IPv6Addr* src_addr, const IPv6Addr* dst_addr, uint8_t next_header, int upper_len, const char* payload, int len) {
char buffer[LEN]; char buffer[LEN];
uint32_t total; uint32_t total;
uint16_t* ptr; uint16_t* ptr;
@ -67,11 +67,11 @@ void IPv6Packet::setNextHeader(uint8_t next_header) {
*(packet + 6) = next_header; *(packet + 6) = next_header;
} }
void IPv6Packet::setSrcAddress(IPv6Addr& address) { void IPv6Packet::setSrcAddress(const IPv6Addr* address) {
memcpy(packet + 8, (void*)&address, 16); memcpy(packet + 8, address, 16);
} }
void IPv6Packet::setDstAddress(IPv6Addr& address) { void IPv6Packet::setDstAddress(const IPv6Addr* address) {
memcpy(packet + 24, (void*)&address, 16); memcpy(packet + 24, address, 16);
} }
void IPv6Packet::setFlow(const char* flow) { void IPv6Packet::setFlow(const char* flow) {
@ -95,10 +95,10 @@ void IPv6Packet::doAction() {
switch(*(packet + 6)) { /* next header */ switch(*(packet + 6)) { /* next header */
case NH_ICMP: case NH_ICMP:
handleICMP(packet + 8, packet + 24, packet + 1, packet + 40, len); handleICMP((IPv6Addr*)(packet + 8), (IPv6Addr*)(packet + 24), packet + 1, packet + 40, len);
break; break;
case NH_UDP: case NH_UDP:
udp::handleUDP(packet + 8, packet + 24, packet + 1, packet + 40, len); udp::handleUDP((IPv6Addr*)(packet + 8), (IPv6Addr*)(packet + 24), packet + 1, packet + 40, len);
break; break;
default: default:
/* no other protocols are known here, sorry :-) */ /* no other protocols are known here, sorry :-) */
@ -107,7 +107,7 @@ void IPv6Packet::doAction() {
} }
} }
void handleICMP(const char* src_addr, const char* dst_addr, const char* flow, char* packet, int len) { void handleICMP(const IPv6Addr* src_addr, const IPv6Addr* dst_addr, const char* flow, char* packet, int len) {
uint8_t type = *(packet); uint8_t type = *(packet);
if (type == ICMP_ECHO_REQUEST_TYPE) { /* if it is ICMPv6 Echo Request, then... */ if (type == ICMP_ECHO_REQUEST_TYPE) { /* if it is ICMPv6 Echo Request, then... */
@ -132,11 +132,8 @@ void handleICMP(const char* src_addr, const char* dst_addr, const char* flow, ch
reply_packet.setPayload(packet, len); reply_packet.setPayload(packet, len);
reply_packet.setFlow(flow); reply_packet.setFlow(flow);
IPv6Addr tmp_addr; reply_packet.setSrcAddress(dst_addr);
memcpy(&tmp_addr, dst_addr, 16); reply_packet.setDstAddress(src_addr);
reply_packet.setSrcAddress(tmp_addr);
memcpy(&tmp_addr, src_addr, 16);
reply_packet.setDstAddress(tmp_addr);
reply_packet.setNextHeader(NH_ICMP); reply_packet.setNextHeader(NH_ICMP);
slip::send((void*)&reply_packet, 40 + len); slip::send((void*)&reply_packet, 40 + len);
} }

4
udp.h
View File

@ -17,10 +17,10 @@ namespace udp {
void setDstPort(uint16_t port); void setDstPort(uint16_t port);
uint16_t getDstPort(); uint16_t getDstPort();
void setPayload(const char* payload, int len); void setPayload(const char* payload, int len);
void computeChecksum(const char* src_addr, const char* dst_addr, int len); void computeChecksum(const ipv6::IPv6Addr* src_addr, const ipv6::IPv6Addr* dst_addr, int len);
}; };
void handleUDP(const char* src_addr, const char* dst_addr, const char* flow, char* packet, int len); void handleUDP(const ipv6::IPv6Addr* src_addr, const ipv6::IPv6Addr* dst_addr, const char* flow, char* packet, int len);
} }
#endif #endif

14
udp.ino
View File

@ -33,13 +33,13 @@ void UDPPacket::setPayload(const char* payload, int len) {
*(packet + 5) = (len + 8) & 0xff; *(packet + 5) = (len + 8) & 0xff;
} }
void UDPPacket::computeChecksum(const char* src_addr, const char* dst_addr, int len) { void UDPPacket::computeChecksum(const ipv6::IPv6Addr* src_addr, const ipv6::IPv6Addr* dst_addr, int len) {
memset(packet + 6, 0, 2); memset(packet + 6, 0, 2);
uint16_t checksum = ipv6::compute_checksum(src_addr, dst_addr, ipv6::NH_UDP, len, packet, len); uint16_t checksum = ipv6::compute_checksum(src_addr, dst_addr, ipv6::NH_UDP, len, packet, len);
memcpy(packet + 6, &checksum, 2); memcpy(packet + 6, &checksum, 2);
} }
void handleUDP(const char* src_addr, const char* dst_addr, const char* flow, char* packet, int len) { void handleUDP(const ipv6::IPv6Addr* src_addr, const ipv6::IPv6Addr* dst_addr, const char* flow, char* packet, int len) {
UDPPacket udp_packet(packet, len); UDPPacket udp_packet(packet, len);
/* swap ports */ /* swap ports */
@ -55,11 +55,11 @@ void handleUDP(const char* src_addr, const char* dst_addr, const char* flow, cha
reply_packet.setPayload((char*)&udp_packet, len); reply_packet.setPayload((char*)&udp_packet, len);
reply_packet.setFlow(flow); reply_packet.setFlow(flow);
ipv6::IPv6Addr tmp_addr; //ipv6::IPv6Addr tmp_addr;
memcpy(&tmp_addr, dst_addr, 16); //memcpy(&tmp_addr, dst_addr, 16);
reply_packet.setSrcAddress(tmp_addr); reply_packet.setSrcAddress(dst_addr);
memcpy(&tmp_addr, src_addr, 16); //memcpy(&tmp_addr, src_addr, 16);
reply_packet.setDstAddress(tmp_addr); reply_packet.setDstAddress(src_addr);
reply_packet.setNextHeader(ipv6::NH_UDP); reply_packet.setNextHeader(ipv6::NH_UDP);
slip::send((void*)&reply_packet, 40 + len); slip::send((void*)&reply_packet, 40 + len);
} }