00001
00002
00003
00004
00005
00006
00007
00008
00009
00017
00018
00019
00020 #ifndef __SERVER
00021 #define __SERVER
00022
00023
00024
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <errno.h>
00028 #include <string.h>
00029 #include <limits.h>
00030 #include <sys/types.h>
00031 #include <sys/wait.h>
00032 #include <sys/socket.h>
00033 #include <netinet/in.h>
00034 #include <arpa/inet.h>
00035 #include <netdb.h>
00036 #include <signal.h>
00037 #include <unistd.h>
00038 #include <ctype.h>
00039 #include <crypt.h>
00040
00041
00042 #ifndef VERSION
00043 #define VERSION "0.2.3"
00044 #endif
00045
00046 #ifndef TRUE
00047 #define TRUE (1==1)
00048 #define FALSE (1!=1)
00049 #endif
00050
00051 #define PORT 7777
00052 #define BACKLOG 10
00053 #define ROOT_UID 0
00054 #define MAX_LOGIN 3
00055 #define LOGIN_SLEEP_TIME 15
00056
00057 #define DEFAULT_CHANNEL 1
00058 #define DEFAULT_RESOLUTION 'M'
00059 #define DEFAULT_NO_SAMPLES 25
00060
00061 #define MIN_CHANNEL 1
00062 #define MAX_CHANNEL 8
00063 #define DATA_STREAM -1
00064
00065 #define BUFFER_SIZE 256
00066 #define MAX_LENGTH 256
00067
00068 #define DELIMITERS " \t\n\x0A\x0D"
00069
00070
00071 #define NOAUTH 0
00072 #define AUTH 1
00073 #define AUTHORISED (1+2)
00074 #define NOTAUTHORISED (1+4)
00075 #define AUTHNOTPROCESSED 8
00076
00077
00078 #define MODENOTPROCESSED (1+2)
00079 #define MODEPROCESSED (1+4)
00080
00081
00082 #define ACQUIRE_SUCCESS 0
00083 #define ACQUIRE_FAILURE 1
00084 #define HIGH_RESOLUTION_STEP 1
00085 #define MEDIUM_RESOLUTION_STEP 2
00086 #define LOW_RESOLUTION_STEP 5
00087 #define STREAM_CHUNK 128
00088
00099 typedef
00100 enum _command_no_t
00101 {
00102 CN_ERROR = -2,
00103 CN_NO_COMMAND = -1,
00104 CN_USER = 0,
00105 CN_PASS,
00106 CN_SET,
00107 CN_GET,
00108 CN_RESOLUTION,
00109 CN_START,
00110 CN_STOP,
00111 CN_HELP,
00112 CN_BYE,
00113 CN_EXIT,
00114 CN_QUIT,
00115 MAX_CN_NO
00116 } command_no_t;
00117
00118
00119 static const char *command[] =
00120 {
00121 "USER",
00122 "PASS",
00123 "SET",
00124 "GET",
00125 "RESOLUTION",
00126 "START",
00127 "STOP",
00128 "HELP",
00129 "BYE",
00130 "EXIT",
00131 "QUIT"
00132 };
00133
00134
00135 static const char *help_message[] =
00136 {
00137 "214 HELP USER OK\n"
00138 "214 USER <username>\n"
00139 "214 Sets user name.\n",
00140
00141 "214 HELP PASS OK\n"
00142 "214 PASS <password>\n"
00143 "214 Sets password for given username.\n"
00144 "214 Username must be specified before.\n",
00145
00146 "214 HELP SET OK\n"
00147 "214 SET <channel number>\n"
00148 "214 Sets channel. Allowed channel numbers\n"
00149 "214 are between zero and eight.\n"
00150 "214 Default channel is 1.\n",
00151
00152 "214 HELP GET OK\n"
00153 "214 GET <number of samples>\n"
00154 "214 Specifes number of samples to send after START command\n"
00155 "214 is given. Number of samples must be either positive\n"
00156 "214 integer or word STREAM. For STREAM samples are sent\n"
00157 "214 continously until STOP is given.\n"
00158 "214 Default value is 25 samples.\n",
00159
00160 "214 HELP RESOLUTION OK\n"
00161 "214 RESOLUTION <resolution>\n"
00162 "214 Sets resolution. Allowed values for resolution are:\n"
00163 "214 H, HIGH - high resolution, 1 ms\n"
00164 "214 M, MEDIUM - medium resolution, 100 ms\n"
00165 "214 L, LOW - low resolution, 10 s\n"
00166 "214 Default value is M.\n",
00167
00168 "214 HELP START OK\n"
00169 "214 START\n"
00170 "214 Sends data. See STOP and GET.\n",
00171
00172 "214 HELP STOP OK\n"
00173 "214 STOP\n"
00174 "214 Stops data stream. See START and GET.\n",
00175
00176 "214 HELP HELP OK\n"
00177 "214 HELP [ <command> ]\n"
00178 "214 Gives useful informations about commands.\n",
00179
00180 "214 HELP BYE OK\n"
00181 "214 BYE\n"
00182 "214 Exit ADC-ZESOI server.\n",
00183
00184 "214 HELP EXIT OK\n"
00185 "214 EXIT\n"
00186 "214 Exit ADC-ZESOI server.\n",
00187
00188 "214 HELP QUIT OK\n"
00189 "214 QUIT\n"
00190 "214 Exit ADC-ZESOI server.\n",
00191
00192 "214 HELP OK\n"
00193 "214 Commands are:\n"
00194 "214 USER GET START HELP QUIT\n"
00195 "214 PASS SET STOP RESOLUTION \n"
00196 "214 For more information use HELP <command>.\n"
00197 };
00198
00199 #define GET_STREAM_ARGUMENT "STREAM"
00200 #define RESOLUTION_HIGH "HIGH"
00201 #define RESOLUTION_H "H"
00202 #define RESOLUTION_MEDIUM "MEDIUM"
00203 #define RESOLUTION_M "M"
00204 #define RESOLUTION_LOW "LOW"
00205 #define RESOLUTION_L "L"
00206 #define RESOLUTION_HC 'H'
00207 #define RESOLUTION_MC 'M'
00208 #define RESOLUTION_LC 'L'
00209
00210
00220 typedef
00221 enum _error_code_t
00222 {
00223 EC_PARSE_FAILURE = -2,
00224 EC_NO_ERROR = -1,
00225
00226 EC_UNKNOWN_COMMAND = 0,
00227
00228 EC_USER_ERROR,
00229 EC_USER_UNKNOWN,
00230 EC_USER_OVERRUN,
00231 EC_USER_NO_NAME,
00232 EC_USER_NO_AUTHORISATION_REQUIRED,
00233
00234 EC_PASS_ERROR,
00235 EC_PASS_INCORRECT,
00236 EC_PASS_EXPIRED,
00237 EC_PASS_NO_USER_NAME,
00238 EC_PASS_ALREADY_AUTHORISED,
00239 EC_PASS_OVERRUN,
00240 EC_PASS_NO_PASS,
00241 EC_PASS_NO_AUTHORISATION_REQUIRED,
00242 EC_PASS_DISCONNECTED,
00243 EC_PASS_PASSWORDLESS,
00244 EC_PASS_DENIED,
00245 EC_PASS_LOCKED,
00246
00247 EC_SET_ERROR,
00248 EC_SET_INVALID,
00249 EC_SET_UNAVAILABE,
00250 EC_SET_UNAUTHORISED,
00251 EC_SET_NO_CHANNEL,
00252
00253 EC_GET_ERROR,
00254 EC_GET_INVALID_NO_OF_SAMPLES,
00255 EC_GET_UNAUTHORISED,
00256 EC_GET_NO_ARGUMENT,
00257
00258 EC_RESOLUTION_ERROR,
00259 EC_RESOLUTION_INVALID,
00260 EC_RESOLUTION_UNAUTHORISED,
00261 EC_RESOLUTION_NO_ARGUMENT,
00262
00263 EC_START_ERROR,
00264 EC_START_UNAUTHORISED,
00265
00266 EC_STOP_ERROR,
00267 EC_STOP_NO_STREAM,
00268 EC_STOP_UNAUTHORISED,
00269
00270 EC_HELP_INVALID_COMMAND
00271 } error_code_t;
00272
00273
00274 static const char *error_message[] =
00275 {
00276 "500 ERROR: Unknown command. See HELP.\n",
00277
00278 "501 USER ERROR\n",
00279 "502 USER ERROR: Unknown user.\n",
00280 "503 USER ERROR: Username to long.\n",
00281 "504 USER ERROR: Must supply user name.\n",
00282 "505 USER ERROR: Authorisation not required.\n",
00283
00284 "510 PASS ERROR\n",
00285 "511 PASS ERROR: Incorrect password.\n",
00286 "512 PASS ERROR: Account expired.\n",
00287 "513 PASS ERROR: No user name.\n",
00288 "514 PASS ERROR: Already authorised.\n",
00289 "515 PASS ERROR: Password to long.\n",
00290 "516 PASS ERROR: Must supply password.\n",
00291 "517 PASS ERROR: Authorisation not required.\n",
00292 "518 PASS ERROR: Maximum login attempts number reached.\n",
00293 "519 PASS ERROR: Paswordless accounts are disabled.\n",
00294 "520 PASS ERROR: Access for %s denied.\n",
00295 "521 PASS ERROR: Account locked.\n",
00296
00297 "530 SET ERROR\n",
00298 "531 SET ERROR: Invalid channel number.\n",
00299 "532 SET ERROR: Channel unavailabe.\n",
00300 "533 SET ERROR: Authorisation required for SET.\n",
00301 "534 SET ERROR: No channel number given.\n",
00302
00303 "535 GET ERROR\n",
00304 "536 GET ERROR: Invalid number of samples.\n",
00305 "537 GET ERROR: Authorisation required for GET.\n",
00306 "538 GET ERROR: Must specify number of samples or stream.\n",
00307
00308 "540 RESOLUTION ERROR\n",
00309 "541 RESOLUTION ERROR: Invalid resolution.\n",
00310 "542 RESOLUTION ERROR: Authorisation required for RESOLUTION.\n",
00311 "543 RESOLUTION ERROR: No resolution specified.\n",
00312
00313 "550 START ERROR\n",
00314 "551 START ERROR: Authorisation required for START.\n",
00315
00316 "555 STOP ERROR\n",
00317 "556 STOP ERROR: No stream.\n",
00318 "557 STOP ERROR: Authorisation required for STOP.\n",
00319
00320 "599 HELP ERROR: Invalid command.\n"
00321 "214 Commands are:\n"
00322 "214 USER GET START HELP QUIT\n"
00323 "214 PASS SET STOP RESOLUTION \n"
00324 "214 For more information use HELP <command>.\n"
00325 };
00326
00327
00328
00336 typedef
00337 enum _ok_message_no_t
00338 {
00339 MN_USER = 0,
00340
00341 MN_PASS,
00342
00343 MN_SET,
00344
00345 MN_GET_STREAM,
00346 MN_GET_SAMPLES,
00347
00348 MN_RESOLUTION,
00349
00350 MN_START_STREAM,
00351 MN_START_SAMPLES,
00352 MN_START_TEST_SAMPLES,
00353 MN_START_TEST_STREAM,
00354 MN_START_RESOLUTION,
00355
00356 MN_STOP,
00357
00358 MN_TRANSFER_COMPLETED,
00359
00360 MN_WELCOME,
00361 MN_GOODBYE
00362 } _ok_message_no_t;
00363
00364
00365 static const char *ok_message[] =
00366 {
00367 "250 USER OK: Username set to %s.\n"
00368 "250 Use PASS to send password.\n",
00369
00370 "250 PASS OK: Hello %s. No restrictions apply.\n",
00371
00372 "250 SET OK: Channel set to %d.\n",
00373
00374 "250 GET OK: Samples will be sent as data stream.\n",
00375 "250 GET OK: Number of samples set to %d.\n",
00376
00377 "250 RESOLUTION OK: Resolution set to %c.\n",
00378
00379 "251 START OK: Sending data stream.\n"
00380 "251 Use STOP to stop transmission.\n",
00381 "250 START OK: Sending %d samples.\n",
00382 "300 Test mode active: Sending %d random samples.\n",
00383 "300 Test mode active: Sending random data stream.\n",
00384 "333 Resoultion is %c.\n",
00385
00386 "250 STOP OK: Stream stopped.\n",
00387
00388 "333 Transfer completed.\n",
00389
00390 "220 Welcome to ADC-ZESOI server v" VERSION " at %s.\n",
00391 "221 BYE OK ADC-ZESOI server v" VERSION " at %s signing off.\n"
00392 };
00393
00394
00395
00404 typedef
00405 struct _command_t
00406 {
00407 command_no_t command_no;
00408 error_code_t error_code;
00409 command_no_t help_code;
00410 int no_of_samples;
00411 int channel;
00412 short int authorised;
00413 short int stream;
00414 short int exit;
00415 char resolution;
00416 char username[MAX_LENGTH+1];
00417 char password[MAX_LENGTH+1];
00418 }
00419 command_t;
00420
00421
00422 int global_fd;
00423 unsigned int global_max_login_no;
00424 short int global_null_ok;
00425 pid_t global_pid;
00426 pid_t global_stream_pid;
00427
00428
00429 #ifdef TEST_MODE
00430 short int global_test_mode = TRUE;
00431 #else
00432 short int global_test_mode = FALSE;
00433 #endif
00434
00435
00436
00437
00438 char * readcommand (int);
00439 command_t parsecommand (const char *, command_t);
00440 command_t executecommand (command_t, command_t, int);
00441
00442
00443 int acquireandsendsamples (int, int, int, char);
00444
00445
00446 void lostconnection (int);
00447 void stopstream (int);
00448 void cleanallchildren (int);
00449
00450
00451
00452 #endif
00453
00454