apps_utils.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef APPS_UTILS_H /** Defined when apps_utils.h has been included. */
  2. #define APPS_UTILS_H
  3. /**
  4. * @nofile apps_utils.h
  5. * @brief
  6. *
  7. * Copyright (C) 2005 Nokia Corporation.
  8. *
  9. * Written by Pekka Pessi <pekka -dot pessi -at- nokia -dot- com>
  10. *
  11. * @STARTLGPL@
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public License
  14. * as published by the Free Software Foundation; either version 2.1 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  25. * 02110-1301 USA
  26. * @ENDLGPL@
  27. *
  28. * @date Created: Thu Apr 8 15:55:15 2004 ppessi
  29. *
  30. */
  31. SOFIA_BEGIN_DECLS
  32. static inline
  33. int proxy_authenticate(context_t *c,
  34. nta_outgoing_t *oreq,
  35. sip_t const *sip,
  36. nta_response_f response_function)
  37. {
  38. if (sip && sip->sip_status->st_status == 407 &&
  39. sip->sip_proxy_authenticate &&
  40. c->c_proxy_auth_retries++ < 3 &&
  41. c->c_proxy && c->c_proxy->url_user && c->c_proxy->url_password) {
  42. url_t *u = c->c_proxy;
  43. msg_t *rmsg = nta_outgoing_getrequest(oreq);
  44. sip_t *rsip = sip_object(rmsg);
  45. if (auc_challenge(&c->c_proxy_auth, c->c_home, sip->sip_proxy_authenticate,
  46. sip_proxy_authorization_class) >= 0
  47. &&
  48. auc_all_credentials(&c->c_proxy_auth, NULL, NULL,
  49. u->url_user, u->url_password) > 0
  50. &&
  51. auc_authorization(&c->c_proxy_auth, rmsg, (msg_pub_t *)rsip,
  52. rsip->sip_request->rq_method_name,
  53. rsip->sip_request->rq_url,
  54. rsip->sip_payload) > 0) {
  55. nta_outgoing_destroy(c->c_orq);
  56. sip_header_remove(rmsg, rsip, (sip_header_t *)rsip->sip_via);
  57. nta_msg_request_complete(rmsg, c->c_leg, 0, NULL, NULL);
  58. c->c_orq = nta_outgoing_tmcreate(c->c_agent, response_function, c, NULL,
  59. rmsg, TAG_END());
  60. return 1;
  61. }
  62. }
  63. return 0;
  64. }
  65. static inline
  66. int server_authenticate(context_t *c,
  67. nta_outgoing_t *oreq,
  68. sip_t const *sip,
  69. nta_response_f response_function)
  70. {
  71. if (sip && sip->sip_status->st_status == 401 &&
  72. sip->sip_www_authenticate &&
  73. c->c_auth_retries++ < 3 &&
  74. c->c_password && c->c_username) {
  75. msg_t *rmsg = nta_outgoing_getrequest(oreq);
  76. sip_t *rsip = sip_object(rmsg);
  77. if (auc_challenge(&c->c_auth, c->c_home, sip->sip_www_authenticate,
  78. sip_authorization_class) >= 0
  79. &&
  80. auc_all_credentials(&c->c_auth, NULL, NULL, c->c_username, c->c_password) > 0
  81. &&
  82. auc_authorization(&c->c_auth, rmsg, (msg_pub_t *)rsip,
  83. rsip->sip_request->rq_method_name,
  84. rsip->sip_request->rq_url,
  85. rsip->sip_payload) > 0) {
  86. nta_outgoing_destroy(c->c_orq);
  87. sip_header_remove(rmsg, rsip, (sip_header_t *)rsip->sip_via);
  88. nta_msg_request_complete(rmsg, c->c_leg, 0, NULL, NULL);
  89. c->c_orq = nta_outgoing_tmcreate(c->c_agent, response_function, c, NULL,
  90. rmsg, TAG_END());
  91. return 1;
  92. }
  93. }
  94. return 0;
  95. }
  96. static inline
  97. int tag_from_header(nta_agent_t *nta,
  98. su_home_t *home,
  99. sip_from_t *f)
  100. {
  101. char *env = getenv("SIPTAG");
  102. char *t = (void *)nta_agent_newtag(home, NULL, nta);
  103. int retval;
  104. if (env) {
  105. char *t1 = su_sprintf(home, "tag=%s-%s", env, t);
  106. su_free(home, t);
  107. t = t1;
  108. }
  109. retval = sip_from_tag(home, f, t);
  110. su_free(home, t);
  111. return retval;
  112. }
  113. SOFIA_END_DECLS
  114. #endif /* !defined APPS_UTILS_H */