29 #if !defined(EXCLUDE_EAI)
31 #include <sys/socket.h>
32 #include <asm-generic/fcntl.h>
35 #include <system_net.h>
41 #include <libFreeWRL.h>
47 #include "SCKHeaders.h"
65 int service_connected[MAX_SERVICE_CHANNELS] ;
66 int service_failed[MAX_SERVICE_CHANNELS] ;
67 int service_onclose[MAX_SERVICE_CHANNELS] ;
68 int service_wanted[MAX_SERVICE_CHANNELS] ;
69 int service_verbose[MAX_SERVICE_CHANNELS] ;
70 int service_status[MAX_SERVICE_CHANNELS] ;
73 unsigned char loopFlags = 0;
76 NO_CLIENT_CONNECTED = 0x1,
78 NO_RETVAL_CHANGE = 0x4
82 struct sockaddr_in servaddr, cliaddr;
85 int SCK_descriptors[MAX_SERVICE_CHANNELS][2] ;
86 int SCK_port[MAX_SERVICE_CHANNELS] ;
89 char *sock_buffers[MAX_SERVICE_CHANNELS] ;
90 int sock_bufcount[MAX_SERVICE_CHANNELS] ;
91 int sock_bufsize[MAX_SERVICE_CHANNELS] ;
96 int privSocketSetup(
int channel,
int *sockfd,
int *listenfd);
97 char *privSocketRead(
int channel,
char *bf,
int *bfct,
int *bfsz,
int *listenfd);
103 int fwlio_RxTx_control(
int channel,
int action) {
104 static int first_time = 1 ;
105 static int service_status[MAX_SERVICE_CHANNELS] ;
106 static int service_justSink[MAX_SERVICE_CHANNELS] ;
108 if (service_verbose[channel] > 1) {
109 printf (
"fwlio_RxTx_control(%d,...) START: called with action code %d\n", channel,action);
110 printf (
"fwlio_RxTx_control: service_status[i] = %d\n", service_status[channel]) ;
111 printf (
"fwlio_RxTx_control: service_justSink[i] = %d\n", service_justSink[channel]) ;
112 printf (
"fwlio_RxTx_control: service_wanted[i] = %d\n", service_wanted[channel]) ;
113 printf (
"fwlio_RxTx_control: service_connected[i] = %d\n", service_connected[channel]) ;
114 printf (
"fwlio_RxTx_control: service_failed[i] = %d\n", service_failed[channel]) ;
115 printf (
"fwlio_RxTx_control: service_onclose[i] = %d\n", service_onclose[channel]) ;
116 printf (
"fwlio_RxTx_control: SCK_descriptors[i][0] = %d\n", SCK_descriptors[channel][0]) ;
117 printf (
"fwlio_RxTx_control: SCK_descriptors[i][1] = %d\n", SCK_descriptors[channel][1]) ;
118 printf (
"fwlio_RxTx_control: SCK_port[i] = %d\n", SCK_port[channel]) ;
120 if (first_time != 0 ) {
122 for (i=0; i<MAX_SERVICE_CHANNELS; i++) {
123 service_status[i] = RxTx_STOP ;
124 service_justSink[i] = 0 ;
125 service_wanted[i] = FALSE;
126 service_connected[i] = FALSE;
127 service_failed[i] = FALSE;
128 service_onclose[i] = FALSE ;
129 service_verbose[i] = SOCKET_SERVICE_DEFAULT_VERBOSE ;
130 SCK_descriptors[i][0] = -1 ;
131 SCK_descriptors[i][1] = -1 ;
134 SCK_port[CHANNEL_EAI] = EAIBASESOCKET ;
143 if( RxTx_STOP == action && service_status[channel] != RxTx_STOP) {
144 if (service_verbose[channel]) {
145 printf (
"Shutting down the socket on logical channel %d\n",channel);
148 if (service_connected[channel] && channel == CHANNEL_EAI) {
149 char *tmpstr = MALLOC(
void *, 10);
150 strcpy(tmpstr,
"QUIT\n\n\n");
151 fwlio_RxTx_sendbuffer(__FILE__,__LINE__,channel, tmpstr);
154 service_status[channel]=RxTx_STOP;
156 if( RxTx_START == action) {
159 if (service_wanted[channel] && service_status[channel] != RxTx_STOP)
return RxTx_START;
162 service_wanted[channel] = TRUE;
165 if (!service_connected[channel]) {
167 int EAIlistenfd = -1;
168 service_failed[channel] = !(privSocketSetup(channel, &EAIsockfd, &EAIlistenfd));
169 if(service_failed[channel]) {
172 SCK_descriptors[channel][MAINSOCK_FD] = EAIsockfd ;
173 SCK_descriptors[channel][CLIENT_FD] = EAIlistenfd ;
174 service_status[channel] = RxTx_REFRESH;
178 if( RxTx_REFRESH == action) {
179 int EAIsockfd, EAIlistenfd;
184 if (!service_wanted[channel])
return 0;
186 EAIsockfd = SCK_descriptors[channel][MAINSOCK_FD] ;
187 EAIlistenfd = SCK_descriptors[channel][CLIENT_FD] ;
188 if (!service_connected[channel]) {
189 service_failed[channel] = !(privSocketSetup(channel,&EAIsockfd,&EAIlistenfd));
190 if(service_failed[channel]) {
193 SCK_descriptors[channel][CLIENT_FD] = EAIlistenfd ;
194 service_status[channel] = RxTx_REFRESH;
198 JAS - service_connected is a boolean, how can it be greater than 1?
199 JAS
if (!(service_connected[channel] > 1)) {
200 JAS
if (service_verbose[channel]) {
201 JAS printf (
"Still no client connection on channel %d\n",channel);
208 if(SCK_descriptors[channel][CLIENT_FD] < 0)
return 0;
210 E_SOCK_bufcount = sock_bufcount[channel];
211 E_SOCK_bufsize = sock_bufsize[channel] ;
212 E_SOCK_buffer = sock_buffers[channel] ;
215 sock_buffers[channel] = privSocketRead(channel,E_SOCK_buffer,&E_SOCK_bufcount, &E_SOCK_bufsize, &EAIlistenfd);
216 if (service_verbose[channel] > 1) {
217 printf (
"Buffer(%d) addr %p\n",channel,sock_buffers[channel] );
220 if(service_justSink[channel] == 1) E_SOCK_bufcount = 0;
221 sock_bufcount[channel] = E_SOCK_bufcount ;
222 sock_bufsize[channel] = E_SOCK_bufsize ;
225 sock_buffers[channel][sock_bufcount[channel]] = 0;
227 if (service_verbose[channel] > 0) {
228 if (sock_bufcount[channel]) {
229 printf (
"fwlio_RxTx_control: sock_bufcount[%d] = %d\nData is :%s:\n\n",
231 sock_bufcount[channel],
232 sock_buffers[channel]);
235 return sock_bufcount[channel] ;
238 if( RxTx_STOP_IF_DISCONNECT == action) {
239 service_onclose[channel] = TRUE ;
241 if( RxTx_EMPTY == action) {
242 sock_bufcount[channel] = 0;
243 sock_buffers[channel][sock_bufcount[channel]] = 0;
245 if( RxTx_MOREVERBOSE == action) {
246 service_verbose[channel] += 1 ;
248 if( RxTx_GET_VERBOSITY == action) {
249 return service_verbose[channel] ;
251 if( RxTx_SILENT == action) {
252 service_verbose[channel] = 0 ;
254 if( RxTx_SINK == action) {
255 service_justSink[channel] = 1;
257 if( RxTx_PENDING == action) {
259 if (service_verbose[channel] > 0) {
260 printf (
"fwlio_RxTx_control: sock_bufcount[%d] (addr %p) = %d\n",
261 channel, sock_buffers[channel], sock_bufcount[channel]) ;
263 return sock_bufcount[channel] ;
265 if( RxTx_STATE == action) {
266 return service_status[channel] ;
268 if (service_verbose[channel] > 1) {
269 printf (
"fwlio_RxTx_control() END:\n");
270 printf (
"fwlio_RxTx_control: service_status[i] = %d\n", service_status[channel]) ;
271 printf (
"fwlio_RxTx_control: service_justSink[i] = %d\n", service_justSink[channel]) ;
272 printf (
"fwlio_RxTx_control: service_wanted[i] = %d\n", service_wanted[channel]) ;
273 printf (
"fwlio_RxTx_control: service_connected[i] = %d\n", service_connected[channel]) ;
274 printf (
"fwlio_RxTx_control: service_failed[i] = %d\n", service_failed[channel]) ;
275 printf (
"fwlio_RxTx_control: service_onclose[i] = %d\n", service_onclose[channel]) ;
276 printf (
"fwlio_RxTx_control: SCK_descriptors[i][0] = %d\n", SCK_descriptors[channel][0]) ;
277 printf (
"fwlio_RxTx_control: SCK_descriptors[i][1] = %d\n", SCK_descriptors[channel][1]) ;
278 printf (
"fwlio_RxTx_control: SCK_port[i] = %d\n", SCK_port[channel]) ;
279 printf (
"fwlio_RxTx_control: sock_bufcount[%d] (addr %p) = length %d\n\n",
280 channel, sock_buffers[channel], sock_bufcount[channel]) ;
286 char * fwlio_RxTx_waitfor(
int channel,
char *str) {
287 return strstr(sock_buffers[channel], str);
290 char *fwlio_RxTx_getbuffer(
int channel) {
292 char *tmpStr = MALLOC(
char *, sock_bufcount[channel] + 1);
293 if (service_verbose[channel]) {
294 printf (
"fwlio_RxTx_getbuffer(%d)\n", channel);
295 printf (
"fwlio_RxTx_getbuffer: Copy %d chars in buffer(%d) from addr %p to %p\n",sock_bufcount[channel],channel,sock_buffers[channel] ,tmpStr );
301 memcpy(tmpStr, sock_buffers[channel], sock_bufcount[channel]);
302 tmpStr[sock_bufcount[channel]] =
'\0';
303 memset(sock_buffers[channel], 0, sock_bufcount[channel]);
304 sock_bufcount[channel] = 0 ;
306 if (service_verbose[channel]) {
307 printf (
"fwlio_RxTx_getbuffer: return %s\n\n",tmpStr );
312 void fwlio_RxTx_sendbuffer(
char *fromFile,
int fromline,
int channel,
char *str) {
318 if (service_verbose[channel]) {
319 printf (
"fwlio_RxTx_sendbuffer(%s,%d,%d,..), sending :\n%s\n(on FD %d)\n",fromFile,fromline,channel,str,SCK_descriptors[channel][CLIENT_FD]);
323 n = send(SCK_descriptors[channel][CLIENT_FD], str, (
unsigned int) strlen(str),0);
326 n = write (SCK_descriptors[channel][CLIENT_FD], str, strlen(str));
328 if (service_verbose[channel]) {
329 printf (
"fwlio_RxTx_sendbuffer(...%d,..), wrote %d 'chars'\n",channel,(
int)n);
332 printf (
"fwlio_RxTx_sendbuffer(...%d,..) write, expected to write %d, actually wrote %d\n",channel,(
int)n,(
int)strlen(str));
342 int privSocketSetup(
int channel,
int *ANONsocketfd,
int *ANONlistenfd) {
346 #define socklen_t int
352 struct sockaddr_in servaddr;
355 if ((service_failed[channel]))
return FALSE;
357 if ((*ANONsocketfd) < 0) {
360 static int wsaStarted;
366 err = WSAStartup(MAKEWORD(2,2), &wsaData);
368 printf(
"WSAStartup failed: %d\n", err);
374 if (((*ANONsocketfd) = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
375 printf (
"WRL_Server: socket error\n");
377 err = WSAGetLastError();
378 printf(
"WSAGetLastError =%d\n",err);
379 if(err == WSANOTINITIALISED) printf(
" WSA Not Initialized - not a successful WSAStartup\n");
382 loopFlags &= ~NO_EAI_CLASS;
386 setsockopt ((*ANONsocketfd), SOL_SOCKET, SO_REUSEADDR, &on, (socklen_t)
sizeof(on));
391 unsigned long iMode = 1;
392 ioctlsocket((*ANONsocketfd), FIONBIO, &iMode);
396 if ((flags=fcntl((*ANONsocketfd),F_GETFL,0)) < 0) {
397 printf (
"EAIServer: trouble gettingsocket flags\n");
398 loopFlags &= ~NO_EAI_CLASS;
403 if (fcntl((*ANONsocketfd), F_SETFL, flags) < 0) {
404 printf (
"EAIServer: trouble setting non-blocking socket\n");
405 loopFlags &= ~NO_EAI_CLASS;
410 if (service_verbose[channel]) {
411 printf (
"privSocketSetup - socket made\n");
416 memset(&servaddr, 0,
sizeof(servaddr));
417 servaddr.sin_family = AF_INET;
418 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
419 servaddr.sin_port = htons(SCK_port[channel]);
420 if (service_verbose[channel]) {
421 printf (
"FreeWRL socket server binding to socket on port %d for channel %d\n",SCK_port[channel], channel);
424 while (bind((*ANONsocketfd), (
struct sockaddr *) &servaddr, (socklen_t)
sizeof(servaddr)) < 0) {
425 loopFlags &= ~NO_EAI_CLASS;
429 if (service_verbose[channel]) {
430 printf (
"FreeWRL socket server: bound to socket %d\n",SCK_port[channel]);
436 if (listen((*ANONsocketfd), 1024) < 0) {
437 printf (
"FreeWRL socket server: listen error\n");
438 loopFlags &= ~NO_EAI_CLASS;
442 if (service_verbose[channel]) {
443 if ( (*ANONsocketfd) >= 0 ) {
444 if (service_verbose[channel] > 1) {
445 printf(
"We have a valid socket fd, %d",(*ANONsocketfd)) ;
448 if ( (*ANONlistenfd) >= 0 ) {
449 printf(
" and we have a client connected fd, %d\n",(*ANONlistenfd)) ;
451 if (service_verbose[channel] > 1) {
452 printf(
" but no client connection (yet)\n") ;
457 if (((*ANONsocketfd) >=0 ) && ((*ANONlistenfd)<0)) {
459 if (service_verbose[channel]>1) {
460 printf(
"We are going to attempt a non-blocking accept()..\n") ;
462 len = (int)
sizeof(cliaddr);
464 if ( ((*ANONlistenfd) = accept((*ANONsocketfd), (
struct sockaddr *) &cliaddr, (
int *)&len)) < 0) {
466 if ( ((*ANONlistenfd) = accept((*ANONsocketfd), (
struct sockaddr *) &cliaddr, (socklen_t *)&len)) < 0) {
468 if (service_verbose[channel]>1) {
469 if (!(loopFlags&NO_CLIENT_CONNECTED)) {
470 printf (
"FreeWRL socket server: no client yet\n");
471 loopFlags |= NO_CLIENT_CONNECTED;
476 loopFlags &= ~NO_CLIENT_CONNECTED;
477 if (service_verbose[channel]) {
478 printf (
"FreeWRL socket server: we have a client\n");
485 if ((*ANONlistenfd) >=0) {
486 if(!service_connected[channel]) {
487 SCK_descriptors[channel][1] = (*ANONlistenfd) ;
489 sock_bufcount[channel] = 0;
490 sock_bufsize[channel] = 2 * EAIREADSIZE;
491 if (service_verbose[channel]) {
492 printf (
"FreeWRL socket server: malloc a buffer,%d\n",sock_bufsize[channel]);
495 sock_buffers[channel] = MALLOC(
char *, sock_bufsize[channel] *
sizeof (
char));
499 service_connected[channel] = TRUE;
501 printf (
"FreeWRL socket server: Why are we in %s,%d? we should not be here!!\n",__FILE__,__LINE__);
506 if (service_verbose[channel]) {
507 if ( !(loopFlags&NO_EAI_CLASS)) {
508 printf (
"FreeWRL socket server: privSocketSetup returning TRUE\n");
509 loopFlags |= NO_EAI_CLASS;
525 char *privSocketRead(
int channel,
char *bf,
int *bfct,
int *bfsz,
int *EAIlistenfd) {
526 int retval, oldRetval;
528 if (service_verbose[channel] > 1) {
529 printf (
"privSocketRead (polling), listenfd %d, buffer addr %p\n",(*EAIlistenfd),(
void *) bf);
536 FD_SET((*EAIlistenfd), &rfds2);
539 retval = select((*EAIlistenfd)+1, &rfds2, NULL, NULL, &tv2);
540 if (service_verbose[channel] > 1) printf (
"select retval %d\n",retval);
542 if (retval != oldRetval) {
543 loopFlags &= NO_RETVAL_CHANGE;
546 if (service_verbose[channel] > 1) {
547 if (!(loopFlags&NO_RETVAL_CHANGE)) {
548 printf (
"privSocketRead, retval %d\n",retval);
549 loopFlags |= NO_RETVAL_CHANGE;
556 retval = recv((*EAIlistenfd), &bf[(*bfct)],EAIREADSIZE,0);
558 retval = (int) read ((*EAIlistenfd), &bf[(*bfct)],EAIREADSIZE);
561 if (service_verbose[channel]) {
562 printf (
"privSocketRead, client is gone!\n");
568 closesocket((*EAIlistenfd));
572 close ((*EAIlistenfd));
575 SCK_descriptors[channel][0] = -1 ;
576 SCK_descriptors[channel][1] = -1 ;
577 service_status[channel] = RxTx_STOP ;
578 service_wanted[channel] = FALSE;
579 service_connected[channel] = FALSE;
580 service_failed[channel] = FALSE;
582 if(service_onclose[channel] == TRUE) {
584 printf (
"FreeWRL:EAI socket closed, exiting...\n");
585 fwl_doQuit(__FILE__,__LINE__);
592 if (service_verbose[channel]>1) {
593 char tmpBuff1[EAIREADSIZE];
594 strncpy(tmpBuff1,&bf[(*bfct)],retval);
595 tmpBuff1[retval] =
'\0';
596 printf (
"privSocketRead %d bytes, max %d bfct %d input=<%s>\n",
597 retval,EAIREADSIZE, *bfct,tmpBuff1);
603 if (((*bfsz) - (*bfct)) <= EAIREADSIZE) {
604 if (service_verbose[channel])
605 printf (
"privSocketRead: HAVE TO REALLOC INPUT MEMORY:bf %p bfsz %d bfct %d\n",bf,*bfsz, *bfct);
606 (*bfsz) += EAIREADSIZE;
607 if (service_verbose[channel])
608 printf (
"privSocketRead: bfsz now %d\n",*bfsz);
609 bf = (
char *)REALLOC (bf, (
unsigned int) (*bfsz));
610 if (service_verbose[channel])
611 printf (
"privSocketRead: REALLOC complete\n");
614 if (service_verbose[channel] > 1) {
615 printf (
"Buffer addr %p\n",(
void *) bf);