Listener.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @file Listener.h
  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. * @section DESCRIPTION
  23. *
  24. * Object used to listen on a socket and accept clients.
  25. */
  26. #ifndef BADVPN_LISTENER_LISTENER_H
  27. #define BADVPN_LISTENER_LISTENER_H
  28. #include <misc/dead.h>
  29. #include <misc/debugcounter.h>
  30. #include <misc/debugin.h>
  31. #include <system/DebugObject.h>
  32. #include <system/BSocket.h>
  33. /**
  34. * Handler function called when it may be possible to accept a client.
  35. * The user can call {@link Listener_Accept} from this handler to accept
  36. * clients.
  37. * If the user does not call {@link Listener_Accept}, a newly connected
  38. * client may be disconnected.
  39. *
  40. * @param user as in {@link Listener_Init}
  41. */
  42. typedef void (*Listener_handler) (void *user);
  43. /**
  44. * Object used to listen on a socket and accept clients.
  45. */
  46. typedef struct {
  47. dead_t dead;
  48. BReactor *reactor;
  49. BSocket sock;
  50. Listener_handler handler;
  51. void *user;
  52. int accepted;
  53. DebugIn d_in_handler;
  54. DebugObject d_obj;
  55. } Listener;
  56. /**
  57. * Initializes the object.
  58. *
  59. * @param o the object
  60. * @param reactor reactor we live in
  61. * @param addr address to listen on. Must not be invalid.
  62. * @param handler handler function called when a connection should be accepted
  63. * @param user value to pass to handler function
  64. * @return 1 on success, 0 on failure
  65. */
  66. int Listener_Init (Listener *o, BReactor *reactor, BAddr addr, Listener_handler handler, void *user) WARN_UNUSED;
  67. /**
  68. * Frees the object.
  69. *
  70. * @param o the object
  71. */
  72. void Listener_Free (Listener *o);
  73. /**
  74. * Accepts a connection.
  75. * Must be called from within the {@link Listener_handler} handler.
  76. *
  77. * @param o the object
  78. * @param sockout uninitialized {@link BSocket} structure to put the new socket in.
  79. * Must not be NULL.
  80. * @param addrout if not NULL, the address of the client will be returned here
  81. * on success
  82. * @return 1 on success, 0 on failure
  83. */
  84. int Listener_Accept (Listener *o, BSocket *sockout, BAddr *addrout) WARN_UNUSED;
  85. #endif