Comment reformat
Avoid C++-style comments, and remove things that just describe exactly
what the code does.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
diff --git a/inarp.c b/inarp.c
index 7a71247..46a9e9c 100644
--- a/inarp.c
+++ b/inarp.c
@@ -39,10 +39,10 @@
)
struct ARP_DATA {
- unsigned char src_mac[ETH_ALEN]; //source mac addr
- unsigned char src_ip[4]; //source ip addr
- unsigned char dest_mac[ETH_ALEN]; //dest mac addr
- unsigned char dest_ip[4]; //dest ip
+ unsigned char src_mac[ETH_ALEN];
+ unsigned char src_ip[4];
+ unsigned char dest_mac[ETH_ALEN];
+ unsigned char dest_ip[4];
};
struct ETH_ARP_PACKET {
struct ethhdr eh;
@@ -66,41 +66,39 @@
struct ethhdr *eh = ð_arp->eh;
struct arphdr *arp = ð_arp->arp;
struct ARP_DATA *arp_data = ð_arp->arp_data;
- /*target address */
struct sockaddr_ll socket_address;
- /*prepare sockaddr_ll */
- /*RAW communication */
+
+ /* Prepare our link-layer address: raw packet interface,
+ * using the ifindex interface, receiving ARP packets
+ */
socket_address.sll_family = PF_PACKET;
- /*we don't use a protocoll above ethernet layer
- ->just use anything here */
socket_address.sll_protocol = htons(ETH_P_ARP);
- /*index of the network device
- see full code later how to retrieve it */
socket_address.sll_ifindex = ifindex;
- /*ARP hardware identifier is ethernet */
socket_address.sll_hatype = ARPHRD_ETHER;
- /*target is another host */
socket_address.sll_pkttype = PACKET_OTHERHOST;
- /*address length */
socket_address.sll_halen = ETH_ALEN;
memcpy(socket_address.sll_addr, dest_mac, ETH_ALEN);
- /*set the frame header */
+
+ /* set the frame header */
memcpy((void *)eh->h_dest, (void *)dest_mac, ETH_ALEN);
memcpy((void *)eh->h_source, (void *)src_mac, ETH_ALEN);
eh->h_proto = htons(ETH_P_ARP);
- /*fill InARP request data for ethernet + ipv4 */
+
+ /* Fill InARP request data for ethernet + ipv4 */
arp->ar_hrd = htons(ARPHRD_ETHER);
arp->ar_pro = htons(ETH_P_ARP);
arp->ar_hln = ETH_ALEN;
arp->ar_pln = 4;
arp->ar_op = htons(ar_op);
- //fill arp ethernet mac & ipv4 info
+
+ /* fill arp ethernet mac & ipv4 info */
arp_data = (void *)(arp + 1);
- memcpy(arp_data->src_mac, (void *)src_mac, ETH_ALEN); //source mac addr
- memcpy(arp_data->src_ip, src_ip, 4); //source ip addr
- memcpy(arp_data->dest_mac, (void *)dest_mac, ETH_ALEN); //dest mac addr
- memcpy(arp_data->dest_ip, dest_ip, 4); //dest ip
- /*send the packet */
+ memcpy(arp_data->src_mac, (void *)src_mac, ETH_ALEN);
+ memcpy(arp_data->src_ip, src_ip, 4);
+ memcpy(arp_data->dest_mac, (void *)dest_mac, ETH_ALEN);
+ memcpy(arp_data->dest_ip, dest_ip, 4);
+
+ /* send the packet */
send_result = sendto(fd, eth_arp, ETH_ARP_FRAME_LEN, 0,
(struct sockaddr *)&socket_address,
sizeof(socket_address));
@@ -165,7 +163,7 @@
}
}
-//return node_id or -1 for error
+/* return node_id or -1 for error */
int get_node_id_by_mac_address(unsigned char *target_mac_addr)
{
char mac_addr[ETH_ALEN];
@@ -176,11 +174,6 @@
break;
}
if (i != MAX_SERVER_NODE) {
-// int j;
-// printf("mac_address for node[%d]:", i+1);
-// for (j=0; j<6; j++)
-// printf("%02x:", 0xff & mac_addr[j]);
-// printf("\n");
return i;
} else
return -1;
@@ -218,7 +211,7 @@
{
int fd, ret;
/*buffer for ethernet frame */
- static unsigned char buffer[ETH_FRAME_LEN]; /* single packets are usually not bigger than 8192 bytes */
+ static unsigned char buffer[ETH_FRAME_LEN];
int send_result = 0;
static struct ifreq ifreq_buffer;
const char *ifname;
@@ -235,18 +228,16 @@
return EXIT_FAILURE;
}
- /*our MAC address */
static unsigned char src_mac[6];
static unsigned char src_ip[4];
int ifindex;
-// int InARP_SERVER;
if (((fd = socket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)))) == -1) {
-// if (((fd = socket (AF_PACKET, SOCK_RAW, 0))) == -1) {
printf("socket: [%s]\n", strerror(errno));
exit(-1);
}
- //local mac address
+
+ /* Query local mac address */
memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer));
strcpy(ifreq_buffer.ifr_name, ifname);
ret = ioctl(fd, SIOCGIFHWADDR, &ifreq_buffer);
@@ -257,7 +248,8 @@
}
memcpy(src_mac, ifreq_buffer.ifr_hwaddr.sa_data, ETH_ALEN);
show_mac_addr(ifname, src_mac);
- //interface index
+
+ /* find the ifindex of the interface we're using */
memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer));
strcpy(ifreq_buffer.ifr_name, ifname);
ret = ioctl(fd, SIOCGIFINDEX, &ifreq_buffer);
@@ -268,30 +260,24 @@
}
ifindex = ifreq_buffer.ifr_ifindex;
- int length = 0; /*length of the received frame */
- static struct ETH_ARP_PACKET *inarp_req = (struct ETH_ARP_PACKET *)buffer; /* single packets are usually not bigger than 8192 bytes */
- static struct ETH_ARP_PACKET inarp_resp; /* single packets are usually not bigger than 8192 bytes */
- //argv[2]: file name used for storing node ip list
- // line format in file: <node_id> <mac_addr> <ip_addr>
- //get based address via EMS
- //set socket as async
-// fcntl(fd, F_SETFL,O_NONBLOCK|FASYNC);
- //got based address
+ /* length of the received frame */
+ int length = 0;
+ static struct ETH_ARP_PACKET *inarp_req =
+ (struct ETH_ARP_PACKET *)buffer;
+ static struct ETH_ARP_PACKET inarp_resp;
+
while (1) {
- //get local ip address
+ /* get local ip address */
memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer));
strcpy(ifreq_buffer.ifr_name, ifname);
ret = ioctl(fd, SIOCGIFADDR, &ifreq_buffer);
if (ret == -1) {
-// printf("ioctl3: [%s]\n", strerror(errno));
sleep(1);
continue;
}
if (AF_INET == ifreq_buffer.ifr_addr.sa_family) {
memcpy(src_ip, &ifreq_buffer.ifr_addr.sa_data[2], 4);
-// show_ip_addr("local", (unsigned char *)&ifreq_buffer.ifr_addr.sa_data[2]);
- //show_ip_addr("local", src_ip);
} else {
printf("unknown address family [%d]!\n",
ifreq_buffer.ifr_addr.sa_family);
@@ -304,12 +290,8 @@
if (length == -1) {
sleep(1);
}
-// printf("length = %d\r\nfd = %d\r\n", length, fd);
if (0 == memcmp(src_mac, inarp_req->eh.h_dest, ETH_ALEN)) {
- if (ntohs(inarp_req->arp.ar_op) == ARPOP_InREQUEST) { //get a inverse arp request
-// printf("[Rsp] InRequest\n");
- //dump_data("got InARP request packet", buffer, length);
- //send inarp response
+ if (ntohs(inarp_req->arp.ar_op) == ARPOP_InREQUEST) {
printf
("src mac =%02x:%02x:%02x:%02x:%02x:%02x\r\n",
@@ -348,38 +330,6 @@
continue;
}
}
-// } else if (ntohs(inarp_req->arp.ar_op) == ARPOP_InREPLY) { //get a InARP Response
-// int node_id;
-// printf("[Rsp] InReply\n");
-// node_id = get_node_id_by_mac_address(inarp_req->arp_data.src_mac);
-// if (node_id == -1) {
-// //invalid node
-// printf("%02x:%02x:%02x:%02x:%02x:%02x does not belong to this rack.",
-// inarp_req->arp_data.src_mac[0],
-// inarp_req->arp_data.src_mac[1],
-// inarp_req->arp_data.src_mac[2],
-// inarp_req->arp_data.src_mac[3],
-// inarp_req->arp_data.src_mac[4],
-// inarp_req->arp_data.src_mac[5]
-// );
-// } else {
- //valid node id
-// node[node_id].update_time.tv_sec = current_time.tv_sec;
- //update or remain node's ip
-// if ((0 != memcmp(node[node_id].dest_ip, inarp_req->arp_data.src_ip, 4))) {
- //ip address is changed.
-// static char ipv4_addr[16];
-// printf("[Rsp] update node[%d] ip @ %u\n", node_id+1, (unsigned)current_time.tv_sec);
-// show_mac_addr("[Rsp] ", inarp_req->arp_data.src_mac);
-// show_ip_addr("[Rsp] ", inarp_req->arp_data.src_ip);
-// memcpy(node[node_id].dest_ip, inarp_req->arp_data.src_ip, 4);
-// sprintf(ipv4_addr, "%d.%d.%d.%d",
-// inarp_req->arp_data.src_ip[0],
-// inarp_req->arp_data.src_ip[1],
-// inarp_req->arp_data.src_ip[2],
-// inarp_req->arp_data.src_ip[3]);
-// printf("[Rsp] update node[%d] ip @ %u\n", node_id+1, (unsigned)current_time.tv_sec);
-// }
memset(buffer, 0, sizeof(buffer));
}
}