Hi,

      When I send a request to the server through my NTA client, I get the
response of the message(407 Unauthorized) in the callback. So, when the
callback function gets the 407 message it invokes authorize_ua() method. The
problem is I am not able to send the request which authorizes me.  Could you
please help me in the following things :-

1)  Please see why I am not able to send an authorizing request message
"again"(authorize_ua() ). I am not at all getting any response.
     In the authorize_ua func the su_root_run(context->c_root) statement
doesn't run.

2)  Can I call a request method from inside of callback function (using
switch case)?
3) Lastly, .What is the alternative of nua_authenticate() in NTA ? I
couldn't find any function like nta_authenticate etc...


Please find the attachment.

Thank You,
Mayur
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * This example program 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 example program 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 program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/*...@page options Query SIP OPTIONS
 * 
 * @section synopsis Synopsis
 * <tt>options [--bind=url] [--from=url] [-a|--all] to </tt>
 *
 * @section description Description
 * The @em sip-options utility sends a SIP OPTIONS request (or any other SIP
 * request) to a SIP server.
 *
 * The @em sip-options tool will print out status line and interesting headers
 * from the response, excluding From, Via, Call-ID, and CSeq. The message
 * body is also printed.
 *
 * @section options Command Line Options
 * The @e options utility accepts following command line options:
 * <dl>
 * <dt>-m url | --contact=url | --bind=url</dt>
 * <dd>Specifies the SIP URL to which the @em options utility binds.
 * </dd>
 * <dt>--1XX | -1</dt>
 * <dd>Print also preliminary responses. If this option is not present,
 *     preliminary responses are silently discarded.
 * </dd>
 * <dt>--all | -a</dt>
 * <dd>All SIP headers will be printed. If the --all option is given, 
 *     the @em options utility also prints @b From, @b Via, @b Call-ID or
 *     @b CSeq headers.
 * </dd>
 * <dt>--from=url</dt>
 * <dd>Specifies the @b From header. Unless this option is used or the
 *     environment variable @c SIPADDRESS is set, local Contact URL is used
 *     as @b From header as well.
 * </dd>
 * <dt>--mf=n</dt>
 * <dd>Specify the initial Max-Forwards count (defaults to 70, stack default).
 * </dd>
 * <dt>--method=s</dt>
 * <dd>Specify the request method (defaults to OPTIONS).
 * </dd>
 * </dl>
 *
 * @section return Return Codes
 * <table>
 * <tr><td>0<td>when successful (a 2XX-series response is received)
 * <tr><td>1<td>when unsuccessful (a 3XX..6XX-series response is received)
 * <tr><td>2<td>initialization failure
 * </table>
 *
 * @section examples Examples
 * You want to query supported features of sip:essip00net.nokia.com:
 * @code
 * $ options sip:essip00net.nokia.com
 * @endcode
 *
 * @section environment Environment
 * #SIPADDRESS, #sip_proxy, #NTA_DEBUG, #TPORT_DEBUG, #TPORT_LOG.
 * 
 * @section bugs Reporting Bugs
 * Report bugs to <sofia-sip-devel@lists.sourceforge.net>.
 *
 * @section author Author
 * Written by Pekka Pessi <pekka -dot pessi -at- nokia -dot- com>
 *
 * @section copyright Copyright
 * Copyright (C) 2006 Nokia Corporation.
 *
 * This program is free software; see the source for copying conditions.
 * There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.
 */






#include "config.h"
#include "src/test.c" // Gets IP Address of local machine
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "sofia-sip/sip.h"

#include "sofia-sip/nta.h"
#include "sofia-sip/sip_header.h"
#include "sofia-sip/sip_tag.h"
#include "sofia-sip/sl_utils.h"
#include "sofia-sip/sip_util.h"
#include "sofia-sip/auth_client.h"
#include "sofia-sip/tport_tag.h"
#include "sofia-sip/su_tag.h"
#include "sofia-sip/msg_types.h"
#include "sofia-sip/nua_tag.h"
#include "src/apps_utils.h"



typedef struct context_s context_t;
#define NTA_OUTGOING_MAGIC_T context_t

typedef struct  ssc_auth_item_s ssc_auth_item_t;

struct context_s {
  su_home_t       c_home[1];
  su_root_t      *c_root;
  nta_agent_t    *c_agent;
  url_t          *c_proxy;
  char const     *c_username;
  char const     *c_password;
  nta_leg_t      *c_leg;
  nta_outgoing_t *c_orq;
  int             c_all;
  int             c_pre;
  int             c_retval;
};
//  auth_client_t  *c_proxy_auth;
//  auth_client_t  *c_auth;
//  unsigned        c_proxy_auth_retries;
//  unsigned        c_auth_retries;


char const name[] = "options";

static
void usage(int rc)
{
  fprintf(rc ? stderr : stdout, 
          "usage: %s OPTIONS url [extra-file]\n"
          "where OPTIONS are\n"
          "    --method=message_method\n"
          "    --username=username \n"
          "    --password=password\n"
          "    --serverip=ip \n"
          "    --serverport=serverport \n"
          "    --listenerport=portno\n"
          "      sip_url_to \n",
          name);
  exit(rc);
}





static int callback(context_t *context, nta_outgoing_t *oreq, sip_t const *sip);
int send_message( context_t *context, char const *o_to, char const *o_method, 
char const *o_username, 
char const *o_password, char const *o_server_ip, char const *o_server_port, 
char const *o_contact_port);
void authorize_ua(context_t *context, nta_outgoing_t *oreq, sip_t const *sip);
static char *readfile(FILE *f);






int main(int argc, char *argv[])
{
  char *s, *v;

  context_t context[1] = {{{SU_HOME_INIT(context)}}};
  char 
    *o_password = NULL, 
    *o_username = NULL,
    *o_contact_port = NULL,
    *o_method = NULL,
    *o_to = NULL,
    *o_server_ip=NULL,
    *o_server_port=NULL;

   su_home_t *home;
    

     #define MATCH(s, o) \
      ((strncmp(s, o, strlen(o)) == 0))
     #define MATCH1(s, o) \
      ((strncmp(s, o, strlen(o)) == 0) && \
       (v = (s[strlen(o)] ? s + strlen(o) : argv++[1])))
     #define MATCH2(s, o) \
      ((strncmp(s, o, strlen(o)) == 0) && \
       (s[strlen(o)] == '=' || s[strlen(o)] == '\0') && \
       (v = s[strlen(o)] ? s + strlen(o) + 1 : argv++[1]))


  while ((s = argv++[1])) {
    if      (!MATCH(s, "-"))        { o_to = s;           break; }
    else if (strcmp(s, "") == 0)    { o_to = argv++[1];   break; }
   
    else if (MATCH2(s, "--method")) { o_method = v; }  
    else if (MATCH2(s, "--username"))     { o_username = v; }
    else if (MATCH2(s, "--password"))   { o_password = v; }
    else if (MATCH2(s, "--serverip"))    { o_server_ip = v; }
    else if (MATCH2(s, "--serverport"))    { o_server_port = v; }
    else if (MATCH2(s, "--listenerport"))       { o_contact_port = v; }
    else if (MATCH(s,  "--help"))    { usage(0); }
    else 
      usage(2);
  }


  if(strcmp(o_method,"MESSAGE")==0)
          {   
              send_message(context, o_to, o_method, o_username, o_password, 
o_server_ip, o_server_port, o_contact_port);
              exit(0);
          }
 
 }







