Listener.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_SYSTEM_LISTENER_H
  27. #define BADVPN_SYSTEM_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. int existing;
  50. BSocket our_sock;
  51. BSocket *sock;
  52. Listener_handler handler;
  53. void *user;
  54. int accepted;
  55. DebugIn d_in_handler;
  56. DebugObject d_obj;
  57. } Listener;
  58. /**
  59. * Initializes the object.
  60. *
  61. * @param o the object
  62. * @param reactor reactor we live in
  63. * @param addr address to listen on. Must not be invalid.
  64. * @param handler handler function called when a connection should be accepted
  65. * @param user value to pass to handler function
  66. * @return 1 on success, 0 on failure
  67. */
  68. int Listener_Init (Listener *o, BReactor *reactor, BAddr addr, Listener_handler handler, void *user) WARN_UNUSED;
  69. /**
  70. * Initializes the object for listening on an existing socket.
  71. * The socket should be already bound and listened.
  72. *
  73. * @param o the object
  74. * @param reactor reactor we live in
  75. * @param sock socket to listen on
  76. * @param handler handler function called when a connection should be accepted
  77. * @param user value to pass to handler function
  78. * @return 1 on success, 0 on failure
  79. */
  80. void Listener_InitExisting (Listener *o, BReactor *reactor, BSocket *sock, Listener_handler handler, void *user);
  81. /**
  82. * Frees the object.
  83. *
  84. * @param o the object
  85. */
  86. void Listener_Free (Listener *o);
  87. /**
  88. * Accepts a connection.
  89. * Must be called from within the {@link Listener_handler} handler.
  90. *
  91. * @param o the object
  92. * @param sockout uninitialized {@link BSocket} structure to put the new socket in.
  93. * Must not be NULL.
  94. * @param addrout if not NULL, the address of the client will be returned here
  95. * on success
  96. * @return 1 on success, 0 on failure
  97. */
  98. int Listener_Accept (Listener *o, BSocket *sockout, BAddr *addrout) WARN_UNUSED;
  99. #endif