home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / base / slp.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  5.4 KB  |  173 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21.  
  22. # Std Lib
  23. import sys
  24. import time
  25. import socket
  26. import select
  27. import struct
  28. import random
  29. import re
  30.  
  31. # Local
  32. from g import *
  33. import utils
  34.  
  35. prod_pat = re.compile(r"""\(\s*x-hp-prod_id\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
  36. mac_pat  = re.compile(r"""\(\s*x-hp-mac\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
  37. num_port_pat = re.compile(r"""\(\s*x-hp-num_port\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
  38. ip_pat =   re.compile(r"""\(\s*x-hp-ip\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
  39. p1_pat =   re.compile(r"""\(\s*x-hp-p1\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
  40. p2_pat =   re.compile(r"""\(\s*x-hp-p2\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
  41. p3_pat =   re.compile(r"""\(\s*x-hp-p3\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
  42. hn_pat =   re.compile(r"""\(\s*x-hp-hn\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
  43.  
  44.  
  45. def detectNetworkDevices(ttl=4, timeout=10): #, xid=None, qappobj = None):
  46.     mcast_addr, mcast_port ='224.0.1.60', 427
  47.     found_devices = {}
  48.  
  49.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  50.  
  51.     x = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  52.     x.connect(('1.2.3.4', 56))
  53.     intf = x.getsockname()[0]
  54.     x.close()
  55.  
  56.     s.setblocking(0)
  57.     ttl = struct.pack('B', ttl) 
  58.  
  59.     try:
  60.         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
  61.         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
  62.     except (AttributeError, socket.error):
  63.         pass
  64.  
  65.     try:
  66.         s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, ttl)
  67.         s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf) + socket.inet_aton('0.0.0.0'))
  68.         s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP ,1)
  69.     except Exception, e:
  70.         log.error("Unable to setup multicast socket for SLP: %s" % e)
  71.         return {}
  72.  
  73.     packet = ''.join(['\x01\x06\x00\x2c\x00\x00\x65\x6e\x00\x03', 
  74.         struct.pack('!H', random.randint(1, 65535)), '\x00\x00\x00\x18service:x-hpnp-discover:\x00\x00\x00\x00'])
  75.  
  76.     try:
  77.         s.sendto(packet, 0, (mcast_addr, mcast_port))
  78.     except socket.error, e:
  79.         log.error("Unable to send broadcast SLP packet: %s" % e)
  80.  
  81.     time_left = timeout
  82.     while time_left > 0:
  83.         start_time = time.time()
  84.         r, w, e = select.select([s], [], [s], time_left)
  85.         time_left -= (time.time() - start_time)
  86.         if not r: continue
  87.  
  88.         data, addr = s.recvfrom(2048)
  89.         update_spinner() 
  90.  
  91.         log.log_data(data, fmt=True, width=32)
  92.  
  93.         try:
  94.             ver, func, length, flags, dialect, lang_code, char_encode, recv_xid, status_code, attr_length = \
  95.                 struct.unpack("!BBHBBHHHHH", data[:16])
  96.  
  97.             x = struct.unpack("!%ds" % attr_length, data[16:])[0].strip()
  98.         except struct.error:
  99.             continue
  100.  
  101.         try:
  102.             num_ports = int(num_port_pat.search(x).group(1))
  103.         except (AttributeError, ValueError):
  104.             num_ports = 1
  105.  
  106.         if num_ports == 0: # Embedded devices
  107.             num_ports = 1
  108.  
  109.         y = {'num_devices' : 0, 'num_ports': num_ports, 'product_id' : '', 
  110.              'status_code': 0, 'device2': '0', 'device3': '0', 'note': '', 'device1': '0'}
  111.  
  112.         # Check port 1
  113.         try:
  114.             y['device1'] = p1_pat.search(x).group(1)
  115.         except AttributeError:
  116.             y['device1'] = '0'
  117.         else:
  118.             y['num_devices'] += 1
  119.  
  120.  
  121.         if num_ports > 1: # Check port 2
  122.             try:
  123.                 y['device2'] = p2_pat.search(x).group(1)
  124.             except AttributeError:
  125.                 y['device2'] = '0'
  126.             else:
  127.                 y['num_devices'] += 1
  128.  
  129.  
  130.             if num_ports > 2: # Check port 3
  131.                 try:
  132.                     y['device3'] = p3_pat.search(x).group(1)
  133.                 except AttributeError:
  134.                     y['device3'] = '0'
  135.                 else:
  136.                     y['num_devices'] += 1
  137.  
  138.         if y['device1'] is None:
  139.             y['device1'] = '0'
  140.  
  141.         if y['device2'] is None:
  142.             y['device2'] = '0'
  143.  
  144.         if y['device3'] is None:
  145.             y['device3'] = '0'
  146.  
  147.         try:
  148.             y['product_id'] = prod_pat.search(x).group(1)
  149.         except AttributeError:
  150.             y['product_id'] = ''
  151.         try:
  152.             y['mac'] = mac_pat.search(x).group(1)
  153.         except AttributeError:
  154.             y['mac'] = ''
  155.         try:
  156.             y['ip'] = ip_pat.search(x).group(1)
  157.         except AttributeError:
  158.             y['ip'] = ''
  159.         try:
  160.             y['hn'] = hn_pat.search(x).group(1)
  161.         except AttributeError:
  162.             y['hn'] = ''
  163.  
  164.         y['status_code'] = status_code
  165.         found_devices[addr[0]] = y
  166.  
  167.         log.debug("Found device: %s" % y)
  168.  
  169.  
  170.     return found_devices
  171.  
  172.  
  173.