]>
Commit | Line | Data |
---|---|---|
f0cbd3ec FB |
1 | /* |
2 | * Copyright (c) 1995 Danny Gasparovski. | |
5fafdf24 TS |
3 | * |
4 | * Please read the file COPYRIGHT for the | |
f0cbd3ec FB |
5 | * terms and conditions of the copyright. |
6 | */ | |
7 | ||
e1c5a2b3 | 8 | #include "qemu-common.h" |
f0cbd3ec FB |
9 | #include <slirp.h> |
10 | #include "ip_icmp.h" | |
ec530c81 FB |
11 | #ifdef __sun__ |
12 | #include <sys/filio.h> | |
13 | #endif | |
f0cbd3ec | 14 | |
9634d903 BS |
15 | static void sofcantrcvmore(struct socket *so); |
16 | static void sofcantsendmore(struct socket *so); | |
17 | ||
18 | #if 0 | |
19 | static void | |
f0cbd3ec FB |
20 | so_init() |
21 | { | |
22 | /* Nothing yet */ | |
23 | } | |
9634d903 | 24 | #endif |
f0cbd3ec FB |
25 | |
26 | struct socket * | |
511d2b14 BS |
27 | solookup(struct socket *head, struct in_addr laddr, u_int lport, |
28 | struct in_addr faddr, u_int fport) | |
f0cbd3ec FB |
29 | { |
30 | struct socket *so; | |
5fafdf24 | 31 | |
f0cbd3ec | 32 | for (so = head->so_next; so != head; so = so->so_next) { |
5fafdf24 | 33 | if (so->so_lport == lport && |
f0cbd3ec FB |
34 | so->so_laddr.s_addr == laddr.s_addr && |
35 | so->so_faddr.s_addr == faddr.s_addr && | |
36 | so->so_fport == fport) | |
37 | break; | |
38 | } | |
5fafdf24 | 39 | |
f0cbd3ec FB |
40 | if (so == head) |
41 | return (struct socket *)NULL; | |
42 | return so; | |
5fafdf24 | 43 | |
f0cbd3ec FB |
44 | } |
45 | ||
46 | /* | |
47 | * Create a new socket, initialise the fields | |
48 | * It is the responsibility of the caller to | |
49 | * insque() it into the correct linked-list | |
50 | */ | |
51 | struct socket * | |
511d2b14 | 52 | socreate(void) |
f0cbd3ec FB |
53 | { |
54 | struct socket *so; | |
5fafdf24 | 55 | |
f0cbd3ec FB |
56 | so = (struct socket *)malloc(sizeof(struct socket)); |
57 | if(so) { | |
58 | memset(so, 0, sizeof(struct socket)); | |
59 | so->so_state = SS_NOFDREF; | |
60 | so->s = -1; | |
61 | } | |
62 | return(so); | |
63 | } | |
64 | ||
65 | /* | |
66 | * remque and free a socket, clobber cache | |
67 | */ | |
68 | void | |
511d2b14 | 69 | sofree(struct socket *so) |
f0cbd3ec FB |
70 | { |
71 | if (so->so_emu==EMU_RSH && so->extra) { | |
72 | sofree(so->extra); | |
73 | so->extra=NULL; | |
74 | } | |
75 | if (so == tcp_last_so) | |
76 | tcp_last_so = &tcb; | |
77 | else if (so == udp_last_so) | |
78 | udp_last_so = &udb; | |
5fafdf24 | 79 | |
f0cbd3ec | 80 | m_free(so->so_m); |
5fafdf24 TS |
81 | |
82 | if(so->so_next && so->so_prev) | |
f0cbd3ec FB |
83 | remque(so); /* crashes if so is not in a queue */ |
84 | ||
85 | free(so); | |
86 | } | |
87 | ||
e1c5a2b3 | 88 | size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np) |
f0cbd3ec | 89 | { |
e1c5a2b3 | 90 | int n, lss, total; |
f0cbd3ec FB |
91 | struct sbuf *sb = &so->so_snd; |
92 | int len = sb->sb_datalen - sb->sb_cc; | |
f0cbd3ec | 93 | int mss = so->so_tcpcb->t_maxseg; |
5fafdf24 | 94 | |
e1c5a2b3 | 95 | DEBUG_CALL("sopreprbuf"); |
f0cbd3ec | 96 | DEBUG_ARG("so = %lx", (long )so); |
5fafdf24 | 97 | |
f0cbd3ec | 98 | len = sb->sb_datalen - sb->sb_cc; |
5fafdf24 | 99 | |
e1c5a2b3 AL |
100 | if (len <= 0) |
101 | return 0; | |
102 | ||
f0cbd3ec | 103 | iov[0].iov_base = sb->sb_wptr; |
66029f6a BS |
104 | iov[1].iov_base = NULL; |
105 | iov[1].iov_len = 0; | |
f0cbd3ec FB |
106 | if (sb->sb_wptr < sb->sb_rptr) { |
107 | iov[0].iov_len = sb->sb_rptr - sb->sb_wptr; | |
108 | /* Should never succeed, but... */ | |
109 | if (iov[0].iov_len > len) | |
110 | iov[0].iov_len = len; | |
111 | if (iov[0].iov_len > mss) | |
112 | iov[0].iov_len -= iov[0].iov_len%mss; | |
113 | n = 1; | |
114 | } else { | |
115 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr; | |
116 | /* Should never succeed, but... */ | |
117 | if (iov[0].iov_len > len) iov[0].iov_len = len; | |
118 | len -= iov[0].iov_len; | |
119 | if (len) { | |
120 | iov[1].iov_base = sb->sb_data; | |
121 | iov[1].iov_len = sb->sb_rptr - sb->sb_data; | |
122 | if(iov[1].iov_len > len) | |
123 | iov[1].iov_len = len; | |
124 | total = iov[0].iov_len + iov[1].iov_len; | |
125 | if (total > mss) { | |
126 | lss = total%mss; | |
127 | if (iov[1].iov_len > lss) { | |
128 | iov[1].iov_len -= lss; | |
129 | n = 2; | |
130 | } else { | |
131 | lss -= iov[1].iov_len; | |
132 | iov[0].iov_len -= lss; | |
133 | n = 1; | |
134 | } | |
135 | } else | |
136 | n = 2; | |
137 | } else { | |
138 | if (iov[0].iov_len > mss) | |
139 | iov[0].iov_len -= iov[0].iov_len%mss; | |
140 | n = 1; | |
141 | } | |
142 | } | |
e1c5a2b3 AL |
143 | if (np) |
144 | *np = n; | |
145 | ||
146 | return iov[0].iov_len + (n - 1) * iov[1].iov_len; | |
147 | } | |
148 | ||
149 | /* | |
150 | * Read from so's socket into sb_snd, updating all relevant sbuf fields | |
151 | * NOTE: This will only be called if it is select()ed for reading, so | |
152 | * a read() of 0 (or less) means it's disconnected | |
153 | */ | |
154 | int | |
511d2b14 | 155 | soread(struct socket *so) |
e1c5a2b3 AL |
156 | { |
157 | int n, nn; | |
158 | struct sbuf *sb = &so->so_snd; | |
159 | struct iovec iov[2]; | |
160 | ||
161 | DEBUG_CALL("soread"); | |
162 | DEBUG_ARG("so = %lx", (long )so); | |
163 | ||
164 | /* | |
165 | * No need to check if there's enough room to read. | |
166 | * soread wouldn't have been called if there weren't | |
167 | */ | |
168 | sopreprbuf(so, iov, &n); | |
5fafdf24 | 169 | |
f0cbd3ec FB |
170 | #ifdef HAVE_READV |
171 | nn = readv(so->s, (struct iovec *)iov, n); | |
172 | DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn)); | |
173 | #else | |
02d2c54c | 174 | nn = recv(so->s, iov[0].iov_base, iov[0].iov_len,0); |
5fafdf24 | 175 | #endif |
f0cbd3ec FB |
176 | if (nn <= 0) { |
177 | if (nn < 0 && (errno == EINTR || errno == EAGAIN)) | |
178 | return 0; | |
179 | else { | |
180 | DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno))); | |
181 | sofcantrcvmore(so); | |
182 | tcp_sockclosed(sototcpcb(so)); | |
183 | return -1; | |
184 | } | |
185 | } | |
5fafdf24 | 186 | |
f0cbd3ec FB |
187 | #ifndef HAVE_READV |
188 | /* | |
189 | * If there was no error, try and read the second time round | |
190 | * We read again if n = 2 (ie, there's another part of the buffer) | |
191 | * and we read as much as we could in the first read | |
192 | * We don't test for <= 0 this time, because there legitimately | |
193 | * might not be any more data (since the socket is non-blocking), | |
194 | * a close will be detected on next iteration. | |
195 | * A return of -1 wont (shouldn't) happen, since it didn't happen above | |
196 | */ | |
17444c9c FB |
197 | if (n == 2 && nn == iov[0].iov_len) { |
198 | int ret; | |
199 | ret = recv(so->s, iov[1].iov_base, iov[1].iov_len,0); | |
200 | if (ret > 0) | |
201 | nn += ret; | |
202 | } | |
5fafdf24 | 203 | |
f0cbd3ec FB |
204 | DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn)); |
205 | #endif | |
5fafdf24 | 206 | |
f0cbd3ec FB |
207 | /* Update fields */ |
208 | sb->sb_cc += nn; | |
209 | sb->sb_wptr += nn; | |
210 | if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen)) | |
211 | sb->sb_wptr -= sb->sb_datalen; | |
212 | return nn; | |
213 | } | |
5fafdf24 | 214 | |
e1c5a2b3 AL |
215 | int soreadbuf(struct socket *so, const char *buf, int size) |
216 | { | |
217 | int n, nn, copy = size; | |
218 | struct sbuf *sb = &so->so_snd; | |
219 | struct iovec iov[2]; | |
220 | ||
221 | DEBUG_CALL("soreadbuf"); | |
222 | DEBUG_ARG("so = %lx", (long )so); | |
223 | ||
224 | /* | |
225 | * No need to check if there's enough room to read. | |
226 | * soread wouldn't have been called if there weren't | |
227 | */ | |
228 | if (sopreprbuf(so, iov, &n) < size) | |
229 | goto err; | |
230 | ||
231 | nn = MIN(iov[0].iov_len, copy); | |
232 | memcpy(iov[0].iov_base, buf, nn); | |
233 | ||
234 | copy -= nn; | |
235 | buf += nn; | |
236 | ||
237 | if (copy == 0) | |
238 | goto done; | |
239 | ||
240 | memcpy(iov[1].iov_base, buf, copy); | |
241 | ||
242 | done: | |
243 | /* Update fields */ | |
244 | sb->sb_cc += size; | |
245 | sb->sb_wptr += size; | |
246 | if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen)) | |
247 | sb->sb_wptr -= sb->sb_datalen; | |
248 | return size; | |
249 | err: | |
250 | ||
251 | sofcantrcvmore(so); | |
252 | tcp_sockclosed(sototcpcb(so)); | |
253 | fprintf(stderr, "soreadbuf buffer to small"); | |
254 | return -1; | |
255 | } | |
256 | ||
f0cbd3ec FB |
257 | /* |
258 | * Get urgent data | |
5fafdf24 | 259 | * |
f0cbd3ec FB |
260 | * When the socket is created, we set it SO_OOBINLINE, |
261 | * so when OOB data arrives, we soread() it and everything | |
262 | * in the send buffer is sent as urgent data | |
263 | */ | |
264 | void | |
511d2b14 | 265 | sorecvoob(struct socket *so) |
f0cbd3ec FB |
266 | { |
267 | struct tcpcb *tp = sototcpcb(so); | |
268 | ||
269 | DEBUG_CALL("sorecvoob"); | |
270 | DEBUG_ARG("so = %lx", (long)so); | |
5fafdf24 | 271 | |
f0cbd3ec FB |
272 | /* |
273 | * We take a guess at how much urgent data has arrived. | |
274 | * In most situations, when urgent data arrives, the next | |
275 | * read() should get all the urgent data. This guess will | |
276 | * be wrong however if more data arrives just after the | |
5fafdf24 | 277 | * urgent data, or the read() doesn't return all the |
f0cbd3ec FB |
278 | * urgent data. |
279 | */ | |
280 | soread(so); | |
281 | tp->snd_up = tp->snd_una + so->so_snd.sb_cc; | |
282 | tp->t_force = 1; | |
283 | tcp_output(tp); | |
284 | tp->t_force = 0; | |
285 | } | |
286 | ||
287 | /* | |
288 | * Send urgent data | |
289 | * There's a lot duplicated code here, but... | |
290 | */ | |
291 | int | |
511d2b14 | 292 | sosendoob(struct socket *so) |
f0cbd3ec FB |
293 | { |
294 | struct sbuf *sb = &so->so_rcv; | |
295 | char buff[2048]; /* XXX Shouldn't be sending more oob data than this */ | |
5fafdf24 | 296 | |
f0cbd3ec | 297 | int n, len; |
5fafdf24 | 298 | |
f0cbd3ec FB |
299 | DEBUG_CALL("sosendoob"); |
300 | DEBUG_ARG("so = %lx", (long)so); | |
301 | DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc); | |
5fafdf24 | 302 | |
f0cbd3ec FB |
303 | if (so->so_urgc > 2048) |
304 | so->so_urgc = 2048; /* XXXX */ | |
5fafdf24 | 305 | |
f0cbd3ec FB |
306 | if (sb->sb_rptr < sb->sb_wptr) { |
307 | /* We can send it directly */ | |
e1c5a2b3 | 308 | n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */ |
f0cbd3ec | 309 | so->so_urgc -= n; |
3b46e624 | 310 | |
f0cbd3ec FB |
311 | DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc)); |
312 | } else { | |
5fafdf24 | 313 | /* |
f0cbd3ec FB |
314 | * Since there's no sendv or sendtov like writev, |
315 | * we must copy all data to a linear buffer then | |
316 | * send it all | |
317 | */ | |
318 | len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr; | |
319 | if (len > so->so_urgc) len = so->so_urgc; | |
320 | memcpy(buff, sb->sb_rptr, len); | |
321 | so->so_urgc -= len; | |
322 | if (so->so_urgc) { | |
323 | n = sb->sb_wptr - sb->sb_data; | |
324 | if (n > so->so_urgc) n = so->so_urgc; | |
325 | memcpy((buff + len), sb->sb_data, n); | |
326 | so->so_urgc -= n; | |
327 | len += n; | |
328 | } | |
e1c5a2b3 | 329 | n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */ |
f0cbd3ec FB |
330 | #ifdef DEBUG |
331 | if (n != len) | |
332 | DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n")); | |
3b46e624 | 333 | #endif |
f0cbd3ec FB |
334 | DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc)); |
335 | } | |
5fafdf24 | 336 | |
f0cbd3ec FB |
337 | sb->sb_cc -= n; |
338 | sb->sb_rptr += n; | |
339 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen)) | |
340 | sb->sb_rptr -= sb->sb_datalen; | |
5fafdf24 | 341 | |
f0cbd3ec FB |
342 | return n; |
343 | } | |
344 | ||
345 | /* | |
5fafdf24 | 346 | * Write data from so_rcv to so's socket, |
f0cbd3ec FB |
347 | * updating all sbuf field as necessary |
348 | */ | |
349 | int | |
511d2b14 | 350 | sowrite(struct socket *so) |
f0cbd3ec FB |
351 | { |
352 | int n,nn; | |
353 | struct sbuf *sb = &so->so_rcv; | |
354 | int len = sb->sb_cc; | |
355 | struct iovec iov[2]; | |
5fafdf24 | 356 | |
f0cbd3ec FB |
357 | DEBUG_CALL("sowrite"); |
358 | DEBUG_ARG("so = %lx", (long)so); | |
5fafdf24 | 359 | |
f0cbd3ec FB |
360 | if (so->so_urgc) { |
361 | sosendoob(so); | |
362 | if (sb->sb_cc == 0) | |
363 | return 0; | |
364 | } | |
365 | ||
366 | /* | |
367 | * No need to check if there's something to write, | |
368 | * sowrite wouldn't have been called otherwise | |
369 | */ | |
5fafdf24 | 370 | |
f0cbd3ec | 371 | len = sb->sb_cc; |
5fafdf24 | 372 | |
f0cbd3ec | 373 | iov[0].iov_base = sb->sb_rptr; |
66029f6a BS |
374 | iov[1].iov_base = NULL; |
375 | iov[1].iov_len = 0; | |
f0cbd3ec FB |
376 | if (sb->sb_rptr < sb->sb_wptr) { |
377 | iov[0].iov_len = sb->sb_wptr - sb->sb_rptr; | |
378 | /* Should never succeed, but... */ | |
379 | if (iov[0].iov_len > len) iov[0].iov_len = len; | |
380 | n = 1; | |
381 | } else { | |
382 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr; | |
383 | if (iov[0].iov_len > len) iov[0].iov_len = len; | |
384 | len -= iov[0].iov_len; | |
385 | if (len) { | |
386 | iov[1].iov_base = sb->sb_data; | |
387 | iov[1].iov_len = sb->sb_wptr - sb->sb_data; | |
388 | if (iov[1].iov_len > len) iov[1].iov_len = len; | |
389 | n = 2; | |
390 | } else | |
391 | n = 1; | |
392 | } | |
393 | /* Check if there's urgent data to send, and if so, send it */ | |
394 | ||
395 | #ifdef HAVE_READV | |
396 | nn = writev(so->s, (const struct iovec *)iov, n); | |
5fafdf24 | 397 | |
f0cbd3ec FB |
398 | DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn)); |
399 | #else | |
e1c5a2b3 | 400 | nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0); |
f0cbd3ec FB |
401 | #endif |
402 | /* This should never happen, but people tell me it does *shrug* */ | |
403 | if (nn < 0 && (errno == EAGAIN || errno == EINTR)) | |
404 | return 0; | |
5fafdf24 | 405 | |
f0cbd3ec FB |
406 | if (nn <= 0) { |
407 | DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n", | |
408 | so->so_state, errno)); | |
409 | sofcantsendmore(so); | |
410 | tcp_sockclosed(sototcpcb(so)); | |
411 | return -1; | |
412 | } | |
5fafdf24 | 413 | |
f0cbd3ec | 414 | #ifndef HAVE_READV |
3bc2175d FB |
415 | if (n == 2 && nn == iov[0].iov_len) { |
416 | int ret; | |
e1c5a2b3 | 417 | ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0); |
3bc2175d FB |
418 | if (ret > 0) |
419 | nn += ret; | |
420 | } | |
f0cbd3ec FB |
421 | DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn)); |
422 | #endif | |
5fafdf24 | 423 | |
f0cbd3ec FB |
424 | /* Update sbuf */ |
425 | sb->sb_cc -= nn; | |
426 | sb->sb_rptr += nn; | |
427 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen)) | |
428 | sb->sb_rptr -= sb->sb_datalen; | |
5fafdf24 | 429 | |
f0cbd3ec FB |
430 | /* |
431 | * If in DRAIN mode, and there's no more data, set | |
432 | * it CANTSENDMORE | |
433 | */ | |
434 | if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0) | |
435 | sofcantsendmore(so); | |
5fafdf24 | 436 | |
f0cbd3ec FB |
437 | return nn; |
438 | } | |
439 | ||
440 | /* | |
441 | * recvfrom() a UDP socket | |
442 | */ | |
443 | void | |
511d2b14 | 444 | sorecvfrom(struct socket *so) |
f0cbd3ec FB |
445 | { |
446 | struct sockaddr_in addr; | |
242acf3a | 447 | socklen_t addrlen = sizeof(struct sockaddr_in); |
5fafdf24 | 448 | |
f0cbd3ec FB |
449 | DEBUG_CALL("sorecvfrom"); |
450 | DEBUG_ARG("so = %lx", (long)so); | |
5fafdf24 | 451 | |
f0cbd3ec FB |
452 | if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */ |
453 | char buff[256]; | |
454 | int len; | |
3b46e624 | 455 | |
5fafdf24 | 456 | len = recvfrom(so->s, buff, 256, 0, |
f0cbd3ec FB |
457 | (struct sockaddr *)&addr, &addrlen); |
458 | /* XXX Check if reply is "correct"? */ | |
3b46e624 | 459 | |
f0cbd3ec FB |
460 | if(len == -1 || len == 0) { |
461 | u_char code=ICMP_UNREACH_PORT; | |
462 | ||
463 | if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST; | |
464 | else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET; | |
3b46e624 | 465 | |
f0cbd3ec FB |
466 | DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n", |
467 | errno,strerror(errno))); | |
468 | icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno)); | |
469 | } else { | |
470 | icmp_reflect(so->so_m); | |
511d2b14 | 471 | so->so_m = NULL; /* Don't m_free() it again! */ |
f0cbd3ec FB |
472 | } |
473 | /* No need for this socket anymore, udp_detach it */ | |
474 | udp_detach(so); | |
475 | } else { /* A "normal" UDP packet */ | |
476 | struct mbuf *m; | |
477 | int len, n; | |
478 | ||
479 | if (!(m = m_get())) return; | |
9634d903 | 480 | m->m_data += IF_MAXLINKHDR; |
3b46e624 | 481 | |
5fafdf24 | 482 | /* |
f0cbd3ec FB |
483 | * XXX Shouldn't FIONREAD packets destined for port 53, |
484 | * but I don't know the max packet size for DNS lookups | |
485 | */ | |
486 | len = M_FREEROOM(m); | |
487 | /* if (so->so_fport != htons(53)) { */ | |
379ff53d | 488 | ioctlsocket(so->s, FIONREAD, &n); |
3b46e624 | 489 | |
f0cbd3ec FB |
490 | if (n > len) { |
491 | n = (m->m_data - m->m_dat) + m->m_len + n + 1; | |
492 | m_inc(m, n); | |
493 | len = M_FREEROOM(m); | |
494 | } | |
495 | /* } */ | |
3b46e624 | 496 | |
f0cbd3ec FB |
497 | m->m_len = recvfrom(so->s, m->m_data, len, 0, |
498 | (struct sockaddr *)&addr, &addrlen); | |
5fafdf24 | 499 | DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n", |
f0cbd3ec FB |
500 | m->m_len, errno,strerror(errno))); |
501 | if(m->m_len<0) { | |
502 | u_char code=ICMP_UNREACH_PORT; | |
503 | ||
504 | if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST; | |
505 | else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET; | |
3b46e624 | 506 | |
f0cbd3ec FB |
507 | DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code)); |
508 | icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno)); | |
509 | m_free(m); | |
510 | } else { | |
511 | /* | |
512 | * Hack: domain name lookup will be used the most for UDP, | |
513 | * and since they'll only be used once there's no need | |
514 | * for the 4 minute (or whatever) timeout... So we time them | |
515 | * out much quicker (10 seconds for now...) | |
516 | */ | |
517 | if (so->so_expire) { | |
518 | if (so->so_fport == htons(53)) | |
519 | so->so_expire = curtime + SO_EXPIREFAST; | |
520 | else | |
521 | so->so_expire = curtime + SO_EXPIRE; | |
522 | } | |
523 | ||
524 | /* if (m->m_len == len) { | |
525 | * m_inc(m, MINCSIZE); | |
526 | * m->m_len = 0; | |
527 | * } | |
528 | */ | |
3b46e624 | 529 | |
5fafdf24 | 530 | /* |
f0cbd3ec FB |
531 | * If this packet was destined for CTL_ADDR, |
532 | * make it look like that's where it came from, done by udp_output | |
533 | */ | |
534 | udp_output(so, m, &addr); | |
535 | } /* rx error */ | |
536 | } /* if ping packet */ | |
537 | } | |
538 | ||
539 | /* | |
540 | * sendto() a socket | |
541 | */ | |
542 | int | |
511d2b14 | 543 | sosendto(struct socket *so, struct mbuf *m) |
f0cbd3ec FB |
544 | { |
545 | int ret; | |
546 | struct sockaddr_in addr; | |
547 | ||
548 | DEBUG_CALL("sosendto"); | |
549 | DEBUG_ARG("so = %lx", (long)so); | |
550 | DEBUG_ARG("m = %lx", (long)m); | |
5fafdf24 | 551 | |
f0cbd3ec FB |
552 | addr.sin_family = AF_INET; |
553 | if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) { | |
554 | /* It's an alias */ | |
555 | switch(ntohl(so->so_faddr.s_addr) & 0xff) { | |
556 | case CTL_DNS: | |
557 | addr.sin_addr = dns_addr; | |
558 | break; | |
559 | case CTL_ALIAS: | |
560 | default: | |
561 | addr.sin_addr = loopback_addr; | |
562 | break; | |
563 | } | |
564 | } else | |
565 | addr.sin_addr = so->so_faddr; | |
566 | addr.sin_port = so->so_fport; | |
567 | ||
568 | DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr))); | |
5fafdf24 | 569 | |
f0cbd3ec FB |
570 | /* Don't care what port we get */ |
571 | ret = sendto(so->s, m->m_data, m->m_len, 0, | |
572 | (struct sockaddr *)&addr, sizeof (struct sockaddr)); | |
573 | if (ret < 0) | |
574 | return -1; | |
5fafdf24 | 575 | |
f0cbd3ec FB |
576 | /* |
577 | * Kill the socket if there's no reply in 4 minutes, | |
578 | * but only if it's an expirable socket | |
579 | */ | |
580 | if (so->so_expire) | |
581 | so->so_expire = curtime + SO_EXPIRE; | |
582 | so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */ | |
583 | return 0; | |
584 | } | |
585 | ||
586 | /* | |
587 | * XXX This should really be tcp_listen | |
588 | */ | |
589 | struct socket * | |
511d2b14 | 590 | solisten(u_int port, u_int32_t laddr, u_int lport, int flags) |
f0cbd3ec FB |
591 | { |
592 | struct sockaddr_in addr; | |
593 | struct socket *so; | |
242acf3a AZ |
594 | int s, opt = 1; |
595 | socklen_t addrlen = sizeof(addr); | |
f0cbd3ec FB |
596 | |
597 | DEBUG_CALL("solisten"); | |
598 | DEBUG_ARG("port = %d", port); | |
599 | DEBUG_ARG("laddr = %x", laddr); | |
600 | DEBUG_ARG("lport = %d", lport); | |
601 | DEBUG_ARG("flags = %x", flags); | |
5fafdf24 | 602 | |
f0cbd3ec FB |
603 | if ((so = socreate()) == NULL) { |
604 | /* free(so); Not sofree() ??? free(NULL) == NOP */ | |
605 | return NULL; | |
606 | } | |
5fafdf24 | 607 | |
f0cbd3ec FB |
608 | /* Don't tcp_attach... we don't need so_snd nor so_rcv */ |
609 | if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) { | |
610 | free(so); | |
611 | return NULL; | |
612 | } | |
613 | insque(so,&tcb); | |
5fafdf24 TS |
614 | |
615 | /* | |
f0cbd3ec FB |
616 | * SS_FACCEPTONCE sockets must time out. |
617 | */ | |
618 | if (flags & SS_FACCEPTONCE) | |
619 | so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2; | |
5fafdf24 | 620 | |
f0cbd3ec FB |
621 | so->so_state = (SS_FACCEPTCONN|flags); |
622 | so->so_lport = lport; /* Kept in network format */ | |
623 | so->so_laddr.s_addr = laddr; /* Ditto */ | |
5fafdf24 | 624 | |
f0cbd3ec FB |
625 | addr.sin_family = AF_INET; |
626 | addr.sin_addr.s_addr = INADDR_ANY; | |
627 | addr.sin_port = port; | |
5fafdf24 | 628 | |
f0cbd3ec | 629 | if (((s = socket(AF_INET,SOCK_STREAM,0)) < 0) || |
b55669bf | 630 | (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0) || |
f0cbd3ec FB |
631 | (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) || |
632 | (listen(s,1) < 0)) { | |
633 | int tmperrno = errno; /* Don't clobber the real reason we failed */ | |
3b46e624 | 634 | |
f0cbd3ec FB |
635 | close(s); |
636 | sofree(so); | |
637 | /* Restore the real errno */ | |
02d2c54c FB |
638 | #ifdef _WIN32 |
639 | WSASetLastError(tmperrno); | |
640 | #else | |
f0cbd3ec | 641 | errno = tmperrno; |
02d2c54c | 642 | #endif |
f0cbd3ec FB |
643 | return NULL; |
644 | } | |
f0cbd3ec | 645 | setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int)); |
5fafdf24 | 646 | |
f0cbd3ec FB |
647 | getsockname(s,(struct sockaddr *)&addr,&addrlen); |
648 | so->so_fport = addr.sin_port; | |
649 | if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr) | |
8dbca8dd | 650 | so->so_faddr = alias_addr; |
f0cbd3ec FB |
651 | else |
652 | so->so_faddr = addr.sin_addr; | |
653 | ||
654 | so->s = s; | |
655 | return so; | |
656 | } | |
657 | ||
9634d903 | 658 | #if 0 |
5fafdf24 | 659 | /* |
f0cbd3ec FB |
660 | * Data is available in so_rcv |
661 | * Just write() the data to the socket | |
662 | * XXX not yet... | |
663 | */ | |
9634d903 | 664 | static void |
f0cbd3ec FB |
665 | sorwakeup(so) |
666 | struct socket *so; | |
667 | { | |
668 | /* sowrite(so); */ | |
669 | /* FD_CLR(so->s,&writefds); */ | |
670 | } | |
5fafdf24 | 671 | |
f0cbd3ec FB |
672 | /* |
673 | * Data has been freed in so_snd | |
674 | * We have room for a read() if we want to | |
675 | * For now, don't read, it'll be done in the main loop | |
676 | */ | |
9634d903 | 677 | static void |
f0cbd3ec FB |
678 | sowwakeup(so) |
679 | struct socket *so; | |
680 | { | |
681 | /* Nothing, yet */ | |
682 | } | |
9634d903 | 683 | #endif |
f0cbd3ec FB |
684 | |
685 | /* | |
686 | * Various session state calls | |
687 | * XXX Should be #define's | |
688 | * The socket state stuff needs work, these often get call 2 or 3 | |
689 | * times each when only 1 was needed | |
690 | */ | |
691 | void | |
511d2b14 | 692 | soisfconnecting(struct socket *so) |
f0cbd3ec FB |
693 | { |
694 | so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE| | |
695 | SS_FCANTSENDMORE|SS_FWDRAIN); | |
696 | so->so_state |= SS_ISFCONNECTING; /* Clobber other states */ | |
697 | } | |
698 | ||
699 | void | |
511d2b14 | 700 | soisfconnected(struct socket *so) |
f0cbd3ec FB |
701 | { |
702 | so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF); | |
703 | so->so_state |= SS_ISFCONNECTED; /* Clobber other states */ | |
704 | } | |
705 | ||
9634d903 BS |
706 | static void |
707 | sofcantrcvmore(struct socket *so) | |
f0cbd3ec FB |
708 | { |
709 | if ((so->so_state & SS_NOFDREF) == 0) { | |
710 | shutdown(so->s,0); | |
02d2c54c FB |
711 | if(global_writefds) { |
712 | FD_CLR(so->s,global_writefds); | |
713 | } | |
f0cbd3ec FB |
714 | } |
715 | so->so_state &= ~(SS_ISFCONNECTING); | |
716 | if (so->so_state & SS_FCANTSENDMORE) | |
717 | so->so_state = SS_NOFDREF; /* Don't select it */ /* XXX close() here as well? */ | |
718 | else | |
719 | so->so_state |= SS_FCANTRCVMORE; | |
720 | } | |
721 | ||
9634d903 BS |
722 | static void |
723 | sofcantsendmore(struct socket *so) | |
f0cbd3ec FB |
724 | { |
725 | if ((so->so_state & SS_NOFDREF) == 0) { | |
02d2c54c FB |
726 | shutdown(so->s,1); /* send FIN to fhost */ |
727 | if (global_readfds) { | |
728 | FD_CLR(so->s,global_readfds); | |
729 | } | |
730 | if (global_xfds) { | |
731 | FD_CLR(so->s,global_xfds); | |
732 | } | |
f0cbd3ec FB |
733 | } |
734 | so->so_state &= ~(SS_ISFCONNECTING); | |
735 | if (so->so_state & SS_FCANTRCVMORE) | |
736 | so->so_state = SS_NOFDREF; /* as above */ | |
737 | else | |
738 | so->so_state |= SS_FCANTSENDMORE; | |
739 | } | |
740 | ||
741 | void | |
511d2b14 | 742 | soisfdisconnected(struct socket *so) |
f0cbd3ec FB |
743 | { |
744 | /* so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */ | |
745 | /* close(so->s); */ | |
746 | /* so->so_state = SS_ISFDISCONNECTED; */ | |
747 | /* | |
748 | * XXX Do nothing ... ? | |
749 | */ | |
750 | } | |
751 | ||
752 | /* | |
753 | * Set write drain mode | |
754 | * Set CANTSENDMORE once all data has been write()n | |
755 | */ | |
756 | void | |
511d2b14 | 757 | sofwdrain(struct socket *so) |
f0cbd3ec FB |
758 | { |
759 | if (so->so_rcv.sb_cc) | |
760 | so->so_state |= SS_FWDRAIN; | |
761 | else | |
762 | sofcantsendmore(so); | |
763 | } |