Package Peach :: Package Generators :: Module turn
[hide private]

Source Code for Module Peach.Generators.turn

  1   
  2  ''' 
  3  [BETA] TURN protocol generator.  TURN is an extension of the STUN 
  4  protocol.  TURN packets are generated using the StunPacket top 
  5  level container with TURN message types and attributes as needed. 
  6   
  7  @author: Michael Eddington 
  8  @version: $Id: Peach.Generators.turn-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
  9  ''' 
 10   
 11  # 
 12  # Copyright (c) 2006-2007 Michael Eddington 
 13  # 
 14  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 15  # of this software and associated documentation files (the "Software"), to deal 
 16  # in the Software without restriction, including without limitation the rights  
 17  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 18  # copies of the Software, and to permit persons to whom the Software is  
 19  # furnished to do so, subject to the following conditions: 
 20  # 
 21  # The above copyright notice and this permission notice shall be included in     
 22  # all copies or substantial portions of the Software. 
 23  # 
 24  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 25  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 26  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 27  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 28  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 29  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 30  # SOFTWARE. 
 31  # 
 32   
 33  # Authors: 
 34  #   Michael Eddington (mike@phed.org) 
 35   
 36  # $Id: Peach.Generators.turn-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 37   
 38  import re, struct, os 
 39  from Peach                                                      import generator 
 40  from Peach.Generators                           import * 
 41  from Peach.Generators.dictionary        import * 
 42  from Peach.Generators.static            import * 
 43  from Peach.Generators.static            import Int8, Int16, Int32 
 44  from Peach.Generators.stun                      import * 
 45   
 46  #__all__ = ['TurnAttribute', 
 47  #                  'TurnHeader', 
 48  #                 ] 
 49   
