PasswordSender.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. DEBUGERROR(&o->d_err, o->handler(o->user, is_error))
  28. }
  29. static void error_handler (PasswordSender *o, int component, int code)
  30. {
  31. ASSERT(component == COMPONENT_SINK)
  32. DebugObject_Access(&o->d_obj);
  33. call_handler(o, 1);
  34. return;
  35. }
  36. static void sent_handler (PasswordSender *o)
  37. {
  38. DebugObject_Access(&o->d_obj);
  39. call_handler(o, 0);
  40. return;
  41. }
  42. void PasswordSender_Init (PasswordSender *o, uint64_t password, int ssl, BSocket *plain_sock, BPRFileDesc *ssl_bprfd, PasswordSender_handler handler, void *user, BReactor *reactor)
  43. {
  44. ASSERT(ssl == 0 || ssl == 1)
  45. // init arguments
  46. o->password = password;
  47. o->ssl = ssl;
  48. if (ssl) {
  49. o->ssl_bprfd = ssl_bprfd;
  50. } else {
  51. o->plain_sock = plain_sock;
  52. }
  53. o->handler = handler;
  54. o->user = user;
  55. // init error handler
  56. FlowErrorDomain_Init(&o->domain, (FlowErrorDomain_handler)error_handler, o);
  57. // init sink
  58. StreamPassInterface *sink_if;
  59. if (o->ssl) {
  60. PRStreamSink_Init(&o->sink.ssl, FlowErrorReporter_Create(&o->domain, COMPONENT_SINK), o->ssl_bprfd, BReactor_PendingGroup(reactor));
  61. sink_if = PRStreamSink_GetInput(&o->sink.ssl);
  62. } else {
  63. StreamSocketSink_Init(&o->sink.plain, FlowErrorReporter_Create(&o->domain, COMPONENT_SINK), o->plain_sock, BReactor_PendingGroup(reactor));
  64. sink_if = StreamSocketSink_GetInput(&o->sink.plain);
  65. }
  66. // init PacketStreamSender
  67. PacketStreamSender_Init(&o->pss, sink_if, sizeof(o->password), BReactor_PendingGroup(reactor));
  68. // init SinglePacketSender
  69. SinglePacketSender_Init(&o->sps, (uint8_t *)&o->password, sizeof(o->password), PacketStreamSender_GetInput(&o->pss), (SinglePacketSender_handler)sent_handler, o, BReactor_PendingGroup(reactor));
  70. DebugObject_Init(&o->d_obj);
  71. DebugError_Init(&o->d_err, BReactor_PendingGroup(reactor));
  72. }
  73. void PasswordSender_Free (PasswordSender *o)
  74. {
  75. DebugError_Free(&o->d_err);
  76. DebugObject_Free(&o->d_obj);
  77. // free SinglePacketSender
  78. SinglePacketSender_Free(&o->sps);
  79. // free PacketStreamSender
  80. PacketStreamSender_Free(&o->pss);
  81. // free sink
  82. if (o->ssl) {
  83. PRStreamSink_Free(&o->sink.ssl);
  84. } else {
  85. StreamSocketSink_Free(&o->sink.plain);
  86. }
  87. }