📄 send_arp.c
字号:
* the packet is really coming from. The other obvious choice would be * the MAC address that is being associated for the VIP. Which was the * previous values. Again, these are typically the same thing. * * Previously set to MAC address being associated with the VIP */#ifdef HAVE_LIBNET_1_0_APIintsend_arp(struct libnet_link_int *l, u_long ip, u_char *device, u_char *macaddr, u_char *broadcast, u_char *netmask, u_short arptype){ int n; u_char *buf; u_char *target_mac; u_char device_mac[6]; u_char bcast_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; u_char zero_mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; if (libnet_init_packet(LIBNET_ARP_H + LIBNET_ETH_H, &buf) == -1) { fprintf(stderr, "libnet_init_packet memory:"); return -1; } /* Convert ASCII Mac Address to 6 Hex Digits. */ /* Ethernet header */ if (get_hw_addr(device, device_mac) < 0) { fprintf(stderr, "Cannot find mac address for %s", device); return -1; } if (libnet_build_ethernet(bcast_mac, device_mac, ETHERTYPE_ARP, NULL, 0, buf) == -1) { fprintf(stderr, "libnet_build_ethernet failed:"); libnet_destroy_packet(&buf); return -1; } if (arptype == ARPOP_REQUEST) { target_mac = zero_mac; } else if (arptype == ARPOP_REPLY) { target_mac = macaddr; } else { fprintf(stderr, "unkonwn arptype:"); return -1; } /* * ARP header */ if (libnet_build_arp(ARPHRD_ETHER, /* Hardware address type */ ETHERTYPE_IP, /* Protocol address type */ 6, /* Hardware address length */ 4, /* Protocol address length */ arptype, /* ARP operation */ macaddr, /* Source hardware addr */ (u_char *)&ip, /* Target hardware addr */ target_mac, /* Destination hw addr */ (u_char *)&ip, /* Target protocol address */ NULL, /* Payload */ 0, /* Payload length */ buf + LIBNET_ETH_H) == -1) { fprintf(stderr, "libnet_build_arp failed:"); libnet_destroy_packet(&buf); return -1; } n = libnet_write_link_layer(l, device, buf, LIBNET_ARP_H + LIBNET_ETH_H); if (n == -1) { fprintf(stderr, "libnet_build_ethernet failed:"); } libnet_destroy_packet(&buf); return (n);}#endif /* HAVE_LIBNET_1_0_API */#ifdef HAVE_LIBNET_1_1_APIintsend_arp(libnet_t* lntag, u_long ip, u_char *device, u_char macaddr[6], u_char *broadcast, u_char *netmask, u_short arptype){ int n; u_char *target_mac; u_char device_mac[6]; u_char bcast_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; u_char zero_mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; if (arptype == ARPOP_REQUEST) { target_mac = zero_mac; } else if (arptype == ARPOP_REPLY) { target_mac = macaddr; } else { fprintf(stderr, "unkonwn arptype:"); return -1; } /* * ARP header */ if (libnet_build_arp(ARPHRD_ETHER, /* hardware address type */ ETHERTYPE_IP, /* protocol address type */ 6, /* Hardware address length */ 4, /* protocol address length */ arptype, /* ARP operation type */ macaddr, /* sender Hardware address */ (u_char *)&ip, /* sender protocol address */ target_mac, /* target hardware address */ (u_char *)&ip, /* target protocol address */ NULL, /* Payload */ 0, /* Length of payload */ lntag, /* libnet context pointer */ 0 /* packet id */ ) == -1 ) { fprintf(stderr, "libnet_build_arp failed:"); return -1; } /* Ethernet header */ if (get_hw_addr((char*)device, device_mac) < 0) { fprintf(stderr, "Cannot find mac address for %s", device); return -1; } if (libnet_build_ethernet(bcast_mac, device_mac, ETHERTYPE_ARP, NULL, 0 , lntag, 0) == -1 ) { fprintf(stderr, "libnet_build_ethernet failed:"); return -1; } n = libnet_write(lntag); if (n == -1) { fprintf(stderr, "libnet_build_ethernet failed:"); } libnet_clear_packet(lntag); return (n);}#endif /* HAVE_LIBNET_1_1_API */intcreate_pid_directory(const char *pidfilename) { int status; struct stat stat_buf; char *pidfilename_cpy; char *dir; pidfilename_cpy = strdup(pidfilename); if (!pidfilename_cpy) { fprintf(stderr, "Memory allocation failure: %s\n", strerror(errno)); return -1; } dir = dirname(pidfilename_cpy); status = stat(dir, &stat_buf); if (status < 0 && errno != ENOENT && errno != ENOTDIR) { fprintf(stderr, "Could not stat pid-file directory " "[%s]: %s", dir, strerror(errno)); free(pidfilename_cpy); return -1; } if (!status) { if (S_ISDIR(stat_buf.st_mode)) { return 0; } fprintf(stderr, "Pid-File directory exists but is " "not a directory [%s]", dir); free(pidfilename_cpy); return -1; } if (mkdir(dir, S_IRUSR|S_IWUSR|S_IXUSR | S_IRGRP|S_IXGRP) < 0) { fprintf(stderr, "Could not create pid-file directory " "[%s]: %s", dir, strerror(errno)); free(pidfilename_cpy); return -1; } free(pidfilename_cpy); return 0;}intwrite_pid_file(const char *pidfilename) { int pidfilefd; char pidbuf[11]; unsigned long pid; ssize_t bytes; if (*pidfilename != '/') { fprintf(stderr, "Invalid pid-file name, must begin with a " "'/' [%s]\n", pidfilename); return -1; } if (create_pid_directory(pidfilename) < 0) { return -1; } while (1) { pidfilefd = open(pidfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR); if (pidfilefd < 0) { if (errno != EEXIST) { /* Old PID file */ fprintf(stderr, "Could not open pid-file " "[%s]: %s", pidfilename, strerror(errno)); return -1; } } else { break; } pidfilefd = open(pidfilename, O_RDONLY, S_IRUSR|S_IWUSR); if (pidfilefd < 0) { fprintf(stderr, "Could not open pid-file " "[%s]: %s", pidfilename, strerror(errno)); return -1; } while (1) { bytes = read(pidfilefd, pidbuf, sizeof(pidbuf)-1); if (bytes < 0) { if (errno == EINTR) { continue; } fprintf(stderr, "Could not read pid-file " "[%s]: %s", pidfilename, strerror(errno)); return -1; } pidbuf[bytes] = '\0'; break; } if(unlink(pidfilename) < 0) { fprintf(stderr, "Could not delete pid-file " "[%s]: %s", pidfilename, strerror(errno)); return -1; } if (!bytes) { fprintf(stderr, "Invalid pid in pid-file " "[%s]: %s", pidfilename, strerror(errno)); return -1; } close(pidfilefd); pid = strtoul(pidbuf, NULL, 10); if (pid == ULONG_MAX && errno == ERANGE) { fprintf(stderr, "Invalid pid in pid-file " "[%s]: %s", pidfilename, strerror(errno)); return -1; } if (kill(pid, SIGKILL) < 0 && errno != ESRCH) { fprintf(stderr, "Error killing old proccess [%lu] " "from pid-file [%s]: %s", pid, pidfilename, strerror(errno)); return -1; } fprintf(stderr, "Killed old send_arp process [%lu]\n", pid); } if (snprintf(pidbuf, sizeof(pidbuf), "%u" , getpid()) >= (int)sizeof(pidbuf)) { fprintf(stderr, "Pid too long for buffer [%u]", getpid()); return -1; } while (1) { bytes = write(pidfilefd, pidbuf, strlen(pidbuf)); if (bytes != strlen(pidbuf)) { if (bytes < 0 && errno == EINTR) { continue; } fprintf(stderr, "Could not write pid-file " "[%s]: %s", pidfilename, strerror(errno)); return -1; } break; } close(pidfilefd); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -