reSIProcate/stack  9694
UAS.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 #if defined (HAVE_POPT_H) 
00006 #include <popt.h>
00007 #else
00008 #ifndef WIN32
00009 #warning "will not work very well without libpopt"
00010 #endif
00011 #endif
00012 
00013 #include <sys/types.h>
00014 #include <iostream>
00015 #include <signal.h>
00016 #include <memory>
00017 
00018 #include "rutil/DnsUtil.hxx"
00019 #include "rutil/Inserter.hxx"
00020 #include "rutil/Logger.hxx"
00021 #include "resip/stack/DeprecatedDialog.hxx"
00022 #include "resip/stack/Helper.hxx"
00023 #include "resip/stack/SipMessage.hxx"
00024 #include "resip/stack/SipStack.hxx"
00025 #include "resip/stack/Uri.hxx"
00026 
00027 using namespace resip;
00028 using namespace std;
00029 
00030 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00031 
00032 static bool finished = false;
00033 
00034 static void
00035 signalHandler(int signo)
00036 {
00037    std::cerr << "Shutting down" << endl;
00038    finished = true;
00039 }
00040 
00041 int
00042 main(int argc, char* argv[])
00043 {
00044 
00045 #ifndef _WIN32
00046    if ( signal( SIGPIPE, SIG_IGN) == SIG_ERR)
00047    {
00048       cerr << "Couldn't install signal handler for SIGPIPE" << endl;
00049       exit(-1);
00050    }
00051 #endif
00052 
00053    if ( signal( SIGINT, signalHandler ) == SIG_ERR )
00054    {
00055       cerr << "Couldn't install signal handler for SIGINT" << endl;
00056       exit( -1 );
00057    }
00058 
00059    if ( signal( SIGTERM, signalHandler ) == SIG_ERR )
00060    {
00061       cerr << "Couldn't install signal handler for SIGTERM" << endl;
00062       exit( -1 );
00063    }
00064 
00065    const char* logType = "cout";
00066    const char* logLevel = "ALERT";
00067    const char* proto = "tcp";
00068    const char* bindAddr = "";
00069 
00070    int seltime = 0;
00071    
00072 #if defined(HAVE_POPT_H)
00073    struct poptOption table[] = {
00074       {"log-type",    'l', POPT_ARG_STRING, &logType,   0, "where to send logging messages", "syslog|cerr|cout"},
00075       {"log-level",   'v', POPT_ARG_STRING, &logLevel,  0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
00076       { "select-time", 's', POPT_ARG_INT,    &seltime,   0, "number of runs in test", 0},
00077       {"protocol",    'p', POPT_ARG_STRING, &proto,     0, "protocol to use (tcp | udp)", 0},
00078       {"bind",        'b', POPT_ARG_STRING, &bindAddr,  0, "interface address to bind to",0},
00079       POPT_AUTOHELP
00080       { NULL, 0, 0, NULL, 0 }
00081    };
00082    
00083    poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
00084    poptGetNextOpt(context);
00085 #endif
00086    Log::initialize(logType, logLevel, argv[0]);
00087 
00088    SipStack receiver;
00089    
00090    int port = 5060;
00091    receiver.addTransport(UDP, port, V4);
00092    receiver.addTransport(TCP, port, V4);
00093 
00094 
00095    NameAddr contact;
00096    contact.uri().scheme() = "sip";
00097    contact.uri().user() = "fluffy";
00098    contact.uri().host() = bindAddr;
00099    
00100    while (!finished)
00101    {
00102 
00103       try
00104       {
00105          receiver.process(seltime);
00106       }
00107       catch(BaseException& e)
00108       {
00109          CritLog(<< "Uncaught Exception: " << e);
00110          assert(0);
00111       }
00112       
00113       SipMessage* request = receiver.receive();
00114       static NameAddr contact;
00115 
00116       if (request)
00117       {
00118          if(request->isRequest())
00119          {
00120             SipMessage response;
00121             try
00122             {
00123             switch (request->header(h_RequestLine).getMethod())
00124             {
00125                case ACK:
00126                   break;
00127                default:
00128                   Helper::makeResponse(response,*request,200,contact);
00129                   receiver.send(response);
00130                   break;
00131             }
00132             }
00133             catch(BaseException& e)
00134             {
00135                CritLog(<< "Uncaught Exception: " << e );
00136                assert(0);
00137             }
00138          }
00139          delete request;
00140       }
00141    }      
00142    
00143 #if defined(HAVE_POPT_H)
00144    poptFreeContext(context);
00145 #endif
00146    return 0;
00147 }
00148 /* ====================================================================
00149  * The Vovida Software License, Version 1.0 
00150  * 
00151  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00152  * 
00153  * Redistribution and use in source and binary forms, with or without
00154  * modification, are permitted provided that the following conditions
00155  * are met:
00156  * 
00157  * 1. Redistributions of source code must retain the above copyright
00158  *    notice, this list of conditions and the following disclaimer.
00159  * 
00160  * 2. Redistributions in binary form must reproduce the above copyright
00161  *    notice, this list of conditions and the following disclaimer in
00162  *    the documentation and/or other materials provided with the
00163  *    distribution.
00164  * 
00165  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00166  *    and "Vovida Open Communication Application Library (VOCAL)" must
00167  *    not be used to endorse or promote products derived from this
00168  *    software without prior written permission. For written
00169  *    permission, please contact vocal@vovida.org.
00170  *
00171  * 4. Products derived from this software may not be called "VOCAL", nor
00172  *    may "VOCAL" appear in their name, without prior written
00173  *    permission of Vovida Networks, Inc.
00174  * 
00175  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00176  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00177  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00178  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00179  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00180  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00181  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00182  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00183  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00184  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00185  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00186  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00187  * DAMAGE.
00188  * 
00189  * ====================================================================
00190  * 
00191  * This software consists of voluntary contributions made by Vovida
00192  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00193  * Inc.  For more information on Vovida Networks, Inc., please see
00194  * <http://www.vovida.org/>.
00195  *
00196  */