PasswordSender.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @file PasswordSender.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 <misc/debug.h>
  23. #include <client/PasswordSender.h>
  24. #define COMPONENT_SINK 1
  25. static void call_handler (PasswordSender *o, int is_error)
  26. {
  27. #ifndef NDEBUG
  28. DEAD_ENTER(o->dead)
  29. #endif
  30. o->handler(o->user, is_error);
  31. #ifndef NDEBUG
  32. ASSERT(DEAD_KILLED)
  33. DEAD_LEAVE(o->dead);
  34. #endif
  35. }
  36. static void error_handler (PasswordSender *o, int component, const void *data)
  37. {
  38. ASSERT(component == COMPONENT_SINK)
  39. DebugObject_Access(&o->d_obj);
  40. call_handler(o, 1);
  41. return;
  42. }
  43. static void sent_handler (PasswordSender *o)
  44. {
  45. DebugObject_Access(&o->d_obj);
  46. call_handler(o, 0);
  47. return;
  48. }
  49. void PasswordSender_Init (PasswordSender *o, uint64_t password, int ssl, BSocket *plain_sock, BPRFileDesc *ssl_bprfd, PasswordSender_handler handler, void *user, BReactor *reactor)
  50. {
  51. ASSERT(ssl == 0 || ssl == 1)
  52. // init arguments
  53. o->password = password;
  54. o->ssl = ssl;
  55. if (ssl) {
  56. o->ssl_bprfd = ssl_bprfd;
  57. } else {
  58. o->plain_sock = plain_sock;
  59. }
  60. o->handler = handler;
  61. o->user = user;
  62. // init dead var
  63. DEAD_INIT(o->dead);
  64. // init error handler
  65. FlowErrorDomain_Init(&o->domain, (FlowErrorDomain_handler)error_handler, o);
  66. // init sink
  67. StreamPassInterface *sink_if;
  68. if (o->ssl) {
  69. PRStreamSink_Init(&o->sink.ssl, FlowErrorReporter_Create(&o->domain, COMPONENT_SINK), o->ssl_bprfd, BReactor_PendingGroup(reactor));
  70. sink_if = PRStreamSink_GetInput(&o->sink.ssl);
  71. } else {
  72. StreamSocketSink_Init(&o->sink.plain, FlowErrorReporter_Create(&o->domain, COMPONENT_SINK), o->plain_sock, BReactor_PendingGroup(reactor));
  73. sink_if = StreamSocketSink_GetInput(&o->sink.plain);
  74. }
  75. // init PacketStreamSender
  76. PacketStreamSender_Init(&o->pss, sink_if, sizeof(o->password), BReactor_PendingGroup(reactor));
  77. // init SinglePacketSender
  78. SinglePacketSender_Init(&o->sps, (uint8_t *)&o->password, sizeof(o->password), PacketStreamSender_GetInput(&o->pss), (SinglePacketSender_handler)sent_handler, o, BReactor_PendingGroup(reactor));
  79. DebugObject_Init(&o->d_obj);
  80. }
  81. void PasswordSender_Free (PasswordSender *o)
  82. {
  83. DebugObject_Free(&o->d_obj);
  84. // free SinglePacketSender
  85. SinglePacketSender_Free(&o->sps);
  86. // free PacketStreamSender
  87. PacketStreamSender_Free(&o->pss);
  88. // free sink
  89. if (o->ssl) {
  90. PRStreamSink_Free(&o->sink.ssl);
  91. } else {
  92. StreamSocketSink_Free(&o->sink.plain);
  93. }
  94. // free dead var
  95. DEAD_KILL(o->dead);
  96. }