home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / base / slp.py < prev    next >
Encoding:
Python Source  |  2006-08-30  |  5.4 KB  |  176 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2003-2006 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.  
  23. # Std Lib
  24. import sys
  25. import time
  26. import socket
  27. import select
  28. import struct
  29. import random
  30. import re
  31.  
  32. # Local
  33. from g import *
  34.  
  35.  
  36. prod_pat = re.compile(r"""\(\s*x-hp-prod_id\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
  37. mac_pat  = re.compile(r"""\(\s*x-hp-mac\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
  38. num_port_pat = re.compile(r"""\(\s*x-hp-num_port\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
  39. ip_pat =   re.compile(r"""\(\s*x-hp-ip\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
  40. p1_pat =   re.compile(r"""\(\s*x-hp-p1\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
  41. p2_pat =   re.compile(r"""\(\s*x-hp-p2\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
  42. p3_pat =   re.compile(r"""\(\s*x-hp-p3\s*=(?:\d\)|\s*(.*?)\s*\))""", re.IGNORECASE)
  43. hn_pat =   re.compile(r"""\(\s*x-hp-hn\s*=\s*(.*?)\s*\)""", re.IGNORECASE)
  44.         
  45.         
  46.  
  47. def detectNetworkDevices(mcast_addr='224.0.1.60', mcast_port=427, ttl=4, timeout=5, xid=None, qappobj = None):
  48.     found_devices = {}
  49.  
  50.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  51.     
  52.     try:
  53.         addr = socket.gethostname()
  54.         intf = socket.gethostbyname(addr)
  55.     except socket.error:
  56.         x=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  57.         x.connect(('1.2.3.4',56))
  58.         intf = x.getsockname()[0]
  59.         x.close()
  60.  
  61.     s.setblocking(0)
  62.     ttl = struct.pack('b', ttl) 
  63.     
  64.     try:
  65.         s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, ttl)
  66.         s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf) + socket.inet_aton('0.0.0.0'))
  67.     except:
  68.         log.error("Unable to setup multicast socket for SLP.")
  69.         return {}
  70.         
  71.     packet = ''.join(['\x01\x06\x00,\x00\x00en\x00\x03', 
  72.                         struct.pack('!H', xid or random.randint(1, 65535)),
  73.                         '\x00\x00\x00\x18service:x-hpnp-discover:\x00\x00\x00\x00'])
  74.     
  75.     try:
  76.         s.sendto(packet, (mcast_addr, mcast_port))
  77.     except socket.error, e:
  78.         log.error("Unable to send broadcast SLP packet: %s" % e)
  79.        
  80.     time_left = timeout
  81.     
  82.  
  83.     while time_left > 0:
  84.         
  85.         if qappobj is not None:
  86.             qappobj.processEvents(0)
  87.         
  88.         start_time = time.time()
  89.         r, w, e = select.select([s], [], [], time_left)
  90.         time_left -= (time.time() - start_time)
  91.         
  92.         if qappobj is not None:
  93.             qappobj.processEvents(0)
  94.         
  95.         if r == []: continue
  96.         
  97.         data, addr = s.recvfrom(1024) 
  98.         ver, func, length, flags, dialect, lang_code, char_encode, recv_xid, status_code, attr_length = \
  99.             struct.unpack("!BBHBBHHHHH", data[:16])
  100.             
  101.         x = struct.unpack("!%ds" % attr_length, data[16:])[0].strip()
  102.         y = {} 
  103.         
  104.         try:
  105.             num_ports = int(num_port_pat.search(x).group(1))
  106.         except (AttributeError, ValueError):
  107.             num_ports = 1
  108.             
  109.         if num_ports == 0: # Embedded devices
  110.             num_ports = 1
  111.          
  112.         y['num_ports'] = num_ports
  113.         y['num_devices'] = 0
  114.         y['device1'] = '0'
  115.         y['device2'] = '0'
  116.         y['device3'] = '0'
  117.         
  118.         # Check port 1
  119.         try:
  120.             y['device1'] = p1_pat.search(x).group(1)
  121.         except AttributeError:
  122.             y['device1'] = '0'
  123.         else:
  124.             y['num_devices'] += 1
  125.         
  126.         
  127.         if num_ports > 1: # Check port 2
  128.             try:
  129.                 y['device2'] = p2_pat.search(x).group(1)
  130.             except AttributeError:
  131.                 y['device2'] = '0'
  132.             else:
  133.                 y['num_devices'] += 1
  134.             
  135.             
  136.             if num_ports > 2: # Check port 3
  137.                 try:
  138.                     y['device3'] = p3_pat.search(x).group(1)
  139.                 except AttributeError:
  140.                     y['device3'] = '0'
  141.                 else:
  142.                     y['num_devices'] += 1
  143.         
  144.         if y['device1'] is None:
  145.             y['device1'] = '0'
  146.         
  147.         if y['device2'] is None:
  148.             y['device2'] = '0'
  149.         
  150.         if y['device3'] is None:
  151.             y['device3'] = '0'
  152.         
  153.         try:
  154.             y['product_id'] = prod_pat.search(x).group(1)
  155.         except AttributeError:
  156.             y['product_id'] = ''
  157.         try:
  158.             y['mac'] = mac_pat.search(x).group(1)
  159.         except AttributeError:
  160.             y['mac'] = ''
  161.         try:
  162.             y['ip'] = ip_pat.search(x).group(1)
  163.         except AttributeError:
  164.             y['ip'] = ''
  165.         try:
  166.             y['hn'] = hn_pat.search(x).group(1)
  167.         except AttributeError:
  168.             y['hn'] = ''
  169.  
  170.         y['status_code'] = status_code
  171.         found_devices[addr[0]] = y
  172.  
  173.         
  174.     return found_devices
  175.     
  176.