2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
13 #include <sys/socket.h>
16 #include "user_util.h"
17 #include "kern_util.h"
21 int tap_open_common(void *dev, char *gate_addr)
25 if(gate_addr == NULL) return(0);
26 if(sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
27 &tap_addr[1], &tap_addr[2], &tap_addr[3]) != 4){
28 printk("Invalid tap IP address - '%s'\n", gate_addr);
34 void tap_check_ips(char *gate_addr, unsigned char *eth_addr)
38 if((gate_addr != NULL) &&
39 (sscanf(gate_addr, "%d.%d.%d.%d", &tap_addr[0],
40 &tap_addr[1], &tap_addr[2], &tap_addr[3]) == 4) &&
41 (eth_addr[0] == tap_addr[0]) &&
42 (eth_addr[1] == tap_addr[1]) &&
43 (eth_addr[2] == tap_addr[2]) &&
44 (eth_addr[3] == tap_addr[3])){
45 printk("The tap IP address and the UML eth IP address"
46 " must be different\n");
50 void read_output(int fd, char *output, int len)
52 int remain, n, actual;
61 n = os_read_file(fd, &remain, sizeof(remain));
62 if(n != sizeof(remain)){
63 printk("read_output - read of length failed, err = %d\n", -n);
68 n = (remain < len) ? remain : len;
69 actual = os_read_file(fd, output, n);
71 printk("read_output - read of data failed, "
72 "err = %d\n", -actual);
80 int net_read(int fd, void *buf, int len)
84 n = os_read_file(fd, buf, len);
93 int net_recvfrom(int fd, void *buf, int len)
97 while(((n = recvfrom(fd, buf, len, 0, NULL, NULL)) < 0) &&
101 if(errno == EAGAIN) return(0);
104 else if(n == 0) return(-ENOTCONN);
108 int net_write(int fd, void *buf, int len)
112 n = os_write_file(fd, buf, len);
121 int net_send(int fd, void *buf, int len)
125 while(((n = send(fd, buf, len, 0)) < 0) && (errno == EINTR)) ;
127 if(errno == EAGAIN) return(0);
130 else if(n == 0) return(-ENOTCONN);
134 int net_sendto(int fd, void *buf, int len, void *to, int sock_len)
138 while(((n = sendto(fd, buf, len, 0, (struct sockaddr *) to,
139 sock_len)) < 0) && (errno == EINTR)) ;
141 if(errno == EAGAIN) return(0);
144 else if(n == 0) return(-ENOTCONN);
148 struct change_pre_exec_data {
153 static void change_pre_exec(void *arg)
155 struct change_pre_exec_data *data = arg;
157 os_close_file(data->close_me);
158 dup2(data->stdout, 1);
161 static int change_tramp(char **argv, char *output, int output_len)
163 int pid, fds[2], err;
164 struct change_pre_exec_data pe_data;
166 err = os_pipe(fds, 1, 0);
168 printk("change_tramp - pipe failed, err = %d\n", -err);
171 pe_data.close_me = fds[0];
172 pe_data.stdout = fds[1];
173 pid = run_helper(change_pre_exec, &pe_data, argv, NULL);
175 read_output(fds[0], output, output_len);
176 os_close_file(fds[0]);
177 os_close_file(fds[1]);
180 CATCH_EINTR(err = waitpid(pid, NULL, 0));
184 static void change(char *dev, char *what, unsigned char *addr,
185 unsigned char *netmask)
187 char addr_buf[sizeof("255.255.255.255\0")];
188 char netmask_buf[sizeof("255.255.255.255\0")];
189 char version[sizeof("nnnnn\0")];
190 char *argv[] = { "uml_net", version, what, dev, addr_buf,
195 sprintf(version, "%d", UML_NET_VERSION);
196 sprintf(addr_buf, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
197 sprintf(netmask_buf, "%d.%d.%d.%d", netmask[0], netmask[1],
198 netmask[2], netmask[3]);
200 output_len = page_size();
201 output = um_kmalloc(output_len);
203 printk("change : failed to allocate output buffer\n");
205 pid = change_tramp(argv, output, output_len);
209 printk("%s", output);
214 void open_addr(unsigned char *addr, unsigned char *netmask, void *arg)
216 change(arg, "add", addr, netmask);
219 void close_addr(unsigned char *addr, unsigned char *netmask, void *arg)
221 change(arg, "del", addr, netmask);
224 char *split_if_spec(char *str, ...)
230 while((arg = va_arg(ap, char **)) != NULL){
233 end = strchr(str, ',');
246 * Overrides for Emacs so that we follow Linus's tabbing style.
247 * Emacs will notice this stuff at the end of the file and automatically
248 * adjust the settings for this buffer only. This must remain at the end
250 * ---------------------------------------------------------------------------
252 * c-file-style: "linux"