reSIProcate/DialogUsageManager  9680
CommandLineParser.cxx
Go to the documentation of this file.
00001 #if defined (HAVE_POPT_H)
00002 #include <popt.h>
00003 #endif
00004 
00005 #include "CommandLineParser.hxx"
00006 #include "rutil/Logger.hxx"
00007 #include "rutil/DnsUtil.hxx"
00008 #include "rutil/ParseException.hxx"
00009 
00010 using namespace resip;
00011 using namespace std;
00012 
00013 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
00014 
00015 CommandLineParser::CommandLineParser(int argc, char** argv)
00016 {
00017    const char* logType = "cout";
00018    const char* logLevel = "INFO";
00019    int encrypt=false;
00020    int sign=false;
00021    int genUserCert = false;
00022    char* tlsDomain = 0;
00023    
00024    int udpPort = 5160;
00025    int tcpPort = 5160;
00026    int tlsPort = 5161;
00027    int dtlsPort = 5161;
00028    
00029    mRegisterDuration = 3600;
00030    int noV4 = false;
00031    int noV6 = false;
00032    
00033    char* inputAor = 0;
00034    const char* password = "";
00035    
00036    char* inputOutboundProxy = 0;
00037    char* inputContact = 0;
00038    char* inputBuddies = 0;
00039    char* inputTarget = 0;
00040    char* passPhrase = 0;
00041    char* certPath = 0;
00042    Data basePath(getenv("HOME"));
00043 
00044 #if defined(HAVE_POPT_H)
00045    struct poptOption table[] = {
00046       {"log-type",     'l', POPT_ARG_STRING, &logType,   0, "where to send logging messages", "syslog|cerr|cout"},
00047       {"log-level",    'v', POPT_ARG_STRING, &logLevel,  0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
00048       {"encrypt",      'e', POPT_ARG_NONE, &encrypt, 0, "whether to encrypt or not", 0},
00049       {"sign",         's', POPT_ARG_NONE, &sign, 0,   "signs messages you send", 0},
00050       {"gen-user-cert",'u', POPT_ARG_NONE, &genUserCert, 0, "generate a new user certificate", 0},
00051       {"tls-domain",   't', POPT_ARG_STRING, &tlsDomain,  0, "act as a TLS server for specified domain", "example.com"},
00052       
00053       {"udp",          0,   POPT_ARG_INT, &udpPort, 0, "add UDP transport on specified port", "5060"},
00054       {"tcp",          0,   POPT_ARG_INT, &tcpPort, 0, "add TCP transport on specified port", "5060"},
00055       {"tls",          0,   POPT_ARG_INT, &tlsPort, 0, "add TLS transport on specified port", "5061"},
00056       {"dtls",         0,   POPT_ARG_INT, &dtlsPort, 0, "add DTLS transport on specified port", "5061"},
00057 
00058       {"register-duration",  0,   POPT_ARG_INT, &mRegisterDuration, 0, "expires for register (0 for no reg)", "3600"},
00059       {"disable-v6",   0,   POPT_ARG_NONE, &noV6, 0, "disable IPV6", 0},
00060       {"disable-v4",   0,   POPT_ARG_NONE, &noV4, 0, "disable IPV4", 0},
00061       // may want to be able to specify that PUBLISH will occur
00062 
00063       {"aor",         'a',  POPT_ARG_STRING, &inputAor,  0, "specify address of record", "sip:alice@example.com"},
00064       {"password",    'p',  POPT_ARG_STRING, &password,  0, "specify password for address of record", "password"},
00065       {"outbound-proxy",'o',  POPT_ARG_STRING, &inputOutboundProxy,  0, "specify uri for outbound proxy (if none present, don't use)", "sip:outbound.example.com"},
00066       {"contact",       'c',  POPT_ARG_STRING, &inputContact,  0, "override default contact", "sip:alice@contact.example.com"},      
00067       {"to",            't',  POPT_ARG_STRING, &inputTarget,  0, "specify target aor", "sip:jane@example.com"},      
00068       {"buddies",       'b',  POPT_ARG_STRING, &inputBuddies,  0, "list of buddy aors", "sip:b1@example.com,sip:b2@example.com"},
00069 
00070       {"pass-phrase",   'k',  POPT_ARG_STRING, &passPhrase,  0, "pass phrase for private key", 0},
00071       {"cert-path",      0,   POPT_ARG_STRING, &certPath,  0, "path for certificates (default ~/.sipCerts)", 0},
00072       
00073       POPT_AUTOHELP
00074       { NULL, 0, 0, NULL, 0 }
00075    };
00076    
00077    poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
00078    poptGetNextOpt(context);
00079 #endif
00080 
00081    mLogType = logType;
00082    mLogLevel = logLevel;
00083    mEncrypt = encrypt;
00084    mSign = sign;
00085    mGenUserCert = genUserCert;
00086    if (tlsDomain) mTlsDomain = tlsDomain;
00087    mUdpPort = udpPort;
00088    mTcpPort = tcpPort;
00089    mTlsPort = tlsPort;
00090    mDtlsPort = dtlsPort;
00091    mNoV4 = noV4;
00092    mNoV6 = noV6;
00093    if (inputAor)
00094    {
00095       mAor = toUri(inputAor, "aor");
00096    }
00097    else
00098    {
00099       mAor.user() = "test";
00100       mAor.host() = DnsUtil::getLocalHostName();
00101    }
00102    
00103    mPassword = password;
00104    mOutboundProxy = toUri(inputOutboundProxy, "outbound proxy");
00105    mContact = toUri(inputContact, "contact");
00106    mBuddies = toUriVector(inputBuddies, "buddies"); // was addList   
00107    mTarget = toUri(inputTarget, "target"); // was dest
00108    if (passPhrase) mPassPhrase = passPhrase;
00109    if (certPath) mCertPath = certPath;
00110    else mCertPath = basePath + "/.sipCerts";
00111    
00112    // pubList for publish targets
00113 
00114    // Free the option parsing context.
00115 #if defined(HAVE_POPT_H)
00116    poptFreeContext(context);
00117 #endif
00118 }
00119 
00120 resip::Uri 
00121 CommandLineParser::toUri(const char* input, const char* description)
00122 {
00123    resip::Uri uri;
00124    try
00125    {
00126       if (input)
00127       {
00128          uri = Uri(input);
00129       }
00130       else
00131       {
00132          //WarningLog (<< "No " << description << " specified");
00133       }
00134    } 
00135    catch (ParseException& e)
00136    {
00137       InfoLog (<< "Caught: " << e);
00138       WarningLog (<< "Can't parse " << description << " : " << input);
00139       exit(-1);
00140    }
00141    return uri;
00142 }
00143 
00144 std::vector<resip::Uri> 
00145 CommandLineParser::toUriVector(const char* input, const char* description)
00146 {
00147    std::vector<Uri> uris; 
00148    if (input)
00149    {
00150       char buffer[2048];
00151       strcpy(buffer, input);
00152 
00153       for (char* token = strtok(buffer, ","); token != 0; token = strtok(0, ","))
00154       {
00155          try
00156          {
00157             uris.push_back(Uri(token));
00158          } 
00159          catch (ParseException& e)
00160          {
00161             InfoLog (<< "Caught: " << e);
00162             WarningLog (<< "Can't parse " << description << " : " << token);
00163             exit(-1);
00164          }
00165       }
00166    }
00167    return uris;
00168 }
00169    
00170 
00171 /* ====================================================================
00172  * The Vovida Software License, Version 1.0 
00173  * 
00174  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00175  * 
00176  * Redistribution and use in source and binary forms, with or without
00177  * modification, are permitted provided that the following conditions
00178  * are met:
00179  * 
00180  * 1. Redistributions of source code must retain the above copyright
00181  *    notice, this list of conditions and the following disclaimer.
00182  * 
00183  * 2. Redistributions in binary form must reproduce the above copyright
00184  *    notice, this list of conditions and the following disclaimer in
00185  *    the documentation and/or other materials provided with the
00186  *    distribution.
00187  * 
00188  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00189  *    and "Vovida Open Communication Application Library (VOCAL)" must
00190  *    not be used to endorse or promote products derived from this
00191  *    software without prior written permission. For written
00192  *    permission, please contact vocal@vovida.org.
00193  *
00194  * 4. Products derived from this software may not be called "VOCAL", nor
00195  *    may "VOCAL" appear in their name, without prior written
00196  *    permission of Vovida Networks, Inc.
00197  * 
00198  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00199  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00200  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00201  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00202  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00203  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00204  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00205  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00206  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00207  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00208  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00209  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00210  * DAMAGE.
00211  * 
00212  * ====================================================================
00213  * 
00214  * This software consists of voluntary contributions made by Vovida
00215  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00216  * Inc.  For more information on Vovida Networks, Inc., please see
00217  * <http://www.vovida.org/>.
00218  *
00219  */