home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gwm18a.zip / spy.c < prev    next >
C/C++ Source or Header  |  1995-07-03  |  3KB  |  128 lines

  1. /*
  2.  *
  3.  * $Id: gwm.shar,v 1.107 1995/07/03 09:24:22 colas Exp $
  4.  */
  5.  
  6. #include "spy.h"
  7.  
  8.  
  9. #ifdef __STDC__
  10. int
  11. KoalaSpy_SendPacket(char *host, /* 0 -> SPY_HOST */
  12.             int port,   /* 0 -> SPY_PORT */
  13.             char *progName,   /* defines the log file */
  14.             char *origin,    /* 0 -> hostname */
  15.             char *msg)
  16. #else /* !__STDC__ */
  17. KoalaSpy_SendPacket(host, port, progName, origin, msg)
  18.     char *host;
  19.     int port;  
  20.     char *progName;
  21.     char *origin;  
  22.     char *msg;
  23. #endif /* !__STDC__ */
  24. {
  25. #ifndef NO_KOALA_SPY
  26. /*
  27.   char *host = SPY_HOST;
  28.   int port = SPY_PORT;
  29.   */
  30.     int rc;
  31.  
  32.     struct servent *sp;
  33.     struct sockaddr_in sin;
  34.     struct hostent *h;
  35.     int sock = 0;
  36.     int flags = 0;
  37.  
  38.     if (getenv("NO_KOALA_SPY")) {
  39.     return 0;
  40.     }
  41.  
  42.     /* getting hostname */
  43.     if (!(h = gethostbyname(host ? host : SPY_HOST))) {
  44.     return 1;
  45.     }
  46.  
  47.     /* creating a socket */
  48.     if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
  49.     return 2;
  50.     }
  51.  
  52.     memset(&sin, 0, sizeof (sin));
  53.     sin.sin_family = AF_INET;
  54.  
  55.     /* providing host server identity */
  56.     memcpy(&sin.sin_addr, h->h_addr, h->h_length);
  57.  
  58.     /* affecting the port number of the server to the sin structure */
  59.     sin.sin_port = htons(port ? port : SPY_PORT);
  60.  
  61.     /* connect to the server */
  62. #ifdef linux
  63.     rc = connect (sock, (struct sockaddr *) &sin, sizeof (sin));
  64. #else
  65.     rc = connect (sock, &sin, sizeof (sin));
  66. #endif
  67.     if (rc < 0) {
  68.     return 3;
  69.     }
  70.  
  71.  
  72.     /* Make all sockets blocking */
  73.     fcntl(sock, F_GETFL, &flags);
  74.     flags &= ~O_NDELAY;
  75.     fcntl(sock, F_SETFL, flags);
  76.  
  77.     /*
  78.     ** Sending the string
  79.     */
  80.     {
  81.     unsigned char buf[SPY_PACKET_SIZE], h[256];
  82.     int index;
  83.     int msgLen;    /* left = msgLen + 2 */
  84.  
  85.     if (! origin)
  86.         gethostname(h, 256);
  87.     else
  88.         strncpy(h, origin, 256);
  89.     h[255] = '\0';
  90.  
  91.     /* create message */
  92.     sprintf(buf, "XXX%s %s : ", progName, h);
  93.     msgLen = strlen(buf);
  94.     strncpy(buf + msgLen, msg, SPY_PACKET_SIZE - msgLen - 1);
  95.     buf[SPY_PACKET_SIZE - 1] ='\0';
  96.  
  97.     msgLen = strlen(buf) + 1;
  98.     if (msgLen > SPY_PACKET_SIZE - 3)
  99.         msgLen = SPY_PACKET_SIZE - 3;
  100.  
  101.     {   /* write magic number and length of message */
  102.         int i = 0;
  103.         buf[i++] = SPY_MAGIC_NUMBER & 0xff;
  104.         buf[i++] = msgLen / 256;
  105.         buf[i++] = msgLen % 256;
  106.     }
  107.  
  108.     /* then message itself */
  109.     {
  110.         int written;
  111.         int index = 0;
  112.         while (index < SPY_PACKET_SIZE) {
  113.         written = write(sock, & buf[index], SPY_PACKET_SIZE - index);
  114.         if (written <= 0) {
  115.             return(6);
  116.         }
  117.         index += written;
  118.         }
  119.     }
  120.  
  121.     close(sock);
  122.     }
  123. #endif /* !NO_KOALA_SPY */
  124.  
  125.     return 0;
  126.  
  127. }
  128.