home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / syspage2.zip / sysphmsg.c < prev   
C/C++ Source or Header  |  2000-02-21  |  5KB  |  185 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /*  SysPHmsg client  -- issue cmommand or message to       */
  4. /*  SysPage daemon from cgi prog.                          */
  5. /*                                                         */
  6. /***********************************************************/
  7. /*
  8.  * Include Files.
  9.  */
  10. #ifdef __OS2__
  11. #include <types.h>
  12. #define perror(x) psock_errno(x)
  13. #define close(x)  soclose(x)
  14. #else
  15. #include <sys/types.h>
  16. #endif
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <fcntl.h>
  23. #include <sys/socket.h>
  24. #include <sys/stat.h>
  25. #include <netinet/in.h>
  26. #include <netdb.h>
  27.  
  28.  
  29. void puthtdesc( char * str )
  30. {
  31.  fprintf( stdout, "Content-TYPE:  text/html\n\n" );
  32.  fprintf( stdout, "<HEAD>\n" );
  33.  fprintf( stdout, "<TITLE>SysPage HTML</TITLE>\n" );
  34.  fprintf( stdout, "</HEAD>\n" );
  35.  fprintf( stdout, "<BODY>\n" );
  36.  fprintf( stdout, "%s\n", str );
  37.  fprintf( stdout, "</BODY>\n" );
  38. }
  39.  
  40.  
  41. /*
  42.  * Client Main.
  43.  */
  44. main(int argc, char **argv)
  45. {
  46.     unsigned short port;       /* port client will connect to              */
  47.     struct hostent *hostnm;    /* server host name information             */
  48.     struct sockaddr_in server; /* server address                           */
  49.     int s;                     /* client socket                            */
  50.     unsigned long requestor;   /* IP addr of requestor                     */
  51.     char * loc, * chp;         /* parsing                                  */
  52.     char emsg[80];             /* error description                        */
  53.     char buf[256],
  54.          sbuf[256],
  55.          obuf[256];            /* data buffers                             */
  56.  
  57.  
  58.     /*
  59.      */
  60.     if (argc != 1)
  61.        {
  62.         fprintf(stdout,
  63.          "Status: 400 hostname port command/message.\n");
  64.          sprintf( emsg, "Arg is: %s\n", argv[1] );
  65.          puthtdesc( "unexpected args" );
  66.         exit(1);
  67.        }
  68.  
  69.     /* get the requestor IP */
  70.     requestor = inet_addr( getenv( "REMOTE_ADDR" ) );
  71.     memcpy( obuf, &requestor, 4 );
  72.  
  73.     /*
  74.      * The host name is the first argument in extra_path. 
  75.      * the command port is the 2nd
  76.      */
  77.     strcpy( buf, getenv( "PATH_INFO" ) );
  78.     if ( buf[0] != '/' ) 
  79.        { fprintf( stdout, "Status: 400 \n" );
  80.          puthtdesc( "No Path_Info" );
  81.          exit(1);
  82.        }
  83.     loc = &buf[0]; loc++;   /* skip slash */
  84.     chp = loc;
  85.     while ( ( *loc ) && ( *loc != '/' ) )
  86.       { loc++;
  87.       }
  88.     if ( *loc != '/' )
  89.        {
  90.        fprintf(stdout, "Status: 400 No UDP port number\n");
  91.        puthtdesc("No UDP port number.");
  92.        exit(2);
  93.        }
  94.     *loc = '\0';
  95.     hostnm = gethostbyname( chp );
  96.     if (hostnm == (struct hostent *) 0)
  97.        {
  98.        fprintf(stdout, "Status: 405 gethostbyname()\n");
  99.        puthtdesc("gethostbyname failed.");
  100.        exit(2);
  101.        }
  102.     loc++; chp = loc;
  103.     while ( ( *loc ) && ( *loc != '/' ) )
  104.       { loc++;
  105.       }
  106.     *loc = '\0';
  107.     /*
  108.      * The port is the second argument.
  109.     */
  110.     port = (unsigned short) atoi( chp );
  111.     
  112.     strcpy( buf, getenv( "QUERY_STRING" ) );
  113.  
  114.     loc = strtok( buf, "=&" );
  115.     if ( strcasecmp( loc, "LogDisp" ) == 0 )
  116.        {
  117.        loc = strtok( NULL, "&+" );
  118.        if ( loc == NULL )
  119.           { fprintf(stdout, "Status: 400 (1) %s\n");
  120.             puthtdesc( "Bad LogDisp value" );
  121.             exit(1);
  122.           }
  123.        strcpy( sbuf, "\001CMD\002" );
  124.        strcat( sbuf, "LogDisp " );
  125.        strcat( sbuf, loc );
  126.        }
  127.     else
  128.     if ( strcasecmp( loc, "LogMesg" ) == 0 )
  129.        {
  130.        loc += strlen(loc) + 1; chp = loc;
  131.        while ( *chp )  /* rest plus to blank */
  132.           { if ( *chp == '+' ) *chp = ' ';
  133.           chp++;
  134.           }
  135.        strcpy( sbuf, loc );
  136.        }
  137.     else
  138.        { fprintf( stdout, "Status: 400 parm-syntax - %s\n", buf );
  139.          puthtdesc("Unknown INPUT value" );
  140.          exit(2);
  141.        }
  142.  
  143.  
  144.     /*
  145.      * Put the server information into the server structure.
  146.      * The port must be put into network byte order.
  147.      */
  148.     server.sin_family      = AF_INET;
  149.     server.sin_port        = htons(port);
  150.     server.sin_addr.s_addr = *((unsigned long *)hostnm->h_addr);
  151.  
  152. #ifdef __OS2__
  153.     s = sock_init();
  154.     if ( s < 0 )
  155.        {
  156.         fprintf(stdout, "Status: 405 sock_init() failed\n");
  157.         puthtdesc( "sock_init error" );
  158.         exit(2);
  159.        }
  160. #endif
  161.  
  162.     /*
  163.      * Get a DGRAM  socket.
  164.      */
  165.     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  166.        {
  167.         fprintf( stdout, "Status: 405 Socket() %s\n", strerror(errno) );
  168.         puthtdesc( "socket error" );
  169.         exit(3);
  170.        }
  171.  
  172.     strcpy( &obuf[4], sbuf );
  173.     if ( sendto(s, obuf, (strlen(sbuf)+5), 0, (struct sockaddr *)&server,
  174.                 sizeof(server) ) < 0 )
  175.        { fprintf( stdout, "Status 405 sendto() %s\n", strerror(errno) );
  176.          puthtdesc( "sendto() error");
  177.          exit(4);
  178.        }
  179.  
  180.     close(s);
  181.  
  182.     fprintf( stdout, "Status: 204 Cmd/Message sent to SysPage\n\n" );
  183.     exit(0);
  184. }
  185.