Listener.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <base/DebugObject.h>
  29. #include <system/BSocket.h>
  30. /**
  31. * Handler function called when it may be possible to accept a client.
  32. * The user can call {@link Listener_Accept} from this handler to accept
  33. * clients.
  34. * If the user does not call {@link Listener_Accept}, a newly connected
  35. * client may be disconnected.
  36. *
  37. * @param user as in {@link Listener_Init}
  38. */
  39. typedef void (*Listener_handler) (void *user);
  40. /**
  41. * Object used to listen on a socket and accept clients.
  42. */
  43. typedef struct {
  44. BReactor *reactor;
  45. int existing;
  46. BSocket our_sock;
  47. BSocket *sock;
  48. Listener_handler handler;
  49. void *user;
  50. BPending accept_job;
  51. DebugObject d_obj;
  52. } Listener;
  53. /**
  54. * Initializes the object.
  55. *
  56. * @param o the object
  57. * @param reactor reactor we live in
  58. * @param addr address to listen on. Must not be invalid.
  59. * @param handler handler function called when a connection should be accepted
  60. * @param user value to pass to handler function
  61. * @return 1 on success, 0 on failure
  62. */
  63. int Listener_Init (Listener *o, BReactor *reactor, BAddr addr, Listener_handler handler, void *user) WARN_UNUSED;
  64. /**
  65. * Initializes the object for listening on an existing socket.
  66. * The socket should be already bound and listened.
  67. *
  68. * @param o the object
  69. * @param reactor reactor we live in
  70. * @param sock socket to listen on
  71. * @param handler handler function called when a connection should be accepted
  72. * @param user value to pass to handler function
  73. * @return 1 on success, 0 on failure
  74. */
  75. void Listener_InitExisting (Listener *o, BReactor *reactor, BSocket *sock, Listener_handler handler, void *user);
  76. /**
  77. * Frees the object.
  78. *
  79. * @param o the object
  80. */
  81. void Listener_Free (Listener *o);
  82. /**
  83. * Accepts a connection.
  84. * Must be called from within the {@link Listener_handler} handler or its jobs, and
  85. * at most once.
  86. *
  87. * @param o the object
  88. * @param sockout uninitialized {@link BSocket} structure to put the new socket in.
  89. * Must not be NULL.
  90. * @param addrout if not NULL, the address of the client will be returned here
  91. * on success
  92. * @return 1 on success, 0 on failure
  93. */
  94. int Listener_Accept (Listener *o, BSocket *sockout, BAddr *addrout) WARN_UNUSED;
  95. #endif