Changeset 556

Show
Ignore:
Timestamp:
04/02/06 03:15:57 (3 years ago)
Author:
edmanm
Message:

Do all the policy parsing and formatting in the Policy class.

Location:
trunk/src
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/config/policy.cpp

    r447 r556  
    5252} 
    5353 
     54/** Constructor. Creates a new Policy object from the string parts. */ 
     55Policy::Policy(QString action, QString address, QString ports) 
     56{ 
     57  /* Set the defaults */ 
     58  _action   = Accept; 
     59  _address  = QHostAddress::Any; 
     60  _fromPort = _toPort = 0; 
     61  _mask = 0; 
     62   
     63  fromString(action + " " + address + ":" + ports); 
     64} 
     65 
    5466/** Constructor. Creates a new Policy object depending on the specified 
    5567 * special policy type. */ 
     
    5971  _address  = QHostAddress::Any; 
    6072  _fromPort = _toPort = 0; 
    61   _mask = 0; 
    62 } 
    63  
    64 /** Constructor. Creates a new policy object based on the given rules. */ 
    65 Policy::Policy(Action action, QHostAddress addr,  
    66                quint16 fromPort, quint16 toPort) 
    67 { 
    68   _action   = action; 
    69   _address  = addr; 
    70   _fromPort = fromPort; 
    71   _toPort   = toPort; 
    7273  _mask = 0; 
    7374} 
     
    134135Policy::toString() 
    135136{ 
    136   QString policy; 
    137    
    138   /* Add the action to take for this policy */ 
    139   policy = (_action == Accept ? "accept " : "reject "); 
    140    
    141   /* Add the address (and mask, if specified) */ 
    142   if (_mask && !_address.isNull()) { 
    143     policy += _address.toString() + "/" + QString::number(_mask); 
    144   } else { 
    145     policy += (_address.isNull() ? "*" : _address.toString()); 
    146   } 
    147    
    148   /* Add the port or port range (if specified) */ 
    149   policy += ":" + (_fromPort ? QString::number(_fromPort) : "*"); 
    150   if (_fromPort && _toPort) { 
    151     policy += "-" + QString::number(_toPort); 
    152   } 
    153   return policy; 
     137  return action() + " " + address() + ":" + ports(); 
    154138} 
    155139 
     140/** Converts the given action to a string. */ 
     141Policy::Action 
     142Policy::toAction(QString action) 
     143{ 
     144  if (action.toLower() == "accept") { 
     145    return Accept; 
     146  } 
     147  return Reject; 
     148} 
     149 
     150/** Returns the action associated with this policy. */ 
     151QString 
     152Policy::action() 
     153{ 
     154  return (_action == Accept ? "accept" : "reject"); 
     155} 
     156 
     157/** Returns the address (and mask, if specified) for this policy. */ 
     158QString 
     159Policy::address() 
     160{ 
     161  if (_mask && !_address.isNull()) { 
     162    return _address.toString() + "/" + QString::number(_mask); 
     163  }  
     164  return (_address.isNull() ? "*" : _address.toString()); 
     165} 
     166 
     167/** Returns the port (or port range, if specified) for this policy. */ 
     168QString 
     169Policy::ports() 
     170{ 
     171  QString ports = (_fromPort ? QString::number(_fromPort) : "*"); 
     172  if (_fromPort && _toPort) { 
     173    ports += "-" + QString::number(_toPort); 
     174  } 
     175  return ports; 
     176} 
     177 
  • trunk/src/config/policy.h

    r447 r556  
    5050  /** Parses the given policy, represented as a string. */ 
    5151  Policy(QString policy); 
     52  /** Parses the given portions of a policy string. */ 
     53  Policy(QString action, QString address, QString ports); 
    5254  /** Creates a policy of the given special type. */ 
    5355  Policy(SpecialPolicy policy); 
    54   /** Create a policy using the specified information. */ 
    55   Policy(Action action, QHostAddress addr,  
    56          quint16 fromPort, quint16 toPort = 0); 
    5756  /** Creates a policy using the specified information. */ 
    5857  Policy(Action action, QHostAddress addr, uchar mask, 
     
    6665  /** Converts this policy to a format Tor understands. */ 
    6766  QString toString(); 
    68  
     67  /** Converts a string action to an Action enum value. */ 
     68  static Action toAction(QString action); 
     69   
    6970  /** Returns the action taken when this policy matches an address. */ 
    70   Action action() { return _action; } 
    71   /** Returns the host address for this policy. */ 
    72   QHostAddress address() { return _address; } 
    73   /** Returns the host address mask for this policy. */ 
    74   uchar mask() { return _mask; } 
    75   /** Returns the first port in a range. */ 
    76   quint16 fromPort() { return _fromPort; } 
    77   /** Returns the last port in a port range. */ 
    78   quint16 toPort() { return _toPort; } 
     71  QString action(); 
     72  /** Returns the host address (including mask, if set) for this policy. */ 
     73  QString address(); 
     74  /** Returns the port or port range for this policy. */ 
     75  QString ports(); 
    7976 
    8077private: 
  • trunk/src/gui/config/serverpage.cpp

    r555 r556  
    142142ServerPage::savePolicy(QTreeWidgetItem *item, ExitPolicy &exitPolicy) 
    143143{ 
    144   /* Build policy string */ 
    145   QString policyString = item->text(COL_ACTION) + " "; 
    146   policyString += item->text(COL_ADDRESS) + ":" + item->text(COL_PORT); 
    147    
    148144  /* Add policy to ServerSettings */ 
    149   exitPolicy.addPolicy(Policy(policyString)); 
     145  exitPolicy.addPolicy(Policy(item->text(COL_ACTION), 
     146                              item->text(COL_ADDRESS), 
     147                              item->text(COL_PORT))); 
    150148} 
    151149 
     
    163161   
    164162  /* Add the policy to the list */ 
    165   addPolicyItem(ui.cmboExitAction->currentText(), ui.lineExitAddress->text(), 
    166                 ui.lineExitMask->text(), ui.lineExitFromPort->text(), 
    167                 ui.lineExitToPort->text()); 
     163  addPolicyItem(Policy(Policy::toAction(ui.cmboExitAction->currentText()),  
     164                       QHostAddress(ui.lineExitAddress->text()), 
     165                       ui.lineExitMask->text().toUShort(),  
     166                       ui.lineExitFromPort->text().toUShort(), 
     167                       ui.lineExitToPort->text().toUShort())); 
    168168 
    169169  /* Clear input text boxes */ 
     
    178178ServerPage::addPolicyItem(Policy policy) 
    179179{ 
    180   /* Convert to strings */ 
    181   QString action = (policy.action() == Policy::Accept ? "accept" : "reject"); 
    182   QHostAddress address = policy.address(); 
    183   QString addr = (address.isNull() ? "*" : address.toString()); 
    184   QString mask = (policy.mask() ? QString::number(policy.mask()) : ""); 
    185   int port = policy.fromPort(); 
    186   QString fromPort = (port ? QString::number(port) : ""); 
    187   port = policy.toPort(); 
    188   QString toPort = (port ? QString::number(port) : ""); 
    189  
    190   /* Add to list */ 
    191   addPolicyItem(action, addr, mask, fromPort, toPort); 
    192 } 
    193  
    194 /** Adds a new QTreeWidget item to the exit policy list */ 
    195 void 
    196 ServerPage::addPolicyItem(QString action, QString address, QString mask, 
    197                           QString fromPort, QString toPort) 
    198 { 
    199180  QTreeWidgetItem *newPolicy = new QTreeWidgetItem(); 
    200    
    201   newPolicy->setText(COL_ACTION, action); 
    202   newPolicy->setTextAlignment(COL_ACTION, Qt::AlignCenter); 
    203    
    204   if (!mask.isEmpty()) { 
    205     address += "/" + mask; 
    206   } 
    207   newPolicy->setText(COL_ADDRESS, address); 
    208   newPolicy->setTextAlignment(COL_ADDRESS, Qt::AlignCenter); 
    209  
    210   if (!fromPort.isEmpty()) { 
    211     if (!toPort.isEmpty() && fromPort != "*") { 
    212       fromPort += "-" + toPort; 
    213     } 
    214   } else { 
    215     fromPort = "*"; 
    216   } 
    217  
    218   newPolicy->setText(COL_PORT, fromPort); 
    219   newPolicy->setTextAlignment(COL_PORT, Qt::AlignCenter); 
    220  
     181 
     182  newPolicy->setText(COL_ACTION,  policy.action()); 
     183  newPolicy->setText(COL_ADDRESS, policy.address()); 
     184  newPolicy->setText(COL_PORT,    policy.ports()); 
     185   
    221186  ui.lstExitPolicies->addTopLevelItem(newPolicy); 
    222187} 
  • trunk/src/gui/config/serverpage.h

    r555 r556  
    7171  int selectedIndex(); 
    7272  /** Adds a new exit policy to the exit policy list */ 
    73   void addPolicyItem(QString action, QString address, QString mask, 
    74                      QString fromPort, QString toPort); 
    7573  void addPolicyItem(Policy policy); 
    7674  /** Saves the policy specified in item to the exitPolicy */