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 | char buff[256];
|
---|
32 | struct hostent *he = NULL;
|
---|
33 |
|
---|
34 | if (gethostname(buff,256) == 0)
|
---|
35 | {
|
---|
36 | he = gethostbyname(buff);
|
---|
37 | if (he)
|
---|
38 | {
|
---|
39 | uint32_t ipv4_addr;
|
---|
40 | ipv4_addr = ntohl((*(struct in_addr*)he->h_addr).s_addr);
|
---|
41 | LogRel(("NAT: host is '%s' => %u.%u.%u.%u\n",
|
---|
42 | buff,
|
---|
43 | ipv4_addr >> 24, (ipv4_addr >> 16) & 0xff,
|
---|
44 | (ipv4_addr >> 8) & 0xff, ipv4_addr & 0xff));
|
---|
45 | }
|
---|
46 | else
|
---|
47 | LogRel(("NAT: host name is '%s' (using 127.0.0.1)\n", buff));
|
---|
48 | }
|
---|
49 | if (he)
|
---|
50 | our_addr = *(struct in_addr *)he->h_addr;
|
---|
51 | if (our_addr.s_addr == 0)
|
---|
52 | our_addr.s_addr = loopback_addr.s_addr;
|
---|
53 | }
|
---|
54 |
|
---|
55 | #if SIZEOF_CHAR_P == 8
|
---|
56 |
|
---|
57 | struct quehead_32 {
|
---|
58 | u_int32_t qh_link;
|
---|
59 | u_int32_t qh_rlink;
|
---|
60 | };
|
---|
61 |
|
---|
62 | void
|
---|
63 | insque_32(PNATState pData, void *a, void *b)
|
---|
64 | {
|
---|
65 | register struct quehead_32 *element = (struct quehead_32 *) a;
|
---|
66 | register struct quehead_32 *head = (struct quehead_32 *) b;
|
---|
67 | struct quehead_32 *link = u32_to_ptr(pData, head->qh_link, struct quehead_32 *);
|
---|
68 |
|
---|
69 | element->qh_link = head->qh_link;
|
---|
70 | element->qh_rlink = ptr_to_u32(pData, head);
|
---|
71 | Assert(link->qh_rlink == element->qh_rlink);
|
---|
72 | link->qh_rlink = head->qh_link = ptr_to_u32(pData, element);
|
---|
73 | }
|
---|
74 |
|
---|
75 | void
|
---|
76 | remque_32(PNATState pData, void *a)
|
---|
77 | {
|
---|
78 | register struct quehead_32 *element = (struct quehead_32 *) a;
|
---|
79 | struct quehead_32 *link = u32_to_ptr(pData, element->qh_link, struct quehead_32 *);
|
---|
80 | struct quehead_32 *rlink = u32_to_ptr(pData, element->qh_rlink, struct quehead_32 *);
|
---|
81 |
|
---|
82 | u32ptr_done(pData, link->qh_rlink, element);
|
---|
83 | link->qh_rlink = element->qh_rlink;
|
---|
84 | rlink->qh_link = element->qh_link;
|
---|
85 | element->qh_rlink = 0;
|
---|
86 | }
|
---|
87 |
|
---|
88 | #endif /* SIZEOF_CHAR_P == 8 */
|
---|
89 |
|
---|
90 | struct quehead {
|
---|
91 | struct quehead *qh_link;
|
---|
92 | struct quehead *qh_rlink;
|
---|
93 | };
|
---|
94 |
|
---|
95 | void
|
---|
96 | insque(PNATState pData, void *a, void *b)
|
---|
97 | {
|
---|
98 | register struct quehead *element = (struct quehead *) a;
|
---|
99 | register struct quehead *head = (struct quehead *) b;
|
---|
100 | element->qh_link = head->qh_link;
|
---|
101 | head->qh_link = (struct quehead *)element;
|
---|
102 | element->qh_rlink = (struct quehead *)head;
|
---|
103 | ((struct quehead *)(element->qh_link))->qh_rlink
|
---|
104 | = (struct quehead *)element;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void
|
---|
108 | remque(PNATState pData, void *a)
|
---|
109 | {
|
---|
110 | register struct quehead *element = (struct quehead *) a;
|
---|
111 | ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
|
---|
112 | ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
|
---|
113 | element->qh_rlink = NULL;
|
---|
114 | /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* #endif */
|
---|
118 |
|
---|
119 |
|
---|
120 | int
|
---|
121 | add_exec(ex_ptr, do_pty, exec, addr, port)
|
---|
122 | struct ex_list **ex_ptr;
|
---|
123 | int do_pty;
|
---|
124 | char *exec;
|
---|
125 | int addr;
|
---|
126 | int port;
|
---|
127 | {
|
---|
128 | struct ex_list *tmp_ptr;
|
---|
129 |
|
---|
130 | /* First, check if the port is "bound" */
|
---|
131 | for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
|
---|
132 | if (port == tmp_ptr->ex_fport && addr == tmp_ptr->ex_addr)
|
---|
133 | return -1;
|
---|
134 | }
|
---|
135 |
|
---|
136 | tmp_ptr = *ex_ptr;
|
---|
137 | *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
|
---|
138 | (*ex_ptr)->ex_fport = port;
|
---|
139 | (*ex_ptr)->ex_addr = addr;
|
---|
140 | (*ex_ptr)->ex_pty = do_pty;
|
---|
141 | (*ex_ptr)->ex_exec = strdup(exec);
|
---|
142 | (*ex_ptr)->ex_next = tmp_ptr;
|
---|
143 | return 0;
|
---|
144 | }
|
---|
145 |
|
---|
146 | #ifndef HAVE_STRERROR
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * For systems with no strerror
|
---|
150 | */
|
---|
151 |
|
---|
152 | extern int sys_nerr;
|
---|
153 | extern char *sys_errlist[];
|
---|
154 |
|
---|
155 | char *
|
---|
156 | strerror(error)
|
---|
157 | int error;
|
---|
158 | {
|
---|
159 | if (error < sys_nerr)
|
---|
160 | return sys_errlist[error];
|
---|
161 | else
|
---|
162 | return "Unknown error.";
|
---|
163 | }
|
---|
164 |
|
---|
165 | #endif
|
---|
166 |
|
---|
167 |
|
---|
168 | #ifndef HAVE_STRDUP
|
---|
169 | char *
|
---|
170 | strdup(str)
|
---|
171 | const char *str;
|
---|
172 | {
|
---|
173 | char *bptr;
|
---|
174 |
|
---|
175 | bptr = (char *)malloc(strlen(str)+1);
|
---|
176 | strcpy(bptr, str);
|
---|
177 |
|
---|
178 | return bptr;
|
---|
179 | }
|
---|
180 | #endif
|
---|
181 |
|
---|
182 | #ifdef BAD_SPRINTF
|
---|
183 |
|
---|
184 | #undef vsprintf
|
---|
185 | #undef sprintf
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * Some BSD-derived systems have a sprintf which returns char *
|
---|
189 | */
|
---|
190 |
|
---|
191 | int
|
---|
192 | vsprintf_len(string, format, args)
|
---|
193 | char *string;
|
---|
194 | const char *format;
|
---|
195 | va_list args;
|
---|
196 | {
|
---|
197 | vsprintf(string, format, args);
|
---|
198 | return strlen(string);
|
---|
199 | }
|
---|
200 |
|
---|
201 | int
|
---|
202 | #ifdef __STDC__
|
---|
203 | sprintf_len(char *string, const char *format, ...)
|
---|
204 | #else
|
---|
205 | sprintf_len(va_alist) va_dcl
|
---|
206 | #endif
|
---|
207 | {
|
---|
208 | va_list args;
|
---|
209 | #ifdef __STDC__
|
---|
210 | va_start(args, format);
|
---|
211 | #else
|
---|
212 | char *string;
|
---|
213 | char *format;
|
---|
214 | va_start(args);
|
---|
215 | string = va_arg(args, char *);
|
---|
216 | format = va_arg(args, char *);
|
---|
217 | #endif
|
---|
218 | vsprintf(string, format, args);
|
---|
219 | return strlen(string);
|
---|
220 | }
|
---|
221 |
|
---|
222 | #endif
|
---|
223 |
|
---|
224 | void
|
---|
225 | u_sleep(usec)
|
---|
226 | int usec;
|
---|
227 | {
|
---|
228 | struct timeval t;
|
---|
229 | fd_set fdset;
|
---|
230 |
|
---|
231 | FD_ZERO(&fdset);
|
---|
232 |
|
---|
233 | t.tv_sec = 0;
|
---|
234 | t.tv_usec = usec * 1000;
|
---|
235 |
|
---|
236 | select(0, &fdset, &fdset, &fdset, &t);
|
---|
237 | }
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * Set fd blocking and non-blocking
|
---|
241 | */
|
---|
242 |
|
---|
243 | void
|
---|
244 | fd_nonblock(fd)
|
---|
245 | int fd;
|
---|
246 | {
|
---|
247 | #ifdef FIONBIO
|
---|
248 | int opt = 1;
|
---|
249 |
|
---|
250 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
251 | #else
|
---|
252 | int opt;
|
---|
253 |
|
---|
254 | opt = fcntl(fd, F_GETFL, 0);
|
---|
255 | opt |= O_NONBLOCK;
|
---|
256 | fcntl(fd, F_SETFL, opt);
|
---|
257 | #endif
|
---|
258 | }
|
---|
259 |
|
---|
260 | void
|
---|
261 | fd_block(fd)
|
---|
262 | int fd;
|
---|
263 | {
|
---|
264 | #ifdef FIONBIO
|
---|
265 | int opt = 0;
|
---|
266 |
|
---|
267 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
268 | #else
|
---|
269 | int opt;
|
---|
270 |
|
---|
271 | opt = fcntl(fd, F_GETFL, 0);
|
---|
272 | opt &= ~O_NONBLOCK;
|
---|
273 | fcntl(fd, F_SETFL, opt);
|
---|
274 | #endif
|
---|
275 | }
|
---|
276 |
|
---|