2013-02-25 75 views
4

我只是想知道爲什麼sockaddr_storage是128字節。我知道必須至少有28個字節的IPv6,但這似乎有點過分,比sockaddr_in6大100個額外的字節。這僅僅是爲了將來證明存儲結構,還是有一個它現在需要的理由?128字節的sockaddr_storage大小

+0

有多大是一個'結構sockaddr_atmsvc'?我也不知道,但我敢打賭它大於28字節。 – 2013-02-25 20:30:00

回答

3

你會在rfc 2553的§3.10和這SO post找到你的問題的答案。

原因是的的組合應至少保持效率對準 IP6和其他協議數據和64位。

從RFC的相關部分:

一個簡單的除了套接字API,可以幫助應用程序
作家是「結構struct sockaddr_storage的」。這種數據結構可以簡化跨多個地址族和
平臺的便攜式代碼編寫。這個數據結構的設計具有以下目標。

- It has a large enough implementation specific maximum size to 
    store the desired set of protocol specific socket address data 
    structures. Specifically, it is at least large enough to 
    accommodate sockaddr_in and sockaddr_in6 and possibly other 
    protocol specific socket addresses too. 
    - It is aligned at an appropriate boundary so protocol specific 
    socket address data structure pointers can be cast to it and 
    access their fields without alignment problems. (e.g. pointers 
    to sockaddr_in6 and/or sockaddr_in can be cast to it and access 
    fields without alignment problems). 
    - It has the initial field(s) isomorphic to the fields of the 
    "struct sockaddr" data structure on that implementation which 
    can be used as a discriminants for deriving the protocol in use. 
    These initial field(s) would on most implementations either be a 
    single field of type "sa_family_t" (isomorphic to sa_family 
    field, 16 bits) or two fields of type uint8_t and sa_family_t 
    respectively, (isomorphic to sa_len and sa_family_t, 8 bits 
    each). 
2

可能有系統想要能夠適應此結構中的struct sockaddr_un。後者具有大約100到110個字符的系統相關路徑長度。 128是一個非常漂亮的偶數。