char serverinbuffer[1024] = "\0"; char serveroutbuffer[1024] = "\0"; FILE *srvfifoin = NULL; FILE *srvfifoout = NULL; server_alert( char * txt ) { monitor_alert(txt); } int serverwork( void ) { int event,len,azlen,altlen,ralen,declen; char *p,*eventp,*azp,*altp,*rap,*decp; len = strlen(serverinbuffer); if( ( len < 7 ) || ( len > 1024 ) ) { return(0); } p = strchr(serverinbuffer,0x0a); if(p != NULL){ p[0] = 0; } p = strchr(serverinbuffer,0x0d); if(p != NULL){ p[0] = 0; } /*this MUST be the first*/ eventp = strstr( serverinbuffer, "EVENT=" ); if(eventp == NULL){ return(0); } eventp = eventp + 6; azp = strstr( eventp, "AZ=" ); altp = strstr( eventp, "ALT=" ); rap = strstr( eventp, "RA=" ); decp = strstr( eventp, "DEC=" ); if(azp != NULL){ if(azp < eventp){ return(0); } azp[-1] = 0; azp = azp + 3; } if(altp != NULL){ if(altp < eventp){ return(0); } altp[-1] = 0; altp = altp + 4; } if(rap != NULL){ if(rap < eventp){ return(0); } rap[-1] = 0; rap = rap + 3; } if(decp != NULL){ if(decp < eventp){ return(0); } decp[-1] = 0; decp = decp + 4; } if(strlen(eventp) < 1){ return(0); } if( (eventp[0] < '0') || (eventp[0] > '9') ){ return(0); } event = atoi(eventp); azlen = 0; altlen = 0; ralen = 0; declen = 0; if(azp != NULL){ azlen = strlen(azp); } if(altp != NULL){ altlen = strlen(altp); } if(rap != NULL){ ralen = strlen(rap); } if(decp != NULL){ declen = strlen(decp); } if( (azlen > 19 ) || (altlen > 19 ) || ( ralen > 19 ) || ( declen > 19 ) ) { return(0); } waitTillLoopFree( ); if(azlen > 0 ){ strcpy(prog.azstr,azp); } if(altlen > 0 ){ strcpy(prog.altstr,altp); } if(ralen > 0 ){ strcpy(prog.rastr,rap); } if(declen > 0 ){ strcpy(prog.decstr,decp); } doLoopEvent( (unsigned char ) event ); sprintf(serveroutbuffer,"EVENT=%s AZ=%s ALT=%s RA=%s DEC=%s\n\0", eventp, prog.azstr, prog.altstr, prog.rastr, prog.decstr ); return(1); } void tcpshutdown( void ) { struct linger lin; if( server.sock == INVALID_SOCKET ){ return; } shutdown(server.sock,2); lin.l_onoff = 1; lin.l_linger = 0; setsockopt(server.sock,SOL_SOCKET,SO_LINGER,&lin,sizeof(lin)); server.sock = INVALID_SOCKET; } void tcpshutdownserver( void ) { struct linger lin; if(server.serversock == INVALID_SOCKET){ return; } tcpshutdown(); shutdown(server.serversock,2); lin.l_onoff = 1; lin.l_linger = 0; setsockopt(server.serversock,SOL_SOCKET,SO_LINGER,&lin,sizeof(lin)); server.serversock = INVALID_SOCKET; } void tcpserverloop ( char *data ) { fd_set rfds; struct timeval tv; int retval; int addrlen; struct sockaddr_in IPaddr; SOCKNR sock; int len; server.serverrun = 1; while( server.serverrun && ( server.serversock != INVALID_SOCKET ) ) { tcpshutdown(); sock = INVALID_SOCKET; FD_ZERO(&rfds); FD_SET(server.serversock,&rfds); retval = select(server.serversock + 1,&rfds,NULL,NULL,&tv); FD_ZERO(&rfds); if(retval <= 0 ){ continue; } addrlen = sizeof(IPaddr); sock = accept(server.serversock,(struct sockaddr *)&IPaddr,&addrlen); if(sock == INVALID_SOCKET){ continue; } server.sock = sock; len = recv( sock, &serverinbuffer[0], 1024,0); if(len < 0 ){ continue; } if( serverwork( ) ){ len = send(sock,&serveroutbuffer[0],1024,0); if(len < 0 ){ ;/*this should be an error */ } } } tcpshutdownserver(); } void fifoserverloop ( char *data ) { int len; server.serverrun = 1; while( server.serverrun ) { srvfifoin = fopen(server.fifoinfile,"rt"); if( srvfifoin == NULL ){ server_alert("Cannot open fifoin"); server.serverrun = 0; continue; } len = fread( &serverinbuffer[0], 128 , 1 , srvfifoin); fclose(srvfifoin); if( serverwork( ) ){ srvfifoout = fopen(server.fifooutfile,"wt"); if( srvfifoout == NULL ){ server_alert("Cannot open fifoout"); server.serverrun = 0; continue; } fwrite( &serveroutbuffer[0], 128 , 1, srvfifoout ); fclose(srvfifoout); } } } int serverstart ( void ) { char *p; struct servent *serv_ptr; SOCKNR sock; int opt; int rsize,ssize; struct protoent *proto; struct sockaddr_in IPaddr; unsigned long IPAddr; if( ! server.start ) { return(1); } if( server.servertype == SERVERTYPETCP ) { server.serversock = INVALID_SOCKET; server.sock = INVALID_SOCKET; if( server.initport == -1 ) { if(strlen( server.envname ) > 0 ){ if( ( p = getenv(server.envname)) ) { server.initport = atoi( p ); } else { if( strlen( server.servicename ) > 0 ) { if( ( serv_ptr = getservbyname(server.servicename, "tcp")) ) { server.initport = ntohs( serv_ptr->s_port ); } } } } } if( server.initport <= 0 ){ server_alert(" Cannot get valid tcp-port"); return(0); } proto = getprotobyname("tcp"); if(proto == NULL){ return(0); } sock = socket( AF_INET, SOCK_STREAM, proto->p_proto); if( sock == INVALID_SOCKET ) { return(0); } server.serversock = sock; opt = 1; if( setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, ( char *)&opt,sizeof(opt)) != 0 ) { tcpshutdownserver(); return(0); } rsize = 1024; if(setsockopt( sock, SOL_SOCKET, SO_RCVBUF, ( char *)&rsize,sizeof(rsize)) != 0 ) { tcpshutdownserver(); return(0); } ssize = 1024; if(setsockopt( sock, SOL_SOCKET, SO_SNDBUF, ( char *)&ssize,sizeof(ssize)) != 0 ) { tcpshutdownserver(); return(0); } (void)bzero(&IPaddr,sizeof(IPaddr)); IPaddr.sin_family = AF_INET; IPaddr.sin_addr.s_addr = INADDR_ANY; IPaddr.sin_port = (unsigned short) htons((unsigned short)server.initport); if(bind(sock,(struct sockaddr *)&IPaddr,(int)sizeof(IPaddr)) != 0) { tcpshutdownserver(); return(0); } if(listen(sock,5) != 0) { tcpshutdownserver(); return(0); } if( pthread_create( &server.serverthread, NULL, (void *)&tcpserverloop, NULL ) != 0 ){ tcpshutdownserver(); return( 0 ); } return(1); } if( server.servertype == SERVERTYPEFIFO ) { if( pthread_create( &server.serverthread, NULL, (void *)&fifoserverloop, NULL ) != 0 ){ return( 0 ); } return(1); } server_alert("Server cannot start, because no valid servertype"); return( 0 ); }