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