簡體   English   中英

python unpack緩沖區數據

[英]python unpack buffer data

我在半徑中使用rlm_python模塊,它以十六進制或二進制格式從coovachilli獲取DHCP位置option82

將其作為字符串捕獲顯示為此值\\001\\027\\002\\025\\001+\\001\\024 ,但查看python顯示的值被截斷,因為option82包含以TLVs-type,length,values編碼的suboptions TLVs-type,length,values表示字段以0x01(circuit ID, per RFC 3046)類型0x01(circuit ID, per RFC 3046)開頭,后跟一個字節長度。

知道如何抓住這個並正確解開選項嗎?

我已經使用struct.unpack解壓縮了字符串但是沒有意義struct.unpack因為它沒有告訴option82字段中的壓縮suboptions option82

Python 2.4.3 (#1, May  5 2011, 16:39:10) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from struct import *
>>> unpack('=hhl',"\001\027\002\025\001+\001\024" )
(5889, 5378, 335620865)

有任何想法嗎?

更新:

Coovachilli是用--locationopt82編譯的,它將位置作為屬性發送,類似這樣......

rad_recv: Accounting-Request packet from host 10.66.53.49 port 53178, id=101, length=342
        ChilliSpot-Version = "1.3.0"
        ChilliSpot-Acct-View-Point = ChilliSpot-Client-View-Point
        Event-Timestamp = "Apr 18 2013 11:59:16 BST"
        User-Name = "3C-D0-F8-4A-05-68"
        Acct-Input-Octets = 0
        Acct-Output-Octets = 22851
        Acct-Input-Gigawords = 0
        Acct-Output-Gigawords = 0
        Acct-Input-Packets = 0
        Acct-Output-Packets = 370
        Acct-Session-Time = 5401
        ChilliSpot-Session-State = Authorized
        Acct-Status-Type = Interim-Update
        Acct-Session-Id = "516fbceb00000002"
        Framed-IP-Address = 10.75.33.46
        NAS-Port-Type = Wireless-802.11
        NAS-Port = 2
        NAS-Port-Id = "00000002"
        Calling-Station-Id = "3C-D0-F8-4A-05-68"
        Called-Station-Id = "00-50-56-B7-66-00"
        NAS-IP-Address = 10.75.32.7
        ChilliSpot-Location = "\001\030\002\026\001+\001\024"
        ChilliSpot-Location-Change-Count = 15
        NAS-Identifier = "VLAN299-REGENT"
        WISPr-Location-ID = "isocc=GR,cc=44,ac=01200,network=mywifi,my_Network_regent"
        WISPr-Location-Name = "REGENT"

freeradius有rlm_python模塊,用於查找記帳請求

def accounting(p):
    file = open("/tmp/test.log","a")
    username = None
    chillilocation = None
    output = StringIO.StringIO()

    for t in p:
        if t[0] == 'User-Name':
            username = t[1]
        elif t[0] == 'ChilliSpot-Location':
            chillilocation = t[1]a
             output.write(t[1])


    content = output.getvalue()


    file.write("I am being called in radius accouting section as %s and location is %s \n" % (username,content))
    file.close()
    print "---Accounting---"
    radiusd.radlog(radiusd.L_INFO, '*** radlog call in accounting (0) ***')
    print
    print p
    return radiusd.RLM_MODULE_OK

我已經嘗試將ChilliSpot-Location存儲在stringstringIO ,使用struct解壓縮但無效,它看起來像TLV格式...

任何想法如何剝離它?

解包不會解包為“有意義”的值。 它將字符串解壓縮為一系列數字字段,以便每個字段的大小由格式字符串的每個字符指定(字母前面的數字是乘數)。

unpack不應該使用八位字節大小的字段嗎?

>>> unpack('= 8B',“\\ 001 \\ 027 \\ 002 \\ 025 \\ 001 + \\ 001 \\ 024”)
(1,23,2,21,1,43,1,20)
#或者'+'可能是分隔符(43):
>>> unpack('= = 6Bh',“\\ 001 \\ 027 \\ 002 \\ 025 \\ 001 + \\ 001 \\ 024”)
(1,23,2,21,1,43,5121)

第一個字節可能是電路子選項代碼(unpack()[0] == 1),其大小為23,這意味着我們沒有得到整個子選項值。 但是我們也可能只包含大小== 10或8字節內容的子選項的包含值。

我不太確定選項82應該包含可讀字符串,盡管“ChilliSpot-Location”可以。 RFC3046表示電路子選項應包含“路由器接口號”,端口號和其他數值等內容。 但rad_recv的屬性真的來自82選項嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM