Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

sip-uas-test.cpp 4.3KB

pirms 10 mēnešiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "sockutil.h"
  2. #include "sip-uac.h"
  3. #include "sip-uas.h"
  4. #include "sip-message.h"
  5. #include "sip-transport.h"
  6. #include "sip-timer.h"
  7. #include "port/ip-route.h"
  8. #include "http-parser.h"
  9. #include "http-header-auth.h"
  10. #include "uri-parse.h"
  11. #include "cstringext.h"
  12. #include "base64.h"
  13. #include "sdp.h"
  14. #include "md5.h"
  15. #include <stdint.h>
  16. #define NAME "34020000001320000001"
  17. #define DOMAIN "192.168.154.1"
  18. struct sip_uas_test_t
  19. {
  20. socket_t udp;
  21. socklen_t addrlen;
  22. struct sockaddr_storage addr;
  23. http_parser_t* parser;
  24. struct sip_agent_t* sip;
  25. };
  26. static int sip_uas_transport_send(void* param, const struct cstring_t* /*protocol*/, const struct cstring_t* /*url*/, const struct cstring_t* /*received*/, int /*rport*/, const void* data, int bytes)
  27. {
  28. struct sip_uas_test_t *test = (struct sip_uas_test_t *)param;
  29. //char p1[1024];
  30. //char p2[1024];
  31. printf("%.*s\n\n", (int)bytes, (const char*)data);
  32. int r = socket_sendto(test->udp, data, bytes, 0, (struct sockaddr*)&test->addr, test->addrlen);
  33. return r == bytes ? 0 : -1;
  34. }
  35. static int sip_uas_oninvite(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, struct sip_dialog_t* dialog, const void* data, int bytes, void** session)
  36. {
  37. const char* ack = "v=0\n"
  38. "o=34020000001320000001 0 0 IN IP4 192.168.128.1\n"
  39. "s=Play\n"
  40. "c=IN IP4 192.168.128.1\n"
  41. "t=0 0\n"
  42. "m=video 20120 RTP/AVP 96 98 97\n"
  43. "a=sendonly\n"
  44. "a=rtpmap:96 PS/90000\n"
  45. "a=rtpmap:98 H264/90000\n"
  46. "a=rtpmap:97 MPEG4/90000\n"
  47. "y=0100000001\n"
  48. "f=v/2/4///a///\n";
  49. const cstring_t* h = sip_message_get_header_by_name(req, "Content-Type");
  50. if (0 == cstrcasecmp(h, "Application/SDP"))
  51. {
  52. sdp_t* sdp = sdp_parse((const char*)data, bytes);
  53. sip_uas_add_header(t, "Content-Type", "application/sdp");
  54. sip_uas_add_header(t, "Contact", "sip:34020000001320000001@192.168.154.1");
  55. assert(0 == sip_uas_reply(t, 200, ack, strlen(ack), param));
  56. sdp_destroy(sdp);
  57. *session = t;
  58. return 0;
  59. }
  60. else
  61. {
  62. assert(0);
  63. *session = t;
  64. return 0;
  65. }
  66. }
  67. /// @param[in] code 0-ok, other-sip status code
  68. /// @return 0-ok, other-error
  69. static int sip_uas_onack(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, struct sip_dialog_t* dialog, int code, const void* data, int bytes)
  70. {
  71. return 0;
  72. }
  73. /// on terminating a session(dialog)
  74. static int sip_uas_onbye(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session)
  75. {
  76. return 0;
  77. }
  78. /// cancel a transaction(should be an invite transaction)
  79. static int sip_uas_oncancel(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session)
  80. {
  81. return 0;
  82. }
  83. /// @param[in] expires in seconds
  84. static int sip_uas_onregister(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const char* user, const char* location, int expires)
  85. {
  86. return 0;
  87. }
  88. static void sip_uas_loop(struct sip_uas_test_t *test)
  89. {
  90. u_short port;
  91. char received[SOCKET_ADDRLEN];
  92. uint8_t buffer[2 * 1024];
  93. do
  94. {
  95. memset(buffer, 0, sizeof(buffer));
  96. test->addrlen = sizeof(test->addr);
  97. int r = socket_recvfrom(test->udp, buffer, sizeof(buffer), 0, (struct sockaddr*)&test->addr, &test->addrlen);
  98. socket_addr_to((struct sockaddr*)&test->addr, test->addrlen, received, &port);
  99. printf("\n[%s:%hu] %s\n", received, port, buffer);
  100. size_t n = r;
  101. if (0 == http_parser_input(test->parser, buffer, &n))
  102. {
  103. struct sip_message_t* request = sip_message_create(SIP_MESSAGE_REQUEST);
  104. r = sip_message_load(request, test->parser);
  105. sip_agent_set_rport(request, received, port);
  106. assert(0 == sip_agent_input(test->sip, request, test));
  107. sip_message_destroy(request);
  108. http_parser_clear(test->parser);
  109. }
  110. } while (1);
  111. }
  112. void sip_uas_test(void)
  113. {
  114. sip_timer_init();
  115. struct sip_uas_handler_t handler;
  116. handler.onregister = sip_uas_onregister;
  117. handler.oninvite = sip_uas_oninvite;
  118. handler.onack = sip_uas_onack;
  119. handler.onbye = sip_uas_onbye;
  120. handler.oncancel = sip_uas_oncancel;
  121. handler.send = sip_uas_transport_send;
  122. struct sip_uas_test_t test;
  123. test.udp = socket_udp();
  124. test.sip = sip_agent_create(&handler);
  125. test.parser = http_parser_create(HTTP_PARSER_REQUEST, NULL, NULL);
  126. socket_bind_any(test.udp, SIP_PORT);
  127. sip_uas_loop(&test);
  128. sip_agent_destroy(test.sip);
  129. socket_close(test.udp);
  130. http_parser_destroy(test.parser);
  131. sip_timer_cleanup();
  132. }