File: xjdserver.c

package info (click to toggle)
xjdic 24-11
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 472 kB
  • sloc: ansic: 5,454; makefile: 136; sh: 55
file content (477 lines) | stat: -rw-r--r-- 11,414 bytes parent folder | download | duplicates (4)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**************************************************************************
*                 X  J  D  S E R V E R                                    *
*              Dictionary Server Program                                  *
*         Japanese-English Dictionary program (X11 version)               *
*                                                   Author: Jim Breen     *
***************************************************************************/
/*  This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include "xjdic.h"

#define SVERBOSE 0

void xjdicrc();
void DicSet ();
unsigned char *DicName(int dn);

int portno=XJ_PORTNO;
int DontFork = FALSE;
int alen;
char host[51];

REQ_PDU pdu_in;
RSP_PDU pdu_out;

struct sockaddr_in fsin;
int sock;

long lo, hi, itok, lo2, hi2, schix, schiy;
int res, i;
long it;
unsigned char ENVname[50];
unsigned char cl_rcfile[100];
int jiver = 14; 
int thisdic = 0;
int DicNum;

extern unsigned char Dnamet[10][100],XJDXnamet[10][100];
extern unsigned char *dicbufft[10];
extern unsigned long diclent[10], indlent[10],indptrt[10];
extern int NoDics,CurrDic;

char DicDir[100];

int passiveUDP(int portno)
{
	struct protoent *ppe;
	struct sockaddr_in sin;
	int s;

	bzero((char *)&sin, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = INADDR_ANY;
	sin.sin_port = htons(portno);
	if ((ppe = getprotobyname("udp")) == 0)
	{
		printf ("Cannot set up UDP!!\n");
		exit(1);
	}
	s = socket(PF_INET, SOCK_DGRAM, ppe->p_proto);
	if (s < 0)
	{
		printf ("Cannot create socket!!: %s\n",strerror(errno));
		exit(1);
	}
	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
	{
		printf ("Cannot bind to port: %d\n",portno);
		exit(1);
	}

	return (s);
}

int checksum_in()
{
	long temp;

	temp = ntohs(pdu_in.xjdreq_type);
	temp+= ntohs(pdu_in.xjdreq_seq);
	temp+= ntohs(pdu_in.xjdreq_dicno);
	temp+= ntohl(pdu_in.xjdreq_indexpos);
	temp+= ntohs(pdu_in.xjdreq_schlen);
	for (i = 0; i < strlen(pdu_in.xjdreq_schstr); i++) temp+= pdu_in.xjdreq_schstr[i];

	if (temp == ntohl(pdu_in.xjdreq_checksum)) return(TRUE);
	return(FALSE);
}

void sendresponse()
{
	long temp;

	temp = ntohs(pdu_out.xjdrsp_type);
	temp+= ntohs(pdu_out.xjdrsp_seq);
	temp+= ntohl(pdu_out.xjdrsp_resindex);
	temp+= ntohl(pdu_out.xjdrsp_dicloc);
	temp+= ntohs(pdu_out.xjdrsp_hitposn);
	temp+= ntohs(pdu_out.xjdrsp_reslen);
	for (i = 0; i < strlen(pdu_out.xjdrsp_resstr); i++)
		temp+= pdu_out.xjdrsp_resstr[i];
	pdu_out.xjdrsp_checksum = htonl(temp);
	if (SVERBOSE) printf("PDU sent: %d %d %d %d %d %d %s %d\n",
			ntohs(pdu_out.xjdrsp_type),
			ntohs(pdu_out.xjdrsp_seq),
			ntohl(pdu_out.xjdrsp_resindex),
			ntohl(pdu_out.xjdrsp_dicloc),
			ntohs(pdu_out.xjdrsp_hitposn),
			ntohs(pdu_out.xjdrsp_reslen),
			pdu_out.xjdrsp_resstr,
			temp);
	i = sendto (sock, (RSP_PDU *) &pdu_out, sizeof(pdu_out)-512+strlen(pdu_out.xjdrsp_resstr)+1,
			0, (struct sockaddr *)&fsin, sizeof(fsin));
	if (i < 0)
	{
		printf ("sendto error!: %s\n",strerror(errno));
	}
}

/*                  M A I N                                      */

main(argc,argv)
int argc;
char **argv;