int send_message(context_t *context, char const *o_to, char const *o_method, 
char const *o_username, char const *o_password, 
                      char const *o_server_ip, char const *o_server_port, char 
const *o_contact_port)
{

  char *extra = NULL; 
  char *o_bind = NULL;
  char *o_http_proxy = NULL;
  char *o_from = NULL;

  char *s, *v;
  char *to_domain;
  char url_from[1024];
  char *str_url_cont;
  char *ipconfig_local;
  char *str_route_url;

  sip_addr_t *from, *to;
  url_string_t *r_uri;


  sip_method_t method = sip_method_message;

  if (!o_to)   usage(2);
  if (!from)  {    fprintf(stderr, "%s: no valid From address\n", name);  
exit(2); }










  su_init();
  su_home_init(context->c_home);

  context->c_root = su_root_create(context);
  context->c_retval = 2;

  su_root_threading(context->c_root, 0);



   if (context->c_root) 
   {
      context->c_agent = nta_agent_create(context->c_root,
                        URL_STRING_MAKE(o_bind),
                        callback, context, 
                        TPTAG_HTTP_CONNECT(o_http_proxy),
                        TAG_END());


    if (context->c_agent) 
      {

      to = sip_to_create(context->c_home, (url_string_t *)o_to);
      
      to_domain = strchr( o_to , '@');            
      int a = sprintf(url_from, "sip:%s%s:%s", o_username,  to_domain, 
o_contact_port);
      from = sip_from_create(context->c_home, url_from ); 
 
      int b = sprintf(str_url_cont , "sip:%...@%s:%s", o_username, 
get_local_ip(), o_contact_port);
      url_t *url_cont = url_make(context->c_home, str_url_cont);
      sip_contact_t *contact_header = sip_contact_create(context->c_home, 
url_cont, TAG_END()); 

      int c = sprintf(str_route_url , "sip:%s:%s;lr", o_server_ip,  
o_server_port);
      url_t *route_url = url_make(context->c_home, str_route_url);
   
      method = sip_method_code(o_method);
     
      r_uri = (url_string_t *)url_hdup(context->c_home, to->a_url);
   


      tag_from_header(context->c_agent, context->c_home, from);

      sip_aor_strip(to->a_url);
      sip_aor_strip(from->a_url);


      context->c_username = from->a_url->url_user;
      context->c_password = from->a_url->url_password;
      from->a_url->url_password = NULL;
      context->c_proxy = url_hdup(context->c_home,   (url_t 
*)getenv("sip_proxy"));



      nta_agent_set_params(context->c_agent,  
NTATAG_SIPFLAGS(MSG_FLG_EXTRACT_COPY),
                                      NTATAG_DEFAULT_PROXY(context->c_proxy),  
TAG_END());


      context->c_leg =  nta_leg_tcreate(context->c_agent, NULL, context, 
                                   SIPTAG_FROM(from), SIPTAG_TO(to), TAG_END());
                                      
      if (context->c_leg) 
          {
                 sip_event_t *sip_event = sip_event_make(context->c_home, 
"sip_event");
                 sip_content_type_t  *sip_content_type_header = 
sip_content_type_make(context->c_home, "sip_content_type");
                 sip_payload_t *sip_payload_message = 
sip_payload_make(context->c_home, "sip_message");
                 sip_separator_t *separator_line =  
sip_separator_make(context->c_home,"sip_saperator");
                 sip_route_t *route_header = sip_route_create(context->c_home, 
route_url, NULL);



           context->c_orq =    nta_outgoing_tcreate(context->c_leg,
                               callback, context, 
                               to->a_url,      
                               method, o_method, r_uri,                
                               SIPTAG_USER_AGENT_STR("ABC XYZ"),
                               SIPTAG_HEADER_STR(extra), 
                               
SIPTAG_ROUTE(route_header),SIPTAG_ROUTE_STR(NULL),
                               SIPTAG_CONTACT(contact_header), 
SIPTAG_CONTACT_STR(NULL),
                               SIPTAG_EVENT(sip_event), 
SIPTAG_EVENT_STR("CPIM"), 
                               SIPTAG_CONTENT_TYPE(sip_content_type_header), 
SIPTAG_CONTENT_TYPE_STR("text/plain"),  
                               
SIPTAG_SEPARATOR(separator_line),SIPTAG_SEPARATOR_STR(NULL),
                               SIPTAG_PAYLOAD(sip_payload_message), 
SIPTAG_PAYLOAD_STR("Hello World"),          
                               TAG_END());


                if (context->c_orq) 
                { 

                  su_root_run(context->c_root); 
                  nta_outgoing_destroy(context->c_orq), context->c_orq = NULL;
                }

         nta_leg_destroy(context->c_leg), context->c_leg = NULL;
        } 

      nta_agent_destroy(context->c_agent), context->c_agent = NULL;
    
   } 
}


      su_root_destroy(context->c_root);
      su_deinit();

  return context->c_retval;
}








