File: spy.c

package info (click to toggle)
gwm 1.8d-2
  • links: PTS
  • area: main
  • in suites: potato, woody
  • size: 5,120 kB
  • ctags: 3,030
  • sloc: ansic: 19,617; makefile: 1,763; lisp: 437; sh: 321; ml: 21
file content (127 lines) | stat: -rw-r--r-- 2,591 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 *
 * $Id: spy.c,v 1.1.1.1 1996/02/09 01:43:16 kevin Exp $
 */

#include "spy.h"


#ifdef __STDC__
int
KoalaSpy_SendPacket(char *host, /* 0 -> SPY_HOST */
		    int port,   /* 0 -> SPY_PORT */
		    char *progName,   /* defines the log file */
		    char *origin,    /* 0 -> hostname */
		    char *msg)
#else /* !__STDC__ */
KoalaSpy_SendPacket(host, port, progName, origin, msg)
    char *host;
    int port;  
    char *progName;
    char *origin;  
    char *msg;
#endif /* !__STDC__ */
{
#ifndef NO_KOALA_SPY
/*
  char *host = SPY_HOST;
  int port = SPY_PORT;
  */
    int rc;

    struct servent *sp;
    struct sockaddr_in sin;
    struct hostent *h;
    int sock = 0;
    int flags = 0;

    if (getenv("NO_KOALA_SPY")) {
	return 0;
    }

    /* getting hostname */
    if (!(h = gethostbyname(host ? host : SPY_HOST))) {
	return 1;
    }

    /* creating a socket */
    if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
	return 2;
    }

    memset(&sin, 0, sizeof (sin));
    sin.sin_family = AF_INET;

    /* providing host server identity */
    memcpy(&sin.sin_addr, h->h_addr, h->h_length);

    /* affecting the port number of the server to the sin structure */
    sin.sin_port = htons(port ? port : SPY_PORT);

    /* connect to the server */
#if defined(linux) || defined(SVR4)
    rc = connect (sock, (struct sockaddr *) &sin, sizeof (sin));
#else
    rc = connect (sock, &sin, sizeof (sin));
#endif
    if (rc < 0) {
	return 3;
    }


    /* Make all sockets blocking */
    fcntl(sock, F_GETFL, &flags);
    flags &= ~O_NDELAY;
    fcntl(sock, F_SETFL, flags);

    /*
    ** Sending the string
    */
    {
	unsigned char buf[SPY_PACKET_SIZE], h[256];
	int index;
	int msgLen;    /* left = msgLen + 2 */

	if (! origin)
	    gethostname(h, 256);
	else
	    strncpy(h, origin, 256);
	h[255] = '\0';

	/* create message */
	sprintf(buf, "XXX%s %s : ", progName, h);
	msgLen = strlen(buf);
	strncpy(buf + msgLen, msg, SPY_PACKET_SIZE - msgLen - 1);
	buf[SPY_PACKET_SIZE - 1] ='\0';

	msgLen = strlen(buf) + 1;
	if (msgLen > SPY_PACKET_SIZE - 3)
	    msgLen = SPY_PACKET_SIZE - 3;

	{   /* write magic number and length of message */
	    int i = 0;
	    buf[i++] = SPY_MAGIC_NUMBER & 0xff;
	    buf[i++] = msgLen / 256;
	    buf[i++] = msgLen % 256;
	}

	/* then message itself */
	{
	    int written;
	    int index = 0;
	    while (index < SPY_PACKET_SIZE) {
		written = write(sock, & buf[index], SPY_PACKET_SIZE - index);
		if (written <= 0) {
		    return(6);
		}
		index += written;
	    }
	}

	close(sock);
    }
#endif /* !NO_KOALA_SPY */

    return 0;

}