FACT++  1.0
webTcpip.c
Go to the documentation of this file.
1 #include <errno.h>
2 #include <dim.h>
3 #include <ctype.h>
4 #ifndef WIN32
5 #include <netdb.h>
6 #endif
7 
10 
11 static 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 
59 static void read_data( int conn_id)
60 {
61  register DNA_CONNECTION *dna_connp = &Dna_conns[conn_id];
62 
63 /*
64 printf("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 
70 static 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);
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 
149 int 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 
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 
199 int 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 
243 void web_write(int conn_id, char *buffer, int size)
244 {
245  tcpip_write( conn_id, buffer, size );
246 }
247 
248 static 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 
269 int web_close(int conn_id)
270 {
271  if(conn_id > 0)
272  {
273  release_conn(conn_id);
274  }
275  return(1);
276 }
277 
278 int 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 }
#define STA_DATA
Definition: dim.h:198
int web_open_server(char *task, void(*read_ast)(), int *protocol, int *port, void(*error_ast)())
Definition: webTcpip.c:199
Definition: dns.c:26
int tcpip_start_listen(int conn_id, void(*ast_routine)())
Definition: tcpip.c:1308
void web_write(int conn_id, char *buffer, int size)
Definition: webTcpip.c:243
static void ast_conn_h(int handle, int svr_conn_id, int protocol)
Definition: webTcpip.c:11
int Tcpip_max_io_data_read
Definition: webTcpip.c:9
int protocol
Definition: dim.h:389
#define STA_DISC
Definition: dim.h:197
static void read_data(int conn_id)
Definition: webTcpip.c:59
#define TCP_SND_BUF_SIZE
Definition: dim.h:166
int dna_open_client(char *server_node, char *server_task, int port, int server_protocol, void(*read_ast)(), void(*error_ast)(), SRC_TYPES src_type)
Definition: dna.c:733
dim_get_env_var("EXTRA_DNS_NODE", extra_dns, sizeof(extra_dns))
#define TCPIP
Definition: dim.h:194
int busy
Definition: dim.h:381
#define FALSE
Definition: dim.h:136
int dna_close(int conn_id)
Definition: dna.c:835
void conn_free(int conn_id)
Definition: conn_handler.c:118
void * malloc()
Definition: EventBuilder.cc:99
#define DIMTCPRDERR
Definition: dim_common.h:293
int web_close(int conn_id)
Definition: webTcpip.c:269
int web_get_port()
Definition: webTcpip.c:185
int Tcpip_max_io_data_write
Definition: webTcpip.c:8
#define TCP_RCV_BUF_SIZE
Definition: dim.h:165
void(* error_ast)()
Definition: dim.h:383
CONN_STATE state
Definition: dim.h:390
#define DIMTCPCNERR
Definition: dim_common.h:298
int curr_size
Definition: dim.h:387
int writing
Definition: dim.h:391
int tcpip_start_read(int conn_id, char *buffer, int size, void(*ast_routine)())
Definition: tcpip.c:949
int tcpip_close(int conn_id)
Definition: tcpip.c:1510
void dna_report_error(int conn_id, int code, char *routine_name, int severity, int errcode)
Definition: dna.c:903
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
int size
Definition: db_dim_server.c:17
int tcpip_write(int conn_id, char *buffer, int size)
Definition: tcpip.c:1389
void free(void *mem)
int saw_init
Definition: dim.h:392
DllExp DIM_NOSHARE DNA_CONNECTION * Dna_conns
Definition: conn_handler.c:31
int web_get_node_name(char *node, char *name)
Definition: webTcpip.c:278
#define DIMTCPLNERR
Definition: dim_common.h:296
int full_size
Definition: dim.h:388
int web_start_read(int conn_id, int size)
Definition: webTcpip.c:149
int * buffer
Definition: dim.h:384
int tcpip_open_server(int conn_id, char *task, int *port)
Definition: tcpip.c:1182
Definition: dim.h:190
int dna_start_read(int conn_id, int size)
Definition: dna.c:234
static void ast_read_h(int conn_id, int status, int size)
Definition: webTcpip.c:70
static void release_conn(int conn_id)
Definition: webTcpip.c:248
int buffer_size
Definition: dim.h:385
int tcpip_failure(int code)
Definition: tcpip.c:1540
void(* read_ast)()
Definition: dim.h:382
#define STA_CONN
Definition: dim.h:199
int tcpip_open_connection(int conn_id, int path)
Definition: tcpip.c:1332
#define DIMTCPOPERR
Definition: dim_common.h:297
char * curr_buffer
Definition: dim.h:386
int conn_get()
Definition: conn_handler.c:79