source: trunk/src/iplog/iplog.cpp@ 10367

Last change on this file since 10367 was 10227, checked in by sandervl, 22 years ago

close and reopen logfile

File size: 4.6 KB
Line 
1#define INCL_DOSPROCESS /* DOS process values (for DosSleep) */
2#define INCL_DOSEXCEPTIONS /* DOS exception values */
3#define INCL_ERRORS /* DOS error values */
4#include <os2.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <types.h>
9#include <netinet/in.h>
10#include <sys/socket.h>
11#include <sys/time.h>
12#include <unistd.h>
13
14 ULONG _System MyTermHandler( PEXCEPTIONREPORTRECORD p1,
15 PEXCEPTIONREGISTRATIONRECORD p2,
16 PCONTEXTRECORD p3,
17 PVOID pv );
18
19
20#define WIN32_IP_LOG_PORT 5001
21
22int s = -1;
23FILE *logfile = NULL;
24
25void main(void)
26{
27 int sockint, namelen, client_address_size;
28 struct sockaddr_in client, server;
29 char buf[4096];
30
31 EXCEPTIONREGISTRATIONRECORD RegRec = {0}; /* Exception Registration Record */
32 APIRET rc = NO_ERROR; /* Return code */
33
34 /* Add MyTermHandler to this thread's chain of exception handlers */
35
36 RegRec.ExceptionHandler = (ERR)MyTermHandler;
37 rc = DosSetExceptionHandler( &RegRec );
38 if (rc != NO_ERROR) {
39 printf("DosSetExceptionHandler error: return code = %u\n",rc);
40 return;
41 }
42
43 /*
44 * Initialize with sockets.
45 */
46 if ((sockint = sock_init()) != 0)
47 {
48 printf(" INET.SYS probably is not running");
49 exit(1);
50 }
51
52 /*
53 * Create a datagram socket in the internet domain and use the
54 * default protocol (UDP).
55 */
56 if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
57 {
58 psock_errno("socket()");
59 exit(1);
60 }
61
62 /*
63 *
64 * Bind my name to this socket so that clients on the network can
65 * send me messages. (This allows the operating system to demultiplex
66 * messages and get them to the correct server)
67 *
68 * Set up the server name. The internet address is specified as the
69 * wildcard INADDR_ANY so that the server can get messages from any
70 * of the physical internet connections on this host. (Otherwise we
71 * would limit the server to messages from only one network interface)
72 */
73 server.sin_family = AF_INET; /* Server is in Internet Domain */
74 server.sin_port = WIN32_IP_LOG_PORT;
75 server.sin_addr.s_addr = INADDR_ANY;/* Server's Internet Address */
76
77 if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0)
78 {
79 psock_errno("bind()");
80 exit(2);
81 }
82
83 logfile = fopen("odin32.log", "wb");
84
85 while(TRUE) {
86 /*
87 * Receive a message on socket s in buf of maximum size 32
88 * from a client. Because the last two paramters
89 * are not null, the name of the client will be placed into the
90 * client data structure and the size of the client address will
91 * be placed into client_address_size.
92 */
93 client_address_size = sizeof(client);
94
95 if(recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &client,
96 &client_address_size) <0)
97 {
98 psock_errno("recvfrom()");
99 exit(4);
100 }
101 /*
102 * Print the message and the name of the client.
103 * The domain should be the internet domain (AF_INET).
104 * The port is received in network byte order, so we translate it to
105 * host byte order before printing it.
106 * The internet address is received as 32 bits in network byte order
107 * so we use a utility that converts it to a string printed in
108 * dotted decimal format for readability.
109 */
110 buf[sizeof(buf)-1] = 0;
111 int len = strlen(buf);
112 fwrite(buf, 1, len, logfile);
113 if(buf[len-1] != '\n') {
114 fwrite("\n", 1, 1, logfile);
115 }
116
117 if(ftell(logfile) > 250*1024*1024) {
118 fclose(logfile);
119 logfile = fopen("odin32.log", "wb");
120 }
121 }
122
123 /*
124 * Deallocate the socket.
125 */
126 soclose(s);
127}
128
129
130 /***************************************************************************/
131 ULONG _System MyTermHandler( PEXCEPTIONREPORTRECORD p1,
132 PEXCEPTIONREGISTRATIONRECORD p2,
133 PCONTEXTRECORD p3,
134 PVOID pv )
135 {
136 switch( p1->ExceptionNum) {
137 case XCPT_PROCESS_TERMINATE:
138 case XCPT_ASYNC_PROCESS_TERMINATE:
139 if(logfile) {
140 fflush(logfile);
141 fprintf(logfile, "*****************************************************\n");
142 fprintf(logfile, "End of logfile\n");
143 fclose(logfile);
144 }
145 if(s != -1) soclose(s);
146 break;
147 default:;
148 }
149
150 return XCPT_CONTINUE_SEARCH; /* Exception not resolved... */
151 }
152
153
Note: See TracBrowser for help on using the repository browser.