author | viric@llimona |
Mon, 24 Sep 2007 13:59:40 +0200 | |
changeset 38 | f1e581c862d5 |
parent 37 | b0588bddef91 |
child 53 | 07500c5c53cb |
permissions | -rw-r--r-- |
33 | 1 |
#include <stdlib.h> |
2 |
#include <assert.h> |
|
3 |
#include <unistd.h> |
|
4 |
#include <stdio.h> |
|
5 |
#include <netinet/in.h> |
|
34 | 6 |
#include <string.h> |
33 | 7 |
#include <sys/socket.h> |
8 |
#include <sys/select.h> |
|
9 |
||
10 |
#include "main.h" |
|
11 |
#include "handlers.h" |
|
12 |
||
13 |
static int served_sockets = 0; |
|
14 |
||
15 |
static int listen_socket = -1; /* not listening */ |
|
16 |
static int connected_sockets = 0; |
|
17 |
static int *conn_sockets = 0; |
|
18 |
||
19 |
static void start_listening(int new) |
|
20 |
{ |
|
21 |
int ls; |
|
22 |
struct sockaddr_in addr; |
|
23 |
int res; |
|
24 |
||
25 |
assert(new > 0); |
|
26 |
||
27 |
ls = socket(AF_INET, SOCK_STREAM, 0); |
|
28 |
if (ls == -1) |
|
29 |
error("Cannot create the tcp listen socket in the server"); |
|
30 |
||
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
31 |
{ |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
32 |
int on = 1; |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
33 |
res = setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
34 |
if (res == -1) |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
35 |
error("Cannot set SO_REUSEADDR"); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
36 |
} |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
37 |
|
33 | 38 |
addr.sin_family = AF_INET; |
39 |
addr.sin_port = htons(command_line.tcp_port); |
|
40 |
addr.sin_addr.s_addr = htonl(INADDR_ANY); |
|
41 |
||
42 |
res = bind(ls, (struct sockaddr *) & addr, sizeof(addr)); |
|
43 |
if (res == -1) |
|
44 |
error("Error binding tcp to port %i", command_line.tcp_port); |
|
45 |
||
46 |
/* NUANCE: 0 backlog. Why should we assure future connections? */ |
|
47 |
res = listen(ls, 0); |
|
48 |
if (res == -1) |
|
49 |
error("Error listening on the binded tcp socket"); |
|
50 |
||
51 |
listen_socket = ls; |
|
52 |
} |
|
53 |
||
54 |
void s_tcp_update_served(int new) |
|
55 |
{ |
|
56 |
if (new > served_sockets && new > 0 ) |
|
57 |
{ |
|
58 |
conn_sockets = realloc(conn_sockets, sizeof(*conn_sockets) * new); |
|
59 |
if (listen_socket == -1) /* not listening */ |
|
60 |
{ |
|
61 |
start_listening(new); |
|
62 |
} |
|
63 |
served_sockets = new; |
|
64 |
} |
|
65 |
else if (new < served_sockets) |
|
66 |
{ |
|
67 |
not_implemented("new < served_sockets at s_tcp_update_served"); |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
void s_tcp_shutdown() |
|
72 |
{ |
|
73 |
int i; |
|
74 |
||
75 |
if (listen_socket != -1) |
|
76 |
{ |
|
77 |
close(listen_socket); |
|
78 |
} |
|
79 |
||
80 |
for (i=0; i < connected_sockets; ++i) |
|
81 |
{ |
|
82 |
close(conn_sockets[i]); |
|
83 |
} |
|
84 |
connected_sockets = 0; |
|
85 |
served_sockets = 0; |
|
86 |
} |
|
87 |
||
88 |
void s_tcp_prepare_read_fdset(fd_set *read_set, int *maxfd) |
|
89 |
{ |
|
90 |
int i; |
|
91 |
||
37
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
92 |
FD_SET(listen_socket, read_set); |
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
93 |
*maxfd = max(*maxfd, listen_socket); |
33 | 94 |
|
95 |
for (i=0; i < connected_sockets; ++i) |
|
96 |
{ |
|
97 |
/* We only accept if we don't have any |
|
98 |
* connetion opened. */ |
|
99 |
FD_SET(conn_sockets[i], read_set); |
|
100 |
*maxfd = max(*maxfd, conn_sockets[i]); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
static void remove_conn_socket(int n) |
|
105 |
{ |
|
106 |
int i; |
|
107 |
||
108 |
assert(n >= 0); |
|
109 |
||
110 |
for(i=n+1; i<connected_sockets; ++i) |
|
111 |
{ |
|
112 |
conn_sockets[i-1] = conn_sockets[i]; |
|
113 |
} |
|
114 |
||
115 |
connected_sockets -= 1; |
|
116 |
||
117 |
/* We shrink the memory until we have an array for served_sockets */ |
|
118 |
if (served_sockets <= connected_sockets) |
|
119 |
{ |
|
120 |
conn_sockets = realloc(conn_sockets, sizeof(*conn_sockets) |
|
121 |
* connected_sockets); |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
static int accept_connection(int ls) |
|
126 |
{ |
|
127 |
int cs; |
|
128 |
cs = accept(ls, 0, 0); |
|
129 |
if (cs == -1) |
|
130 |
not_implemented("accept tcp socket error check"); |
|
131 |
||
132 |
/* TODO: Prepare unblocking sockets if needed */ |
|
133 |
||
134 |
return cs; |
|
135 |
} |
|
136 |
||
137 |
void s_tcp_process_read_fdset(fd_set *read_set) |
|
138 |
{ |
|
139 |
int i; |
|
140 |
/* Active streams */ |
|
141 |
for (i=0; i < connected_sockets; ++i) |
|
142 |
{ |
|
143 |
if (FD_ISSET(conn_sockets[i], read_set)) |
|
144 |
{ |
|
145 |
int res; |
|
146 |
res = read(conn_sockets[i], stream_buffer, |
|
147 |
stream_buffer_size); |
|
148 |
if (res == 0) |
|
149 |
{ |
|
150 |
close(conn_sockets[i]); |
|
151 |
remove_conn_socket(i); |
|
152 |
} else |
|
153 |
{ |
|
154 |
app_control_remote_send_to_stdin(stream_buffer, res); |
|
155 |
} |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
/* Listen connection */ |
|
37
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
160 |
if (FD_ISSET(listen_socket, read_set)) |
33 | 161 |
{ |
37
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
162 |
if (served_sockets > connected_sockets) |
33 | 163 |
{ |
36
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
164 |
int s; |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
165 |
s = accept_connection(listen_socket); |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
166 |
conn_sockets[connected_sockets++] = s; |
da427c23d755
Added dumps, telnet_filter, applied filters in tm, improved telnet experience.
viric@llimona
parents:
34
diff
changeset
|
167 |
welcome_new_client_socket(s); |
37
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
168 |
} else |
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
169 |
{ |
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
170 |
int s; |
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
171 |
s = accept_connection(listen_socket); |
b0588bddef91
Option for xterm resizing. Over max connections, close() at once.
viric@llimona
parents:
36
diff
changeset
|
172 |
close(s); |
33 | 173 |
} |
174 |
} |
|
175 |
} |
|
176 |
||
177 |
void s_tcp_send_to_connected(const char *buffer, size_t size) |
|
178 |
{ |
|
179 |
int i; |
|
180 |
||
181 |
for (i=0; i < connected_sockets; ++i) |
|
182 |
{ |
|
183 |
write(conn_sockets[i], buffer, size); |
|
184 |
} |
|
185 |
} |