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(const char *cp, struct in_addr *ia)
|
---|
14 | {
|
---|
15 | u_int32_t addr = inet_addr(cp);
|
---|
16 | if (addr == 0xffffffff)
|
---|
17 | return 0;
|
---|
18 | ia->s_addr = addr;
|
---|
19 | return 1;
|
---|
20 | }
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * Get our IP address and put it in our_addr
|
---|
25 | */
|
---|
26 | void
|
---|
27 | getouraddr(PNATState pData)
|
---|
28 | {
|
---|
29 | our_addr.s_addr = loopback_addr.s_addr;
|
---|
30 | }
|
---|
31 |
|
---|
32 | struct quehead
|
---|
33 | {
|
---|
34 | struct quehead *qh_link;
|
---|
35 | struct quehead *qh_rlink;
|
---|
36 | };
|
---|
37 |
|
---|
38 | void
|
---|
39 | insque(PNATState pData, void *a, void *b)
|
---|
40 | {
|
---|
41 | register struct quehead *element = (struct quehead *) a;
|
---|
42 | register struct quehead *head = (struct quehead *) b;
|
---|
43 | element->qh_link = head->qh_link;
|
---|
44 | head->qh_link = (struct quehead *)element;
|
---|
45 | element->qh_rlink = (struct quehead *)head;
|
---|
46 | ((struct quehead *)(element->qh_link))->qh_rlink = (struct quehead *)element;
|
---|
47 | }
|
---|
48 |
|
---|
49 | void
|
---|
50 | remque(PNATState pData, void *a)
|
---|
51 | {
|
---|
52 | register struct quehead *element = (struct quehead *) a;
|
---|
53 | ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
|
---|
54 | ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
|
---|
55 | element->qh_rlink = NULL;
|
---|
56 | /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
|
---|
57 | }
|
---|
58 |
|
---|
59 | int
|
---|
60 | add_exec(struct ex_list **ex_ptr, int do_pty, char *exec, int addr, int port)
|
---|
61 | {
|
---|
62 | struct ex_list *tmp_ptr;
|
---|
63 |
|
---|
64 | /* First, check if the port is "bound" */
|
---|
65 | for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next)
|
---|
66 | {
|
---|
67 | if (port == tmp_ptr->ex_fport && addr == tmp_ptr->ex_addr)
|
---|
68 | return -1;
|
---|
69 | }
|
---|
70 |
|
---|
71 | tmp_ptr = *ex_ptr;
|
---|
72 | *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
|
---|
73 | (*ex_ptr)->ex_fport = port;
|
---|
74 | (*ex_ptr)->ex_addr = addr;
|
---|
75 | (*ex_ptr)->ex_pty = do_pty;
|
---|
76 | (*ex_ptr)->ex_exec = strdup(exec);
|
---|
77 | (*ex_ptr)->ex_next = tmp_ptr;
|
---|
78 | return 0;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Set fd blocking and non-blocking
|
---|
84 | */
|
---|
85 | void
|
---|
86 | fd_nonblock(int fd)
|
---|
87 | {
|
---|
88 | #ifdef FIONBIO
|
---|
89 | int opt = 1;
|
---|
90 |
|
---|
91 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
92 | #else
|
---|
93 | int opt;
|
---|
94 |
|
---|
95 | opt = fcntl(fd, F_GETFL, 0);
|
---|
96 | opt |= O_NONBLOCK;
|
---|
97 | fcntl(fd, F_SETFL, opt);
|
---|
98 | #endif
|
---|
99 | }
|
---|
100 |
|
---|
101 | void
|
---|
102 | fd_block(int fd)
|
---|
103 | {
|
---|
104 | #ifdef FIONBIO
|
---|
105 | int opt = 0;
|
---|
106 |
|
---|
107 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
108 | #else
|
---|
109 | int opt;
|
---|
110 |
|
---|
111 | opt = fcntl(fd, F_GETFL, 0);
|
---|
112 | opt &= ~O_NONBLOCK;
|
---|
113 | fcntl(fd, F_SETFL, opt);
|
---|
114 | #endif
|
---|
115 | }
|
---|
116 |
|
---|