PyXR

c:\python24\lib\site-packages\win32\lib \ netbios.py



0001 import win32wnet
0002 import struct
0003 import string
0004 
0005 # Constants generated by h2py from nb30.h
0006 NCBNAMSZ = 16
0007 MAX_LANA = 254
0008 NAME_FLAGS_MASK = 0x87
0009 GROUP_NAME = 0x80
0010 UNIQUE_NAME = 0x00
0011 REGISTERING = 0x00
0012 REGISTERED = 0x04
0013 DEREGISTERED = 0x05
0014 DUPLICATE = 0x06
0015 DUPLICATE_DEREG = 0x07
0016 LISTEN_OUTSTANDING = 0x01
0017 CALL_PENDING = 0x02
0018 SESSION_ESTABLISHED = 0x03
0019 HANGUP_PENDING = 0x04
0020 HANGUP_COMPLETE = 0x05
0021 SESSION_ABORTED = 0x06
0022 ALL_TRANSPORTS = "M\0\0\0"
0023 MS_NBF = "MNBF"
0024 NCBCALL = 0x10
0025 NCBLISTEN = 0x11
0026 NCBHANGUP = 0x12
0027 NCBSEND = 0x14
0028 NCBRECV = 0x15
0029 NCBRECVANY = 0x16
0030 NCBCHAINSEND = 0x17
0031 NCBDGSEND = 0x20
0032 NCBDGRECV = 0x21
0033 NCBDGSENDBC = 0x22
0034 NCBDGRECVBC = 0x23
0035 NCBADDNAME = 0x30
0036 NCBDELNAME = 0x31
0037 NCBRESET = 0x32
0038 NCBASTAT = 0x33
0039 NCBSSTAT = 0x34
0040 NCBCANCEL = 0x35
0041 NCBADDGRNAME = 0x36
0042 NCBENUM = 0x37
0043 NCBUNLINK = 0x70
0044 NCBSENDNA = 0x71
0045 NCBCHAINSENDNA = 0x72
0046 NCBLANSTALERT = 0x73
0047 NCBACTION = 0x77
0048 NCBFINDNAME = 0x78
0049 NCBTRACE = 0x79
0050 ASYNCH = 0x80
0051 NRC_GOODRET = 0x00
0052 NRC_BUFLEN = 0x01
0053 NRC_ILLCMD = 0x03
0054 NRC_CMDTMO = 0x05
0055 NRC_INCOMP = 0x06
0056 NRC_BADDR = 0x07
0057 NRC_SNUMOUT = 0x08
0058 NRC_NORES = 0x09
0059 NRC_SCLOSED = 0x0a
0060 NRC_CMDCAN = 0x0b
0061 NRC_DUPNAME = 0x0d
0062 NRC_NAMTFUL = 0x0e
0063 NRC_ACTSES = 0x0f
0064 NRC_LOCTFUL = 0x11
0065 NRC_REMTFUL = 0x12
0066 NRC_ILLNN = 0x13
0067 NRC_NOCALL = 0x14
0068 NRC_NOWILD = 0x15
0069 NRC_INUSE = 0x16
0070 NRC_NAMERR = 0x17
0071 NRC_SABORT = 0x18
0072 NRC_NAMCONF = 0x19
0073 NRC_IFBUSY = 0x21
0074 NRC_TOOMANY = 0x22
0075 NRC_BRIDGE = 0x23
0076 NRC_CANOCCR = 0x24
0077 NRC_CANCEL = 0x26
0078 NRC_DUPENV = 0x30
0079 NRC_ENVNOTDEF = 0x34
0080 NRC_OSRESNOTAV = 0x35
0081 NRC_MAXAPPS = 0x36
0082 NRC_NOSAPS = 0x37
0083 NRC_NORESOURCES = 0x38
0084 NRC_INVADDRESS = 0x39
0085 NRC_INVDDID = 0x3B
0086 NRC_LOCKFAIL = 0x3C
0087 NRC_OPENERR = 0x3f
0088 NRC_SYSTEM = 0x40
0089 NRC_PENDING = 0xff
0090 
0091 
0092 UCHAR = "B"
0093 WORD = "H"
0094 DWORD = "I"
0095 USHORT = "H"
0096 ULONG = "I"
0097 
0098 ADAPTER_STATUS_ITEMS = [
0099     ("6s",        "adapter_address"),
0100     (UCHAR,   "rev_major"),
0101     (UCHAR,   "reserved0"),
0102     (UCHAR,   "adapter_type"),
0103     (UCHAR,   "rev_minor"),
0104     (WORD,    "duration"),
0105     (WORD,    "frmr_recv"),
0106     (WORD,    "frmr_xmit"),
0107 
0108     (WORD,    "iframe_recv_err"),
0109 
0110     (WORD,    "xmit_aborts"),
0111     (DWORD,   "xmit_success"),
0112     (DWORD,   "recv_success"),
0113 
0114     (WORD,    "iframe_xmit_err"),
0115 
0116     (WORD,    "recv_buff_unavail"),
0117     (WORD,    "t1_timeouts"),
0118     (WORD,    "ti_timeouts"),
0119     (DWORD,   "reserved1"),
0120     (WORD,    "free_ncbs"),
0121     (WORD,    "max_cfg_ncbs"),
0122     (WORD,    "max_ncbs"),
0123     (WORD,    "xmit_buf_unavail"),
0124     (WORD,    "max_dgram_size"),
0125     (WORD,    "pending_sess"),
0126     (WORD,    "max_cfg_sess"),
0127     (WORD,    "max_sess"),
0128     (WORD,    "max_sess_pkt_size"),
0129     (WORD,    "name_count"),
0130 ]
0131 
0132 NAME_BUFFER_ITEMS = [
0133     (str(NCBNAMSZ) + "s", "name"),
0134     (UCHAR,   "name_num"),
0135     (UCHAR,   "name_flags"),
0136 ]
0137 
0138 SESSION_HEADER_ITEMS = [
0139     (UCHAR,   "sess_name"),
0140     (UCHAR,   "num_sess"),
0141     (UCHAR,   "rcv_dg_outstanding"),
0142     (UCHAR,   "rcv_any_outstanding"),
0143 ]
0144 
0145 SESSION_BUFFER_ITEMS = [
0146     (UCHAR,   "lsn"),
0147     (UCHAR,   "state"),
0148     (str(NCBNAMSZ)+"s",   "local_name"),
0149     (str(NCBNAMSZ)+"s",   "remote_name"),
0150     (UCHAR,   "rcvs_outstanding"),
0151     (UCHAR,   "sends_outstanding"),
0152 ]
0153 
0154 LANA_ENUM_ITEMS = [
0155     ("B",   "length"),         # Number of valid entries in lana[]
0156     (str(MAX_LANA+1) + "s", "lana"),
0157 ]
0158 
0159 FIND_NAME_HEADER_ITEMS = [
0160     (WORD,    "node_count"),
0161     (UCHAR,   "reserved"),
0162     (UCHAR,   "unique_group"),
0163 ]
0164 
0165 FIND_NAME_BUFFER_ITEMS = [
0166     (UCHAR,   "length"),
0167     (UCHAR,   "access_control"),
0168     (UCHAR,   "frame_control"),
0169     ("6s"   "destination_addr"),
0170     ("6s", "source_addr"), 
0171     ("18s", "routing_info"),
0172 ]
0173 
0174 ACTION_HEADER_ITEMS = [
0175     (ULONG,   "transport_id"),
0176     (USHORT,  "action_code"),
0177     (USHORT,  "reserved"),
0178 ]
0179     
0180 del UCHAR, WORD, DWORD, USHORT, ULONG
0181 
0182 NCB = win32wnet.NCB
0183 def Netbios(ncb):
0184     ob = ncb.Buffer
0185     is_ours = hasattr(ob, "_pack")
0186     if is_ours:
0187         ob._pack()
0188     try:
0189         return win32wnet.Netbios(ncb)
0190     finally:
0191         if is_ours:
0192             ob._unpack()
0193         
0194 class NCBStruct:
0195     def __init__(self, items):
0196         self._format = string.join(map(lambda item: item[0], items), "")
0197         self._items = items
0198         self._buffer_ = win32wnet.NCBBuffer(struct.calcsize(self._format))
0199 
0200         for format, name in self._items:
0201             if len(format)==1:
0202                 if format == 'c':
0203                     val = '\0'
0204                 else:
0205                     val = 0
0206             else:
0207                 l = int(format[:-1])
0208                 val = '\0' * l
0209             self.__dict__[name] = val
0210 
0211     def _pack(self):
0212         vals = []
0213         for format, name in self._items:
0214             try:
0215                 vals.append(self.__dict__[name])
0216             except KeyError:
0217                 vals.append(None)
0218         
0219         self._buffer_[:] = apply( struct.pack, (self._format,) + tuple(vals) )
0220 
0221     def _unpack(self):
0222         items = struct.unpack(self._format, self._buffer_)
0223         assert len(items)==len(self._items), "unexpected number of items to unpack!"
0224         for (format, name), val in map(None, self._items, items):
0225             self.__dict__[name] = val
0226 
0227     def __setattr__(self, attr, val):
0228         if not self.__dict__.has_key(attr) and attr[0]!='_':
0229             for format, attr_name in self._items:
0230                 if attr==attr_name:
0231                     break
0232             else:
0233                 raise AttributeError, attr
0234         self.__dict__[attr] = val
0235 
0236 def ADAPTER_STATUS():
0237     return NCBStruct(ADAPTER_STATUS_ITEMS)
0238 
0239 def NAME_BUFFER():
0240     return NCBStruct(NAME_BUFFER_ITEMS)
0241 
0242 def SESSION_HEADER():
0243     return NCBStruct(SESSION_HEADER_ITEMS)
0244 
0245 def SESSION_BUFFER():
0246     return NCBStruct(SESSION_BUFFER_ITEMS)
0247 
0248 def LANA_ENUM():
0249     return NCBStruct(LANA_ENUM_ITEMS)
0250 
0251 def FIND_NAME_HEADER():
0252     return NCBStruct(FIND_NAME_HEADER_ITEMS)
0253 
0254 def FIND_NAME_BUFFER():
0255     return NCBStruct(FIND_NAME_BUFFER_ITEMS)
0256 
0257 def ACTION_HEADER():
0258     return NCBStruct(ACTION_HEADER_ITEMS)
0259 
0260 if __name__=='__main__':
0261     # code ported from "HOWTO: Get the MAC Address for an Ethernet Adapter"
0262     # MS KB ID: Q118623 
0263     ncb = NCB()
0264     ncb.Command = NCBENUM
0265     la_enum = LANA_ENUM()
0266     ncb.Buffer = la_enum
0267     rc = Netbios(ncb)
0268     if rc != 0: raise RuntimeError, "Unexpected result %d" % (rc,)
0269     for i in range(la_enum.length):
0270         ncb.Reset()
0271         ncb.Command = NCBRESET
0272         ncb.Lana_num = ord(la_enum.lana[i])
0273         rc = Netbios(ncb)
0274         if rc != 0: raise RuntimeError, "Unexpected result %d" % (rc,)
0275         ncb.Reset()
0276         ncb.Command = NCBASTAT
0277         ncb.Lana_num = ord(la_enum.lana[i])
0278         ncb.Callname = "*               "
0279         adapter = ADAPTER_STATUS()
0280         ncb.Buffer = adapter
0281         Netbios(ncb)
0282         print "Adapter address:",
0283         for ch in adapter.adapter_address:
0284             print "%02x" % (ord(ch),) ,
0285         print
0286 

Generated by PyXR 0.9.4
SourceForge.net Logo