tsip_transport.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO 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
  19. * along with DOUBANGO.
  20. *
  21. */
  22. /**@file tsip_transport.h
  23. * @brief SIP transport.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #ifndef TINYSIP_TRANSPORT_H
  29. #define TINYSIP_TRANSPORT_H
  30. #include "tinysip_config.h"
  31. #include "tinysip/tsip_message.h"
  32. #include "tnet_transport.h"
  33. #include "tsk_object.h"
  34. #include "tsk_list.h"
  35. #include "tsk_string.h"
  36. TSIP_BEGIN_DECLS
  37. #define TSIP_TRANSPORT(self) ((tsip_transport_t*)(self))
  38. enum {
  39. TSIP_TRANSPORT_IDX_UDP,
  40. TSIP_TRANSPORT_IDX_DTLS,
  41. TSIP_TRANSPORT_IDX_TCP,
  42. TSIP_TRANSPORT_IDX_TLS,
  43. TSIP_TRANSPORT_IDX_WS,
  44. TSIP_TRANSPORT_IDX_WSS,
  45. TSIP_TRANSPORT_IDX_MAX
  46. };
  47. typedef struct tsip_transport_idx_xs {
  48. int idx;
  49. const char* name;
  50. enum tnet_socket_type_e type;
  51. }
  52. tsip_transport_idx_xt;
  53. const tsip_transport_idx_xt* tsip_transport_get_by_name(const char* name);
  54. int tsip_transport_get_idx_by_name(const char* name);
  55. enum tnet_socket_type_e tsip_transport_get_type_by_name(const char* name);
  56. typedef struct tsip_transport_stream_peer_s {
  57. TSK_DECLARE_OBJECT;
  58. tnet_fd_t local_fd; // not owner: do not close
  59. enum tnet_socket_type_e type;
  60. tsk_bool_t connected;
  61. uint64_t time_latest_activity; // in milliseconds
  62. uint64_t time_added; // in milliseconds
  63. tsk_bool_t got_valid_sip_msg; // whether we got at least one valid SIP message on this peer
  64. tsk_buffer_t *rcv_buff_stream;
  65. tsk_buffer_t *snd_buff_stream;
  66. // list of dialogs managed by this peer
  67. tsk_strings_L_t *dialogs_cids;
  68. // temp buffers used to send/recv websocket data before (un)masking
  69. struct {
  70. void* rcv_buffer;
  71. uint64_t rcv_buffer_size;
  72. void* snd_buffer;
  73. uint64_t snd_buffer_size;
  74. tsk_bool_t handshaking_done;
  75. } ws;
  76. tnet_ip_t remote_ip;
  77. tnet_port_t remote_port;
  78. }
  79. tsip_transport_stream_peer_t;
  80. TINYSIP_GEXTERN const tsk_object_def_t *tsip_transport_stream_peer_def_t;
  81. typedef tsk_list_t tsip_transport_stream_peers_L_t;
  82. typedef struct tsip_transport_s {
  83. TSK_DECLARE_OBJECT;
  84. tsk_bool_t initialized;
  85. int32_t idx;
  86. const struct tsip_stack_s *stack;
  87. tnet_socket_type_t type;
  88. struct sockaddr_storage pcscf_addr;
  89. tnet_fd_t connectedFD;
  90. tnet_transport_handle_t *net_transport;
  91. const char *scheme;
  92. const char *protocol;
  93. const char *via_protocol;
  94. const char *service; /**< NAPTR service name */
  95. tsip_transport_stream_peers_L_t* stream_peers;
  96. int32_t stream_peers_count;
  97. }
  98. tsip_transport_t;
  99. #define TSIP_DECLARE_TRANSPORT tsip_transport_t __transport__
  100. typedef tsk_list_t tsip_transports_L_t; /**< List of @ref tsip_transport_t elements. */
  101. int tsip_transport_init(tsip_transport_t* self, tnet_socket_type_t type, const struct tsip_stack_s *stack, const char *host, tnet_port_t port, const char* description);
  102. int tsip_transport_deinit(tsip_transport_t* self);
  103. int tsip_transport_tls_set_certs(tsip_transport_t *self, const char* ca, const char* pbk, const char* pvk);
  104. tsk_size_t tsip_transport_send(const tsip_transport_t* self, const char *branch, tsip_message_t *msg, const char* destIP, int32_t destPort);
  105. tsk_size_t tsip_transport_send_raw(const tsip_transport_t* self, const char* dst_host, tnet_port_t dst_port, const void* data, tsk_size_t size, const char* callid);
  106. tsk_size_t tsip_transport_send_raw_ws(const tsip_transport_t* self, tnet_fd_t local_fd, const void* data, tsk_size_t size, const char* callid);
  107. tsip_uri_t* tsip_transport_get_uri(const tsip_transport_t *self, int lr);
  108. int tsip_transport_add_stream_peer_2(tsip_transport_t *self, tnet_fd_t local_fd, enum tnet_socket_type_e type, tsk_bool_t connected, const char* remote_host, tnet_port_t remote_port);
  109. #define tsip_transport_add_stream_peer(self, local_fd, type, connected) tsip_transport_add_stream_peer_2((self), (local_fd), (type), (connected), tsk_null, 0)
  110. #define tsip_transport_stream_peers_lock(self) tsk_list_lock((self)->stream_peers)
  111. #define tsip_transport_stream_peers_unlock(self) tsk_list_unlock((self)->stream_peers)
  112. tsip_transport_stream_peer_t* tsip_transport_find_stream_peer_by_local_fd(tsip_transport_t *self, tnet_fd_t local_fd);
  113. tsip_transport_stream_peer_t* tsip_transport_pop_stream_peer_by_local_fd(tsip_transport_t *self, tnet_fd_t local_fd);
  114. tsip_transport_stream_peer_t* tsip_transport_find_stream_peer_by_remote_ip(tsip_transport_t *self, const char* remote_ip, tnet_port_t remote_port, enum tnet_socket_type_e type);
  115. tsk_bool_t tsip_transport_have_stream_peer_with_remote_ip(tsip_transport_t *self, const char* remote_ip, tnet_port_t remote_port, enum tnet_socket_type_e type);
  116. tsk_bool_t tsip_transport_have_stream_peer_with_local_fd(tsip_transport_t *self, tnet_fd_t local_fd);
  117. int tsip_transport_remove_stream_peer_by_local_fd(tsip_transport_t *self, tnet_fd_t local_fd);
  118. int tsip_transport_remove_callid_from_stream_peers(tsip_transport_t *self, const char* callid, tsk_bool_t* removed);
  119. tsk_bool_t tsip_transport_stream_peer_have_callid(const tsip_transport_stream_peer_t* self, const char* callid);
  120. int tsip_transport_stream_peer_add_callid(tsip_transport_stream_peer_t* self, const char* callid);
  121. int tsip_transport_stream_peer_remove_callid(tsip_transport_stream_peer_t* self, const char* callid, tsk_bool_t *removed);
  122. int tsip_transport_stream_peers_cleanup(tsip_transport_t *self);
  123. #define tsip_transport_tls_set_certs(transport, ca, pbk, pvk, verify) (transport ? tnet_transport_tls_set_certs(transport->net_transport, ca, pbk, pvk, verify) : -1)
  124. #define tsip_transport_start(transport) (transport ? tnet_transport_start(transport->net_transport) : -1)
  125. #define tsip_transport_isready(transport) (transport ? tnet_transport_isready(transport->net_transport) : -1)
  126. #define tsip_transport_issecure(transport) (transport ? tnet_transport_issecure(transport->net_transport) : 0)
  127. #define tsip_transport_isconnected(transport) (transport ? tnet_transport_isconnected(transport->net_transport, transport->connectedFD) : 0)
  128. #define tsip_transport_get_description(transport) (transport ? tnet_transport_get_description(transport->net_transport) : 0)
  129. #define tsip_transport_get_ip_n_port(transport, ip, port) (transport ? tnet_transport_get_ip_n_port(transport->net_transport, transport->connectedFD, ip, port) : -1)
  130. #define tsip_transport_get_public_ip_n_port(transport, ip, port) (transport ? tnet_transport_get_public_ip_n_port(transport->net_transport, transport->connectedFD, ip, port) : -1)
  131. #define tsip_transport_connectto(transport, host, port, type) (transport ? (transport->connectedFD=tnet_transport_connectto(transport->net_transport, host, port, type)) : TNET_INVALID_FD)
  132. #define tsip_transport_connectto_2(transport, host, port) (transport ? (transport->connectedFD=tnet_transport_connectto_2(transport->net_transport, host, port)) : TNET_INVALID_FD)
  133. #define tsip_transport_set_callback(transport, callback, callback_data) (transport ? tnet_transport_set_callback(transport->net_transport, callback, callback_data) : -1)
  134. #define tsip_transport_have_socket(transport, fd) (transport ? tnet_transport_have_socket(transport->net_transport, fd) : 0)
  135. #define tsip_transport_add_socket(transport, fd, type, take_ownership, isClient) (transport ? tnet_transport_add_socket(transport->net_transport, fd, type, take_ownership, isClient, tsk_null) : -1)
  136. #define tsip_transport_remove_socket(transport, fd) (transport ? tnet_transport_remove_socket(transport->net_transport, fd) : -1)
  137. //#define tsip_transport_get_socket_type(transport) (transport ? tnet_transport_get_socket_type(transport->net_transport) : tnet_socket_type_invalid)
  138. #define tsip_transport_shutdown(transport) (transport ? tnet_transport_shutdown(transport->net_transport) : -1)
  139. tsip_transport_t* tsip_transport_create(struct tsip_stack_s* stack, const char* host, tnet_port_t port, tnet_socket_type_t type, const char* description);
  140. TINYSIP_GEXTERN const tsk_object_def_t *tsip_transport_def_t;
  141. TSIP_END_DECLS
  142. #endif /* TINYSIP_TRANSPORT_H */