55#include "network.h"
66#include "rtl8139.h"
77
8- arp_table_entry_t arp_table [ 512 ];
9- int arp_table_size ;
10- int arp_table_curr ;
8+ #define ARP_TABLE_MAX 16
9+ arp_table_entry_t arp_table [ ARP_TABLE_MAX ] = { 0 } ;
10+ int arp_table_curr = 0 ;
1111
1212uint8_t broadcast_mac_address [] = {0xff , 0xff , 0xff , 0xff , 0xff , 0xff };
1313
@@ -20,12 +20,10 @@ void arp_handle_packet(arp_packet_t *arp_packet) {
2020 // save some packet field
2121 memcpy (dst_hardware_addr , arp_packet -> src_hardware_addr , 6 );
2222 memcpy (dst_protocol_addr , arp_packet -> src_protocol_addr , 4 );
23- // reply arp request, if the ip address matches(have to hard code the IP
24- // eveywhere, because I don't have dhcp yet)
23+
2524 if (ntohs (arp_packet -> opcode ) == ARP_REQUEST ) {
2625 if (!memcmp (arp_packet -> dst_protocol_addr , my_ip , 4 )) {
27- // set source MAC address, IP address (hardcode the IP address as 10.2.2.3
28- // until we really get one..)
26+
2927 get_mac_addr (arp_packet -> src_hardware_addr );
3028 memcpy (arp_packet -> src_protocol_addr , my_ip , 4 );
3129
@@ -50,24 +48,22 @@ void arp_handle_packet(arp_packet_t *arp_packet) {
5048 ethernet_send_packet (dst_hardware_addr , (uint8_t * )arp_packet ,
5149 sizeof (arp_packet_t ), ETHERNET_TYPE_ARP );
5250 serial_debug (
53- "sent an arp reply to %d.%d.%d.%d" , arp_packet -> dst_protocol_addr [0 ],
54- arp_packet -> dst_protocol_addr [1 ], arp_packet -> dst_protocol_addr [2 ],
55- arp_packet -> dst_protocol_addr [3 ]);
51+ "sent an arp reply to %d.%d.%d.%d %x:%x:%x:%x" ,
52+ arp_packet -> dst_protocol_addr [0 ], arp_packet -> dst_protocol_addr [1 ],
53+ arp_packet -> dst_protocol_addr [2 ], arp_packet -> dst_protocol_addr [3 ],
54+ arp_packet -> dst_hardware_addr [0 ], arp_packet -> dst_hardware_addr [1 ],
55+ arp_packet -> dst_hardware_addr [2 ], arp_packet -> dst_hardware_addr [3 ]);
5656 }
5757 } else if (ntohs (arp_packet -> opcode ) == ARP_REPLY ) {
58- return ; // reply
58+ // reply
5959 } else {
6060 return ; // not for us
6161 }
62-
63- // now, store the ip-mac address mapping relation
64- memcpy (& arp_table [arp_table_curr ].ip_addr , dst_protocol_addr , 4 );
65- memcpy (& arp_table [arp_table_curr ].mac_addr , dst_hardware_addr , 6 );
66- if (arp_table_size < 512 )
67- arp_table_size ++ ;
68- // wrap around
69- if (arp_table_curr >= 512 )
70- arp_table_curr = 0 ;
62+ uint8_t mac [6 ] = {0 };
63+ if (arp_lookup (mac , arp_packet -> src_protocol_addr ) == 0 ) {
64+ arp_lookup_add (arp_packet -> src_hardware_addr ,
65+ arp_packet -> src_protocol_addr );
66+ }
7167}
7268
7369void arp_send_packet (uint8_t * dst_hardware_addr , uint8_t * dst_protocol_addr ) {
@@ -76,6 +72,7 @@ void arp_send_packet(uint8_t *dst_hardware_addr, uint8_t *dst_protocol_addr) {
7672 // set source MAC address, IP address (hardcode the IP address as 10.2.2.3
7773 // until we really get one..)
7874 get_mac_addr (arp_packet .src_hardware_addr );
75+ // TODO
7976 arp_packet .src_protocol_addr [0 ] = 10 ;
8077 arp_packet .src_protocol_addr [1 ] = 0 ;
8178 arp_packet .src_protocol_addr [2 ] = 2 ;
@@ -103,20 +100,20 @@ void arp_send_packet(uint8_t *dst_hardware_addr, uint8_t *dst_protocol_addr) {
103100 sizeof (arp_packet_t ), ETHERNET_TYPE_ARP );
104101}
105102
106- void arp_lookup_add (uint8_t * ret_hardware_addr , uint8_t * ip_addr ) {
103+ void arp_lookup_add (uint8_t * hardware_addr , uint8_t * ip_addr ) {
107104 memcpy (& arp_table [arp_table_curr ].ip_addr , ip_addr , 4 );
108- memcpy (& arp_table [arp_table_curr ].mac_addr , ret_hardware_addr , 6 );
109- if (arp_table_size < 512 )
110- arp_table_size ++ ;
105+ memcpy (& arp_table [arp_table_curr ].mac_addr , hardware_addr , 6 );
106+ arp_table_curr ++ ;
111107 // wrap around
112- if (arp_table_curr >= 512 )
108+ if (arp_table_curr >= ARP_TABLE_MAX )
113109 arp_table_curr = 0 ;
114110}
115111
112+ // supply a destination mac and an ip to search
113+ // 0 not found
116114int arp_lookup (uint8_t * ret_hardware_addr , uint8_t * ip_addr ) {
117- uint32_t ip_entry = * ((uint32_t * )(ip_addr ));
118115 for (int i = 0 ; i < 512 ; i ++ ) {
119- if (arp_table [i ].ip_addr == ip_entry ) {
116+ if (! memcmp (( uint8_t * ) & arp_table [i ].ip_addr , ip_addr , 4 ) ) {
120117 memcpy (ret_hardware_addr , & arp_table [i ].mac_addr , 6 );
121118 return 1 ;
122119 }
@@ -125,10 +122,8 @@ int arp_lookup(uint8_t *ret_hardware_addr, uint8_t *ip_addr) {
125122}
126123
127124void arp_init () {
128- uint8_t broadcast_ip [4 ];
129- uint8_t broadcast_mac [6 ];
125+ uint8_t broadcast_ip [4 ] = { 0xff } ;
126+ uint8_t broadcast_mac [6 ] = { 0xff } ;
130127
131- memset (broadcast_ip , 0xff , 4 );
132- memset (broadcast_mac , 0xff , 6 );
133128 arp_lookup_add (broadcast_mac , broadcast_ip );
134- }
129+ }
0 commit comments