Comment servir des pintes de Guinness "over Internet" ;) Reprise/Fork d'un très ancien code d'un pilier de f.m.b.l : http://tnemeth.free.fr/projets/guinness-server.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

guinnessd.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * guinnessd
  3. * architecture clients/serveur guinness
  4. * Thomas Nemeth -- le 15 juin 2001
  5. *
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <time.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include <pthread.h>
  14. #include <fcntl.h>
  15. #include <getopt.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include <locale.h>
  22. #include "defines.h"
  23. #include "xmem.h"
  24. #include "tools.h"
  25. #include "printlog.h"
  26. #include "lists.h"
  27. #include "broadcast.h"
  28. #include "drinks.h"
  29. #include "clients.h"
  30. #include "guinnessd.h"
  31. #include "commands.h"
  32. #include "config.h"
  33. /* Config specifique serveur */
  34. char *adr_ip = NULL;
  35. int port = 0;
  36. int online = TRUE;
  37. char *fichierlog = NULL;
  38. char *fichiercfg = NULL;
  39. char *chemin = NULL;
  40. char *admin_passwd = NULL;
  41. FILE *logfile = NULL;
  42. FILE *outerr = NULL;
  43. Elt *clients_list = NULL;
  44. Elt *drinks_list = NULL;
  45. /* Config specifique connexion des clients */
  46. char *pseudo = NULL;
  47. char *drink = NULL;
  48. char *logout = NULL;
  49. #ifdef SunOS
  50. char *crypt(const char *key, const char *salt);
  51. #else
  52. extern char *crypt __P ((__const char *__key, __const char *__salt));
  53. #endif
  54. void install_handler ();
  55. /*
  56. * Gestionnaire de signal SIGPIPE, SIGTERM, SIGQUIT et SIGINT
  57. *
  58. */
  59. void handler_signaux (int sig) {
  60. switch (sig) {
  61. case SIGPIPE:
  62. printlog (LOG_NOTIFY, "Signal SIGPIPE recu...\n");
  63. install_handler ();
  64. break;
  65. case SIGTERM:
  66. case SIGQUIT:
  67. case SIGINT:
  68. online = FALSE;
  69. printlog (LOG_NOTIFY, "Signal de terminaison recu...\n");
  70. break;
  71. default:
  72. printlog (LOG_NOTIFY, "Signal recu...\n");
  73. }
  74. }
  75. /*
  76. * Lecture du fichier de config : recuperation des valeurs
  77. *
  78. */
  79. char *get_string_from_token (char *line) {
  80. char *result = line;
  81. char *tmp;
  82. int keyval = 0;
  83. while ( (keyval != 1) && (result [0] != 0) ) {
  84. if (result [0] == '=') keyval = 1;
  85. if (keyval == 0) result++;
  86. }
  87. tmp = result++;
  88. while (tmp [0] != 0) {
  89. if (tmp [0] == '\n') tmp [0] = 0;
  90. tmp++;
  91. }
  92. return result;
  93. }
  94. /*
  95. * Lecture du fichier de config
  96. *
  97. */
  98. void load_config () {
  99. FILE *file;
  100. char tmpstr[MAXSTRLEN + 1];
  101. char *value;
  102. /* Ouverture du fichier */
  103. if ( (file = fopen (fichiercfg?fichiercfg:CONFIG_FILE, "r") ) != NULL) {
  104. /* Lecture du fichier */
  105. while (! feof (file) ) {
  106. memset (tmpstr, 0, MAXSTRLEN + 1);
  107. fgets (tmpstr, (int) MAXSTRLEN, file);
  108. value = get_string_from_token (tmpstr);
  109. if ( (strncmp (tmpstr, "port=", 5) == 0) && (port == 0) )
  110. port = atoi (value);
  111. if ( (strncmp (tmpstr, "passwd=", 7) == 0) &&
  112. (admin_passwd == NULL) )
  113. admin_passwd = xstrdup (crypt (value, "Gs") + 2);
  114. if ( (strncmp (tmpstr, "rep=", 4) == 0) && (chemin == NULL) )
  115. chemin = xstrdup (value);
  116. if ( (strncmp (tmpstr, "pseudo=", 7) == 0) && (pseudo == NULL) )
  117. pseudo = xstrdup (value);
  118. if ( (strncmp (tmpstr, "drink=", 6) == 0) && (drink == NULL) )
  119. drink = xstrdup (value);
  120. if ( (strncmp (tmpstr, "logout=", 7) == 0) && (logout == NULL) )
  121. logout = xstrdup (value);
  122. }
  123. fclose (file);
  124. #ifdef DEBUG
  125. } else {
  126. fprintf (stderr, "Pas de fichier de config (%s).\n", CONFIG_FILE);
  127. #endif
  128. }
  129. }
  130. /*
  131. * Aide
  132. *
  133. */
  134. void Usage (int help) {
  135. printf ("guinnessd by Thomas Nemeth - v %s (compilation %s : %s)\n",
  136. VERSION, OS_TYPE, COMPIL_DATE);
  137. if (help)
  138. printf ("Usage : guinnessd [-h] [-v] [-b] [-p port] "
  139. "[-s passwd] [-d chemin] [-l fichier] [-f fichier]\n"
  140. " -h : aide sur les parametres\n"
  141. " -v : affiche la version\n"
  142. " -b : detache le serveur du terminal\n"
  143. " -a adresse : specifie l'adresse du serveur\n"
  144. " -p port : specifie le numero du port\n"
  145. " -s passwd : specifie le mot de passe d'aministration\n"
  146. " -d chemin : indique le chemin ou se"
  147. " trouvent les ascii-arts\n"
  148. " -l fichier : fichier de log\n"
  149. " -f fichier : fichier de configuration\n\n");
  150. exit (1);
  151. }
  152. /*
  153. * Traitement des arguments
  154. *
  155. */
  156. int traite_argv (int argc, char *argv[]) {
  157. int option;
  158. int detach = FALSE;
  159. /* Verification des parametres */
  160. while ((option = getopt (argc, argv, "hvba:p:s:d:l:f:")) != -1) {
  161. switch (option) {
  162. case 'h' :
  163. Usage (TRUE);
  164. break;
  165. case 'v' :
  166. Usage (FALSE);
  167. break;
  168. case 'b' :
  169. detach = TRUE;
  170. break;
  171. case 'a' :
  172. adr_ip = optarg;
  173. break;
  174. case 'p' :
  175. port = atoi (optarg);
  176. break;
  177. case 's' :
  178. admin_passwd = xstrdup (crypt (optarg, "Gs") + 2);
  179. break;
  180. case 'd' :
  181. if (! chemin) {
  182. if (optarg[0] == '/') {
  183. chemin = xstrdup (optarg);
  184. } else {
  185. char *Pwd;
  186. Pwd = xmalloc ((MAXSTRLEN + 1) * sizeof (char));
  187. getcwd (Pwd, MAXSTRLEN);
  188. chemin = xmalloc ( (strlen (Pwd) + strlen (optarg) + 2)
  189. * sizeof (char) );
  190. snprintf (chemin, MAXSTRLEN, "%s/%s",
  191. Pwd, optarg);
  192. }
  193. }
  194. break;
  195. case 'l' :
  196. if (! fichierlog) {
  197. fichierlog = xstrdup (optarg);
  198. }
  199. break;
  200. case 'f' :
  201. if (! fichiercfg) {
  202. fichiercfg = xstrdup (optarg);
  203. }
  204. break;
  205. default:
  206. Usage (TRUE);
  207. break;
  208. }
  209. }
  210. if (optind < argc) {
  211. if (argc - optind == 1)
  212. fprintf (stderr, "%s: option inconnue --", argv[0]);
  213. else
  214. fprintf (stderr, "%s: options inconnues --", argv[0]);
  215. for ( ; optind < argc ; optind++)
  216. fprintf (stderr, " %s", argv[optind]);
  217. fprintf (stderr, "\n");
  218. Usage (TRUE);
  219. }
  220. return detach;
  221. }
  222. /*
  223. * Fonction initiant la connexion.
  224. *
  225. */
  226. int initiate (int socket_service, userinfos *infos) {
  227. int cont = TRUE;
  228. char userdatas[MAXSTRLEN];
  229. char *datas, *token, *saveptr;
  230. char delim[] = "\n\r";
  231. char *nom_distant = NULL;
  232. char *ip_distant = NULL;
  233. int port_distant = 0;
  234. int port_local = 0;
  235. time_t now;
  236. struct tm *today;
  237. memset (userdatas, 0, MAXSTRLEN);
  238. /* Recuperation des infos sur le connecte */
  239. get_sock_infos (socket_service,
  240. &nom_distant, &ip_distant,
  241. &port_local, &port_distant);
  242. if (ip_distant) {
  243. snprintf (infos->ip, MAXSTRLEN, "%s", ip_distant);
  244. free (ip_distant);
  245. } else sprintf (infos->ip, "0.0.0.0");
  246. if (nom_distant) {
  247. snprintf (infos->host, MAXSTRLEN, "%s", nom_distant);
  248. free (nom_distant);
  249. } else sprintf (infos->host, "none");
  250. infos->port = port_distant;
  251. printlog (LOG_NOTIFY, "Connexion entrante : %s %s\n",
  252. infos->ip, infos->host ? infos->host : "");
  253. printlog (LOG_NOTIFY, "Ports (loc/dist) : %d / %d\n",
  254. port_local, infos->port);
  255. /* PROTOCOLE DE CONNEXION */
  256. /* 1. envoi des commandes du serveur */
  257. if ( (cont = send_servercmds (socket_service)) == FALSE)
  258. return cont;
  259. /* 2. attente des donnees du connecte */
  260. if ( (cont = read_infos (socket_service, userdatas)) == FALSE)
  261. return cont;
  262. /* FIN DU PROTOCOLE : Vericifations / ajout dans la liste / affichage */
  263. datas = xstrdup (userdatas);
  264. /* Nom d'utilisateur */
  265. token = strtok_r (datas, delim, &saveptr);
  266. snprintf (infos->nom, MAXSTRLEN, "%s", token ? token : pseudo);
  267. /* Boisson preferee */
  268. token = strtok_r (NULL, delim, &saveptr);
  269. snprintf (infos->prefb, MAXSTRLEN, "%s", token ? token : drink);
  270. /* Message d'au-revoir */
  271. token = strtok_r (NULL, delim, &saveptr);
  272. snprintf (infos->logout, MAXSTRLEN, "%s", token ? token : logout);
  273. /* Date de connexion */
  274. time (&now);
  275. today = localtime (&now);
  276. strftime (infos->cxdate, MAXSTRLEN, "%a %d %b %Y %T", today);
  277. printlog (LOG_NOTIFY, "Utilisateur : [%s]\n", infos->nom);
  278. printlog (LOG_NOTIFY, "Boisson preferee : [%s]\n", infos->prefb);
  279. printlog (LOG_NOTIFY, "Message de logout : [%s]\n", infos->logout);
  280. printlog (LOG_NOTIFY, "Date de connexion : [%s]\n", infos->cxdate);
  281. pthread_mutex_lock (&mutex_clients);
  282. if (exist_elt (clients_list, infos->nom)) {
  283. char nick_ok[MAXSTRLEN+1];
  284. snprintf (nick_ok, MAXSTRLEN, "@%s", infos->nom);
  285. snprintf (infos->nom, MAXSTRLEN, "%s", nick_ok);
  286. send_infos (socket_service, "@Pseudo deja utilise !\n");
  287. printlog (LOG_NOTIFY, "Pseudo deje utilise => %s\n", infos->nom);
  288. pthread_mutex_unlock (&mutex_clients);
  289. return FALSE;
  290. }
  291. send_infos (socket_service, "Bienvenue sur le serveur de Guinness.\n");
  292. add_client (infos);
  293. pthread_mutex_unlock (&mutex_clients);
  294. if (! exist_elt (drinks_list, infos->prefb))
  295. send_infos (socket_service,
  296. "Votre boisson preferee n'est pas disponible sur ce"
  297. " serveur !\nVous aurez des Guinness a la place.\n");
  298. /* Passage en mode non bloquant */
  299. fcntl (socket_service, F_SETFL, O_NONBLOCK);
  300. return cont;
  301. }
  302. /*
  303. * Fonction de traitement de la connexion avec les clients
  304. *
  305. */
  306. void thread_service (int *sock_serv) {
  307. pthread_t tid = pthread_self ();
  308. long old_id = get_broadcastid ();
  309. int socket_service = (int) *sock_serv;
  310. int cont = TRUE;
  311. int nb;
  312. userinfos infos;
  313. char commande[MAXSTRLEN + 1];
  314. pthread_detach (tid);
  315. memset (infos.nom, 0, MAXSTRLEN + 1);
  316. memset (infos.prefb, 0, MAXSTRLEN + 1);
  317. memset (infos.logout, 0, MAXSTRLEN + 1);
  318. infos.admin = FALSE;
  319. infos.cold = FALSE;
  320. cont = initiate (socket_service, &infos);
  321. if (cont) {
  322. snprintf (commande, MAXSTRLEN,
  323. "%s a rejoint le serveur de Guinness.\n", infos.nom);
  324. broadcast (MESSAGE, NULL, commande);
  325. }
  326. #ifdef DEBUG
  327. printlog (LOG_NOTIFY, "Pret a recevoir des commandes.\n");
  328. #endif
  329. while (cont == TRUE) {
  330. memset (commande, 0, MAXSTRLEN + 1);
  331. /* Lecture des caracteres recus */
  332. do {
  333. sleep (1);
  334. nb = read (socket_service, commande, MAXSTRLEN);
  335. if ( (nb == -1) || (nb == 0) ) {
  336. if ( (errno == EAGAIN) && (nb == -1) ) continue;
  337. if ( (errno == EPIPE) || (nb == 0) ) {
  338. cont = FALSE;
  339. } else {
  340. printlog (LOG_ERROR, "Erreur de connexion reception !\n");
  341. }
  342. }
  343. } while ( (cont != FALSE) &&
  344. (nb < 0) &&
  345. (new_message (old_id) != TRUE) );
  346. #ifdef DEBUG
  347. printlog (LOG_NOTIFY, "Commande recue.\n");
  348. #endif
  349. if (cont == TRUE) {
  350. /* Traitement de la commande */
  351. if (nb > 0)
  352. cont = reply (socket_service, commande, &infos);
  353. /* Broadcast */
  354. if (new_message (old_id) == TRUE) {
  355. old_id = get_broadcastid ();
  356. cont = send_broadcast (socket_service, &infos);
  357. #ifdef DEBUG
  358. printlog (LOG_ERROR, "Emission broadcast pour %s (id = %ld)\n",
  359. infos.nom, old_id);
  360. #endif
  361. }
  362. }
  363. }
  364. printlog (LOG_NOTIFY, "Bye bye %s...\n", infos.nom);
  365. if (infos.nom[0] != '@') {
  366. pthread_mutex_lock (&mutex_clients);
  367. remove_client (&infos);
  368. pthread_mutex_unlock (&mutex_clients);
  369. broadcast (QUITTER, infos.nom, infos.logout);
  370. }
  371. close (socket_service);
  372. }
  373. /*
  374. * Installation du gestionnaire de signal SIGPIPE et autres...
  375. *
  376. */
  377. void install_handler () {
  378. signal (SIGPIPE, handler_signaux);
  379. signal (SIGTERM, handler_signaux);
  380. signal (SIGQUIT, handler_signaux);
  381. signal (SIGINT, handler_signaux);
  382. }
  383. /*
  384. * Initialisation generale
  385. *
  386. */
  387. void guinnessd_init (int argc, char *argv[]) {
  388. pthread_mutexattr_t mutex_attr;
  389. char *cptr;
  390. setlocale (LC_ALL, "");
  391. install_handler ();
  392. /* Valeurs par defaut */
  393. logfile = stdout;
  394. outerr = stderr;
  395. /* est-ce bien la place pour initialiser des trucs avant le parsing de
  396. la ligne de commande ? Eg: la variable d'environnement DRINKS_DIR
  397. */
  398. if (NULL!=(cptr=getenv("DRINKS_DIR"))) {
  399. chemin = xstrdup(cptr);
  400. }
  401. /* Traitement des parametres */
  402. if (traite_argv (argc, argv) == TRUE) {
  403. switch (fork()) {
  404. case -1: /* erreur */
  405. perror ("fork()");
  406. exit (-1);
  407. case 0: /* le fils */
  408. setsid ();
  409. break;
  410. default: /* le pere */
  411. exit (0);
  412. }
  413. }
  414. /* Lecture du fichier de configuration */
  415. load_config ();
  416. /* Initialisation des semaphores */
  417. pthread_mutexattr_init (&mutex_attr);
  418. pthread_mutex_init (&mutex_broadcast, &mutex_attr);
  419. pthread_mutex_init (&mutex_clients, &mutex_attr);
  420. /* Affectation des parametres */
  421. if (IS_NOT_GOOD (pseudo)) SET_STRING (pseudo, DEFAULT_PSEUDO);
  422. if (IS_NOT_GOOD (drink)) SET_STRING (drink, DEFAULT_DRINK);
  423. if (IS_NOT_GOOD (logout)) SET_STRING (logout, DEFAULT_LOGOUT);
  424. if (port == 0) port = DEFAULT_SERVER_PORT;
  425. if (fichierlog) {
  426. if ((logfile = fopen (fichierlog, "a"))) {
  427. outerr = logfile;
  428. } else {
  429. fprintf (stderr, "Impossible d'ouvrir le fichier %s : %s\n",
  430. fichierlog, strerror(errno));
  431. }
  432. }
  433. /* Option pour le buffer de logs */
  434. setvbuf (logfile, NULL, _IOLBF, 0);
  435. /* Creation de la liste de boissons par defaut*/
  436. add_elt (&drinks_list, "guinness");
  437. if (! chemin) chemin = xstrdup (DRINKS_DIR);
  438. drinks_list_files (chemin);
  439. drinks_display_list ();
  440. }
  441. /*
  442. * Fonction principale
  443. *
  444. */
  445. int main (int argc, char *argv[]) {
  446. int socket_ecoute;
  447. int socket_service;
  448. socklen_t lg_adresse = sizeof (struct sockaddr_in);
  449. struct sockaddr_in adresse;
  450. pthread_t pthread_id;
  451. guinnessd_init (argc, argv);
  452. /* Installation de l'ecoute du serveur */
  453. if ( (socket_ecoute = install_server (port, adr_ip, NULL)) == -1) {
  454. printlog (LOG_ERROR, "Impossible d'installer le service.\n");
  455. return -1;
  456. }
  457. /* Passage en mode non-bloquant */
  458. fcntl (socket_ecoute, F_SETFL, O_NONBLOCK);
  459. /* Boucle d'attente et d'ouverture de connexions */
  460. printlog (LOG_NOTIFY, "Serveur en attente de connexions (port %d)...\n",
  461. port);
  462. while (online == TRUE) {
  463. sleep (1);
  464. /* Attente d'une connexion */
  465. socket_service = accept (socket_ecoute, (struct sockaddr *) &adresse,
  466. &lg_adresse);
  467. /* SIGPIPE */
  468. if ( (socket_service == -1) && (errno == EINTR) ) continue;
  469. /* PAS DE CONNEXION => mode non bloquant pour attendre un ordre de
  470. * terminaison
  471. */
  472. if ( (socket_service == -1) && (errno == EAGAIN) ) continue;
  473. /* Autre erreur socket */
  474. if (socket_service == -1) {
  475. /* Erreur */
  476. printlog (LOG_ERROR,
  477. "Erreur d'acceptation de socket, errno = %d.\n",
  478. errno);
  479. perror (__FILE__ " accept");
  480. close (socket_ecoute);
  481. return -1;
  482. }
  483. /* Ici connexion acceptee */
  484. printlog (LOG_NOTIFY, "Connexion acceptee...\n");
  485. /* Lancement d'une activite de traitement */
  486. if (pthread_create (&pthread_id, NULL/*pthread_attr_default*/,
  487. (void *) thread_service,
  488. (void *) &socket_service) == -1) {
  489. printlog (LOG_ERROR, "Erreur creation thread de service.\n");
  490. close (socket_service);
  491. }
  492. /* fflush (logfile); */
  493. sleep (2);
  494. }
  495. printlog (LOG_NOTIFY, "Arret du serveur demande.\n");
  496. /* fflush (logfile); */
  497. close (socket_ecoute);
  498. free_list (&clients_list);
  499. free_list (&drinks_list);
  500. pthread_mutex_destroy (&mutex_broadcast);
  501. pthread_mutex_destroy (&mutex_clients);
  502. return 0;
  503. }