Changeset 256 for etherws/trunk/etherws.py
- Timestamp:
- 10/10/13 00:43:48 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
etherws/trunk/etherws.py
r255 r256 46 46 import base64 47 47 import socket 48 import struct 48 49 import urllib2 49 50 import hashlib … … 271 272 def _del_privattr(self, name, obj): 272 273 return delattr(obj, self._privattr(name)) 274 275 276 class NetworkInterface(object): 277 SIOCGIFADDR = 0x8915 # from <linux/sockios.h> 278 SIOCSIFADDR = 0x8916 279 SIOCGIFNETMASK = 0x891b 280 SIOCSIFNETMASK = 0x891c 281 SIOCGIFMTU = 0x8921 282 SIOCSIFMTU = 0x8922 283 284 def __init__(self, ifname): 285 self._ifname = struct.pack('16s', str(ifname)[:15]) 286 287 def _ioctl(self, req, data): 288 try: 289 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 290 return fcntl.ioctl(sock.fileno(), req, data) 291 finally: 292 sock.close() 293 294 @property 295 def address(self): 296 try: 297 data = struct.pack('16s18x', self._ifname) 298 ret = self._ioctl(self.SIOCGIFADDR, data) 299 return socket.inet_ntoa(ret[20:24]) 300 except IOError: 301 return '' 302 303 @property 304 def netmask(self): 305 try: 306 data = struct.pack('16s18x', self._ifname) 307 ret = self._ioctl(self.SIOCGIFNETMASK, data) 308 return socket.inet_ntoa(ret[20:24]) 309 except IOError: 310 return '' 311 312 @property 313 def mtu(self): 314 try: 315 data = struct.pack('16s18x', self._ifname) 316 ret = self._ioctl(self.SIOCGIFMTU, data) 317 return struct.unpack('i', ret[16:20])[0] 318 except IOError: 319 return 0 320 321 @address.setter 322 def address(self, addr): 323 data = struct.pack('16si4s10x', self._ifname, socket.AF_INET, 324 socket.inet_aton(addr)) 325 self._ioctl(self.SIOCSIFADDR, data) 326 327 @netmask.setter 328 def netmask(self, addr): 329 data = struct.pack('16si4s10x', self._ifname, socket.AF_INET, 330 socket.inet_aton(addr)) 331 self._ioctl(self.SIOCSIFNETMASK, data) 332 333 @mtu.setter 334 def mtu(self, mtu): 335 data = struct.pack('16si12x', self._ifname, mtu) 336 self._ioctl(self.SIOCSIFMTU, data) 273 337 274 338 … … 478 542 if self.closed: 479 543 raise ValueError('I/O operation on closed netdev') 480 return ''544 return NetworkInterface(self.target).address 481 545 482 546 @property … … 484 548 if self.closed: 485 549 raise ValueError('I/O operation on closed netdev') 486 return ''550 return NetworkInterface(self.target).netmask 487 551 488 552 @property … … 490 554 if self.closed: 491 555 raise ValueError('I/O operation on closed netdev') 492 return ''556 return NetworkInterface(self.target).mtu 493 557 494 558 @address.setter … … 496 560 if self.closed: 497 561 raise ValueError('I/O operation on closed netdev') 498 raise NotImplementedError('unsupported')562 NetworkInterface(self.target).address = address 499 563 500 564 @netmask.setter … … 502 566 if self.closed: 503 567 raise ValueError('I/O operation on closed netdev') 504 raise NotImplementedError('unsupported')568 NetworkInterface(self.target).netmask = netmask 505 569 506 570 @mtu.setter … … 508 572 if self.closed: 509 573 raise ValueError('I/O operation on closed netdev') 510 raise NotImplementedError('unsupported')574 NetworkInterface(self.target).mtu = mtu 511 575 512 576 def open(self):
Note: See TracChangeset
for help on using the changeset viewer.