^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <sys/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net_user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <um_malloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) int tap_open_common(void *dev, char *gate_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int tap_addr[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (gate_addr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) &tap_addr[1], &tap_addr[2], &tap_addr[3]) != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) printk(UM_KERN_ERR "Invalid tap IP address - '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) gate_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int tap_addr[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if ((gate_addr != NULL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) &tap_addr[1], &tap_addr[2], &tap_addr[3]) == 4) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) (eth_addr[0] == tap_addr[0]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) (eth_addr[1] == tap_addr[1]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) (eth_addr[2] == tap_addr[2]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) (eth_addr[3] == tap_addr[3])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) printk(UM_KERN_ERR "The tap IP address and the UML eth IP "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) "address must be different\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* Do reliable error handling as this fails frequently enough. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) void read_output(int fd, char *output, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int remain, ret, expected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (output == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) output = &c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) len = sizeof(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) *output = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ret = read(fd, &remain, sizeof(remain));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (ret != sizeof(remain)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) expected = sizeof(remain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) str = "length";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) while (remain != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) expected = (remain < len) ? remain : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ret = read(fd, output, expected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (ret != expected) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) str = "data";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) remain -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) printk(UM_KERN_ERR "read_output - read of %s failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) "errno = %d\n", str, -ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) printk(UM_KERN_ERR "read_output - read of %s failed, read only "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) "%d of %d bytes\n", str, ret, expected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int net_read(int fd, void *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) n = read(fd, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if ((n < 0) && (errno == EAGAIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) else if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int net_recvfrom(int fd, void *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) CATCH_EINTR(n = recvfrom(fd, buf, len, 0, NULL, NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (n < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (errno == EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) else if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int net_write(int fd, void *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) n = write(fd, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if ((n < 0) && (errno == EAGAIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) else if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int net_send(int fd, void *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) CATCH_EINTR(n = send(fd, buf, len, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (n < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (errno == EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) else if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) CATCH_EINTR(n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) sock_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (n < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (errno == EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) else if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct change_pre_exec_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int close_me;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int stdout_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static void change_pre_exec(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct change_pre_exec_data *data = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) close(data->close_me);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) dup2(data->stdout_fd, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int change_tramp(char **argv, char *output, int output_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int pid, fds[2], err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct change_pre_exec_data pe_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) err = os_pipe(fds, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) printk(UM_KERN_ERR "change_tramp - pipe failed, err = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) pe_data.close_me = fds[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) pe_data.stdout_fd = fds[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) pid = run_helper(change_pre_exec, &pe_data, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (pid > 0) /* Avoid hang as we won't get data in failure case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) read_output(fds[0], output, output_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) close(fds[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) close(fds[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (pid > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) helper_wait(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static void change(char *dev, char *what, unsigned char *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unsigned char *netmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) char addr_buf[sizeof("255.255.255.255\0")];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) char netmask_buf[sizeof("255.255.255.255\0")];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) char version[sizeof("nnnnn\0")];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) char *argv[] = { "uml_net", version, what, dev, addr_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) netmask_buf, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) char *output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) int output_len, pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) sprintf(version, "%d", UML_NET_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) sprintf(addr_buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) netmask[2], netmask[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) output_len = UM_KERN_PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) output = uml_kmalloc(output_len, UM_GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (output == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) printk(UM_KERN_ERR "change : failed to allocate output "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) "buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) pid = change_tramp(argv, output, output_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (pid < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) kfree(output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (output != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) printk("%s", output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) kfree(output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) change(arg, "add", addr, netmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) change(arg, "del", addr, netmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) char *split_if_spec(char *str, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) char **arg, *end, *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) va_start(ap, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) while ((arg = va_arg(ap, char **)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (*str == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) end = strchr(str, ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (end != str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) *arg = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (end == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) *end++ = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) str = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) ret = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }