apps_utils.h 3.79 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#ifndef APPS_UTILS_H /** Defined when apps_utils.h has been included. */
#define APPS_UTILS_H "$Id: apps_utils.h,v 1.1.1.1 2005/07/20 20:36:03 kaiv Exp $"

/**
 * @nofile apps_utils.h
 * @brief 
 * 
 * Copyright (C) 2005 Nokia Research Center.
 *
 * Written by Pekka Pessi <pekka -dot pessi -at- nokia -dot- com>
 *
 * @STARTLGPL@
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 * @ENDLGPL@
 * 
 * @par Contributor(s):
 * 
 * @date Created: Thu Apr  8 15:55:15 2004 ppessi
 * 
 * $Date: 2005/07/20 20:36:03 $
 */

static inline
int proxy_authenticate(context_t *c,
		       nta_outgoing_t *oreq,
		       sip_t const *sip,
		       nta_response_f response_function)
{
  if (sip && sip->sip_status->st_status == 407 && 
      sip->sip_proxy_authenticate &&
      c->c_proxy_auth_retries++ < 3 && 
      c->c_proxy && c->c_proxy->url_user && c->c_proxy->url_password) {
    url_t *u = c->c_proxy;
    msg_t *rmsg = nta_outgoing_getrequest(oreq);
    sip_t *rsip = sip_object(rmsg);

    if (auc_challenge(&c->c_proxy_auth, c->c_home, sip->sip_proxy_authenticate,
		      sip_proxy_authorization_class) >= 0 
	&&
	auc_all_credentials(&c->c_proxy_auth, NULL, NULL, 
			    u->url_user, u->url_password) > 0
	&& 
	auc_authorization(&c->c_proxy_auth, rmsg, (msg_pub_t *)rsip,
			  rsip->sip_request->rq_method_name,
			  rsip->sip_request->rq_url,
			  rsip->sip_payload) > 0) {
      nta_outgoing_destroy(c->c_orq);
      sip_header_remove(rmsg, rsip, (sip_header_t *)rsip->sip_via);
      nta_msg_request_complete(rmsg, c->c_leg, 0, NULL, NULL);
      c->c_orq = nta_outgoing_tmcreate(c->c_agent, response_function, c, NULL, 
				       rmsg, TAG_END());
      return 1;
    }
  }

  return 0;
}				   

static inline
int server_authenticate(context_t *c,
			nta_outgoing_t *oreq,
			sip_t const *sip,
			nta_response_f response_function)
{
  if (sip && sip->sip_status->st_status == 401 && 
      sip->sip_www_authenticate &&
      c->c_auth_retries++ < 3 && 
      c->c_password && c->c_username) {
    msg_t *rmsg = nta_outgoing_getrequest(oreq);
    sip_t *rsip = sip_object(rmsg);

    if (auc_challenge(&c->c_auth, c->c_home, sip->sip_www_authenticate,
		      sip_authorization_class) >= 0 
	&&
	auc_all_credentials(&c->c_auth, NULL, NULL, c->c_username, c->c_password) > 0
	&& 
	auc_authorization(&c->c_auth, rmsg, (msg_pub_t *)rsip,
			  rsip->sip_request->rq_method_name,
			  rsip->sip_request->rq_url,
			  rsip->sip_payload) > 0) {
      nta_outgoing_destroy(c->c_orq);
      sip_header_remove(rmsg, rsip, (sip_header_t *)rsip->sip_via);
      nta_msg_request_complete(rmsg, c->c_leg, 0, NULL, NULL);
      c->c_orq = nta_outgoing_tmcreate(c->c_agent, response_function, c, NULL, 
				       rmsg, TAG_END());
      return 1;
    }
  }

  return 0;
}				   

static inline
int tag_from_header(nta_agent_t *nta,
		    su_home_t *home,
		    sip_from_t *f)
{
  char *env = getenv("SIPTAG");
  char *t = (void *)nta_agent_newtag(home, NULL, nta);
  int retval;

  if (env) {
    char *t1 = su_sprintf(home, "tag=%s-%s", env, t);
    su_free(home, t);
    t = t1;
  }

  retval = sip_from_tag(home, f, t);

  su_free(home, t);

  return retval;
}

#endif /* !defined APPS_UTILS_H */