source: trunk/src/iplog/iplog.cpp

Last change on this file was 21481, checked in by dmik, 15 years ago

iplog: No need in unistd.h (which conflicts with the LIBC one).

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