Changeset 203

Show
Ignore:
Timestamp:
01/28/06 02:03:58 (3 years ago)
Author:
edmanm
Message:

Add support for GETCONF, SETCONF, SAVECONF, and RESETCONF.

Location:
trunk/src/control
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/control/torcontrol.cpp

    r186 r203  
    368368} 
    369369 
     370/** Sets each configuration key in \emph map to the value associated with its 
     371 * key. */ 
     372bool 
     373TorControl::setConf(QHash<QString,QString> map, QString *errmsg) 
     374{ 
     375  ControlCommand cmd("SETCONF"); 
     376  ControlReply reply; 
     377  QString arg, value; 
     378   
     379  /* Add each keyvalue to the argument list */ 
     380  foreach (QString key, map.keys()) { 
     381    arg = key; 
     382    value = map.value(key); 
     383    if (value.length() > 0) { 
     384      arg += "=" + value; 
     385    } 
     386    cmd.addArgument(arg); 
     387  } 
     388   
     389  /* Send the new configuration values to Tor */ 
     390  if (!send(cmd, reply, errmsg)) { 
     391    return false; 
     392  } else { 
     393    if (reply.getStatus() != "250") { 
     394      if (errmsg) { 
     395        *errmsg = reply.getLine().getMessage(); 
     396      } 
     397      return false; 
     398    } 
     399  } 
     400  return true; 
     401} 
     402 
     403/** Sets a single configuration key to the given value. */ 
     404bool 
     405TorControl::setConf(QString key, QString value, QString *errmsg) 
     406{ 
     407  QHash<QString,QString> map; 
     408  map.insert(key, value); 
     409  return setConf(map, errmsg); 
     410} 
     411 
     412/** Gets a set of configuration keyvalues and stores them in \emph map. */ 
     413bool 
     414TorControl::getConf(QHash<QString,QString> &map, QString *errmsg) 
     415{ 
     416  ControlCommand cmd("GETCONF"); 
     417  ControlReply reply; 
     418 
     419  /* Add the keys as arguments to the GETINFO message */ 
     420  foreach (QString key, map.keys()) { 
     421    cmd.addArgument(key); 
     422  } 
     423  
     424  /* Ask Tor for the specified info values */ 
     425  if (send(cmd, reply, errmsg)) { 
     426    /* Parse the response for the returned values */ 
     427    foreach (ReplyLine line, reply.getLines()) { 
     428      if (line.getStatus() != "250") { 
     429        if (errmsg) { 
     430          *errmsg = line.getMessage(); 
     431        } 
     432        return false; 
     433      } 
     434 
     435      /* Split the "key=val" line and map them */ 
     436      QStringList keyval = line.getMessage().split("="); 
     437      if (keyval.size() == 2) { 
     438        map.insert(keyval.at(0), keyval.at(1)); 
     439      } 
     440    } 
     441  } else { 
     442    /* Sending the control command failed */ 
     443    return false; 
     444  } 
     445  return true; 
     446} 
     447 
     448/** Gets a single configuration keyvalue. */ 
     449bool 
     450TorControl::getConf(QString key, QString &value, QString *errmsg) 
     451{ 
     452  QHash<QString,QString> map; 
     453  map.insert(key, ""); 
     454 
     455  if (getConf(map, errmsg)) { 
     456    value = map.value(key); 
     457    return true; 
     458  } 
     459  return false; 
     460} 
     461 
     462/** Asks Tor to save the current configuration to its torrc. */ 
     463bool 
     464TorControl::saveConf(QString *errmsg) 
     465{ 
     466  ControlCommand cmd("SAVECONF"); 
     467  ControlReply reply; 
     468 
     469  if(!send(cmd, reply, errmsg)) { 
     470    return false; 
     471  } else { 
     472    if (reply.getStatus() != "250") { 
     473      if (errmsg) { 
     474        *errmsg = reply.getMessage(); 
     475      } 
     476      return false; 
     477    } 
     478  } 
     479  return true; 
     480} 
     481 
     482/** Tells Tor to reset the given configuration keys back to defaults. */ 
     483bool 
     484TorControl::resetConf(QList<QString> keys, QString *errmsg) 
     485{ 
     486  ControlCommand cmd("RESETCONF"); 
     487  ControlReply reply; 
     488 
     489  /* Add each key to the argument list */ 
     490  foreach (QString key, keys) { 
     491    cmd.addArgument(key); 
     492  } 
     493   
     494  if(!send(cmd, reply, errmsg)) { 
     495    return false; 
     496  } else { 
     497    if (reply.getStatus() != "250") { 
     498      if (errmsg) { 
     499        *errmsg = reply.getMessage(); 
     500      } 
     501      return false; 
     502    } 
     503  } 
     504  return true; 
     505} 
     506 
  • trunk/src/control/torcontrol.h

    r186 r203  
    4242  /** Signals that can be sent by the controller */ 
    4343  enum Signal { 
    44     SignalReload, SignalShutdown, SignalDump, SignalDebug, SignalHalt 
     44    SignalReload,   /**< SIGHUP: Reloads config items and refetch directory */ 
     45    SignalShutdown, /**< SIGINT: Controlled shutdown */ 
     46    SignalDump,     /**< SIGUSR1: Log information about current circuits */ 
     47    SignalDebug,    /**< SIGUSR2: Switch all open logs to loglevel debug */ 
     48    SignalHalt      /**< SIGTERM: Immediate shutdown */ 
    4549  }; 
    4650  
    47  
    4851  /** Default constructor */ 
    4952  TorControl(); 
     
    9093   
    9194 
     95  /** Sets each configuration key in \emph map to the value associated with its key. */ 
     96  bool setConf(QHash<QString,QString> map, QString *errmsg = 0); 
     97  /** Sets a single configuration key to the given value. */ 
     98  bool setConf(QString key, QString value, QString *errmsg = 0); 
     99  /** Gets a set of configuration keyvalues and stores them in \emph map. */ 
     100  bool getConf(QHash<QString,QString> &map, QString *errmsg = 0); 
     101  /** Gets a single configuration keyvalue. */ 
     102  bool getConf(QString key, QString &value, QString *errmsg = 0); 
     103  /** Asks Tor to save the current configuration to its torrc */ 
     104  bool saveConf(QString *errmsg = 0); 
     105  /** Tells Tor to reset the given configuration keys back to defaults. */ 
     106  bool resetConf(QList<QString> keys, QString *errmsg = 0); 
     107 
    92108signals: 
    93109  /** Emitted when the Tor process has started */