DF-3298 / setstate.c
/* * DF-3298 PoC helper - force carp(4) state via SIOCSVH (carpr_state). * Does read-modify-write (like ifconfig) so the key/params survive. * * Build: cc -O2 -o setstate setstate.c * Usage: setstate carp0 2 (2 = MASTER) */ #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <net/if.h> #include <netinet/in.h> #include <netinet/ip_carp.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char **argv) { struct carpreq carpr; struct ifreq ifr; int s, state; if (argc != 3) { fprintf(stderr, "usage: %s <carpif> <0|1|2>\n", argv[0]); return 2; } state = atoi(argv[2]); s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { perror("socket"); return 1; } memset(&ifr, 0, sizeof(ifr)); memset(&carpr, 0, sizeof(carpr)); strlcpy(ifr.ifr_name, argv[1], sizeof(ifr.ifr_name)); ifr.ifr_data = (caddr_t)&carpr; if (ioctl(s, SIOCGVH, &ifr) == -1) { perror("SIOCGVH"); return 1; } carpr.carpr_state = state; if (ioctl(s, SIOCSVH, &ifr) == -1) { perror("SIOCSVH"); return 1; } printf("%s: state set to %d\n", argv[1], state); return 0; } |