BIPCServer.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 socket
  35. if (BSocket_Init(&o->sock, reactor, BADDR_TYPE_UNIX, BSOCKET_TYPE_STREAM) < 0) {
  36. DEBUG("BSocket_Init failed");
  37. goto fail0;
  38. }
  39. // bind socket
  40. if (BSocket_BindUnix(&o->sock, path) < 0) {
  41. DEBUG("BSocket_BindUnix failed (%d)", BSocket_GetError(&o->sock));
  42. goto fail1;
  43. }
  44. // listen socket
  45. if (BSocket_Listen(&o->sock, -1) < 0) {
  46. DEBUG("BSocket_Listen failed (%d)", BSocket_GetError(&o->sock));
  47. goto fail1;
  48. }
  49. // init listener
  50. Listener_InitExisting(&o->listener, reactor, &o->sock, (Listener_handler)listener_handler, o);
  51. DebugObject_Init(&o->d_obj);
  52. return 1;
  53. fail1:
  54. BSocket_Free(&o->sock);
  55. fail0:
  56. return 0;
  57. }
  58. void BIPCServer_Free (BIPCServer *o)
  59. {
  60. DebugObject_Free(&o->d_obj);
  61. // free listener
  62. Listener_Free(&o->listener);
  63. // free socket
  64. BSocket_Free(&o->sock);
  65. }