BIPCServer.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @file BIPCServer.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <ipc/BIPCServer.h>
  23. static void listener_handler (BIPCServer *o)
  24. {
  25. DebugObject_Access(&o->d_obj);
  26. o->handler(o->user);
  27. return;
  28. }
  29. int BIPCServer_Init (BIPCServer *o, const char *path, BIPCServer_handler handler, void *user, BReactor *reactor)
  30. {
  31. // init arguments
  32. o->handler = handler;
  33. o->user = user;
  34. // init dead var
  35. DEAD_INIT(o->dead);
  36. // init socket
  37. if (BSocket_Init(&o->sock, reactor, BADDR_TYPE_UNIX, BSOCKET_TYPE_STREAM) < 0) {
  38. DEBUG("BSocket_Init failed");
  39. goto fail0;
  40. }
  41. // bind socket
  42. if (BSocket_BindUnix(&o->sock, path) < 0) {
  43. DEBUG("BSocket_BindUnix failed (%d)", BSocket_GetError(&o->sock));
  44. goto fail1;
  45. }
  46. // listen socket
  47. if (BSocket_Listen(&o->sock, -1) < 0) {
  48. DEBUG("BSocket_Listen failed (%d)", BSocket_GetError(&o->sock));
  49. goto fail1;
  50. }
  51. // init listener
  52. Listener_InitExisting(&o->listener, reactor, &o->sock, (Listener_handler)listener_handler, o);
  53. DebugObject_Init(&o->d_obj);
  54. return 1;
  55. fail1:
  56. BSocket_Free(&o->sock);
  57. fail0:
  58. return 0;
  59. }
  60. void BIPCServer_Free (BIPCServer *o)
  61. {
  62. DebugObject_Free(&o->d_obj);
  63. // free listener
  64. Listener_Free(&o->listener);
  65. // free socket
  66. BSocket_Free(&o->sock);
  67. // free dead var
  68. DEAD_KILL(o->dead);
  69. }