@@ -24,16 +24,18 @@ void get_ip_str(char *ip_str, uint8_t *ip) {
2424 }
2525}
2626
27- uint16_t ip_calculate_checksum (ip_packet_t * packet ) {
27+ uint16_t _ip_calculate_checksum (ip_packet_t * packet , int size ) {
2828 // treat the packet header as a 2-byte-integer array
2929 // sum all integers up and flip all bits
30- int array_size = sizeof (ip_packet_t ) / 2 ;
30+ if (size == 0 ) {
31+ size = sizeof (ip_packet_t ) / 2 ;
32+ }
3133 uint32_t sum = 0 ;
3234 uint16_t value ;
3335
3436 uint8_t * byte_ptr = (uint8_t * )packet ;
3537
36- for (int i = 0 ; i < array_size ; i ++ ) {
38+ for (int i = 0 ; i < size ; i ++ ) {
3739 memcpy (& value , & byte_ptr [i * 2 ], sizeof (uint16_t ));
3840 sum += flip_short (value );
3941 }
@@ -45,7 +47,16 @@ uint16_t ip_calculate_checksum(ip_packet_t *packet) {
4547 return ret ;
4648}
4749
50+ uint16_t ip_calculate_checksum (ip_packet_t * packet ) {
51+ return _ip_calculate_checksum (packet , 0 );
52+ }
53+
4854void ip_send_packet (uint8_t * dst_ip , void * data , uint32_t len ) {
55+ _ip_send_packet (dst_ip , data , len , PROTOCOL_UDP ); // Assuming UDP for DHCP
56+ }
57+
58+ void _ip_send_packet (uint8_t * dst_ip , void * data , uint32_t len ,
59+ uint8_t protocol ) {
4960 serial_debug ("sending ip packet to %d.%d.%d.%d with data length %d" ,
5061 dst_ip [0 ], dst_ip [1 ], dst_ip [2 ], dst_ip [3 ], len );
5162
@@ -69,16 +80,15 @@ void ip_send_packet(uint8_t *dst_ip, void *data, uint32_t len) {
6980
7081 // TTL and protocol
7182 packet -> ttl = 64 ;
72- packet -> protocol = PROTOCOL_UDP ; // Assuming UDP for DHCP
83+ packet -> protocol = protocol ;
7384
7485 uint8_t my_ip_address [4 ] = {0 , 0 , 0 , 0 };
7586
7687 memcpy (packet -> src_ip , my_ip_address , 4 );
7788 memcpy (packet -> dst_ip , dst_ip , 4 );
7889
7990 packet -> header_checksum = 0 ;
80- packet -> header_checksum =
81- ip_calculate_checksum (packet );
91+ packet -> header_checksum = ip_calculate_checksum (packet );
8292 serial_debug ("ip checksum = %x" , ntohs (packet -> header_checksum ));
8393
8494 memcpy (packet -> data , data , len );
0 commit comments