pass_communication.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /*
  2. * Copyright (c) 2019 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <sys/socket.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/un.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <poll.h>
  31. #include <time.h>
  32. #include <zlib.h>
  33. #include "../common/common_defs.h"
  34. #include "../commhub/commhub.h"
  35. #include "../commhub/client_utils.h"
  36. #include "passdb.h"
  37. #include "rules.h"
  38. #include "rfid_decoder.h"
  39. //----------GLOBAL STATE VARIABLES
  40. int flush_in_progress = 0; //This flag is used to tell if there is a flush in progress
  41. time_t last_sync_attempt = 0; //Time of the last sync attempt in seconds since epoch
  42. time_t last_sync_ack = 0; //Time of the last ack from the server (update, delete, or nop)
  43. int commhub_fd = -1; //File descriptor of our connection to the comm hub
  44. int server_fd = -1; //File descriptor of our connection to the sync server
  45. int zflush_in_progress = 0;
  46. void *zflush_data = NULL;
  47. int zflush_data_size = 0;
  48. int zflush_idx = 0;
  49. time_t last_piu_update = 0; //Last PIU status update
  50. pass_status real_pass_status = {0};
  51. int update_piu_status_message(int busy)
  52. {
  53. struct message_record outgoing;
  54. time_t now = time(NULL);
  55. struct tm t;
  56. char *status = "READY";
  57. int am_pm;
  58. int hour;
  59. if(busy)
  60. {
  61. status = "BUSY";
  62. }
  63. else
  64. {
  65. if( real_pass_status.flush_status == FLUSH_STATUS_APPLY )
  66. {
  67. status = "APPLY";
  68. }
  69. else if(real_pass_status.flush_status == FLUSH_STATUS_WRITE)
  70. {
  71. status = "SAVING";
  72. }
  73. else if( !rules_loaded() ||
  74. (real_pass_status.flush_status == FLUSH_STATUS_LEGACY) ||
  75. (real_pass_status.flush_status == FLUSH_STATUS_DOWNLOAD) )
  76. {
  77. status = "LOADING";
  78. }
  79. else if( !driver_stat.logged_in_driver )
  80. {
  81. status = "SEE DRIVER";
  82. }
  83. }
  84. if(commhub_fd >= 0)
  85. {
  86. localtime_r(&now, &t);
  87. if( (t.tm_hour < 12) )
  88. {
  89. am_pm = 0;
  90. if(t.tm_hour == 0)
  91. {
  92. hour = 12;
  93. }
  94. else
  95. {
  96. hour = t.tm_hour;
  97. }
  98. }
  99. else
  100. {
  101. am_pm = 1;
  102. if(t.tm_hour == 12)
  103. {
  104. hour = 12;
  105. }
  106. else
  107. {
  108. hour = t.tm_hour - 12;
  109. }
  110. }
  111. format_piu_message(&outgoing, 0, 0, 0, "%d/%d %d:%02d %s %s", t.tm_mon + 1, t.tm_mday, hour, t.tm_min, am_pm?"PM":"AM", status);
  112. send_message(commhub_fd, &outgoing);
  113. if(real_pass_status.flush_status == FLUSH_STATUS_DOWNLOAD)
  114. {
  115. format_piu_message(&outgoing, 1, 0, 0, "Downloading %d%%", real_pass_status.progress_indicator);
  116. send_message(commhub_fd, &outgoing);
  117. }
  118. else if(real_pass_status.flush_status == FLUSH_STATUS_APPLY)
  119. {
  120. format_piu_message(&outgoing, 1, 0, 0, "Applying %d%%", real_pass_status.progress_indicator);
  121. send_message(commhub_fd, &outgoing);
  122. }
  123. else if(real_pass_status.flush_status == FLUSH_STATUS_WRITE)
  124. {
  125. format_piu_message(&outgoing, 1, 0, 0, "Saving Database");
  126. send_message(commhub_fd, &outgoing);
  127. }
  128. else
  129. {
  130. format_piu_message(&outgoing, 1, 0, 0, "");
  131. send_message(commhub_fd, &outgoing);
  132. }
  133. }
  134. return 0;
  135. }
  136. //This function attempts to connect to the pass server...
  137. int connect_to_pass_server()
  138. {
  139. int fd;
  140. int retval;
  141. struct sockaddr_in addr;
  142. fd = socket(PF_INET, SOCK_STREAM, 0);
  143. if(fd < 0)
  144. return -1;
  145. addr.sin_family = AF_INET;
  146. addr.sin_port = htons(PASS_SERVER_PORT);
  147. inet_aton(PASS_SERVER_IP, &addr.sin_addr);
  148. retval = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
  149. if(retval < 0)
  150. {
  151. close(fd);
  152. return -2;
  153. }
  154. return fd;
  155. }
  156. int do_database_flush(passdb_context *ctx)
  157. {
  158. int retval;
  159. //Detach from the current pass database
  160. detach_from_passdb(ctx);
  161. //Format a new database file...
  162. format_new_passdb();
  163. retval = attach_to_passdb(ctx);
  164. if( DB_FAIL(retval) ) //If we fail to attach to our new database
  165. {
  166. fprintf(stderr, "Error (%d) attaching to pass database during flush attempt\n", retval); //report failure
  167. return retval;
  168. }
  169. else //Otherwise, report success.
  170. {
  171. printf("Pass database flushed!\n");
  172. return 0;
  173. }
  174. }
  175. int send_query_message(int fd, passdb_context *ctx)
  176. {
  177. char query[LINE_BUFFER_SIZE] = {0};
  178. int i, n;
  179. int retval;
  180. if(fd < 0)
  181. return -1;
  182. n = sprintf(query, "QUERY\t%llu\n", ctx->seq);
  183. // printf("Sending: \"%s\"\n", query);
  184. i = 0;
  185. while(i < n)
  186. {
  187. retval = send(fd, query + i, n - i, 0);
  188. if(retval <= 0)
  189. return -1;
  190. i += retval;
  191. }
  192. return 0;
  193. }
  194. int send_flushreq_message(int fd)
  195. {
  196. char query[LINE_BUFFER_SIZE] = {0};
  197. int i, n;
  198. int retval;
  199. if(fd < 0)
  200. return -1;
  201. n = sprintf(query, "QUERY\t0\n");
  202. // printf("Sending: \"%s\"\n", query);
  203. i = 0;
  204. while(i < n)
  205. {
  206. retval = send(fd, query + i, n - i, 0);
  207. if(retval <= 0)
  208. return -1;
  209. i += retval;
  210. }
  211. return 0;
  212. }
  213. // This function sends our local buspass state structure (real_pass_status) to other modules if either the force flag is set,
  214. //or if it differs from the last one received from the IPC hub.
  215. //
  216. // This function returns:
  217. //
  218. // < 0 Failure in communication (IPC socket is down...)
  219. // == 0 No news to send (force was not set and our status has not changed)
  220. // == 1 News was successfully sent.
  221. //
  222. // This is useful because often the next thing we want to do after sending a status update is update the display on the
  223. // PIU, but we only want to do that when the update represents a meaningful change which the user will notice (so as not
  224. // to flood that serial port with redundant change notifications).
  225. //
  226. int send_pass_state(int force)
  227. {
  228. struct message_record outgoing_msg;
  229. int retval;
  230. // Only actually perform the send if EITHER: The force flag has been set, OR the status message we are sending
  231. //differs from the last status message we received from ourselves via the IPC hub (which should model what other
  232. //modules think out status currently is).
  233. if(force || memcmp(&real_pass_status, &pass_stat, sizeof(real_pass_status)))
  234. {
  235. prepare_message(&outgoing_msg, MAILBOX_PASS_STATUS, &real_pass_status, sizeof(real_pass_status));
  236. retval = send_message(commhub_fd,&outgoing_msg);
  237. return (retval < 0)?retval:1;
  238. }
  239. return 0;
  240. }
  241. // This function updates our local buspass state structure (real_pass_status) from the internal state of other data structures
  242. //and using the two parameters (flush_status (see ../commhub/commhub.h for values) and a progress indicater (in percent),
  243. //a copy of our local status to other modules if it differs from the last state (using the send_pass_state() function above).
  244. int update_pass_state(int flush_status, int progress)
  245. {
  246. real_pass_status.progress_indicator = 0;
  247. switch(flush_status)
  248. {
  249. case FLUSH_STATUS_DOWNLOAD: //This means we're in the process of downloading a ZFLUSH dataset
  250. real_pass_status.flush_status = FLUSH_STATUS_DOWNLOAD;
  251. if(progress == 0) //if no progress indicator number is supplied
  252. {
  253. if(zflush_data_size != 0) //and it is possible to calculate
  254. {
  255. //use our download count / expected size to calculate a percentage
  256. real_pass_status.progress_indicator = (100 * zflush_idx) / zflush_data_size;
  257. }
  258. }
  259. else //if a progress indicator is supplied, simnply use it...
  260. {
  261. real_pass_status.progress_indicator = progress;
  262. }
  263. break;
  264. case FLUSH_STATUS_APPLY: //In the case of APPLY, WRITE, or LEGACY, just take the progress number on faith
  265. case FLUSH_STATUS_WRITE:
  266. case FLUSH_STATUS_LEGACY:
  267. {
  268. real_pass_status.flush_status = flush_status;
  269. real_pass_status.progress_indicator = progress;
  270. }
  271. break;
  272. default: //Otherwise, just let the other modules know we're feeling normal today...
  273. real_pass_status.flush_status = FLUSH_STATUS_NORMAL;
  274. break;
  275. }
  276. //inform other modules of any changes...
  277. return send_pass_state(0);
  278. }
  279. int do_zflush(passdb_context *ctx, void *data, int len);
  280. //Cleanup after (or abort in progress) a ZFLUSH attempt
  281. int zflush_cleanup()
  282. {
  283. zflush_idx = 0;
  284. flush_in_progress = 0;
  285. zflush_in_progress = 0;
  286. zflush_data_size = 0;
  287. if(zflush_data)
  288. {
  289. free(zflush_data);
  290. }
  291. zflush_data = NULL;
  292. return 0;
  293. }
  294. int handle_pass_update_request(char *line, passdb_context *ctx, int sync)
  295. {
  296. rider_record foo = {0};
  297. char buffer[LINE_BUFFER_SIZE];
  298. int input_idx = 0;
  299. int eol = 0;
  300. //Extract the first tab-delimited field from the input line...
  301. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  302. //If that field is blank, then we ignore this line
  303. if( buffer[0] == '\0' )
  304. {
  305. return 0;
  306. }
  307. if( !strcasecmp(buffer, "UPDATE") ) //========================================== UPDATE
  308. {
  309. //------------------------------------------------------------------ seq
  310. if( eol )
  311. {
  312. fprintf(stderr, "UPDATE: Premature end of line!\n");
  313. return -1;
  314. }
  315. //Get the next field (this should be sequence number...)
  316. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  317. foo.seq = strtoull(buffer, NULL, 10);
  318. if( foo.seq == 0 )
  319. {
  320. fprintf(stderr, "UPDATE: Blank or zero sequence number not allowed!\n");
  321. return -1;
  322. }
  323. //------------------------------------------------------------------ ID
  324. if( eol )
  325. {
  326. fprintf(stderr, "UPDATE: Premature end of line!\n");
  327. return -1;
  328. }
  329. //Get the next field (this should be Rider ID...)
  330. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  331. foo.id = strtoull(buffer, NULL, 10);
  332. if( foo.id == 0 )
  333. {
  334. fprintf(stderr, "UPDATE: Blank or zero rider ID not allowed!\n");
  335. return -1;
  336. }
  337. //------------------------------------------------------------------ Magstripe
  338. if( eol )
  339. {
  340. fprintf(stderr, "UPDATE: Premature end of line!\n");
  341. return -1;
  342. }
  343. //Get the next field (this should be the Magstripe Value...)
  344. input_idx += get_field(foo.magstripe_value, line + input_idx, sizeof(foo.magstripe_value), &eol);
  345. //------------------------------------------------------------------ RFID
  346. if( eol )
  347. {
  348. fprintf(stderr, "UPDATE: Premature end of line!\n");
  349. return -1;
  350. }
  351. //Get the next field (this should be the RFID Value...)
  352. input_idx += get_field(foo.rfid_value, line + input_idx, sizeof(foo.rfid_value), &eol);
  353. //------------------------------------------------------------------ Rule Name
  354. if( eol )
  355. {
  356. fprintf(stderr, "UPDATE: Premature end of line!\n");
  357. return -1;
  358. }
  359. //Get the next field (this should be the Rule Name...)
  360. input_idx += get_field(foo.rule_name, line + input_idx, sizeof(foo.rule_name), &eol);
  361. //------------------------------------------------------------------ Rule Parameter
  362. if( eol )
  363. {
  364. fprintf(stderr, "UPDATE: Premature end of line!\n");
  365. return -1;
  366. }
  367. //Get the next field (this should be the Rule Parameter...)
  368. input_idx += get_field(foo.rule_param, line + input_idx, sizeof(foo.rule_param), &eol);
  369. //If we have extra fields, that's BAD NEWS!
  370. if( !eol )
  371. {
  372. fprintf(stderr, "UPDATE: Too many fields!\n");
  373. return -1;
  374. }
  375. //If everything else is okay, go ahead and update our local database
  376. last_sync_ack = time(NULL);
  377. return update_rider(ctx, &foo, sync); //returning the status of that operation
  378. }
  379. else if( !strcasecmp(buffer, "DELETE") ) //========================================== DELETE
  380. {
  381. //------------------------------------------------------------------ seq
  382. if( eol )
  383. {
  384. fprintf(stderr, "DELETE: Premature end of line!\n");
  385. return -1;
  386. }
  387. //Get the next field (this should be sequence number...)
  388. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  389. foo.seq = strtoull(buffer, NULL, 10);
  390. if( foo.seq == 0 )
  391. {
  392. fprintf(stderr, "DELETE: Blank or zero sequence number not allowed!\n");
  393. return -1;
  394. }
  395. //------------------------------------------------------------------ ID
  396. if( eol )
  397. {
  398. fprintf(stderr, "DELETE: Premature end of line!\n");
  399. return -1;
  400. }
  401. //Get the next field (this should be Rider ID...)
  402. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  403. foo.id = strtoull(buffer, NULL, 10);
  404. if( foo.id == 0 )
  405. {
  406. fprintf(stderr, "DELETE: Blank or zero rider ID not allowed!\n");
  407. return -1;
  408. }
  409. //If we have extra fields, that's BAD NEWS!
  410. if( !eol )
  411. {
  412. fprintf(stderr, "DELETE: Too many fields!\n");
  413. return -1;
  414. }
  415. //If everything else is okay, go ahead and update our local database
  416. last_sync_ack = time(NULL);
  417. return delete_rider(ctx, &foo, sync); //returning the status of that operation
  418. }
  419. else if( !strcasecmp(buffer, "FLUSH") ) //========================================== FLUSH
  420. {
  421. flush_in_progress = 1;
  422. if(update_pass_state(FLUSH_STATUS_LEGACY, 0) > 0)
  423. {
  424. update_piu_status_message(0);
  425. }
  426. fprintf(stderr, "Legacy Flush Requested!\n");
  427. last_sync_ack = time(NULL);
  428. return do_database_flush(ctx);
  429. }
  430. else if( !strcasecmp(buffer, "FLUSHDONE") ) //========================================== FLUSHDONE
  431. {
  432. flush_in_progress = 0;
  433. if(update_pass_state(FLUSH_STATUS_NORMAL, 0) > 0)
  434. {
  435. update_piu_status_message(0);
  436. }
  437. fprintf(stderr, "Legacy Flush Done!\n");
  438. last_sync_ack = time(NULL);
  439. return 0;
  440. }
  441. else if( !strcasecmp(buffer, "ZFLUSH") ) //========================================== FLUSH
  442. {
  443. //------------------------------------------------------------------ seq
  444. if( eol )
  445. {
  446. fprintf(stderr, "ZFLUSH: Premature end of line!\n");
  447. return -1;
  448. }
  449. //Get the next field (this should be sequence number...)
  450. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  451. zflush_data_size = strtoull(buffer, NULL, 10);
  452. zflush_data = malloc(zflush_data_size);
  453. if(zflush_data == NULL)
  454. {
  455. fprintf(stderr, "Can't malloc %d bytes for ZFLUSH!\n", zflush_data_size);
  456. return FAIL_MEM;
  457. }
  458. printf("ZFLUSH: Downloading %d bytes.\n", zflush_data_size);
  459. zflush_idx = 0;
  460. zflush_in_progress = 1;
  461. flush_in_progress = 1;
  462. if(update_pass_state(FLUSH_STATUS_DOWNLOAD, 0) > 0) //Let update_pass_state() calculate our download percentage
  463. {
  464. update_piu_status_message(0);
  465. }
  466. last_sync_ack = time(NULL);
  467. return 0;
  468. }
  469. else if( !strcasecmp(buffer, "ZFLUSHDONE") ) //========================================== FLUSHDONE
  470. {
  471. fprintf(stderr, "ZFlush applying...\n");
  472. if(update_pass_state(FLUSH_STATUS_APPLY, 0) > 0) //Mark that we're starting an apply operation
  473. {
  474. update_piu_status_message(0);
  475. }
  476. do_zflush(ctx, zflush_data, zflush_data_size);
  477. zflush_cleanup();
  478. if(update_pass_state(FLUSH_STATUS_NORMAL, 0) > 0)
  479. {
  480. update_piu_status_message(0);
  481. }
  482. fprintf(stderr, "ZFlush Done!\n");
  483. last_sync_ack = time(NULL);
  484. return 0;
  485. }
  486. else if( !strcasecmp(buffer, "NOP") ) //========================================== NOP (this just updates ACK time)
  487. {
  488. last_sync_ack = time(NULL);
  489. return 0;
  490. }
  491. else
  492. {
  493. fprintf(stderr, "Unrecognized command: %s\n", buffer);
  494. return -1;
  495. }
  496. }
  497. int do_zflush(passdb_context *ctx, void *data, int len)
  498. {
  499. char line[LINE_BUFFER_SIZE] = {0};
  500. z_stream foo = {0};
  501. int zretval;
  502. int retval;
  503. int i,j,k;
  504. int nout;
  505. struct message_record incoming_msg;
  506. foo.zalloc = Z_NULL;
  507. foo.zfree = Z_NULL;
  508. foo.opaque = Z_NULL;
  509. foo.next_in = zflush_data;
  510. foo.avail_in = zflush_data_size;
  511. foo.next_out = (void *)line;
  512. foo.avail_out = sizeof(line);
  513. foo.total_out = 0;
  514. // printf("do_zflush(%p, %p, %d)\n", ctx, data, len);
  515. retval = inflateInit(&foo);
  516. if(retval != Z_OK)
  517. {
  518. fprintf(stderr, "inflateInit failed: Zlib error %d\n", retval);
  519. return FAIL_DATABASE;
  520. }
  521. retval = do_database_flush(ctx);
  522. if( DB_FAIL(retval) )
  523. {
  524. fprintf(stderr, "Database failure during ZFLUSH while performing flush\n");
  525. return retval;
  526. }
  527. do
  528. {
  529. RESET_WATCHDOG();
  530. //Respond to PING messages from the supervisor process lest we get killed off too early...
  531. while(message_socket_status(commhub_fd, MSG_RECV) & MSG_RECV)
  532. {
  533. if(get_message(commhub_fd, &incoming_msg) >= 0)
  534. {
  535. process_message(&incoming_msg);
  536. }
  537. }
  538. zretval = inflate(&foo, Z_NO_FLUSH);
  539. if( (zretval == Z_NEED_DICT) || (zretval == Z_DATA_ERROR) || (zretval == Z_MEM_ERROR) || (zretval == Z_STREAM_ERROR) )
  540. {
  541. fprintf(stderr, "Error inflating data: Zlib error %d\n", zretval);
  542. inflateEnd(&foo);
  543. return FAIL_DATABASE;
  544. }
  545. //The number of output bytes generated is the size of the buffer - number of free bytes
  546. nout = sizeof(line) - foo.avail_out;
  547. // printf("-- %d output bytes\n", nout);
  548. j = 0;
  549. //Scan through this buffer looking for EOL characters
  550. for(i=0; i < nout; i++)
  551. {
  552. if(line[i] == '\n') //if we've found one
  553. {
  554. line[i] = '\0'; //make it a terminating nul
  555. // printf("ZFLUSH->%s\n", line + j);
  556. //handle our pass update
  557. #ifdef ZFLUSH_SLOW_BUT_SAFE
  558. //In slow but safe mode, we msync() (or write()) the memory page containing each record after each update.
  559. //This is hell on the flash, and it takes a long time, but it means that we cannot POSSIBLY write a lower
  560. //sequence number record to the physical storage media before having physically written all previous
  561. //sequence numbers in the batch.
  562. retval = handle_pass_update_request(line + j, ctx, 1);
  563. #else
  564. //In fast mode, we apply all of our update to the memory window without calling msync() or write(), and then
  565. //(see below) when we're done rewriting the mmap()'d window for the local database, then we make one call
  566. //to msync() or write() and splat it all down to flash at once. This gets us a better compression ratio
  567. //on the large chunks of file (like the Cornell passes) which change infrequently and it produces less
  568. //filesystem fragmentation, but it means that it is theoretically possible for a power event to cause a
  569. //partial write to corrupt the local database (requiring a new flush). That being said, when running on
  570. //top of JFFS2, this should cause file truncation, which will be easily detectable and trigger another
  571. //flush request next time the application comes up.
  572. retval = handle_pass_update_request(line + j, ctx, 0);
  573. #endif
  574. if( DB_FAIL(retval) )
  575. {
  576. inflateEnd(&foo);
  577. fprintf(stderr, "Database failure during ZFLUSH while processing line \"%s\"\n", line + j);
  578. return retval;
  579. }
  580. //mark the first character of the next line
  581. j = i + 1;
  582. }
  583. }
  584. //k will be equal to the number of leftover bytes
  585. k = i - j;
  586. // printf("-- i = %d j = %d k = %d\n", i, j, k);
  587. //copy those leftovers back to the beginning
  588. for(i = 0; i < k; i++)
  589. {
  590. line[i] = line[j + i];
  591. }
  592. //and update our output buffer structure
  593. foo.next_out = (void *)(line + k);
  594. foo.avail_out = sizeof(line) - k;
  595. if(foo.avail_out == 0)
  596. {
  597. fprintf(stderr, "Line too long in decompressed ZFLUSH data!\n");
  598. inflateEnd(&foo);
  599. return FAIL_DATABASE;
  600. }
  601. // Update the other modules with our status (APPLY) and a progress indicator based on the amount of compressed data
  602. //read as a percentage of the total ZFLUSH data blob.
  603. if(update_pass_state(FLUSH_STATUS_APPLY, (foo.total_in * 100) / zflush_data_size) > 0)
  604. {
  605. update_piu_status_message(0);
  606. }
  607. } while(zretval != Z_STREAM_END);
  608. inflateEnd(&foo);
  609. //In fast mode, we have to ensure that we write our big block of changes back to secondary storage all at once
  610. #ifndef ZFLUSH_SLOW_BUT_SAFE
  611. if(update_pass_state(FLUSH_STATUS_WRITE, 0) > 0) //Let the other modules know we're performing the final write...
  612. {
  613. update_piu_status_message(0);
  614. }
  615. fprintf(stderr, "Doing one-shot file write...\n");
  616. // This resets the watchdog timer for a double-long timeframe (120 seconds these days...) allowing the write to be
  617. //slow as dirt (this is rarely needed, but if the flash was badly fragmented it can take some time to coalesce free
  618. //blocks and possibly perform some erase cycles.
  619. RESET_WATCHDOG_LONG(2);
  620. // This calls msync() if we're working with a non-broken implementation of mmap() (i.e. non-jffs2) or write()
  621. //to take our updated state from RAM and save it to the underlying file.
  622. sync_all_riders(ctx);
  623. fprintf(stderr, "Done.\n");
  624. #endif
  625. // After completing a ZFLUSH operation, we want to hang up on the server (and reconnect asap). The server should
  626. //also hang up on us after completing transmission of a ZFLUSH dataset. This effectively clears the buffers going both
  627. //ways which prevents tha case where a high communication (or server-side database) lag causes two "QUERY 0" messages
  628. //to be issued in a row, the first one triggering (correctly) a ZFLUSH which will be completed, then the server will read
  629. //the second (now obsolete) "QUERY 0" message and respond to it with another (now just useless and annoying) ZFLUSH.
  630. close(server_fd); //We just do a rude close... The server does a proper shutdown(fd, 2), and the FIN will have reached
  631. //us LONG before we execute this...
  632. server_fd = -1; //Mark the TCP connection to the server as dead so we flush buffers and restart the connection next
  633. //time through the loop.
  634. return 0;
  635. }
  636. //callback handles MAILBOX_UPDATE_PASSES
  637. message_callback_return handle_update_passes_message(struct message_record *msg, void *param)
  638. {
  639. //flag our last sync attempt as NEVER
  640. last_sync_attempt = 0;
  641. return MESSAGE_HANDLED_CONT;
  642. }
  643. //callback handles MAILBOX_FLUSH_PASSES
  644. message_callback_return handle_flush_passes_message(struct message_record *msg, void *param)
  645. {
  646. //We only want to allow a FLUSH_PASSES request to go through if the following conditions are met:
  647. if( (server_fd >= 0) && //1: We have a connection to the server
  648. (tunnel_is_up()) && //2: That connection is real (i.e. the SSH tunnel is in a known good state)
  649. (real_pass_status.flush_status == FLUSH_STATUS_NORMAL) //3: we are not currently in the middle of doing a flush...
  650. )
  651. {
  652. send_flushreq_message(server_fd);
  653. }
  654. return MESSAGE_HANDLED_CONT;
  655. }
  656. //callback handles MAILBOX_STATUS_REQUEST
  657. message_callback_return handle_status_request_message(struct message_record *msg, void *param)
  658. {
  659. // If we're responding to a status request message, we have to force a send even if it seems redundant because odds are
  660. //the requesting module just came on line and has no current status data for us.
  661. send_pass_state(1);
  662. return MESSAGE_HANDLED_CONT;
  663. }
  664. int reject_unknown_card(char *cred)
  665. {
  666. struct message_record bill_msg;
  667. struct message_record user_msg;
  668. struct message_record rider_msg;
  669. format_piu_message(&rider_msg, 1, PIU_PRIORITY_FARE, PASSENGER_MESSAGE_DURATION, "%s %s", REJECT_STR, "UNKNOWN CARD");
  670. format_driver_message(&user_msg, LOGLEVEL_REJECT, "Unknown card");
  671. format_billing_message(&bill_msg, REJECT_STR, "UNKNOWN-CARD", "", "Unknown card", cred, 0, 0);
  672. if(commhub_fd >= 0)
  673. {
  674. send_message(commhub_fd, &bill_msg);
  675. send_message(commhub_fd, &user_msg);
  676. send_message(commhub_fd, &rider_msg);
  677. return 0;
  678. }
  679. else
  680. {
  681. return -1;
  682. }
  683. }
  684. #ifdef REJECT_IF_NO_DRIVER
  685. int reject_no_driver()
  686. {
  687. struct message_record user_msg;
  688. struct message_record rider_msg;
  689. format_piu_message(&rider_msg, 1, PIU_PRIORITY_FARE, PASSENGER_MESSAGE_DURATION, "%s", "SEE DRIVER");
  690. format_driver_message(&user_msg, LOGLEVEL_REJECT, "Not Logged In!");
  691. if(commhub_fd >= 0)
  692. {
  693. send_message(commhub_fd, &user_msg);
  694. send_message(commhub_fd, &rider_msg);
  695. return 0;
  696. }
  697. else
  698. {
  699. return -1;
  700. }
  701. }
  702. #endif
  703. message_callback_return handle_token_mag_message(struct message_record *msg, void *param)
  704. {
  705. int idx;
  706. char cred[LINE_BUFFER_SIZE] = {0};
  707. passdb_context *ctx = (passdb_context *)param;
  708. if(!ctx)
  709. return MESSAGE_HANDLED_CONT;
  710. update_piu_status_message(1);
  711. #ifdef REJECT_IF_NO_DRIVER
  712. if( (driver_stat.logged_in_driver <= 0) || (stop_stat.paddle <= 0) )
  713. {
  714. reject_no_driver();
  715. return MESSAGE_HANDLED_CONT;
  716. }
  717. #endif
  718. idx = smart_find_mag(ctx, (char *)msg->payload, cred);
  719. if(idx < 0)
  720. {
  721. reject_unknown_card(cred);
  722. }
  723. else
  724. {
  725. process_rider(ctx, idx, cred);
  726. }
  727. return MESSAGE_HANDLED_CONT;
  728. }
  729. message_callback_return handle_token_rfid_message(struct message_record *msg, void *param)
  730. {
  731. int idx;
  732. char cred[LINE_BUFFER_SIZE] = {0};
  733. passdb_context *ctx = (passdb_context *)param;
  734. if(!ctx)
  735. return MESSAGE_HANDLED_CONT;
  736. update_piu_status_message(1);
  737. #ifdef REJECT_IF_NO_DRIVER
  738. if( (driver_stat.logged_in_driver <= 0) || (stop_stat.paddle <= 0) )
  739. {
  740. reject_no_driver();
  741. return MESSAGE_HANDLED_CONT;
  742. }
  743. #endif
  744. idx = smart_find_rf(ctx, (char *)msg->payload, cred);
  745. if(idx < 0)
  746. {
  747. reject_unknown_card(cred);
  748. }
  749. else
  750. {
  751. process_rider(ctx, idx, cred);
  752. }
  753. return MESSAGE_HANDLED_CONT;
  754. }
  755. message_callback_return update_anti_passback_cache(struct message_record *msg, void *param)
  756. {
  757. apb_flush_if_needed();
  758. return MESSAGE_HANDLED_CONT;
  759. }
  760. message_callback_return handle_rule_call(struct message_record *msg, void *param)
  761. {
  762. process_driver_rulecall( (driver_rulecall *)msg->payload );
  763. return MESSAGE_HANDLED_CONT;
  764. }
  765. void maintain_ipc_hub_connect(char *progname)
  766. {
  767. struct message_record outgoing_msg;
  768. if(commhub_fd < 0) //if we have no connection to the communication hub
  769. {
  770. commhub_fd = connect_to_message_server(progname); //try and get one
  771. // printf("commhub_fd = %d\n", commhub_fd);
  772. if(commhub_fd >= 0) //if it worked
  773. {
  774. //Subscribe to the command mailboxes we act on
  775. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_TOKEN_MAG, strlen(MAILBOX_TOKEN_MAG));
  776. send_message(commhub_fd,&outgoing_msg);
  777. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_TOKEN_RFID, strlen(MAILBOX_TOKEN_RFID));
  778. send_message(commhub_fd,&outgoing_msg);
  779. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_FLUSH_PASSES, strlen(MAILBOX_FLUSH_PASSES));
  780. send_message(commhub_fd,&outgoing_msg);
  781. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_UPDATE_PASSES, strlen(MAILBOX_UPDATE_PASSES));
  782. send_message(commhub_fd,&outgoing_msg);
  783. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_RULE_CALL, strlen(MAILBOX_RULE_CALL));
  784. send_message(commhub_fd,&outgoing_msg);
  785. //Subscribe to the relevant status management mailboxes
  786. subscribe_to_default_messages(commhub_fd);
  787. //Request updated status information...
  788. prepare_message(&outgoing_msg, MAILBOX_STATUS_REQUEST, "", 0);
  789. send_message(commhub_fd,&outgoing_msg);
  790. }
  791. }
  792. }
  793. int main(int argc, char **argv)
  794. {
  795. struct pollfd fds[2];
  796. int nfds = 0;
  797. int poll_return = 0;
  798. int read_return = 0;
  799. int i;
  800. struct message_record incoming_msg;
  801. char input_line[LINE_BUFFER_SIZE] = {0};
  802. int input_idx = 0;
  803. int checked_idx = 0;
  804. passdb_context ctx = {0};
  805. //------------------
  806. configure_signal_handlers(argv[0]);
  807. maintain_ipc_hub_connect(argv[0]);
  808. read_return = attach_to_passdb(&ctx);
  809. if( read_return < 0 )
  810. {
  811. fprintf(stderr, "Database is missing or corrupt. Attempting to format new database.\n");
  812. read_return = format_new_passdb();
  813. if( read_return < 0 )
  814. {
  815. fprintf(stderr, "Database cannot be created. Aborting!\n");
  816. return -1;
  817. }
  818. else
  819. {
  820. read_return = attach_to_passdb(&ctx);
  821. if( read_return < 0 )
  822. {
  823. fprintf(stderr, "New database is ALSO missing or corrupt. Aborting.\n");
  824. return -1;
  825. }
  826. }
  827. }
  828. //Register our default keep-up-with-system status callbacks
  829. register_system_status_callbacks();
  830. //Add our module-specific callbacks
  831. register_dispatch_callback(MAILBOX_UPDATE_PASSES, CALLBACK_USER(1), handle_update_passes_message, NULL);
  832. register_dispatch_callback(MAILBOX_FLUSH_PASSES, CALLBACK_USER(2), handle_flush_passes_message, NULL);
  833. register_dispatch_callback(MAILBOX_STATUS_REQUEST, CALLBACK_USER(3), handle_status_request_message, NULL);
  834. register_dispatch_callback(MAILBOX_TOKEN_RFID, CALLBACK_USER(4), handle_token_rfid_message, &ctx);
  835. register_dispatch_callback(MAILBOX_TOKEN_MAG, CALLBACK_USER(5), handle_token_mag_message, &ctx);
  836. register_dispatch_callback(MAILBOX_RULE_CALL, CALLBACK_USER(6), handle_rule_call, &ctx);
  837. //Handle status updates which require us to check expiration of the anti-passback cache
  838. register_dispatch_callback(MAILBOX_GPS_STATUS, CALLBACK_USER(6), update_anti_passback_cache, NULL);
  839. register_dispatch_callback(MAILBOX_STOP_STATUS, CALLBACK_USER(7), update_anti_passback_cache, NULL);
  840. while( exit_request_status == EXIT_REQUEST_NONE )
  841. {
  842. time_t now = time(NULL);
  843. RESET_WATCHDOG();
  844. maintain_ipc_hub_connect(argv[0]);
  845. if( (now - last_piu_update) > 0)
  846. {
  847. update_piu_status_message(0);
  848. last_piu_update = now;
  849. }
  850. if(server_fd < 0) //If we don't have a connection to the sync server...
  851. {
  852. if( (now - last_sync_attempt) > DEFAULT_CONNECT_RETRY ) //See if it is time to try again
  853. {
  854. if( tunnel_is_up() ) //and if the tunnel thinks it is up
  855. {
  856. server_fd = connect_to_pass_server(); //if so, try again...
  857. if(server_fd >= 0) //if it worked
  858. {
  859. input_idx = 0; //reset our buffer index
  860. last_sync_attempt = 0;
  861. zflush_cleanup();;
  862. }
  863. else
  864. {
  865. last_sync_attempt = now;
  866. }
  867. }
  868. }
  869. }
  870. if(!rules_loaded())
  871. {
  872. load_rules(RULES_FILE);
  873. }
  874. if(rfid_pattern_loaded() <= 0)
  875. {
  876. load_rfid_decode_patterns(RFID_PATTERN_FILE);
  877. }
  878. if(hup_request_status)
  879. {
  880. hup_request_status = 0;
  881. unload_rules();
  882. load_rules(RULES_FILE);
  883. load_rfid_decode_patterns(RFID_PATTERN_FILE);
  884. }
  885. nfds=0;
  886. if(server_fd >= 0)
  887. {
  888. fds[nfds].fd = server_fd;
  889. fds[nfds].events = POLLIN;
  890. if( (now - last_sync_attempt) > DEFAULT_PASS_SYNC_RETRY )
  891. {
  892. // printf("We want to sync...\n");
  893. fds[nfds].events |= POLLOUT;
  894. }
  895. nfds++;
  896. }
  897. if(commhub_fd >= 0)
  898. {
  899. fds[nfds].fd = commhub_fd;
  900. fds[nfds].events = POLLIN;
  901. nfds++;
  902. }
  903. if(nfds > 0)
  904. {
  905. poll_return = poll(fds, nfds, POLL_TIMEOUT);
  906. }
  907. else
  908. {
  909. usleep(POLL_TIMEOUT * 1000);
  910. poll_return = 0;
  911. }
  912. if(poll_return <= 0)
  913. {
  914. continue;
  915. }
  916. for(i=0; i < nfds; i++)
  917. {
  918. if( fds[i].fd == server_fd )
  919. {
  920. if(fds[i].revents & POLLIN) //If we just got some input...
  921. {
  922. if(zflush_in_progress) //And we're in the middle of downloading ZFLUSH data
  923. {
  924. //Read it into our ZFLUSH buffer
  925. read_return = recv(fds[i].fd, zflush_data + zflush_idx, zflush_data_size - zflush_idx, 0);
  926. //If the socket has closed politely (0), or had an error other than EINTR...
  927. if( (read_return == 0) || ((read_return < 0) && (errno != EINTR)) )
  928. {
  929. printf("Lost touch with server. This may be OK if we just finished downloading ZFLUSH data\n");
  930. close(server_fd);
  931. server_fd = -1;
  932. break;
  933. }
  934. else
  935. {
  936. if(read_return > 0) //Otherwise, if we got some real data
  937. {
  938. zflush_idx += read_return; //advance our download pointer
  939. //We want to let the update_pass_state() function compute our progress bar...
  940. update_pass_state(FLUSH_STATUS_DOWNLOAD, 0);
  941. update_piu_status_message(0);
  942. last_sync_attempt = now; //remember our last sync time
  943. if(zflush_idx == zflush_data_size) //if we've got the whole shebang...
  944. {
  945. //Flag that we're done downloading zflush blob and returning to command mode...
  946. zflush_in_progress = 0;
  947. // We don't have to do anything else about this, because the next thing we will hear
  948. //from the server is ZFLUSHDONE which will cause us to apply our downloaded zflush data.
  949. }
  950. }
  951. }
  952. }
  953. else //If we are not in the middle of a ZFLUSH
  954. {
  955. read_return = recv(fds[i].fd, input_line + input_idx, sizeof(input_line) - input_idx, 0);
  956. //If the socket has closed politely (0), or had an error other than EINTR...
  957. if( (read_return == 0) || ((read_return < 0) && (errno != EINTR)) )
  958. {
  959. printf("Lost touch with server.\n");
  960. close(server_fd);
  961. server_fd = -1;
  962. break;
  963. }
  964. else
  965. {
  966. if(read_return > 0) //and we got some real data
  967. {
  968. input_idx += read_return; //advance our input index
  969. do //loop through the data we've received...
  970. {
  971. if(zflush_in_progress) //if we have begun a ZFLUSH at some point,
  972. {
  973. if(input_idx >= zflush_data_size)
  974. {
  975. int j,k;
  976. memcpy(zflush_data, input_line, zflush_data_size);
  977. zflush_idx = zflush_data_size;
  978. zflush_in_progress = 0;
  979. k = input_idx - zflush_data_size;
  980. for(j = 0; j < k; j++)
  981. {
  982. input_line[j] = input_line[j + zflush_data_size];
  983. }
  984. input_idx = j;
  985. checked_idx = 0;
  986. }
  987. else
  988. {
  989. // Steal the rest of our input buffer and tack it on to the beginning of the zflush
  990. //buffer. We do this because otherwise we'd have the beginning of the zflush data blob
  991. //sitting around collecting dust in the line buffer and we'd download the rest into the
  992. //zflush buffer and be unable to process it because the beginning would still be
  993. //sitting around in this other buffer.
  994. memcpy(zflush_data, input_line, input_idx);
  995. zflush_idx = input_idx; //advance the zflush buffer index appropriately
  996. checked_idx = input_idx = 0; //clear our line buffer index as it is now empty
  997. last_sync_attempt = now; //mark our last sync time as now
  998. //printf("Added %d leftover bytes to zflush_data\n", input_idx);
  999. break; //break out of the do loop since we're done with our input buffer
  1000. }
  1001. }
  1002. //Advance until we either hit the end of the buffer, or we hit a line-terminator
  1003. while(checked_idx < input_idx)
  1004. {
  1005. if( (input_line[checked_idx] == '\r') || (input_line[checked_idx] == '\n') )
  1006. {
  1007. break;
  1008. }
  1009. else
  1010. {
  1011. checked_idx++;
  1012. }
  1013. }
  1014. //If we didn't hit the end of the input... (meaning we got a whole line...)
  1015. if(checked_idx != input_idx)
  1016. {
  1017. int j,k;
  1018. //Null terminate the line we got as a string...
  1019. input_line[checked_idx] = '\0';
  1020. //printf("Got Command \"%s\"\n", input_line);
  1021. if( handle_pass_update_request(input_line, &ctx, 1) < 0 )
  1022. {
  1023. // printf("Command Failed: \"%s\"\n", input_line);
  1024. }
  1025. else
  1026. {
  1027. real_pass_status.last_ack_time = now;
  1028. }
  1029. last_sync_attempt = now; //remember this server contact time
  1030. real_pass_status.last_sync_time = now; //add it to our passdb status
  1031. send_pass_state(0); //update other modules if this represents a change
  1032. // Now that we've done that, we can bump the rest of characters to the beginning
  1033. //of the next line...
  1034. k = input_idx - (checked_idx + 1);
  1035. //copy those characters the the beginning of the buffer...
  1036. for(j=0; j < k; j++)
  1037. {
  1038. input_line[j] = input_line[j + checked_idx + 1];
  1039. }
  1040. input_idx = j; //Move the index to point to the next free byte in the buffer
  1041. checked_idx = 0; // Move the 'checked' index to the beginning so that next time
  1042. //we start counting index 0 as the beginning of the next line when
  1043. //it is time to start scanning for EOL.
  1044. }
  1045. // If we have hit an overflow condition such that our buffer is full and no newline
  1046. //has been received, we want to hang up on the server and try again because something is
  1047. //seriously borked...
  1048. if(input_idx == sizeof(input_line))
  1049. {
  1050. fprintf(stderr, "Input overrun from server (line too long).\n");
  1051. close(server_fd);
  1052. server_fd = -1;
  1053. break;
  1054. }
  1055. } while(checked_idx < input_idx); // While we have unchecked data (that we have yet to check
  1056. //for containing an end-of-line.
  1057. }
  1058. }
  1059. }
  1060. }
  1061. else //Be sure we process all input before closing a remotely closed socket...
  1062. {
  1063. //If we've lost connection, break this loop and poll all over again
  1064. if(fds[i].revents & (POLLERR | POLLHUP | POLLNVAL))
  1065. {
  1066. printf("Lost touch with server.\n");
  1067. close(server_fd);
  1068. server_fd = -1;
  1069. break;
  1070. }
  1071. }
  1072. if(fds[i].revents & POLLOUT) //If we have flagged that we intend to send a query and poll says it's OK...
  1073. {
  1074. // printf("Trying to write to server...\n");
  1075. if(real_pass_status.flush_status == FLUSH_STATUS_NORMAL)
  1076. {
  1077. send_query_message(server_fd, &ctx);
  1078. real_pass_status.last_sync_time = now;
  1079. send_pass_state(0);
  1080. }
  1081. else
  1082. {
  1083. printf("Skipping query opportunity because we are mid-flush.\n");
  1084. }
  1085. //and then update our last sync attempt time
  1086. last_sync_attempt = now;
  1087. }
  1088. }
  1089. else if( fds[i].fd == commhub_fd )
  1090. {
  1091. //If we've lost connection, break this loop and poll all over again
  1092. if(fds[i].revents & (POLLERR | POLLHUP | POLLNVAL))
  1093. {
  1094. close(commhub_fd);
  1095. commhub_fd = -1;
  1096. break;
  1097. }
  1098. if(fds[i].revents & POLLIN)
  1099. {
  1100. // printf("Trying to read from hub...\n");
  1101. read_return = get_message(commhub_fd, &incoming_msg);
  1102. if( read_return < 0 )
  1103. {
  1104. close(commhub_fd);
  1105. commhub_fd = -1;
  1106. break;
  1107. }
  1108. process_message(&incoming_msg); //This passes the received message through the callback list
  1109. }
  1110. }
  1111. }
  1112. }
  1113. printf("Detatching from Pass Database\n");
  1114. detach_from_passdb(&ctx);
  1115. printf("Closing connections\n");
  1116. if(server_fd >= 0)
  1117. {
  1118. close(server_fd);
  1119. server_fd = -1;
  1120. }
  1121. if(commhub_fd >= 0)
  1122. {
  1123. close(commhub_fd);
  1124. server_fd = -1;
  1125. }
  1126. printf("Goodbye.\n");
  1127. return 0;
  1128. }