reSIProcate/stack  9694
testForce.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 #include <iostream>
00006 
00007 #if defined (HAVE_POPT_H) 
00008 #include <popt.h>
00009 #else
00010 #ifndef WIN32
00011 #warning "will not work very well without libpopt"
00012 #endif
00013 #endif
00014 
00015 #include "resip/stack/SipStack.hxx"
00016 //#include "resip/stack/UdpTransport.hxx"
00017 #include "resip/stack/Helper.hxx"
00018 #include "resip/stack/SipMessage.hxx"
00019 #include "resip/stack/Uri.hxx"
00020 #include "rutil/Data.hxx"
00021 #include "rutil/DnsUtil.hxx"
00022 #include "rutil/Logger.hxx"
00023 #include "rutil/DataStream.hxx"
00024 #include "resip/stack/MethodHash.hxx"
00025 
00026 using namespace resip;
00027 using namespace std;
00028 
00029 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00030 
00031 int
00032 main(int argc, char* argv[])
00033 {
00034    char* logType = 0;
00035    char* logLevel = 0;
00036    int sipPort = 5060;
00037    char * ruri = 0;
00038    char * toUri = 0;
00039    char * fromUri = 0;
00040    char * contactUri = 0;
00041    char * targetUri = 0;
00042    char * method = 0;
00043    int seltime = 500;
00044 
00045 #if defined (HAVE_POPT_H) 
00046    struct poptOption table[] = {
00047        {"sip-port",   'p', POPT_ARG_INT,    &sipPort,   0, "Port for SIP stack to listen / send on.",0 },
00048        {"select-time", 'T', POPT_ARG_INT,    &seltime,  0, "Select upper bound in ms.", 0},
00049        {"log-type",    'l', POPT_ARG_STRING, &logType,   0, "where to send logging messages", "syslog|cerr|cout"},
00050        {"log-level",   'v', POPT_ARG_STRING, &logLevel,  0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
00051        {"ruri",        'r', POPT_ARG_STRING, &ruri,      0, "The Request-URI for this message", 0},
00052        {"to",          't', POPT_ARG_STRING, &toUri,     0, "The To: Header URI", 0},
00053        {"from",        'f', POPT_ARG_STRING, &fromUri,   0, "The From: Header URI", 0},
00054        {"ctc",         'm', POPT_ARG_STRING, &contactUri,0, "The Contact: URI", 0},
00055        {"target-uri",  'g', POPT_ARG_STRING, &targetUri, 0, "The target (forced) URI", 0},
00056        {"method",      'M', POPT_ARG_STRING, &method,    0, "The method to use", "REGISTER|INVITE|OPTIONS|MESSAGE|CANCEL|ACK|BYE"},
00057        POPT_AUTOHELP
00058        { 0, 0, 0, 0, 0 }
00059    };
00060    poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
00061    poptGetNextOpt(context);
00062    poptFreeContext(context);
00063 #endif
00064 
00065    Log::initialize(logType, logLevel, argv[0]);
00066    
00067    if (!ruri)
00068    {
00069        ErrLog(<<"Not much to do with an RURI");
00070        return -1;
00071    }
00072    
00073    if (!toUri)
00074    {
00075        InfoLog(<<"No To URI, using URI." << ruri);
00076        toUri = ruri;
00077    }
00078    if (!fromUri)
00079    {
00080        InfoLog(<<"No From URI, using To URI." << toUri);
00081        fromUri = toUri;
00082    }
00083 
00084    MethodTypes meth(OPTIONS);
00085 
00086    if (method)
00087    {
00088        meth=getMethodType(method);
00089        if (meth == UNKNOWN)
00090        {
00091            ErrLog(<<"Unknown method, using OPTIONS for now: " << method );
00092            meth = OPTIONS;
00093        }
00094    }
00095 
00096    InfoLog(<<"Using method: " << resip::MethodNames[meth]);
00097 
00098    // resolve method.
00099    
00100    Fifo<Message> txFifo;
00101 
00102    auto_ptr<SipStack> stack( new SipStack );
00103 
00104    stack->addTransport(resip::UDP,sipPort);
00105    stack->addTransport(resip::TCP,sipPort);
00106 
00107    NameAddr ruriNA(ruri);
00108    
00109    NameAddr from(fromUri);
00110    NameAddr to(toUri);
00111 
00112    InfoLog (<< "Creating messages");
00113 
00114    auto_ptr<SipMessage> msg(new SipMessage);
00115 
00116    NameAddr contact;
00117    if (!contactUri)
00118    {
00119        InfoLog(<<"Warning: leaving contact: header empty.");
00120    }
00121    else
00122    {
00123        contact = NameAddr(contactUri);
00124        msg->header(h_Contacts).push_front(contact);
00125    }
00126 
00127    if (targetUri)
00128    {
00129        Uri forceTarget(targetUri);
00130        InfoLog(<<"Setting force target to " << forceTarget);
00131        msg->setForceTarget(forceTarget);
00132    }
00133 
00134    if (meth != UNKNOWN)
00135    {
00136        msg->header(h_RequestLine) = RequestLine(meth);
00137        msg->header(h_CSeq).method() = meth;
00138    }
00139    else
00140    {
00141        msg->header(h_RequestLine) = RequestLine(UNKNOWN);
00142        msg->header(h_RequestLine).unknownMethodName() = method;
00143        msg->header(h_CSeq).method() = UNKNOWN;
00144        msg->header(h_CSeq).unknownMethodName() = Data(method);
00145    }
00146 
00147    msg->header(h_RequestLine).uri() = ruriNA.uri();
00148    msg->header(h_To) = to;
00149    msg->header(h_From) = from;
00150    msg->header(h_CSeq).sequence() = 1;
00151 
00152    Via v; 
00153    msg->header(h_Vias).push_front(v);
00154    msg->header(h_CallId).value() = Helper::computeCallId();
00155 
00156 
00157 
00158    //DnsUtil::inet_pton("127.0.0.1", in);
00159    //Tuple dest(in, target.uri().port(), UDP);
00160    //InfoLog (<< "Sending to " << dest);
00161 
00162    InfoLog(<<"Sending Message: " << msg->brief());
00163    DebugLog(<<"Sending Message:" << endl << *msg);
00164 
00165    stack->send(*msg);
00166    bool done = false;
00167    while(!done)
00168    {
00169        FdSet fdset; 
00170        stack->buildFdSet(fdset);
00171        fdset.selectMilliSeconds(seltime); 
00172        stack->process(fdset);
00173        SipMessage* reply = stack->receive();
00174        if (!reply) continue;
00175 
00176        InfoLog(<<"Got reply: " << reply->brief());
00177        DebugLog(<<"Got reply:" << endl << *reply);
00178        ++done;
00179        
00180    }
00181 
00182    InfoLog (<< "Finished ");
00183    
00184 
00185    return 0;
00186 }
00187 /* ====================================================================
00188  * The Vovida Software License, Version 1.0 
00189  * 
00190  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00191  * 
00192  * Redistribution and use in source and binary forms, with or without
00193  * modification, are permitted provided that the following conditions
00194  * are met:
00195  * 
00196  * 1. Redistributions of source code must retain the above copyright
00197  *    notice, this list of conditions and the following disclaimer.
00198  * 
00199  * 2. Redistributions in binary form must reproduce the above copyright
00200  *    notice, this list of conditions and the following disclaimer in
00201  *    the documentation and/or other materials provided with the
00202  *    distribution.
00203  * 
00204  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00205  *    and "Vovida Open Communication Application Library (VOCAL)" must
00206  *    not be used to endorse or promote products derived from this
00207  *    software without prior written permission. For written
00208  *    permission, please contact vocal@vovida.org.
00209  *
00210  * 4. Products derived from this software may not be called "VOCAL", nor
00211  *    may "VOCAL" appear in their name, without prior written
00212  *    permission of Vovida Networks, Inc.
00213  * 
00214  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00215  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00216  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00217  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00218  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00219  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00220  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00221  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00222  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00223  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00224  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00225  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00226  * DAMAGE.
00227  * 
00228  * ====================================================================
00229  * 
00230  * This software consists of voluntary contributions made by Vovida
00231  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00232  * Inc.  For more information on Vovida Networks, Inc., please see
00233  * <http://www.vovida.org/>.
00234  *
00235  */