Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * Copyright 2016 Foxconn |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | ******************************************************************************/ |
| 16 | #define MAX_SERVER_NODE 24 |
| 17 | #define SKU1_MAX_SERVER_NODES 24 |
| 18 | |
| 19 | #define ETH_ARP_FRAME_LEN ( sizeof(struct ethhdr)+\ |
| 20 | sizeof(struct arphdr)+\ |
| 21 | ((ETH_ALEN+4)*2)\ |
| 22 | ) |
| 23 | |
| 24 | struct ARP_DATA { |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 25 | unsigned char src_mac[ETH_ALEN]; //source mac addr |
| 26 | unsigned char src_ip[4]; //source ip addr |
| 27 | unsigned char dest_mac[ETH_ALEN]; //dest mac addr |
| 28 | unsigned char dest_ip[4]; //dest ip |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 29 | }; |
| 30 | struct ETH_ARP_PACKET { |
| 31 | struct ethhdr eh; |
| 32 | struct arphdr arp; |
| 33 | struct ARP_DATA arp_data; |
| 34 | }; |
| 35 | |
| 36 | struct ETH_ARP_PACKET *inarp_req; |
| 37 | |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 38 | static char based_mac[6] = { 0, 0, 0, 0, 0, 0 }; |
| 39 | |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 40 | int send_arp_packet(int fd, |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 41 | int ifindex, |
| 42 | struct ETH_ARP_PACKET *eth_arp, |
| 43 | __be16 ar_op, |
| 44 | unsigned char *src_mac, |
| 45 | unsigned char *src_ip, |
| 46 | unsigned char *dest_mac, unsigned char *dest_ip) |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 47 | { |
| 48 | int send_result = 0; |
| 49 | struct ethhdr *eh = ð_arp->eh; |
| 50 | struct arphdr *arp = ð_arp->arp; |
| 51 | struct ARP_DATA *arp_data = ð_arp->arp_data; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 52 | /*target address */ |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 53 | struct sockaddr_ll socket_address; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 54 | /*prepare sockaddr_ll */ |
| 55 | /*RAW communication */ |
| 56 | socket_address.sll_family = PF_PACKET; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 57 | /*we don't use a protocoll above ethernet layer |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 58 | ->just use anything here */ |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 59 | socket_address.sll_protocol = htons(ETH_P_ARP); |
| 60 | /*index of the network device |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 61 | see full code later how to retrieve it */ |
| 62 | socket_address.sll_ifindex = ifindex; |
| 63 | /*ARP hardware identifier is ethernet */ |
| 64 | socket_address.sll_hatype = ARPHRD_ETHER; |
| 65 | /*target is another host */ |
| 66 | socket_address.sll_pkttype = PACKET_OTHERHOST; |
| 67 | /*address length */ |
| 68 | socket_address.sll_halen = ETH_ALEN; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 69 | memcpy(socket_address.sll_addr, dest_mac, ETH_ALEN); |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 70 | /*set the frame header */ |
| 71 | memcpy((void *)eh->h_dest, (void *)dest_mac, ETH_ALEN); |
| 72 | memcpy((void *)eh->h_source, (void *)src_mac, ETH_ALEN); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 73 | eh->h_proto = htons(ETH_P_ARP); |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 74 | /*fill InARP request data for ethernet + ipv4 */ |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 75 | arp->ar_hrd = htons(ARPHRD_ETHER); |
| 76 | arp->ar_pro = htons(ETH_P_ARP); |
| 77 | arp->ar_hln = ETH_ALEN; |
| 78 | arp->ar_pln = 4; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 79 | arp->ar_op = htons(ar_op); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 80 | //fill arp ethernet mac & ipv4 info |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 81 | arp_data = (void *)(arp + 1); |
| 82 | memcpy(arp_data->src_mac, (void *)src_mac, ETH_ALEN); //source mac addr |
| 83 | memcpy(arp_data->src_ip, src_ip, 4); //source ip addr |
| 84 | memcpy(arp_data->dest_mac, (void *)dest_mac, ETH_ALEN); //dest mac addr |
| 85 | memcpy(arp_data->dest_ip, dest_ip, 4); //dest ip |
| 86 | /*send the packet */ |
| 87 | send_result = sendto(fd, eth_arp, ETH_ARP_FRAME_LEN, 0, |
| 88 | (struct sockaddr *)&socket_address, |
| 89 | sizeof(socket_address)); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 90 | if (send_result == -1) { |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 91 | printf("sendto: [%s]\n", strerror(errno)); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 92 | } |
| 93 | return send_result; |
| 94 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 95 | |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 96 | void show_mac_addr(char *name, unsigned char *mac_addr) |
| 97 | { |
| 98 | int i; |
| 99 | printf("%s MAC address: ", name); |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 100 | for (i = 0; i < 6; i++) { |
| 101 | printf("%.2X%c", (unsigned char)mac_addr[i], |
| 102 | (i == 5) ? '\n' : ':'); |
| 103 | } |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 104 | return; |
| 105 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 106 | |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 107 | void show_ip_addr(char *desc, unsigned char *ip_addr) |
| 108 | { |
| 109 | int i; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 110 | printf("%s IPv4 address: ", desc); |
| 111 | for (i = 0; i < 4; i++) { |
| 112 | printf("%d%c", (unsigned char)ip_addr[i], |
| 113 | (i == 3) ? '\n' : '.'); |
| 114 | } |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 115 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 116 | |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 117 | void dump_data(char *desc, unsigned char *buffer, int length) |
| 118 | { |
| 119 | int i; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 120 | printf("%s & length is [%d]:\n", desc, length); |
| 121 | for (i = 0; i < length; i++) { |
| 122 | printf("[%02X]", 0xff & buffer[i]); |
| 123 | if ((i & 0xf) == 0x7) |
| 124 | printf(" - "); |
| 125 | if ((i & 0xf) == 0xf) |
| 126 | printf("\n"); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 127 | } |
| 128 | printf("\n"); |
| 129 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 130 | |
| 131 | void get_mac_address_for_node_id(int node_id, char *mac_addr) |
| 132 | { |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 133 | int i; |
| 134 | int add = node_id; |
| 135 | int carry = 0; |
| 136 | int mac; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 137 | for (i = 5; i >= 0; i--) { |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 138 | mac = (int)based_mac[i]; |
| 139 | mac += (add + carry); |
| 140 | add = 0; |
| 141 | if (mac > 255) { |
| 142 | carry = 1; |
| 143 | mac -= 256; |
| 144 | } else { |
| 145 | carry = 0; |
| 146 | } |
| 147 | mac_addr[i] = (char)mac; |
| 148 | } |
| 149 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 150 | |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 151 | //return node_id or -1 for error |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 152 | int get_node_id_by_mac_address(unsigned char *target_mac_addr) |
| 153 | { |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 154 | char mac_addr[ETH_ALEN]; |
| 155 | int i; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 156 | for (i = 0; i < MAX_SERVER_NODE; i++) { |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 157 | get_mac_address_for_node_id(i, mac_addr); |
| 158 | if (0 == memcmp(target_mac_addr, mac_addr, ETH_ALEN)) |
| 159 | break; |
| 160 | } |
| 161 | if (i != MAX_SERVER_NODE) { |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 162 | // int j; |
| 163 | // printf("mac_address for node[%d]:", i+1); |
| 164 | // for (j=0; j<6; j++) |
| 165 | // printf("%02x:", 0xff & mac_addr[j]); |
| 166 | // printf("\n"); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 167 | return i; |
| 168 | } else |
| 169 | return -1; |
| 170 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 171 | |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 172 | #if ENABLE_MONOTONIC_SYSCALL |
| 173 | #include <sys/syscall.h> |
| 174 | /* Old glibc (< 2.3.4) does not provide this constant. We use syscall |
| 175 | * directly so this definition is safe. */ |
| 176 | #ifndef CLOCK_MONOTONIC |
| 177 | #define CLOCK_MONOTONIC 1 |
| 178 | #endif |
| 179 | |
| 180 | unsigned FAST_FUNC monotonic_sec(void) |
| 181 | { |
| 182 | struct timespec ts; |
| 183 | get_mono(&ts); |
| 184 | return ts.tv_sec; |
| 185 | } |
| 186 | |
| 187 | #else |
| 188 | |
| 189 | unsigned monotonic_sec(void) |
| 190 | { |
| 191 | return time(NULL); |
| 192 | } |
| 193 | #endif |
| 194 | //query ip addr for server node w/ mac addr |
| 195 | //inarp "ethX" "peer mac addr(xx:xx:xx:xx:xx:xx)" |
| 196 | //inarp daemon to handle inarp packets |
| 197 | //inarp "ethX" "file for storing node list" |
| 198 | void InvARPServerTask( |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 199 | /** the internal task ID of this task (in) */ |
| 200 | ULONG thread_input) |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 201 | { |
| 202 | int fd, ret; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 203 | /*buffer for ethernet frame */ |
| 204 | static unsigned char buffer[ETH_FRAME_LEN]; /* single packets are usually not bigger than 8192 bytes */ |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 205 | int send_result = 0; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 206 | static struct ifreq ifreq_buffer; |
| 207 | |
| 208 | /*our MAC address */ |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 209 | static unsigned char src_mac[6]; |
| 210 | static unsigned char src_ip[4]; |
| 211 | int ifindex; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 212 | // int InARP_SERVER; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 213 | |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 214 | if (((fd = socket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)))) == -1) { |
| 215 | // if (((fd = socket (AF_PACKET, SOCK_RAW, 0))) == -1) { |
| 216 | printf("socket: [%s]\n", strerror(errno)); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 217 | exit(-1); |
| 218 | } |
| 219 | //local mac address |
| 220 | memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer)); |
| 221 | strcpy(ifreq_buffer.ifr_name, "ncsi0"); |
| 222 | ret = ioctl(fd, SIOCGIFHWADDR, &ifreq_buffer); |
| 223 | if (ret == -1) { |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 224 | printf("ioctl2: [%s]\n", strerror(errno)); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 225 | close(fd); |
| 226 | exit(-1); |
| 227 | } |
| 228 | memcpy(src_mac, ifreq_buffer.ifr_hwaddr.sa_data, ETH_ALEN); |
| 229 | show_mac_addr("ncsi0", src_mac); |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 230 | //interface index |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 231 | memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer)); |
| 232 | strcpy(ifreq_buffer.ifr_name, "ncsi0"); |
| 233 | ret = ioctl(fd, SIOCGIFINDEX, &ifreq_buffer); |
| 234 | if (ret == -1) { |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 235 | printf("ioctl4: [%s]\n", strerror(errno)); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 236 | close(fd); |
| 237 | exit(-1); |
| 238 | } |
| 239 | ifindex = ifreq_buffer.ifr_ifindex; |
| 240 | |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 241 | int length = 0; /*length of the received frame */ |
| 242 | static struct ETH_ARP_PACKET *inarp_req = (struct ETH_ARP_PACKET *)buffer; /* single packets are usually not bigger than 8192 bytes */ |
| 243 | static struct ETH_ARP_PACKET inarp_resp; /* single packets are usually not bigger than 8192 bytes */ |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 244 | static struct { |
| 245 | unsigned char dest_ip[4]; |
| 246 | struct timespec update_time; |
| 247 | } node[SKU1_MAX_SERVER_NODES]; |
| 248 | struct timespec current_time; |
| 249 | //argv[2]: file name used for storing node ip list |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 250 | // line format in file: <node_id> <mac_addr> <ip_addr> |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 251 | //get based address via EMS |
| 252 | //set socket as async |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 253 | // fcntl(fd, F_SETFL,O_NONBLOCK|FASYNC); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 254 | //got based address |
| 255 | while (1) { |
| 256 | //get local ip address |
| 257 | memset(&ifreq_buffer, 0x00, sizeof(ifreq_buffer)); |
| 258 | strcpy(ifreq_buffer.ifr_name, "ncsi0"); |
| 259 | ret = ioctl(fd, SIOCGIFADDR, &ifreq_buffer); |
| 260 | if (ret == -1) { |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 261 | // printf("ioctl3: [%s]\n", strerror(errno)); |
| 262 | sleep(1); |
| 263 | continue; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 264 | } |
| 265 | |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 266 | if (AF_INET == ifreq_buffer.ifr_addr.sa_family) { |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 267 | memcpy(src_ip, &ifreq_buffer.ifr_addr.sa_data[2], 4); |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 268 | // show_ip_addr("local", (unsigned char *)&ifreq_buffer.ifr_addr.sa_data[2]); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 269 | //show_ip_addr("local", src_ip); |
| 270 | } else { |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 271 | printf("unknown address family [%d]!\n", |
| 272 | ifreq_buffer.ifr_addr.sa_family); |
| 273 | sleep(1); |
| 274 | continue; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 275 | } |
| 276 | |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 277 | memset((void *)&inarp_resp, 0, sizeof inarp_resp); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 278 | length = recvfrom(fd, buffer, ETH_ARP_FRAME_LEN, 0, NULL, NULL); |
| 279 | if (length == -1) { |
| 280 | sleep(1); |
| 281 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 282 | // printf("length = %d\r\nfd = %d\r\n", length, fd); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 283 | current_time.tv_sec = (unsigned)monotonic_sec(); |
| 284 | if (0 == memcmp(src_mac, inarp_req->eh.h_dest, ETH_ALEN)) { |
| 285 | if (ntohs(inarp_req->arp.ar_op) == ARPOP_InREQUEST) { //get a inverse arp request |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 286 | // printf("[Rsp] InRequest\n"); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 287 | //dump_data("got InARP request packet", buffer, length); |
| 288 | //send inarp response |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 289 | |
| 290 | printf |
| 291 | ("src mac =%02x:%02x:%02x:%02x:%02x:%02x\r\n", |
| 292 | inarp_req->arp_data.src_mac[0], |
| 293 | inarp_req->arp_data.src_mac[1], |
| 294 | inarp_req->arp_data.src_mac[2], |
| 295 | inarp_req->arp_data.src_mac[3], |
| 296 | inarp_req->arp_data.src_mac[4], |
| 297 | inarp_req->arp_data.src_mac[5] |
| 298 | ); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 299 | printf("src ip =%d:%d:%d:%d\r\n", |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 300 | inarp_req->arp_data.src_ip[0], |
| 301 | inarp_req->arp_data.src_ip[1], |
| 302 | inarp_req->arp_data.src_ip[2], |
| 303 | inarp_req->arp_data.src_ip[3] |
| 304 | ); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 305 | int fd_1; |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 306 | if (((fd_1 = |
| 307 | socket(AF_PACKET, SOCK_RAW, 0))) == -1) { |
| 308 | printf("socket: [%s]\n", |
| 309 | strerror(errno)); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 310 | exit(-1); |
| 311 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 312 | send_result = |
| 313 | send_arp_packet(fd_1, ifindex, &inarp_resp, |
| 314 | ARPOP_InREPLY, |
| 315 | inarp_req->arp_data. |
| 316 | dest_mac, src_ip, |
| 317 | inarp_req->arp_data.src_mac, |
| 318 | inarp_req->arp_data.src_ip); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 319 | close(fd_1); |
| 320 | if (send_result == -1) { |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 321 | printf("[Rsp] sendto: [%s]\n", |
| 322 | strerror(errno)); |
| 323 | sleep(1); |
| 324 | continue; |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 325 | } |
| 326 | } |
Jeremy Kerr | 5514f7b | 2016-02-25 12:41:44 +0800 | [diff] [blame] | 327 | // } else if (ntohs(inarp_req->arp.ar_op) == ARPOP_InREPLY) { //get a InARP Response |
| 328 | // int node_id; |
| 329 | // printf("[Rsp] InReply\n"); |
| 330 | // node_id = get_node_id_by_mac_address(inarp_req->arp_data.src_mac); |
| 331 | // if (node_id == -1) { |
| 332 | // //invalid node |
| 333 | // printf("%02x:%02x:%02x:%02x:%02x:%02x does not belong to this rack.", |
| 334 | // inarp_req->arp_data.src_mac[0], |
| 335 | // inarp_req->arp_data.src_mac[1], |
| 336 | // inarp_req->arp_data.src_mac[2], |
| 337 | // inarp_req->arp_data.src_mac[3], |
| 338 | // inarp_req->arp_data.src_mac[4], |
| 339 | // inarp_req->arp_data.src_mac[5] |
| 340 | // ); |
| 341 | // } else { |
| 342 | //valid node id |
| 343 | // node[node_id].update_time.tv_sec = current_time.tv_sec; |
| 344 | //update or remain node's ip |
| 345 | // if ((0 != memcmp(node[node_id].dest_ip, inarp_req->arp_data.src_ip, 4))) { |
| 346 | //ip address is changed. |
| 347 | // static char ipv4_addr[16]; |
| 348 | // printf("[Rsp] update node[%d] ip @ %u\n", node_id+1, (unsigned)current_time.tv_sec); |
| 349 | // show_mac_addr("[Rsp] ", inarp_req->arp_data.src_mac); |
| 350 | // show_ip_addr("[Rsp] ", inarp_req->arp_data.src_ip); |
| 351 | // memcpy(node[node_id].dest_ip, inarp_req->arp_data.src_ip, 4); |
| 352 | // sprintf(ipv4_addr, "%d.%d.%d.%d", |
| 353 | // inarp_req->arp_data.src_ip[0], |
| 354 | // inarp_req->arp_data.src_ip[1], |
| 355 | // inarp_req->arp_data.src_ip[2], |
| 356 | // inarp_req->arp_data.src_ip[3]); |
| 357 | // printf("[Rsp] update node[%d] ip @ %u\n", node_id+1, (unsigned)current_time.tv_sec); |
| 358 | // } |
| 359 | memset(buffer, 0, sizeof(buffer)); |
Jeremy Kerr | 42dc98c | 2016-02-25 10:23:14 +0800 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | close(fd); |
| 363 | return 0; |
| 364 | } |