{
	int iterlimit,i;
  	unsigned char *dicenv,strtmp[50];
	unsigned char xap[50];

	
  	printf("XJDSERVER Version %s (Japanese Dictionary Server) Copyright J.W.Breen 2003.\n",SVER);

	dicenv = (unsigned char *)getenv("XJDIC");
	if (!dicenv) dicenv = (unsigned char *)DEFAULT_DICDIR;
	if (strlen(dicenv) <= 2)
	{
		dicenv = (unsigned char *)getcwd(ENVname,sizeof(ENVname));
		if (dicenv == NULL)
		{
			printf("Cannot extract working directory!\n");
			exit(1);
		}
	}
	else
	{
		strcpy (ENVname,dicenv);
	}
        strcpy (Dnamet[0], "kanjidic");
        strcpy (Dnamet[1], "edict");
        strcpy (XJDXnamet[0], "kanjidic.xjdx");
	strcpy (XJDXnamet[1], "edict.xjdx");
	NoDics = 1;
	cl_rcfile[0] = 0;
/* process command-line options*/
	if (argc > 1)
	{
		for (i = 1; i < argc; i++)
		{
			strcpy(xap,argv[i]);
			if ((xap[0] == '-') && (xap[1] == 'h'))
			{
				printf("Command-line options:\n");
				printf("\t -c control_file\n");
				printf("\t -K (do not create daemon)\n");
				printf("\t -P nnnn (use UDP port nnnn\n");
				printf("\t -k kanji_dic_file\n");
				printf("\t -d dictionary_file (up to 9)\n");
				printf("\t -h (this information)\n");
				exit(0);
			}
			if ((xap[0] == '-') && (xap[1] == 'c'))
			{
				if(xap[2] != 0)
				{
					strcpy(strtmp,xap+2);
				}
				else
				{
					i++;
					strcpy(xap,argv[i]);
					strcpy (strtmp,xap);
				}
				strcpy (cl_rcfile,strtmp);
				printf ("Using control-file: %s\n",cl_rcfile);
			}
			if ((xap[0] == '-') && (xap[1] == 'd'))
			{
				if (thisdic == 0)
				{
					thisdic = 1;
				}
				else
				{
					thisdic++;
					NoDics++;
				}
				if(xap[2] != 0)
				{
					strcpy(strtmp,xap+2);
				}
				else
				{
					i++;
					strcpy(xap,argv[i]);
					strcpy (strtmp,xap);
				}
				strcpy (Dnamet[thisdic],strtmp);
				strcpy (XJDXnamet[thisdic],strtmp);
				strcat (XJDXnamet[thisdic],".xjdx");
				printf("Command-line request to use dictionary files: %s and %s\n",Dnamet[thisdic],XJDXnamet[thisdic]);
				continue;
			}
			if ((xap[0] == '-') && (xap[1] == 'K'))
			{
				DontFork = TRUE;
				continue;
			}
			if ((xap[0] == '-') && (xap[1] == 'P'))
			{
				if(xap[2] != 0)
				{
					portno = atoi(xap+2);
				}
				else
				{
					i++;
					strcpy(xap,argv[i]);
					portno = atoi(xap);
				}
				printf("Command-line request to use port: %d\n",portno);
				continue;
			}
			if ((xap[0] == '-') && (xap[1] == 'k'))
			{
				if(xap[2] != 0)
				{
					strcpy(strtmp,xap+2);
				}
				else
				{
					i++;
					strcpy(xap,argv[i]);
					strcpy (strtmp,xap);
				}
				strcpy (Dnamet[0],strtmp);
				strcpy (XJDXnamet[0],strtmp);
				strcat (XJDXnamet[0],".xjdx");
				printf("Command-line request to use kanji dictionary files: %s and %s\n",Dnamet[0],XJDXnamet[0]);
				continue;
			}
		}
	}
	xjdicrc();
  	printf ("Loading Dictionary and Index files.  Please wait...\n");
/* printf("TEST! NoDics = %d\n",NoDics); */
/* for(i = 0;i <= NoDics; i++) printf("TEST! Dnamet[] = %s\n",Dnamet[i]); */
	if (!DontFork)
	{
		i = fork();
		if (i < 0)
		{
			printf("error when forking: %s\n",strerror(errno));
			exit(1);
		}
		if (i) exit(0);
	}
  	DicSet ();
	sock = passiveUDP(portno);
	printf ("Server Loaded & Ready\n");
/*
	From here on, the code loops endlessly, reading  requests from the
	UDP port, looking up the dictionary, and sending back the answers.
*/
  	while (TRUE)
	{
		alen = sizeof(fsin);
		if (recvfrom(sock, (REQ_PDU *) &pdu_in, sizeof(pdu_in), 0, 
			(struct sockaddr *)&fsin, &alen) < 0)
		{
			fprintf (stderr,"recvfrom failure: %s\n",strerror(errno));
			continue;
		}

		if (!checksum_in) continue; 	/* ignore requests with bad checksums*/
		pdu_out.xjdrsp_seq = pdu_in.xjdreq_seq;
		pdu_out.xjdrsp_resindex = htonl(0);
		pdu_out.xjdrsp_dicloc = htonl(0);
		pdu_out.xjdrsp_hitposn = htons(0);
		pdu_out.xjdrsp_reslen= htons(0);
		pdu_out.xjdrsp_resstr[0] = 0;
		pdu_in.xjdreq_type = ntohs(pdu_in.xjdreq_type);
		pdu_in.xjdreq_dicno = ntohs(pdu_in.xjdreq_dicno);
		pdu_in.xjdreq_schlen = ntohs(pdu_in.xjdreq_schlen);
		pdu_in.xjdreq_indexpos = ntohl(pdu_in.xjdreq_indexpos);


		/* printf ("PDU: %d received\n",pdu_in.xjdreq_type);*/
		if (pdu_in.xjdreq_type == XJ_HULLO)
		{
			pdu_out.xjdrsp_type = htons(XJ_OK);
			pdu_out.xjdrsp_hitposn = htons(NoDics);
			pdu_out.xjdrsp_resstr[0] = 0;
			for (i = 0; i<=NoDics;i++)
			{
				sprintf(strtmp,"#%d%s",i,DicName(i));
				strcat(pdu_out.xjdrsp_resstr,strtmp);
			}
			pdu_out.xjdrsp_reslen = htons(strlen(pdu_out.xjdrsp_resstr));
			sendresponse();
			continue;
		}

		if (pdu_in.xjdreq_dicno > NoDics)
		{
			pdu_out.xjdrsp_type = htons(XJ_PROTERR);
			sendresponse();
			continue;
		}
		DicNum = pdu_in.xjdreq_dicno;
		hi = indptrt[pdu_in.xjdreq_dicno];

		if (pdu_in.xjdreq_type == XJ_FIND)
		{
			lo = 1;
			iterlimit = MAXITER;
			while(TRUE)
			{
				if (!iterlimit--) break;
  				it = (lo+hi)/2;
				res = Kstrcmp(pdu_in.xjdreq_schlen,pdu_in.xjdreq_schstr);
				if (res == 0)
				{
					itok = it;
					lo2 = lo;
					hi2 = it;
					while (TRUE)
					{
						if(lo2+1 >= hi2) break;
						it = (lo2+hi2)/2;
						res = Kstrcmp(pdu_in.xjdreq_schlen,pdu_in.xjdreq_schstr);
						if (res == 0)
						{
							hi2 = it;
							itok = it;
							continue;
						}
						else
						{
							lo2 = it+1;
						}
					}
					it = itok;
					res = 0;
					break;
				}
				if (res < 0)
				{
					hi = it-1;
				}
				else
				{
					lo = it+1;
				}
				if (lo > hi) break;
			}
			if (res != 0)
			{
				pdu_out.xjdrsp_type = htons(XJ_NBG);
				sendresponse();
				continue;
			}
	/* as the above sometimes misses the first matching entry, step back to the
    	first  */
			while (TRUE)
			{
				if(Kstrcmp(pdu_in.xjdreq_schlen,pdu_in.xjdreq_schstr) == 0)
				{
					it--;
					if (it == 0)
					{
						it = 1;
						break;
					}
					continue;
				}
				else
				{
					it++;
					break;
				}
			}
		}
	
	/*	Get next entry. Check (a) if the caller hasn't gone off the end of the
		table, and (b) if the (next) entry matches.		*/
	
		if (pdu_in.xjdreq_type == XJ_ENTRY)
		{
			if (pdu_in.xjdreq_indexpos > hi)
			{
				pdu_out.xjdrsp_type = htons(XJ_NBG);
				sendresponse();
				continue;
			}
			it = pdu_in.xjdreq_indexpos;
			res = Kstrcmp(pdu_in.xjdreq_schlen,pdu_in.xjdreq_schstr);
			if (res != 0)
			{
				pdu_out.xjdrsp_type = htons(XJ_NBG);
				sendresponse();
				continue;
			}
		}
		if (pdu_in.xjdreq_type == XJ_GET)
		{
			if (pdu_in.xjdreq_indexpos > hi)
			{
				pdu_out.xjdrsp_type = htons(XJ_NBG);
				sendresponse();
				continue;
			}
			it = pdu_in.xjdreq_indexpos;
		}
	
	/*  Common code to set up the return parameters for both call types  */
		schix = jindex(it);
		schiy = schix;
		pdu_out.xjdrsp_resindex = htonl(it);
	/* back off to the start of this line   */
		while ((dbchar(schix) != 0x0a) && (schix >= 0)) schix--;
		schix++;
		pdu_out.xjdrsp_dicloc = htonl(schix);
		pdu_out.xjdrsp_hitposn = htons(schiy - schix);
		if (pdu_in.xjdreq_type == XJ_ENTRY)
		{
			pdu_out.xjdrsp_resindex = htonl(schix);
		}
		if (pdu_in.xjdreq_type == XJ_GET)
		{
			pdu_out.xjdrsp_resindex = htonl(schix);
		}
		for (i = 0; i < 510 && dbchar(schix+i) != 0x0a; i++)
		{
			pdu_out.xjdrsp_resstr[i] = dbchar(schix+i);
		}
		pdu_out.xjdrsp_resstr[i+0] = 0x0a; /* NL tells user s/w that it is the end of an entry */
		pdu_out.xjdrsp_resstr[i+1] = 0;
		pdu_out.xjdrsp_reslen = htons(strlen(pdu_out.xjdrsp_resstr));
		pdu_out.xjdrsp_type = htons(XJ_OK);
		sendresponse();
		continue;
	}
}