utils_func.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * presence module - presence server implementation
  3. *
  4. * Copyright (C) 2006 Voice Sistem S.R.L.
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio 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 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio 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 this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. /*! \file
  24. * \brief Kamailio presence module :: Utility functions
  25. * \ref utils_func.c
  26. * \ingroup presence
  27. */
  28. #ifndef UTILS_FUNC_H
  29. #define UTILS_FUNC_H
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "../../mem/mem.h"
  34. #include "../../dprint.h"
  35. #include "../../str.h"
  36. #include "../../parser/msg_parser.h"
  37. #include "../../parser/parse_event.h"
  38. #define LCONTACT_BUF_SIZE 1024
  39. #define BAD_EVENT_CODE 489
  40. #define INTERVAL_TOO_BRIEF 423
  41. #define EVENT_DIALOG_SLA(ev) \
  42. ((ev)->type == EVENT_DIALOG \
  43. && ((ev)->params.hooks.event_dialog.sla \
  44. || (ev)->params.hooks.event_dialog.ma))
  45. static inline int uandd_to_uri(str user, str domain, str *out)
  46. {
  47. int size;
  48. if(out==0)
  49. return -1;
  50. size = user.len + domain.len+7;
  51. out->s = (char*)pkg_malloc(size);
  52. if(out->s == NULL)
  53. {
  54. LM_ERR("no more memory\n");
  55. return -1;
  56. }
  57. strcpy(out->s,"sip:");
  58. out->len = 4;
  59. if(user.s!=NULL && user.len>0)
  60. {
  61. memcpy(out->s+out->len, user.s, user.len);
  62. out->len += user.len;
  63. out->s[out->len++] = '@';
  64. }
  65. memcpy(out->s + out->len, domain.s, domain.len);
  66. out->len += domain.len;
  67. out->s[out->len] = '\0';
  68. return 0;
  69. }
  70. static inline int ps_fill_local_contact(struct sip_msg* msg, str *contact)
  71. {
  72. str ip;
  73. char* proto;
  74. int port;
  75. int len;
  76. int plen;
  77. contact->s= (char*)pkg_malloc(LCONTACT_BUF_SIZE);
  78. if(contact->s== NULL)
  79. {
  80. LM_ERR("No more memory\n");
  81. goto error;
  82. }
  83. memset(contact->s, 0, LCONTACT_BUF_SIZE);
  84. contact->len= 0;
  85. plen = 3;
  86. if(msg->rcv.proto== PROTO_NONE || msg->rcv.proto==PROTO_UDP)
  87. proto= "udp";
  88. else
  89. if(msg->rcv.proto== PROTO_TLS )
  90. proto= "tls";
  91. else
  92. if(msg->rcv.proto== PROTO_TCP)
  93. proto= "tcp";
  94. else
  95. if(msg->rcv.proto== PROTO_SCTP) {
  96. proto= "sctp";
  97. plen = 4;
  98. }
  99. else
  100. if(msg->rcv.proto== PROTO_WS || msg->rcv.proto== PROTO_WSS) {
  101. proto= "ws";
  102. plen = 2;
  103. }
  104. else
  105. {
  106. LM_ERR("unsupported proto\n");
  107. goto error;
  108. }
  109. ip.s= ip_addr2a(&msg->rcv.dst_ip);
  110. if(ip.s== NULL)
  111. {
  112. LM_ERR("transforming ip_addr to ascii\n");
  113. goto error;
  114. }
  115. ip.len= strlen(ip.s);
  116. port = msg->rcv.dst_port;
  117. if(strncmp(ip.s, "sip:", 4)!=0)
  118. {
  119. strncpy(contact->s, "sip:", 4);
  120. contact->len+= 4;
  121. }
  122. strncpy(contact->s+contact->len, ip.s, ip.len);
  123. contact->len += ip.len;
  124. if(contact->len> LCONTACT_BUF_SIZE - 21)
  125. {
  126. LM_ERR("buffer overflow\n");
  127. goto error;
  128. }
  129. len= sprintf(contact->s+contact->len, ":%d;transport=" , port);
  130. if(len< 0)
  131. {
  132. LM_ERR("unsuccessful sprintf\n");
  133. goto error;
  134. }
  135. contact->len+= len;
  136. strncpy(contact->s+ contact->len, proto, plen);
  137. contact->len += plen;
  138. return 0;
  139. error:
  140. if(contact->s!=NULL)
  141. pkg_free(contact->s);
  142. contact->s = 0;
  143. contact->len = 0;
  144. return -1;
  145. }
  146. //str* int_to_str(long int n);
  147. int a_to_i (char *s,int len);
  148. void to64frombits(unsigned char *out, const unsigned char *in, int inlen);
  149. int send_error_reply(struct sip_msg* msg, int reply_code, str reply_str);
  150. #endif