~mimose/+junk/hplip-3.16.11

« back to all changes in this revision

Viewing changes to base/slp.py

  • Committer: guoyalong
  • Date: 2017-09-20 10:13:05 UTC
  • Revision ID: guoyalong@kylinos.cn-20170920101305-82zaolzpv1qghz29
Modified debian/control & debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# (c) Copyright 2003-2015 HP 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
from . import utils
 
34
from .sixext import to_bytes_utf8, to_unicode, to_string_utf8
 
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
def createSocketsWithsetOption(ttl=4):
 
46
    s=None
 
47
    
 
48
    try:
 
49
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
 
50
        x = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 
51
        x.connect(('1.2.3.4', 56))
 
52
        intf = x.getsockname()[0]
 
53
        x.close()
 
54
        s.setblocking(0)
 
55
        ttl = struct.pack('B', ttl) 
 
56
    except socket.error:
 
57
        log.error("Network error")
 
58
        if s:
 
59
            s.close()
 
60
        return None
 
61
        
 
62
    try:
 
63
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
 
64
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
 
65
    except (AttributeError, socket.error):
 
66
        pass
 
67
 
 
68
    try:
 
69
        s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, ttl)
 
70
        s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf) + socket.inet_aton('0.0.0.0'))
 
71
        s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP ,1)
 
72
    except Exception as e:
 
73
        log.error("Unable to setup multicast socket for SLP: %s" % e)
 
74
        if s:
 
75
            s.close()
 
76
        return None
 
77
    return s
 
78
 
 
79
 
 
80
def detectNetworkDevices(ttl=4, timeout=10): #, xid=None, qappobj = None):
 
81
    mcast_addr, mcast_port ='224.0.1.60', 427
 
82
    found_devices = {}
 
83
 
 
84
    s = createSocketsWithsetOption(ttl)
 
85
    if not s:
 
86
        return {}
 
87
 
 
88
    packet = b''.join([to_bytes_utf8('\x01\x06\x00\x2c\x00\x00\x65\x6e\x00\x03'), 
 
89
        struct.pack('!H', random.randint(1, 65535)), to_bytes_utf8('\x00\x00\x00\x18service:x-hpnp-discover:\x00\x00\x00\x00')])
 
90
 
 
91
    try:
 
92
        s.sendto(packet, 0, (mcast_addr, mcast_port))
 
93
    except socket.error as e:
 
94
        log.error("Unable to send broadcast SLP packet: %s" % e)
 
95
 
 
96
    time_left = timeout
 
97
    while time_left > 0:
 
98
        start_time = time.time()
 
99
        r, w, e = select.select([s], [], [s], time_left)
 
100
        time_left -= (time.time() - start_time)
 
101
        if not r: continue
 
102
 
 
103
        data, addr = s.recvfrom(2048)
 
104
        update_spinner() 
 
105
 
 
106
        log.log_data(data, width=32)
 
107
 
 
108
        try:
 
109
            ver, func, length, flags, dialect, lang_code, char_encode, recv_xid, status_code, attr_length = \
 
110
                struct.unpack("!BBHBBHHHHH", data[:16])
 
111
 
 
112
            x = struct.unpack("!%ds" % attr_length, data[16:])[0].strip()
 
113
        except struct.error:
 
114
            continue
 
115
        x= to_string_utf8(x)
 
116
        try:
 
117
            num_ports = int(num_port_pat.search(x).group(1))
 
118
        except (AttributeError, ValueError):
 
119
            num_ports = 1
 
120
 
 
121
        if num_ports == 0: # Embedded devices
 
122
            num_ports = 1
 
123
 
 
124
        y = {'num_devices' : 0, 'num_ports': num_ports, 'product_id' : '', 
 
125
             'status_code': 0, 'device2': '0', 'device3': '0', 'note': '', 'device1': '0'}
 
126
 
 
127
        # Check port 1
 
128
        try:
 
129
            y['device1'] = p1_pat.search(x).group(1)
 
130
        except AttributeError:
 
131
            y['device1'] = '0'
 
132
        else:
 
133
            y['num_devices'] += 1
 
134
 
 
135
 
 
136
        if num_ports > 1: # Check port 2
 
137
            try:
 
138
                y['device2'] = p2_pat.search(x).group(1)
 
139
            except AttributeError:
 
140
                y['device2'] = '0'
 
141
            else:
 
142
                y['num_devices'] += 1
 
143
 
 
144
 
 
145
            if num_ports > 2: # Check port 3
 
146
                try:
 
147
                    y['device3'] = p3_pat.search(x).group(1)
 
148
                except AttributeError:
 
149
                    y['device3'] = '0'
 
150
                else:
 
151
                    y['num_devices'] += 1
 
152
 
 
153
        if y['device1'] is None:
 
154
            y['device1'] = '0'
 
155
 
 
156
        if y['device2'] is None:
 
157
            y['device2'] = '0'
 
158
 
 
159
        if y['device3'] is None:
 
160
            y['device3'] = '0'
 
161
 
 
162
        try:
 
163
            y['product_id'] = prod_pat.search(x).group(1)
 
164
        except AttributeError:
 
165
            y['product_id'] = ''
 
166
        try:
 
167
            y['mac'] = mac_pat.search(x).group(1)
 
168
        except AttributeError:
 
169
            y['mac'] = ''
 
170
        try:
 
171
            y['ip'] = ip_pat.search(x).group(1)
 
172
        except AttributeError:
 
173
            y['ip'] = ''
 
174
        try:
 
175
            y['hn'] = hn_pat.search(x).group(1)
 
176
        except AttributeError:
 
177
            y['hn'] = ''
 
178
 
 
179
        y['status_code'] = status_code
 
180
        found_devices[addr[0]] = y
 
181
 
 
182
        log.debug("Found device: %s" % y)
 
183
 
 
184
    s.close()
 
185
    return found_devices
 
186
 
 
187