1.[代码]lmcc_common.py
#!/usr/bin/env python
"Common Tools module for LMCC project"
import struct
import ctypes
def fieldcode(fieldname):
"return field code by field name"
field_2_code = {}
field_2_code['SESSION_KEY'] = '2'
field_2_code['STATUS'] = '3'
field_2_code['USER_ID'] = '4'
field_2_code['NAS_IP'] = '5'
field_2_code['NAS_PORT'] = '6'
field_2_code['NAS_PORT_TYPE'] = '7'
field_2_code['SERVICE_TYPE'] = '8'
field_2_code['SERVICE_PORT'] = '9'
field_2_code['SERVICE_IP'] = '10'
field_2_code['SESSION_START_TIME'] = '11'
field_2_code['SESSION_STATUS'] = '12'
field_2_code['SESSION_TIME'] = '13'
field_2_code['MAX_CONNS'] = '14'
field_2_code['USER_TYPE'] = '15'
field_2_code['SESSION_ID'] = '16'
field_2_code['CALLER'] = '17'
field_2_code['CALLEE'] = '18'
field_2_code['RESERVED1'] = '19'
field_2_code['RESERVED2'] = '20'
field_2_code['RESERVED3'] = '21'
field_2_code['RESERVED4'] = '22'
field_2_code['LAST_ALIVE_TIME'] = '23'
return field_2_code.get(fieldname)
def htons(num):
return struct.pack('!H', num)
def htonl(num):
return struct.pack('!I', num)
def ntohs(data):
return struct.unpack('!H', data)[0]
def ntohl(data):
return struct.unpack('!I', data)[0]
def ntohb(data):
return struct.unpack('B', data)[0]
def wraptlv(tlvs):
"wrap tlv"
if len(tlvs) == 0:
return [0, '']
bodylen = 0
body = ''
for item in tlvs:
attrtype = int(item[0])
attrvalue = item[1]
attrlen = len(attrvalue)
formatstr1 = "%ds" % attrlen
buf = ctypes.create_string_buffer(attrlen + 3)
offset = 0
struct.pack_into("B", buf, offset, attrtype)
offset = offset + 1
struct.pack_into("!H", buf, offset, attrlen + 3)
offset = offset + 2
struct.pack_into(formatstr1, buf, offset, attrvalue)
body = body + buf.raw
bodylen = bodylen + len(buf.raw)
return [bodylen, body]
def unwrapfieldtlv(data):
"unwrap field tlv"
return data
def unwraprecordtlv(data):
"unwrap record tlv"
record = []
offset = 0
while offset < len(data):
attrtypedata = data[offset:offset + 1]
attrtype = ntohb(attrtypedata)
offset = offset + 1
attrlendata = data[offset:offset + 2]
attrlen = ntohs(attrlendata)
offset = offset + 2
if attrlen - 3 > 0:
attrvaluedata = data[offset:offset + attrlen - 3]
offset = offset + attrlen - 3
if attrtype == 0:
attrvalue = unwraprecordtlv(attrvaluedata)
else:
attrvalue = unwrapfieldtlv(attrvaluedata)
record.append((str(attrtype), attrvalue))
else:
record.append((str(attrtype), ''))
return record
def unwraptlv(data):
"unwrap tlv"
result = []
offset = 0
while offset < len(data):
attrtypedata = data[offset:offset + 1]
attrtype = ntohb(attrtypedata)
offset = offset + 1
attrlendata = data[offset:offset + 2]
attrlen = ntohs(attrlendata)
offset = offset + 2
if attrlen - 3 > 0:
attrvaluedata = data[offset:offset + attrlen - 3]
offset = offset + attrlen - 3
if attrtype == 0:
attrvalue = unwraprecordtlv(attrvaluedata)
else:
attrvalue = unwrapfieldtlv(attrvaluedata)
result.append((str(attrtype), attrvalue))
else:
result.append((str(attrtype), ''))
return result
def sendpacket(sock, serialno, msgtype, errcode, tlvs):
"wrap data packet and send it"
totollen = 0
bodylen = 0
offset = 0
body = ''
wraptlvdata = wraptlv(tlvs)
bodylen = wraptlvdata[0]
body = wraptlvdata[1]
totollen = bodylen + 12
formatstr = "%ds" % bodylen
data = ctypes.create_string_buffer(totollen)
offset = 0
struct.pack_into("!I", data, offset, totollen)
offset = offset + 4
struct.pack_into("!I", data, offset, serialno)
offset = offset + 4
struct.pack_into("B", data, offset, msgtype)
offset = offset + 1
struct.pack_into("!H", data, offset, errcode)
offset = offset + 2
struct.pack_into("B", data, offset, 0)
offset = offset + 1
if bodylen > 0:
struct.pack_into(formatstr, data, offset, body)
sock.send(data)
def receivepacket(sock):
"receive packet and unwrap it"
data = sock.recv(4)
totallen = ntohl(data)
data = sock.recv(4)
serialno = ntohl(data)
data = sock.recv(1)
msgtype = ntohb(data)
data = sock.recv(2)
errcode = ntohs(data)
data = sock.recv(1)
attrnum = ntohb(data)
bodylen = totallen - 12
if bodylen > 0:
data = sock.recv(bodylen)
wraptlvdata = unwraptlv(data)
return [serialno, msgtype, errcode, attrnum, wraptlvdata]
return [serialno, msgtype, errcode, attrnum]
if __name__ == '__main__':
print 'test!'