source: trunk/FACT++/dim/src/webDid/webTcpip.c@ 18679

Last change on this file since 18679 was 15282, checked in by tbretz, 12 years ago
Updated to v20r7.
File size: 6.7 KB
Line 
1#include <errno.h>
2#include <dim.h>
3#include <ctype.h>
4#ifndef WIN32
5#include <netdb.h>
6#endif
7
8int Tcpip_max_io_data_write = TCP_SND_BUF_SIZE - 16;
9int Tcpip_max_io_data_read = TCP_RCV_BUF_SIZE - 16;
10
11static void ast_conn_h(int handle, int svr_conn_id, int protocol)
12{
13 register DNA_CONNECTION *dna_connp;
14 register int tcpip_code;
15 register int conn_id;
16 int web_start_read();
17
18 if(protocol){}
19 conn_id = conn_get();
20/*
21 if(!conn_id)
22 dim_panic("In ast_conn_h: No more connections\n");
23*/
24 dna_connp = &Dna_conns[conn_id] ;
25 dna_connp->error_ast = Dna_conns[svr_conn_id].error_ast;
26 tcpip_code = tcpip_open_connection( conn_id, handle );
27
28 if(tcpip_failure(tcpip_code))
29 {
30 dna_report_error(conn_id, tcpip_code,
31 "Connecting to", DIM_ERROR, DIMTCPCNERR);
32 conn_free(conn_id);
33 } else {
34 dna_connp->state = RD_HDR;
35 dna_connp->buffer = (int *)malloc(TCP_RCV_BUF_SIZE);
36 memset(dna_connp->buffer, 0, TCP_RCV_BUF_SIZE);
37/*
38 if(!dna_connp->buffer)
39 {
40 printf("Error in DNA - handle_connection malloc returned 0\n");
41 fflush(stdout);
42 }
43*/
44 dna_connp->buffer_size = TCP_RCV_BUF_SIZE;
45 dna_connp->read_ast = Dna_conns[svr_conn_id].read_ast;
46 dna_connp->saw_init = FALSE;
47 web_start_read(conn_id, TCP_RCV_BUF_SIZE); /* sizeof(DNA_NET) */
48 /* Connection arrived. Signal upper layer ? */
49 dna_connp->read_ast(conn_id, NULL, 0, STA_CONN);
50 }
51 tcpip_code = tcpip_start_listen(svr_conn_id, ast_conn_h);
52 if(tcpip_failure(tcpip_code))
53 {
54 dna_report_error(svr_conn_id, tcpip_code,
55 "Listening at", DIM_ERROR, DIMTCPLNERR);
56 }
57}
58
59static void read_data( int conn_id)
60{
61 register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id];
62
63/*
64printf("passing up %d bytes, conn_id %d\n",dna_connp->full_size, conn_id);
65*/
66 dna_connp->read_ast(conn_id, dna_connp->buffer,
67 dna_connp->full_size, STA_DATA);
68}
69
70static void ast_read_h( int conn_id, int status, int size )
71{
72 register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id];
73
74 if(!dna_connp->buffer) /* The connection has already been closed */
75 {
76 return;
77 }
78 if(status == 1)
79 {
80/*
81 next_size = dna_connp->curr_size;
82 buff = (char *) dna_connp->curr_buffer;
83 if(size < next_size)
84 {
85 max_io_data = Tcpip_max_io_data_read;
86 read_size = ((next_size - size) > max_io_data) ?
87 max_io_data : next_size - size;
88 dna_connp->curr_size -= size;
89 dna_connp->curr_buffer += size;
90 tcpip_code = tcpip_start_read(conn_id, buff + size,
91 read_size, ast_read_h);
92 if(tcpip_failure(tcpip_code))
93 {
94#ifndef WIN32
95 if(errno == ENOTSOCK)
96 {
97 if(dna_connp->read_ast)
98 dna_connp->read_ast(conn_id, NULL, 0, STA_DISC);
99 }
100 else
101#endif
102 {
103 dna_report_error(conn_id, tcpip_code,
104 "Reading from", DIM_ERROR, DIMTCPRDERR);
105 }
106 }
107 return;
108 }
109 switch(dna_connp->state)
110 {
111 case RD_HDR :
112 if(is_header(conn_id))
113 {
114 if( dna_connp->state == RD_DATA )
115 {
116 next_size = vtohl(dna_connp->buffer[1]);
117 dna_start_read(conn_id, next_size);
118 }
119 else
120 {
121 dna_connp->state = RD_HDR;
122 dna_start_read(conn_id, READ_HEADER_SIZE);
123 }
124 }
125 break;
126 case RD_DATA :
127 read_data(conn_id);
128 dna_connp->state = RD_HDR;
129 dna_start_read(conn_id, READ_HEADER_SIZE);
130 break;
131 default:
132 break;
133 }
134*/
135 dna_connp->full_size = size;
136 read_data(conn_id);
137 dna_start_read(conn_id, TCP_RCV_BUF_SIZE);
138 }
139 else
140 {
141 /*
142 printf("Connection lost. Signal upper layer\n");
143 */
144 if(dna_connp->read_ast)
145 dna_connp->read_ast(conn_id, NULL, 0, STA_DISC);
146 }
147}
148
149int web_start_read(int conn_id, int size)
150{
151 register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id];
152 register int tcpip_code, read_size;
153 int max_io_data;
154
155 if(!dna_connp->busy)
156 {
157 return(0);
158 }
159
160 dna_connp->curr_size = size;
161 dna_connp->full_size = size;
162 if(size > dna_connp->buffer_size)
163 {
164 dna_connp->buffer =
165 (int *) realloc(dna_connp->buffer, (size_t)size);
166 memset(dna_connp->buffer, 0, (size_t)size);
167 dna_connp->buffer_size = size;
168 }
169 dna_connp->curr_buffer = (char *) dna_connp->buffer;
170 max_io_data = Tcpip_max_io_data_read;
171 read_size = (size > max_io_data) ? max_io_data : size ;
172
173 tcpip_code = tcpip_start_read(conn_id, dna_connp->curr_buffer,
174 read_size, ast_read_h);
175 if(tcpip_failure(tcpip_code)) {
176 dna_report_error(conn_id, tcpip_code,
177 "Reading from", DIM_ERROR, DIMTCPRDERR);
178
179 return(0);
180 }
181
182 return(1);
183}
184
185int web_get_port()
186{
187 int ret;
188 char ports[64];
189 int port = 2500;
190
191 ret = dim_get_env_var("DIM_DID_PORT", ports, 64);
192 if(ret)
193 {
194 sscanf(ports,"%d",&port);
195 }
196 return port;
197}
198
199int web_open_server(char *task, void (*read_ast)(), int *protocol, int *port, void (*error_ast)())
200{
201 register DNA_CONNECTION *dna_connp;
202 register int tcpip_code;
203 register int conn_id;
204
205 conn_id = dna_open_client("", "", 0, 0,
206 0, 0, 0);
207 dna_close(conn_id);
208/*
209 if(!DNA_Initialized)
210 {
211 conn_arr_create(SRC_DNA);
212 DNA_Initialized = TRUE;
213 }
214*/
215 *protocol = PROTOCOL;
216 conn_id = conn_get();
217 dna_connp = &Dna_conns[conn_id];
218/*
219 if(!conn_id)
220 dim_panic("In dna_open_server: No more connections\n");
221*/
222 dna_connp->protocol = TCPIP;
223 dna_connp->error_ast = error_ast;
224 tcpip_code = tcpip_open_server(conn_id, task, port);
225 if(tcpip_failure(tcpip_code))
226 {
227 dna_report_error(conn_id, tcpip_code,
228 "Opening server port", DIM_ERROR, DIMTCPOPERR);
229 conn_free(conn_id);
230 return(0);
231 }
232 dna_connp->writing = FALSE;
233 dna_connp->read_ast = read_ast;
234 tcpip_code = tcpip_start_listen(conn_id, ast_conn_h);
235 if(tcpip_failure(tcpip_code))
236 {
237 dna_report_error(conn_id, tcpip_code, "Listening at", DIM_ERROR, DIMTCPLNERR);
238 return(0);
239 }
240 return(conn_id);
241}
242
243void web_write(int conn_id, char *buffer, int size)
244{
245 tcpip_write( conn_id, buffer, size );
246}
247
248static void release_conn(int conn_id)
249{
250 register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id] ;
251
252 DISABLE_AST
253 if(dna_connp->busy)
254 {
255 tcpip_close(conn_id);
256 if(dna_connp->buffer)
257 {
258 free(dna_connp->buffer);
259 dna_connp->buffer = 0;
260 dna_connp->buffer_size = 0;
261 }
262 dna_connp->read_ast = NULL;
263 dna_connp->error_ast = NULL;
264 conn_free(conn_id);
265 }
266 ENABLE_AST
267}
268
269int web_close(int conn_id)
270{
271 if(conn_id > 0)
272 {
273 release_conn(conn_id);
274 }
275 return(1);
276}
277
278int web_get_node_name(char *node, char *name)
279{
280 int a,b,c,d;
281/* Fix for gcc 4.6 "dereferencing type-punned pointer will break strict-aliasing rules"?!*/
282 unsigned char ipaddr_buff[4];
283 unsigned char *ipaddr = ipaddr_buff;
284 struct hostent *host;
285
286 strcpy(name, node);
287 if(isdigit(node[0]))
288 {
289 sscanf(node,"%d.%d.%d.%d",&a, &b, &c, &d);
290 ipaddr[0] = (unsigned char)a;
291 ipaddr[1] = (unsigned char)b;
292 ipaddr[2] = (unsigned char)c;
293 ipaddr[3] = (unsigned char)d;
294 if( (host = gethostbyaddr(ipaddr, sizeof(ipaddr), AF_INET)) == (struct hostent *)0 )
295 {
296 return(0);
297 }
298 else
299 {
300 strcpy(name,host->h_name);
301 return(1);
302 }
303 }
304 return(0);
305}
Note: See TracBrowser for help on using the repository browser.