test_tls.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 2014 Mamadou DIOP.
  2. *
  3. * This file is part of Open Source Doubango Framework.
  4. *
  5. * DOUBANGO is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * DOUBANGO is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with DOUBANGO.
  17. *
  18. */
  19. #ifndef TNET_TEST_TLS_H
  20. #define TNET_TEST_TLS_H
  21. #define TEST_TLS_REMOTE_IP "192.168.16.225"
  22. #define TEST_TLS_REMOTE_PORT 4061
  23. #define TLS_TEST_SIP_MESSAGE \
  24. "REGISTER sip:micromethod.com SIP/2.0\r\n" \
  25. "Via: SIP/2.0/%s %s:%d;rport;branch=z9hG4bK1245420841406%d\r\n" \
  26. "From: <sip:mamadou@micromethod.com>;tag=29358\r\n" \
  27. "To: <sip:mamadou@micromethod.com>\r\n" \
  28. "Call-ID: M-fa53180346f7f55ceb8d8670f9223dbb\r\n" \
  29. "CSeq: 201 REGISTER\r\n" \
  30. "Max-Forwards: 70\r\n" \
  31. "Contact: <sip:mamadou@%s:%d;transport=%s>\r\n" \
  32. "Expires: 10\r\n" \
  33. "\r\n"
  34. static int tnet_tls_cb(const tnet_transport_event_t* e)
  35. {
  36. switch(e->type) {
  37. case event_data: {
  38. TSK_DEBUG_INFO("--- TLS ---\n%s\n", (const char*)e->data);
  39. break;
  40. }
  41. case event_closed:
  42. case event_connected:
  43. default: {
  44. break;
  45. }
  46. }
  47. return 0;
  48. }
  49. void test_tls()
  50. {
  51. tnet_transport_handle_t *transport = tnet_transport_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, tnet_socket_type_tls_ipv4, "TLS/IPV4 TRANSPORT");
  52. tnet_ip_t ip;
  53. tnet_port_t port;
  54. tnet_fd_t fd = TNET_INVALID_FD;
  55. if(tnet_transport_start(transport)) {
  56. TSK_DEBUG_ERROR("Failed to create %s.", tnet_transport_get_description(transport));
  57. return;
  58. }
  59. /* Set our callback function */
  60. tnet_transport_set_callback(transport, tnet_tls_cb, "callbackdata");
  61. /* Connect to the SIP Registrar */
  62. if((fd = tnet_transport_connectto_2(transport, TEST_TLS_REMOTE_IP, TEST_TLS_REMOTE_PORT)) == TNET_INVALID_FD) {
  63. TSK_DEBUG_ERROR("Failed to connect %s.", tnet_transport_get_description(transport));
  64. return;
  65. }
  66. if(tnet_sockfd_waitUntilWritable(fd, TNET_CONNECT_TIMEOUT)) {
  67. TSK_DEBUG_ERROR("%d milliseconds elapsed and the socket is still not connected.", TNET_CONNECT_TIMEOUT);
  68. tnet_transport_remove_socket(transport, &fd);
  69. return;
  70. }
  71. /* Send our SIP message */
  72. {
  73. char* message = 0;
  74. tnet_transport_get_ip_n_port(transport, fd, &ip, &port);
  75. tsk_sprintf(&message, TLS_TEST_SIP_MESSAGE, "TLS", ip, port, port, ip, port, "tls");
  76. if(!tnet_transport_send(transport, fd, message, strlen(message))) {
  77. TSK_DEBUG_ERROR("Failed to send data using TCP/IPv4 transport.");
  78. TSK_FREE(message);
  79. return;
  80. }
  81. TSK_FREE(message);
  82. }
  83. TSK_OBJECT_SAFE_FREE(transport);
  84. }
  85. #endif /* TNET_TEST_TLS_H */