1 | /*
|
---|
2 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
3 | *
|
---|
4 | * Please read the file COPYRIGHT for the
|
---|
5 | * terms and conditions of the copyright.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define WANT_SYS_IOCTL_H
|
---|
9 | #include <slirp.h>
|
---|
10 |
|
---|
11 | #ifndef HAVE_INET_ATON
|
---|
12 | int
|
---|
13 | inet_aton(cp, ia)
|
---|
14 | const char *cp;
|
---|
15 | struct in_addr *ia;
|
---|
16 | {
|
---|
17 | u_int32_t addr = inet_addr(cp);
|
---|
18 | if (addr == 0xffffffff)
|
---|
19 | return 0;
|
---|
20 | ia->s_addr = addr;
|
---|
21 | return 1;
|
---|
22 | }
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | /*
|
---|
26 | * Get our IP address and put it in our_addr
|
---|
27 | */
|
---|
28 | void
|
---|
29 | getouraddr(PNATState pData)
|
---|
30 | {
|
---|
31 | our_addr.s_addr = loopback_addr.s_addr;
|
---|
32 | }
|
---|
33 |
|
---|
34 | #if SIZEOF_CHAR_P == 8 && !defined(VBOX_WITH_BSD_REASS)
|
---|
35 |
|
---|
36 | struct quehead_32 {
|
---|
37 | u_int32_t qh_link;
|
---|
38 | u_int32_t qh_rlink;
|
---|
39 | };
|
---|
40 |
|
---|
41 | void
|
---|
42 | insque_32(PNATState pData, void *a, void *b)
|
---|
43 | {
|
---|
44 | register struct quehead_32 *element = (struct quehead_32 *) a;
|
---|
45 | register struct quehead_32 *head = (struct quehead_32 *) b;
|
---|
46 | struct quehead_32 *link = u32_to_ptr(pData, head->qh_link, struct quehead_32 *);
|
---|
47 |
|
---|
48 | element->qh_link = head->qh_link;
|
---|
49 | element->qh_rlink = ptr_to_u32(pData, head);
|
---|
50 | Assert(link->qh_rlink == element->qh_rlink);
|
---|
51 | link->qh_rlink = head->qh_link = ptr_to_u32(pData, element);
|
---|
52 | }
|
---|
53 |
|
---|
54 | void
|
---|
55 | remque_32(PNATState pData, void *a)
|
---|
56 | {
|
---|
57 | register struct quehead_32 *element = (struct quehead_32 *) a;
|
---|
58 | struct quehead_32 *link = u32_to_ptr(pData, element->qh_link, struct quehead_32 *);
|
---|
59 | struct quehead_32 *rlink = u32_to_ptr(pData, element->qh_rlink, struct quehead_32 *);
|
---|
60 |
|
---|
61 | u32ptr_done(pData, link->qh_rlink, element);
|
---|
62 | link->qh_rlink = element->qh_rlink;
|
---|
63 | rlink->qh_link = element->qh_link;
|
---|
64 | element->qh_rlink = 0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | #endif /* SIZEOF_CHAR_P == 8 && !VBOX_WITH_BSD_REASS */
|
---|
68 |
|
---|
69 | struct quehead {
|
---|
70 | struct quehead *qh_link;
|
---|
71 | struct quehead *qh_rlink;
|
---|
72 | };
|
---|
73 |
|
---|
74 | void
|
---|
75 | insque(PNATState pData, void *a, void *b)
|
---|
76 | {
|
---|
77 | register struct quehead *element = (struct quehead *) a;
|
---|
78 | register struct quehead *head = (struct quehead *) b;
|
---|
79 | element->qh_link = head->qh_link;
|
---|
80 | head->qh_link = (struct quehead *)element;
|
---|
81 | element->qh_rlink = (struct quehead *)head;
|
---|
82 | ((struct quehead *)(element->qh_link))->qh_rlink
|
---|
83 | = (struct quehead *)element;
|
---|
84 | }
|
---|
85 |
|
---|
86 | void
|
---|
87 | remque(PNATState pData, void *a)
|
---|
88 | {
|
---|
89 | register struct quehead *element = (struct quehead *) a;
|
---|
90 | ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
|
---|
91 | ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
|
---|
92 | element->qh_rlink = NULL;
|
---|
93 | /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* #endif */
|
---|
97 |
|
---|
98 |
|
---|
99 | int
|
---|
100 | add_exec(ex_ptr, do_pty, exec, addr, port)
|
---|
101 | struct ex_list **ex_ptr;
|
---|
102 | int do_pty;
|
---|
103 | char *exec;
|
---|
104 | int addr;
|
---|
105 | int port;
|
---|
106 | {
|
---|
107 | struct ex_list *tmp_ptr;
|
---|
108 |
|
---|
109 | /* First, check if the port is "bound" */
|
---|
110 | for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
|
---|
111 | if (port == tmp_ptr->ex_fport && addr == tmp_ptr->ex_addr)
|
---|
112 | return -1;
|
---|
113 | }
|
---|
114 |
|
---|
115 | tmp_ptr = *ex_ptr;
|
---|
116 | *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
|
---|
117 | (*ex_ptr)->ex_fport = port;
|
---|
118 | (*ex_ptr)->ex_addr = addr;
|
---|
119 | (*ex_ptr)->ex_pty = do_pty;
|
---|
120 | (*ex_ptr)->ex_exec = strdup(exec);
|
---|
121 | (*ex_ptr)->ex_next = tmp_ptr;
|
---|
122 | return 0;
|
---|
123 | }
|
---|
124 |
|
---|
125 | #ifndef HAVE_STRERROR
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * For systems with no strerror
|
---|
129 | */
|
---|
130 |
|
---|
131 | extern int sys_nerr;
|
---|
132 | extern char *sys_errlist[];
|
---|
133 |
|
---|
134 | char *
|
---|
135 | strerror(error)
|
---|
136 | int error;
|
---|
137 | {
|
---|
138 | if (error < sys_nerr)
|
---|
139 | return sys_errlist[error];
|
---|
140 | else
|
---|
141 | return "Unknown error.";
|
---|
142 | }
|
---|
143 |
|
---|
144 | #endif
|
---|
145 |
|
---|
146 |
|
---|
147 | #ifndef HAVE_STRDUP
|
---|
148 | char *
|
---|
149 | strdup(str)
|
---|
150 | const char *str;
|
---|
151 | {
|
---|
152 | char *bptr;
|
---|
153 |
|
---|
154 | bptr = (char *)malloc(strlen(str)+1);
|
---|
155 | strcpy(bptr, str);
|
---|
156 |
|
---|
157 | return bptr;
|
---|
158 | }
|
---|
159 | #endif
|
---|
160 |
|
---|
161 | #ifdef BAD_SPRINTF
|
---|
162 |
|
---|
163 | #undef vsprintf
|
---|
164 | #undef sprintf
|
---|
165 |
|
---|
166 | /*
|
---|
167 | * Some BSD-derived systems have a sprintf which returns char *
|
---|
168 | */
|
---|
169 |
|
---|
170 | int
|
---|
171 | vsprintf_len(string, format, args)
|
---|
172 | char *string;
|
---|
173 | const char *format;
|
---|
174 | va_list args;
|
---|
175 | {
|
---|
176 | vsprintf(string, format, args);
|
---|
177 | return strlen(string);
|
---|
178 | }
|
---|
179 |
|
---|
180 | int
|
---|
181 | #ifdef __STDC__
|
---|
182 | sprintf_len(char *string, const char *format, ...)
|
---|
183 | #else
|
---|
184 | sprintf_len(va_alist) va_dcl
|
---|
185 | #endif
|
---|
186 | {
|
---|
187 | va_list args;
|
---|
188 | #ifdef __STDC__
|
---|
189 | va_start(args, format);
|
---|
190 | #else
|
---|
191 | char *string;
|
---|
192 | char *format;
|
---|
193 | va_start(args);
|
---|
194 | string = va_arg(args, char *);
|
---|
195 | format = va_arg(args, char *);
|
---|
196 | #endif
|
---|
197 | vsprintf(string, format, args);
|
---|
198 | return strlen(string);
|
---|
199 | }
|
---|
200 |
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | void
|
---|
204 | u_sleep(usec)
|
---|
205 | int usec;
|
---|
206 | {
|
---|
207 | struct timeval t;
|
---|
208 | fd_set fdset;
|
---|
209 |
|
---|
210 | FD_ZERO(&fdset);
|
---|
211 |
|
---|
212 | t.tv_sec = 0;
|
---|
213 | t.tv_usec = usec * 1000;
|
---|
214 |
|
---|
215 | select(0, &fdset, &fdset, &fdset, &t);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /*
|
---|
219 | * Set fd blocking and non-blocking
|
---|
220 | */
|
---|
221 |
|
---|
222 | void
|
---|
223 | fd_nonblock(fd)
|
---|
224 | int fd;
|
---|
225 | {
|
---|
226 | #ifdef FIONBIO
|
---|
227 | int opt = 1;
|
---|
228 |
|
---|
229 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
230 | #else
|
---|
231 | int opt;
|
---|
232 |
|
---|
233 | opt = fcntl(fd, F_GETFL, 0);
|
---|
234 | opt |= O_NONBLOCK;
|
---|
235 | fcntl(fd, F_SETFL, opt);
|
---|
236 | #endif
|
---|
237 | }
|
---|
238 |
|
---|
239 | void
|
---|
240 | fd_block(fd)
|
---|
241 | int fd;
|
---|
242 | {
|
---|
243 | #ifdef FIONBIO
|
---|
244 | int opt = 0;
|
---|
245 |
|
---|
246 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
247 | #else
|
---|
248 | int opt;
|
---|
249 |
|
---|
250 | opt = fcntl(fd, F_GETFL, 0);
|
---|
251 | opt &= ~O_NONBLOCK;
|
---|
252 | fcntl(fd, F_SETFL, opt);
|
---|
253 | #endif
|
---|
254 | }
|
---|
255 |
|
---|