50 -class TurnHeader(StunHeader):
51 ''' 52 This generator creates a TURN header. A TURN header 53 is 20 bytes long. 54 ''' 55 56 ALLOCATE_REQUEST = 0x0003 57 ALLOCATE_RESPONSE = 0x0103 58 ALLOCATE_ERROR_RESPONSE = 0x0113 59 SEND_REQUEST = 0x0004 60 SEND_RESPONSE = 0x0104 61 SEND_ERROR_RESPONSE = 0x0114 62 DATA_INDICATION = 0x0115 63 SET_ACTIVE_DEST_REQUEST = 0x0006 64 SET_ACTIVE_DEST_RESPONSE = 0x0106 65 SET_ACTIVE_DEST_ERROR_RESPONSE = 0x0116
66 67
68 -class TurnAttribute(StunAttribute):
69 ''' 70 This generator creates a STUN attribute. 71 ''' 72 73 LIFETIME = 0x000d 74 ALTERNATE_SERVER = 0x000e 75 MAGIC_COOKIE = 0x000f 76 BANDWIDTH = 0x0010 77 DESTINATION_ADDRESS = 0x0011 78 REMOTE_ADDRESS = 0x0012 79 DATA = 0x0013 80 NONCE = 0x0014 81 REALM = 0x0015 82 NAT_ADDRESS = 0x0080
83 84
85 -class TurnNatAddressAttribute(StunMappedAddressAttribute):
86 ''' 87 88 IP Family 1 byte 89 IP Port 2 bytes 90 IP Address 4 bytes 91 92 ''' 93
94 - def __init__(self, group, port, address, family = Static('\01')):
95 ''' 96 @type group: Group 97 @param group: Group to use 98 @type family: Generator 99 @param family: Family (defaults to 0x01 IPV4) 100 @type port: Generator 101 @param port: Port number 102 @type address: Generator 103 @param address: 32bit IPv4 address 104 ''' 105 StunMappedAddressAttribute.__init__(self, group, port, address, family); 106 self._type = TurnAttribute.NAT_ADDRESS
107 108
109 -class TurnLifetimeAttribute(TurnAttribute):
110 ''' 111 LIFETIME 112 113 The lifetime attribute represents the duration for which the server 114 will maintain a binding in the absence of data traffic either from or 115 to the client. It is a 32 bit value representing the number of 116 seconds remaining until expiration. 117 118 0 1 2 3 119 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 120 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 121 | Lifetime | 122 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 123 124 ''' 125
126 - def __init__(self, group, lifetime):
127 ''' 128 @type group: Group 129 @param group: Group to use 130 @type lifetime: Generator 131 @param lifetime: Lifetime in seconds 132 ''' 133 self.setGroup(group) 134 if lifetime != None: 135 self._generator = lifetime 136 self._type = TurnAttribute.LIFETIME
137
138 - def _getValue(self):
139 return self._generator.getValue()
140 141
142 -class TurnAlternateServerAttribute(TurnAttribute, StunMappedAddressAttribute):
143 ''' 144 ALTERNATE-SERVER 145 146 The alternate server represents an alternate IP address and port for 147 a different TURN server to try. It is encoded in the same way as 148 MAPPED-ADDRESS. 149 ''' 150
151 - def __init__(self, group, port, address, family = 0x01):
152 ''' 153 @type group: Group 154 @param group: Group to use 155 @type family: Generator 156 @param family: Family (defaults to 0x01 IPV4) 157 @type port: Generator 158 @param port: Port number 159 @type address: Generator 160 @param address: 32bit IPv4 address 161 ''' 162 StunMappedAddressAttribute.__init__(self, group, port, address, family) 163 self._type = TurnAttribute.ALTERNATE_SERVER
164 165
166 -class TurnMagicCookieAttribute(TurnAttribute):
167 ''' 168 MAGIC-COOKIE 169 170 The MAGIC-COOKIE is used by TURN clients and servers to disambiguate 171 TURN traffic from data traffic. Its value ix 0x72c64bc6. 172 173 0 1 2 3 174 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 175 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 176 |0|1|1|1|0|0|1|0|1|1|0|0|0|1|1|0|0|1|0|0|1|0|1|1|1|1|0|0|0|1|1|0| 177 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 178 ''' 179 180 _magicCookie = 0x72c64bc6 # 4 byte 181
182 - def __init__(self, group, magicCookie = None):
183 ''' 184 @type group: Group 185 @param group: Group to use 186 @type magicCookie: Generator 187 @param magicCookie: Magic cookie, default is 0x72c64bc6 188 ''' 189 self.setGroup(group) 190 if lifetime != None: 191 self._lifetime = lifetime 192 self._type = TurnAttribute.MAGIC_COOKIE
193
194 - def _getValue(self):
195 if isinstance(self._magicCookie, Generator): 196 return Int32(self._magicCookie.getValue(), 0, 0).getValue() 197 198 return Int32(self._magicCookie.getValue(), 0, 0).getValue()
199 200
201 -class TurnBandwidthAttribute(TurnAttribute):
202 ''' 203 BANDWIDTH 204 205 The bandwidth attribute represents the peak bandwidth, measured in 206 kbits per second, that the client expects to use on the binding. The 207 value represents the sum in the receive and send directions. 208 [[Editors note: Need to define leaky bucket parameters for this.]] 209 210 0 1 2 3 211 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 212 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 213 | Bandwidth | 214 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 215 ''' 216
217 - def __init__(self, group, bandwidth):
218 ''' 219 @type group: Group 220 @param group: Group to use 221 @type lifetime: Generator 222 @param lifetime: Lifetime in seconds 223 ''' 224 self.setGroup(group) 225 if bandwidth != None: 226 self._generator = bandwidth 227 self._type = TurnAttribute.BANDWIDTH
228
229 - def _getValue(self):
230 return self._generator.getValue()
231 232
233 -class TurnDestinationAddressAttribute(StunMappedAddressAttribute):
234 ''' 235 DESTINATION-ADDRESS 236 237 The DESTINATION-ADDRESS is present in Send Requests and Set Active 238 Destination Requests. It specifies the address and port where the 239 data is to be sent. It is encoded in the same way as MAPPED-ADDRESS. 240 ''' 241
242 - def __init__(self, group, port, address, family = 0x01):
243 ''' 244 @type group: Group 245 @param group: Group to use 246 @type family: Generator 247 @param family: Family (defaults to 0x01 IPV4) 248 @type port: Generator 249 @param port: Port number 250 @type address: Generator 251 @param address: 32bit IPv4 address 252 ''' 253 StunMappedAddressAttribute.__init__(self, group, port, address, family) 254 self._type = TurnAttribute.DESTINATION_ADDRESS
255 256
257 -class TurnRemoteAddressAttribute(StunMappedAddressAttribute):
258 ''' 259 REMOTE-ADDRESS 260 261 The REMOTE-ADDRESS is present in Data Indications. It specifies the 262 address and port from which a packet was received. It is encoded in 263 the same way as MAPPED-ADDRESS. 264 ''' 265
266 - def __init__(self, group, port, address, family = 0x01):
267 ''' 268 @type group: Group 269 @param group: Group to use 270 @type family: Generator 271 @param family: Family (defaults to 0x01 IPV4) 272 @type port: Generator 273 @param port: Port number 274 @type address: Generator 275 @param address: 32bit IPv4 address 276 ''' 277 StunMappedAddressAttribute.__init__(self, group, port, address, family) 278 self._type = TurnAttribute.REMOTE_ADDRESS
279 280
281 -class TurnDataAttribute(TurnAttribute):
282 ''' 283 DATA 284 285 The DATA attribute is present in Send Requests and Data Indications. 286 It contains raw payload data that is to be sent (in the case of a 287 Send Request) or was received (in the case of a Data Indication). 288 ''' 289
290 - def __init__(self, group, data):
291 ''' 292 @type group: Group 293 @param group: Group to use 294 @type data: Generator 295 @param data: Data to send through 296 ''' 297 self.setGroup(group) 298 if data != None: 299 self._generator = data 300 self._type = TurnAttribute.DATA
301
302 - def _getValue(self):
303 return self._generator.getValue()
304 305
306 -class TurnNonceAttribute(TurnAttribute):
307 ''' 308 NONCE 309 310 The NONCE attribute is present in Shared Secret Requests and Shared 311 Secret Error responses. It contains a sequence of qdtext or quoted- 312 pair, which are defined in [6]. 313 ''' 314 315 _nonce = 0 # 4 byte 316
317 - def __init__(self, group, nonce):
318 ''' 319 @type group: Group 320 @param group: Group to use 321 @type nonce: Generator 322 @param nonce: Nonce stuff 323 ''' 324 self.setGroup(group) 325 if nonce != None: 326 self._nonce = nonce 327 self._type = TurnAttribute.NONCE
328
329 - def _getValue(self):
330 return self._nonce.getValue()
331 332
333 -class TurnRealmAttribute(TurnAttribute):
334 ''' 335 REALM 336 337 The REALM attribute is present in Shared Secret Requests and Shared 338 Secret Responses. It contains text which meets the grammar for 339 "realm" as described in RFC 3261, and will thus contain a quoted 340 string (including the quotes). 341 ''' 342 343 _realm = None 344
345 - def __init__(self, group, realm):
346 ''' 347 @type group: Group 348 @param group: Group to use 349 @type realm: Generator 350 @param realm: Realm string ("fooyah.com") 351 ''' 352 self.setGroup(group) 353 if realm != None: 354 self._realm = realm 355 self._type = TurnAttribute.REALM
356
357 - def _getValue(self):
358 return self._realm.getValue()
359 360 361 # end 362