forked from PolMine/RcppCWB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
1280 lines (1132 loc) · 31.1 KB
/
Copy pathserver.c
File metadata and controls
1280 lines (1132 loc) · 31.1 KB
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* IMS Open Corpus Workbench (CWB)
* Copyright (C) 1993-2006 by IMS, University of Stuttgart
* Copyright (C) 2007- by the respective contributers (see file AUTHORS)
*
* 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 2, 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 (in the file "COPYING", or available via
* WWW at http://www.gnu.org/copyleft/gpl.html).
*/
#ifdef __MINGW__
#include <winsock2.h>
#define socklen_t int
#endif
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/time.h>
#ifndef __MINGW__
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
#include "../cl/cl.h" /* this is the local cl.h header */
#include "../cqp/options.h"
#include "../cqp/corpmanag.h"
#include "../cqp/parse_actions.h"
#include "server.h"
#include "auth.h"
#include "log.h"
#include "cqi.h"
/** the att hash is initially sized to 2 to the power of 14 */
#define ATTHASHSIZE 16384
void Rprintf(const char *, ...);
/** Error strings are limited to this many bytes. @see cqi_error_string */
#define GENERAL_ERROR_SIZE 1024
#ifndef MSG_WAITALL
/* Linux doesn't define the MSG_WAITALL flag (ditto MinGW), but under normal conditions
it _does_ wait for the entire amount of data requested to arrive; so we
just set MSG_WAITALL to 0 (nothing) in this case */
#define MSG_WAITALL 0
#endif
/*
*
* Global variables for CQPSERVER
*
*/
int sockfd; /**< Connection in: file-descriptor integer */
int connfd; /**< Connection out: file-descriptor integer */
FILE *conn_out; /**< Connection out: stream for buffered output (don't forget to flush()) */
struct sockaddr_in my_addr, client_addr;
struct hostent *remote_host;
char *remote_address;
int cqi_errno = CQI_STATUS_OK; /**< CQi last error */
/** String describing the last CQi error. This can be queried by the client. */
char cqi_error_string[GENERAL_ERROR_SIZE] = "No error.";
/*
*
* Error messages
*
*/
/**
* Prints an error message to cqpserver's STDERR and then exits.
*
* This function reports an error in SENDING data to the outgoing connection.
*
* @param function String containing the name of the function where the error was raised.
*/
void
cqi_send_error(char *function)
{
cqiserver_log(Error, "ERROR CQi data send failure in function\n\t%s() <server.c>", function);
exit(1);
}
/**
* Prints an error message to cqpserver's STDERR and then exits.
*
* This function reports an error in READING data from the incoming connection.
*
* @param function String containing the name of the function where the error was raised.
*/
void
cqi_recv_error(char *function)
{
cqiserver_log(Error, "ERROR CQi data recv failure in function\n\t%s() <server.c>\n", function);
exit(1);
}
/**
* Prints an error message to cqpserver's STDERR and then exits.
*
* This function reports a (miscellanrous) internal error in a cqpserver function.
*
* @param function String containing the name of the function where the error was raised.
* @param cause String containing a description of what caused the error.
*/
void
cqi_internal_error(char *function, char *cause)
{
cqiserver_log(Error, "ERROR Internal error in function\n\t%s() <server.c>\n\t''%s''", function, cause);
exit(1);
}
/**
* General error reporting function.
*
* Note that unlike other CQi error functions, this function sends an error
* message to the outgoing connection, rather than printing to the server's
* STDERR. Also note that the program doesn't exit!
*
* The error message is placed into the global variable cqi_error_string,
* whence it can be accessed by the client if the CQI_CTRL_LAST_GENERAL_ERROR
* is sent to the server.
*
* @param errstring String containing the error message.
*/
void
cqi_general_error(char *errstring)
{
if (strlen(errstring) >= GENERAL_ERROR_SIZE)
cqi_internal_error("cqi_general_error", "Error message too long.");
strcpy(cqi_error_string, errstring);
cqi_command(CQI_ERROR_GENERAL_ERROR);
}
/*
*
* Connection
*
*/
/**
* Wait for, and then process, an attempt by a client to initiate a connection
* to cqpserver via TCP/IP.
*
* Note that this function may or may not fork the cqpserver process.
*
* If forking happens, then the child handles the connection,
* whereas the parent carries on waiting for further connections.
*
* On Windows, forking never happens (since Windows doesn't support it).
*
* On *nix, forking happens UNLESS the global private_server is true.
* (Actually, if private_server is true, then forking still happens,
* but the parent process immeidately exits.)
*
* @param port The integer identifier of the port to listen on.
* @return A > 0 value ( the socket ID of the incoming connection)
* if all is OK; otherwise -1.
*/
int
accept_connection(int port)
{
const int on = 1;
socklen_t sin_size = sizeof(struct sockaddr_in);
#ifndef __MINGW__
pid_t child_pid;
#endif
#ifndef __MINGW__
if (SIG_ERR == signal(SIGCHLD, SIG_IGN)) {
perror("ERROR Can't ignore SIGCHLD");
exit(1);
}
#endif
if (port <= 0)
port = CQI_PORT;
cqiserver_debug_msg("Opening socket and binding to port %d", port);
#ifdef __MINGW__
/* startup the use of the Winsock DLL */
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2); /* 2.2 is the higher version */
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
char buffer[50];
sprintf(buffer,"ERROR WSAStartup failed with error: %d\n",err);
perror(buffer);
return -1;
}
#endif
sockfd = socket(AF_INET, SOCK_STREAM, 0);
#ifndef __MINGW__
if (sockfd < 0) {
#else
if (sockfd == INVALID_SOCKET) {
#endif
perror("ERROR Can't create socket");
return -1;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof(int)) < 0)
perror("WARNING Can't set address reuse option"); /* can be ignored... */
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
if (localhost)
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); /* loopback device */
else
my_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* all network devices on local machine */
memset(&(my_addr.sin_zero), '\0', 8);
if (0 != bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))) {
perror("ERROR Can't bind socket to port");
return -1;
}
cqiserver_log(Info, "Waiting for client on port #%d.\n", port);
if (0 != listen(sockfd, 5)) {
perror("ERROR listen() failed");
return -1;
}
#ifndef __MINGW__
/* if called with '-q', fork() and quit before waiting for connections */
if (server_quit) {
pid_t pid = fork();
if (pid != 0) {
/* parent returns to caller */
close(sockfd);
exit(cqiserver_log(Info, "[child is running in background now, parent server quits]") || cqp_error_status);
}
}
#else
/* no forking in Windows! */
#endif
while (42) {
/* when run as a private server, we'll only wait for up to 10 seconds */
if (private_server) {
struct timeval tv;
fd_set read_fd;
tv.tv_sec = 10;
tv.tv_usec = 0;
FD_ZERO(&read_fd); /* select() on sockfd */
FD_SET(sockfd, &read_fd);
if (0 >= select(sockfd+1, &read_fd, NULL, NULL, &tv) || !FD_ISSET(sockfd, &read_fd))
exit(cqiserver_log(Error, "Port #%d timed out in private server mode. Aborting.", port) || cqp_error_status);
}
connfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
if (connfd < 0) {
perror("ERROR Can't establish connection");
return -1;
}
cqiserver_debug_msg("Connection established. Looking up client's name.");
remote_address = inet_ntoa(client_addr.sin_addr);
remote_host = gethostbyaddr((void *)&(client_addr.sin_addr), 4, AF_INET);
cqiserver_log(Info, "Connection established with %s (%s)", remote_address, remote_host ? remote_host->h_name : "name unknown");
#ifndef __MINGW__
/* spawn a server to handle the request */
child_pid = fork();
if (child_pid < 0) {
perror("ERROR can't fork() server");
return -1;
}
if (0 == child_pid)
break; /* the child exits the listen() loop */
/* this is the listening 'parent', which exits immediately in private_server mode */
cqiserver_log(Info, "Spawned child CQPserver, pid = %d.", (int)child_pid);
close(connfd); /* this is the child's connection */
if (private_server) {
close(sockfd);
exit(cqiserver_log(Info, "Accepting no more connections (private server).") || cqp_error_status);
/* SIGCHLD should be reaped by calling process */
}
#else
/* no forking in Windows!
* So all we can do here is break the loop, assuming we are a child.
*
* Note this means: the Windows version of cqpserver accepts only one client!
* (see here for some discussion:
* http://www.gamedev.net/community/forums/topic.asp?topic_id=360290
* http://stackoverflow.com/questions/985281/what-is-the-closest-thing-windows-has-to-fork
* )
* We print the "private server" message unconditionally.
*/
cqiserver_log(Info, "Accepting no more connections (private server).\n");
break;
#endif
}/* endwhile true */
/* this is the child serving the new CQi connection */
cqiserver_debug_msg("** new CQPserver created, initiating CQi session");
close(sockfd);
/* check if remote host is in validation list */
if (!check_host(client_addr.sin_addr)) {
cqiserver_log(Info, "WARNING %s not in list, connection refused!\n", remote_address);
cqiserver_log(Info, "Exit. (pid = %d)\n", (int)getpid());
close(connfd);
exit(1);
}
#ifndef __MINGW__
/* create a buffered stream to the outgoing connection */
if (!(conn_out = fdopen(connfd, "w"))) {
perror("ERROR Can't switch CQi connection to buffered output");
close(connfd);
return -1;
}
#endif
cqiserver_debug_msg("creating attribute hash (size = %d)", ATTHASHSIZE);
make_attribute_hash(ATTHASHSIZE);
return connfd;
}
/*
*
* Server -> Client communication
*
*/
/* communication primitives (no auto-flush; use cqi_flush()) */
/**
* Flushes the stream from the CQP server to the client program,
* emptying its buffer.
*
* Note that under windows, buffered output is not possible, so
* this function does nothing.
*
* @return Boolean: true if everything OK, otherwise false.
*/
int
cqi_flush(void)
{
#ifdef __MINGW__
return 1;
#else
cqiserver_snoop("FLUSH");
if (EOF == fflush(conn_out)) {
perror("ERROR cqi_flush()");
return 0;
}
return 1;
#endif
}
/**
* Sends a BYTE to the client.
*
* This function should be called via one of the cqi_data_* functions
* and not on its own.
*
* This is the fundamental "sending" function, and the only one that calls the underlying
* OS-specific file stream/socket functions.
*
* @param n The byte to send. NOTE that as the parameter is an int, numbers bigger than
* 0xff can be passed. BUT all content except the lowest-order 8-bits are discarded
* (0xff is used as a mask with bitwise-and).
* @param nosnoop Boolean: if true, snoop functionality is overridden (to allow for non-repetition
* of messages when called from a function that has already printed a message)
*/
int
cqi_send_byte(int n, int nosnoop)
{
#ifdef __MINGW__
const char prep = (const char) 0xff & n;
/* prep = (unsigned char) 0xff & n; */
#endif
if (!nosnoop)
cqiserver_snoop("SEND BYTE %02X [= %d]", n, n);
/* note that the actual sending is wrapped in an "if" whose content differs between OSes */
if (
#ifndef __MINGW__
EOF == putc(0xff & n, conn_out)
#else
1 != send(connfd, &prep, 1, MSG_WAITALL)
#endif
) {
perror("ERROR cqi_send_byte()");
return 0;
}
return 1;
}
/**
* Sends a WORD to the client.
*
* A word consists of two bytes in network order. Since CQi commands are
* two bytes, this function can be used to send commands.
*
* This function should be called via one of the cqi_data_* functions
* and not on its own.
*/
int
cqi_send_word(int n)
{
cqiserver_snoop("SEND WORD %04X [= %d]", n, n);
if (
/* exploit the fact that cqi_send_byte() only uses the lowest 8 bytes of its argument */
!(cqi_send_byte(n>>8, 1)) ||
!(cqi_send_byte(n, 1))
) {
perror("ERROR cqi_send_word()");
return 0;
}
return 1;
}
/**
* Sends an INT to the client.
*
* An int consists of four bytes in network order.
*
* This function should be called via one of the cqi_data_* functions
* and not on its own.
*
* @return Boolean: true if everything OK, otherwise false.
*/
int
cqi_send_int(int n)
{
cqiserver_snoop("SEND INT %08X [= %d]", n, n);
if (
/* exploit the fact that cqi_send_byte() only uses the lowest 8 bytes of its argument */
!(cqi_send_byte(n>>24, 1)) ||
!(cqi_send_byte(n>>16, 1)) ||
!(cqi_send_byte(n>>8, 1)) ||
!(cqi_send_byte(n, 1))
) {
perror("ERROR cqi_send_int()");
return 0;
}
return 1;
}
/**
* Sends a STRING to the client.
*
* CQi strings are NOT null-terminated -- so while the argument to this
* function needs to be a null-terminated string, the string that actually
* gets sent across the network will not be.
*
* This function should be called via one of the cqi_data_* functions
* and not on its own.
*
* @return Boolean: true if everything OK, otherwise false.
*/
int
cqi_send_string(const char *str)
{
int len;
if (!str) {
if (!cqi_send_word(0)) {
perror("ERROR cqi_send_string()");
return 0;
}
return 1;
}
len = strlen(str);
if (!cqi_send_word(len)) {
perror("ERROR cqi_send_string()");
return 0;
}
cqiserver_snoop("SEND CHAR[] '%s'", str);
/* we can afford to mangle len and str because we're at the end of the function */
while (--len >= 0)
if (!cqi_send_byte(*str++, 1)) {
perror("ERROR cqi_send_string()");
return 0;
}
return 1;
}
/**
* Sends a BYTE[] (byte list) to the client.
*
* This function should be called via one of the cqi_data_* functions
* and not on its own.
*
* @param list pointer to a block of bytes to send.
* @param l the number of bytes to send.
* @param as_boolean if true, send 1 for any true byte, 0 for 0.
*
* @return Boolean: true if everything OK, otherwise false.
*/
int
cqi_send_byte_list(cqi_byte *list, int l, int as_boolean)
{
if (!cqi_send_int(l)) {
perror("ERROR cqi_send_byte_list()");
return 0;
}
while (--l >= 0) {
if (!cqi_send_byte(as_boolean ? (*list ? CQI_CONST_TRUE : CQI_CONST_FALSE) : *list, 0)) {
perror("ERROR cqi_send_byte_list()");
return 0;
}
list++;
}
return 1;
}
/**
* Sends an INT[] (integer list) to the client.
*
* This function should be called via one of the cqi_data_* functions
* and not on its own.
*
* @param list pointer to a block of integers to send.
* @param l the number of integers to send.
*
* @return Boolean: true if everything OK, otherwise false.
*/
int
cqi_send_int_list(int *list, int l)
{
if (!cqi_send_int(l)) {
perror("ERROR cqi_send_int_list()");
return 0;
}
while (--l >= 0) {
if (!cqi_send_int(*list++)) {
perror("ERROR cqi_send_int_list()");
return 0;
}
}
return 1;
}
/**
* Sends a STRING[] (string list) to the client.
*
* This function should be called via one of the cqi_data_* functions
* and not on its own.
*
* @param list pointer to a block of pointers-to-strings; the strings will be sent.
* @param l the number of strings to send.
*/
int
cqi_send_string_list(char **list, int l)
{
if (!cqi_send_int(l)) {
perror("ERROR cqi_send_string_list()");
return 0;
}
while (--l >= 0) {
if (!cqi_send_string(*list++)) {
perror("ERROR cqi_send_string_list()");
return 0;
}
}
return 1;
}
/**
* Sends a general CQi command, without any arguments.
*/
void
cqi_command(int command)
{
if (!cqi_send_word(command) || !cqi_flush())
cqi_send_error("cqi_command");
}
/**
* Sends a byte of data to the client.
*/
void
cqi_data_byte(int n)
{
if (!cqi_send_word(CQI_DATA_BYTE) || !cqi_send_byte(n ? CQI_CONST_TRUE : CQI_CONST_FALSE, 0) || !cqi_flush())
cqi_send_error("cqi_data_byte");
}
/**
* Sends a boolean to the client.
*/
void
cqi_data_bool(int n)
{
if (!cqi_send_word(CQI_DATA_BOOL) || !cqi_send_byte(n ? CQI_CONST_TRUE : CQI_CONST_FALSE, 0) || !cqi_flush())
cqi_send_error("cqi_data_bool");
}
/**
* Sends an integer to the client.
*/
void
cqi_data_int(int n)
{
if (!cqi_send_word(CQI_DATA_INT) || !cqi_send_int(n) || !cqi_flush())
cqi_send_error("cqi_data_int");
}
/**
* Sends a string to the client.
*/
void
cqi_data_string(const char *str)
{
if (!cqi_send_word(CQI_DATA_STRING) || !cqi_send_string(str) || !cqi_flush())
cqi_send_error("cqi_data_string");
}
/**
* Sends a byte list to the client.
*
* @param list pointer to a block of bytes to send.
* @param l the number of bytes to send.
*/
void
cqi_data_byte_list(cqi_byte *list, int l)
{
if (!cqi_send_word(CQI_DATA_BYTE_LIST) || !cqi_send_byte_list(list, l, 0) || !cqi_flush())
cqi_send_error("cqi_data_byte_list");
}
/**
* Sends a list of booleans to the client.
*
* @param list pointer to a block of booleans (each occupying one byte) to send.
* @param l the number of bytes to send.
*/
void
cqi_data_bool_list(cqi_byte *list, int l)
{
if (!cqi_send_word(CQI_DATA_BOOL_LIST) || !cqi_send_byte_list(list, l, 1) || !cqi_flush())
cqi_send_error("cqi_data_bool_list");
}
/**
* Sends a list of integers to the client.
*
* @param list pointer to a block of integers to send.
* @param l the number of integers to send.
*/
void
cqi_data_int_list(int *list, int l)
{
if (!cqi_send_word(CQI_DATA_INT_LIST) || !cqi_send_int_list(list, l) || !cqi_flush())
cqi_send_error("cqi_data_int_list");
}
/**
* Sends a list of strings to the client.
*
* @param list pointer to a block of pointers-to-strings; the strings will be sent.
* @param l the number of strings to send.
*/
void
cqi_data_string_list(char **list, int l)
{
if (!cqi_send_word(CQI_DATA_STRING_LIST) || !cqi_send_string_list(list, l) || !cqi_flush())
cqi_send_error("cqi_data_string_list");
}
/**
* Sends a sequence of two integers to the client.
*
* @param n1 The first integer sent
* @param n2 The second integer sent
*/
void
cqi_data_int_int(int n1, int n2)
{
if (
!cqi_send_word(CQI_DATA_INT_INT) ||
!cqi_send_int(n1) ||
!cqi_send_int(n2) ||
!cqi_flush()
)
cqi_send_error("cqi_data_int_int");
}
/**
* Sends a sequence of four integers to the client.
*
* @param n1 The first integer sent
* @param n2 The second integer sent
* @param n3 The third integer sent
* @param n4 The fourth integer sent
*/
void
cqi_data_int_int_int_int(int n1, int n2, int n3, int n4)
{
if (
!cqi_send_word(CQI_DATA_INT_INT_INT_INT) ||
!cqi_send_int(n1) ||
!cqi_send_int(n2) ||
!cqi_send_int(n3) ||
!cqi_send_int(n4) ||
!cqi_flush()
)
cqi_send_error("cqi_data_int_int_int_int");
}
/*
*
* Client -> Server communication
*
*/
int
cqi_recv_bytes(cqi_byte *buf, int bytes)
{
if (bytes <= 0)
return 1;
cqiserver_snoop("RECV BYTE[%d]", bytes);
if (bytes != recv(connfd, buf, bytes, MSG_WAITALL)) {
perror("ERROR cqi_recv_bytes()");
return 0;
}
return 1;
}
int
cqi_recv_byte(void)
{
cqi_byte b;
if (1 != recv(connfd, &b, 1, MSG_WAITALL)) {
perror("ERROR cqi_recv_byte()");
return EOF;
}
cqiserver_snoop("RECV BYTE 0x%02X", b);
return b;
}
int
cqi_read_byte(void)
{
int b = cqi_recv_byte();
if (b == EOF)
cqi_recv_error("cqi_read_byte");
return b;
}
int
cqi_read_bool(void)
{
int b = cqi_recv_byte() ? CQI_CONST_TRUE : CQI_CONST_FALSE;
if (b == EOF)
cqi_recv_error("cqi_read_bool");
return b;
}
int
cqi_read_word(void)
{
int n = cqi_read_byte();
n = (n << 8) | cqi_read_byte();
cqiserver_snoop("READ WORD %04X [= %d]", n, n);
return n;
}
int
cqi_read_int(void)
{
int n = cqi_read_byte();
int minus_bits = ((int)-1) ^ 0xFFFFFFFF; /* extra minus bits if int is > 32 bit*/
n = (n << 8) | cqi_read_byte();
n = (n << 8) | cqi_read_byte();
n = (n << 8) | cqi_read_byte();
if (n & 0x80000000) /* negative 32bit quantity */
n |= minus_bits; /* expand to full size of int type */
cqiserver_snoop("READ INT %08X [= %d]", n, n);
return n;
}
char *
cqi_read_string(void)
{
char *s;
int len = cqi_read_word();
s = (char *) cl_malloc(len + 1);
if (!cqi_recv_bytes((cqi_byte *)s, len))
cqi_recv_error("cqi_read_string");
s[len] = '\0';
cqiserver_snoop("READ CHAR[] '%s'", s);
return s;
}
int
cqi_read_command(void)
{
int command;
cqiserver_debug_msg("waiting for command");
command = cqi_read_byte();
while (command == CQI_PAD)
command = cqi_read_byte();
command = (command << 8) | cqi_read_byte();
return command;
}
int
cqi_read_byte_list(cqi_byte **list)
{
int i, len;
len = cqi_read_int();
if (len <= 0) {
*list = NULL;
return 0;
}
*list = (cqi_byte *) cl_malloc(len);
for (i=0; i<len; i++)
(*list)[i] = cqi_read_byte();
cqiserver_snoop("READ BYTE[%d]", len);
return len;
}
int
cqi_read_bool_list(cqi_byte **list)
{
int i, len;
len = cqi_read_int();
if (len <= 0) {
*list = NULL;
return 0;
}
*list = (cqi_byte *) cl_malloc(len);
for (i=0; i<len; i++)
(*list)[i] = cqi_read_byte() ? CQI_CONST_TRUE : CQI_CONST_FALSE;
cqiserver_snoop("READ BOOL[%d]", len);
return len;
}
int
cqi_read_int_list(int **list)
{
int i, len;
len = cqi_read_int();
if (len <= 0) {
*list = NULL;
return 0;
}
*list = (int *) cl_malloc(len * sizeof(int));
for (i=0; i<len; i++)
(*list)[i] = cqi_read_int();
cqiserver_snoop("READ INT[%d]", len);
return len;
}
int
cqi_read_string_list(char ***list)
{
int i, len;
len = cqi_read_int();
if (len <= 0) {
*list = NULL;
return 0;
}
*list = (char **) cl_malloc(len * sizeof(char *));
for (i=0; i<len; i++)
(*list)[i] = cqi_read_string();
cqiserver_snoop("READ STRING[%d]\n", len);
return len;
}
/*
*
* Naming conventions & specifier split
*
*/
char cqi_id_uc_first[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
char cqi_id_uc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_-0123456789";
char cqi_id_lc_first[] = "abcdefghijklmnopqrstuvwxyz_";
char cqi_id_lc[] = "abcdefghijklmnopqrstuvwxyz_-0123456789";
char cqi_id_all[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-0123456789";
/**
* Makes sure that an identifier complies with the specified convention regarding its initial and subsequent characters.
*
* If the identifier does not conform, cqi_errno is set and the function returns false.
*
* @param name The identifier to check.
* @param init_cap If true, the initial char must be uppercase or underscore.
* If false, it can be lowercase or underscore.
* @param rest_cap If true, subsequent chars must be uppercase or underscore.
* If false, they must be lowercase or underscore or hyphen.
* @param rest_any If true, rest_cap is overridden: any valid identifier char is accepted.
* If false, the value given as rest_cap is enforced.
*
*
* @return Boolean: is the identifier in accordance with convention?
*/
int
check_identifier_convention(char *name, int init_cap, int rest_cap, int rest_any)
{
char *valid_init = init_cap ? cqi_id_uc_first : cqi_id_lc_first;
char *valid_rest = rest_any ? cqi_id_all : (rest_cap ? cqi_id_uc : cqi_id_lc);
if (
!strchr(valid_init, *name)
||
strspn(name+1, valid_rest) != strlen(name+1)
) {
cqi_errno = CQI_ERROR_SYNTAX_ERROR;
return 0;
}
cqi_errno = CQI_STATUS_OK;
return 1;
}
// now implemented as macro in server.h:
//int
//check_corpus_name(char *name)
//{
// if (
// !strchr(cqi_id_uc_first, *name)
// ||
// strspn(name+1, cqi_id_uc) != strlen(name+1)
// ) {
// cqi_errno = CQI_ERROR_SYNTAX_ERROR;
// return 0;
// }
// cqi_errno = CQI_STATUS_OK;
// return 1;
//}
//int
//check_attribute_name(char *name)
//{
// if (
// !strchr(cqi_id_lc_first, *name)
// ||
// strspn(name+1, cqi_id_lc) != strlen(name+1)
// ) {
// cqi_errno = CQI_ERROR_SYNTAX_ERROR;
// return 0;
// }
// cqi_errno = CQI_STATUS_OK;
// return 1;