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"
22 int tap_open_common(void *dev
, char *gate_addr
)
26 if(gate_addr
== NULL
) return(0);
27 if(sscanf(gate_addr
, "%d.%d.%d.%d", &tap_addr
[0],
28 &tap_addr
[1], &tap_addr
[2], &tap_addr
[3]) != 4){
29 printk("Invalid tap IP address - '%s'\n", gate_addr
);
35 void tap_check_ips(char *gate_addr
, char *eth_addr
)
39 if((gate_addr
!= NULL
) &&
40 (sscanf(gate_addr
, "%d.%d.%d.%d", &tap_addr
[0],
41 &tap_addr
[1], &tap_addr
[2], &tap_addr
[3]) == 4) &&
42 (eth_addr
[0] == tap_addr
[0]) &&
43 (eth_addr
[1] == tap_addr
[1]) &&
44 (eth_addr
[2] == tap_addr
[2]) &&
45 (eth_addr
[3] == tap_addr
[3])){
46 printk("The tap IP address and the UML eth IP address"
47 " must be different\n");
51 void read_output(int fd
, char *output
, int len
)
53 int remain
, n
, actual
;
62 n
= os_read_file(fd
, &remain
, sizeof(remain
));
63 if(n
!= sizeof(remain
)){
64 printk("read_output - read of length failed, err = %d\n", -n
);
69 n
= (remain
< len
) ? remain
: len
;
70 actual
= os_read_file(fd
, output
, n
);
72 printk("read_output - read of data failed, "
73 "err = %d\n", -actual
);
81 int net_read(int fd
, void *buf
, int len
)
85 n
= os_read_file(fd
, buf
, len
);
94 int net_recvfrom(int fd
, void *buf
, int len
)
98 while(((n
= recvfrom(fd
, buf
, len
, 0, NULL
, NULL
)) < 0) &&
102 if(errno
== EAGAIN
) return(0);
105 else if(n
== 0) return(-ENOTCONN
);
109 int net_write(int fd
, void *buf
, int len
)
113 n
= os_write_file(fd
, buf
, len
);
122 int net_send(int fd
, void *buf
, int len
)
126 while(((n
= send(fd
, buf
, len
, 0)) < 0) && (errno
== EINTR
)) ;
128 if(errno
== EAGAIN
) return(0);
131 else if(n
== 0) return(-ENOTCONN
);
135 int net_sendto(int fd
, void *buf
, int len
, void *to
, int sock_len
)
139 while(((n
= sendto(fd
, buf
, len
, 0, (struct sockaddr
*) to
,
140 sock_len
)) < 0) && (errno
== EINTR
)) ;
142 if(errno
== EAGAIN
) return(0);
145 else if(n
== 0) return(-ENOTCONN
);
149 struct change_pre_exec_data
{
154 static void change_pre_exec(void *arg
)
156 struct change_pre_exec_data
*data
= arg
;
158 os_close_file(data
->close_me
);
159 dup2(data
->stdout
, 1);
162 static int change_tramp(char **argv
, char *output
, int output_len
)
164 int pid
, fds
[2], err
;
165 struct change_pre_exec_data pe_data
;
167 err
= os_pipe(fds
, 1, 0);
169 printk("change_tramp - pipe failed, err = %d\n", -err
);
172 pe_data
.close_me
= fds
[0];
173 pe_data
.stdout
= fds
[1];
174 pid
= run_helper(change_pre_exec
, &pe_data
, argv
, NULL
);
176 os_close_file(fds
[1]);
177 read_output(fds
[0], output
, output_len
);
179 CATCH_EINTR(err
= waitpid(pid
, NULL
, 0));
183 static void change(char *dev
, char *what
, unsigned char *addr
,
184 unsigned char *netmask
)
186 char addr_buf
[sizeof("255.255.255.255\0")];
187 char netmask_buf
[sizeof("255.255.255.255\0")];
188 char version
[sizeof("nnnnn\0")];
189 char *argv
[] = { "uml_net", version
, what
, dev
, addr_buf
,
194 sprintf(version
, "%d", UML_NET_VERSION
);
195 sprintf(addr_buf
, "%d.%d.%d.%d", addr
[0], addr
[1], addr
[2], addr
[3]);
196 sprintf(netmask_buf
, "%d.%d.%d.%d", netmask
[0], netmask
[1],
197 netmask
[2], netmask
[3]);
199 output_len
= page_size();
200 output
= um_kmalloc(output_len
);
202 printk("change : failed to allocate output buffer\n");
204 pid
= change_tramp(argv
, output
, output_len
);
208 printk("%s", output
);
213 void open_addr(unsigned char *addr
, unsigned char *netmask
, void *arg
)
215 change(arg
, "add", addr
, netmask
);
218 void close_addr(unsigned char *addr
, unsigned char *netmask
, void *arg
)
220 change(arg
, "del", addr
, netmask
);
223 char *split_if_spec(char *str
, ...)
229 while((arg
= va_arg(ap
, char **)) != NULL
){
232 end
= strchr(str
, ',');
245 * Overrides for Emacs so that we follow Linus's tabbing style.
246 * Emacs will notice this stuff at the end of the file and automatically
247 * adjust the settings for this buffer only. This must remain at the end
249 * ---------------------------------------------------------------------------
251 * c-file-style: "linux"