static int  callback(context_t *context, nta_outgoing_t *oreq,  sip_t const 
*sip)
{

 if (proxy_authenticate(context, oreq, sip, callback))
    return 0;
 if (server_authenticate(context, oreq, sip, callback))
    return 0;
                                   
 if (sip->sip_status->st_status >= 200 || context->c_pre) 
  {

    sip_header_t *h = (sip_header_t *)sip->sip_status;

    char hname[64];
    printf("\n\n");


    for (; h; h = (sip_header_t *)h->sh_succ) 
          {

              if (!context->c_all) 
                        {
                              if (sip_is_from(h) ||
                                      sip_is_via(h) ||
                                      sip_is_call_id(h) ||
                                      sip_is_cseq(h) ||
                                      sip_is_content_length(h))
                                      continue;
                        }
              if (h->sh_class->hc_name) 
                        {
                                snprintf(hname, sizeof hname, "%s:%%s\n", 
h->sh_class->hc_name);
                                sl_header_print(stdout, hname, h);
                        }
              else 
                        {
                                sl_header_print(stdout, NULL, h); 
                        }
          }
  }
      printf("\n\n");


     if (sip->sip_status->st_status >= 200)
      {
       context->c_retval = sip->sip_status->st_status >= 300;
       su_root_break(context->c_root);
      }
 

     switch(sip->sip_status->st_status)
      {
       case 401 : authorize_ua(context, oreq, sip);  break;
       default : break;
      }


   if (sip->sip_status->st_status >= 200) 
    {
     context->c_retval = sip->sip_status->st_status >= 300;
     su_root_break(context->c_root);
    } 

}











void authorize_ua(context_t *context, nta_outgoing_t *oreq, sip_t const *sip)
{                       
url_string_t *r_uri;
char *auth_str;

if (context->c_root)
{
sip_method_t method = sip_method_message;
char const *o_method = "MESSAGE";

r_uri = (url_string_t *)url_hdup(context->c_home, sip->sip_to->a_url);
                                                                                
                        
sip_www_authenticate_t const *wa = sip->sip_www_authenticate;
msg_param_t const *au_params = wa->au_params;
const char *realm = msg_params_find(au_params, "realm=");
const char *nonce = msg_params_find(au_params, "nonce=");
const char *scheme= "MD5";
const char *o_username = "abc";
const char *o_password = "abc";
auth_str = su_sprintf(context->c_home, "%s:%s:%s:%s", scheme , realm, 
o_username, o_password);


context->c_proxy = url_hdup(context->c_home, (url_t *)getenv("sip_proxy"));

nta_agent_set_params(context->c_agent, NTATAG_SIPFLAGS(MSG_FLG_EXTRACT_COPY),
                           NTATAG_DEFAULT_PROXY(context->c_proxy), TAG_END());


char url_from[1024];
int a = sprintf(url_from, "sip:a...@abplrt.com:5060");
sip_addr_t *from;
from = sip_from_create(context->c_home, url_from );
tag_from_header(context->c_agent, context->c_home, from);


nta_leg_destroy(context->c_leg), context->c_leg = NULL;

      if (context->c_leg) 
       {           context->c_orq =       nta_outgoing_tcreate(context->c_leg,
                               callback, context,
                               sip->sip_to->a_url,
                               method, o_method , r_uri,
                               SIPTAG_USER_AGENT_STR("ABC XYZ"),
                               SIPTAG_HEADER_STR(NULL),
                               
SIPTAG_ROUTE(sip->sip_route),SIPTAG_ROUTE_STR(NULL),
                               SIPTAG_CONTACT(sip->sip_contact), 
SIPTAG_CONTACT_STR(NULL),
                               SIPTAG_EVENT(sip->sip_event), 
SIPTAG_EVENT_STR("CPIM"),
                               SIPTAG_CONTENT_TYPE(sip->sip_content_type), 
SIPTAG_CONTENT_TYPE_STR("text/plain"),
                               
SIPTAG_SEPARATOR(sip->sip_separator),SIPTAG_SEPARATOR_STR(NULL),
                               SIPTAG_PAYLOAD(sip->sip_payload), 
SIPTAG_PAYLOAD_STR("Authentic!"),
          //                   SIPTAG_AUTHORIZATION(sip->sip_authorization) , 
SIPTAG_AUTHORIZATION_STR(auth_str),
                               TAG_END());

         if (context->c_orq) 
          {

          su_root_run(context->c_root);   

//         nta_outgoing_destroy(context->c_orq), context->c_orq = NULL;
          nta_leg_destroy(context->c_leg), context->c_leg = NULL;
          }
          printf("%s\n","Out !");
    }
  }
}



------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Sofia-sip-devel mailing list
Sofia-sip-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sofia-sip-devel

Reply via email to