內核網絡四層結構:BSD socket層、INET socket層、IP層、數據鏈路層
sock結構工作在INET socket層,所有BSD socket層的操作都通過struct sock
及其域字段prot指針轉化為prot鎖指向的函數處理,所以sock結構是維系
BSD socket層和INET socket層的紐帶,其重要性是不言而喻的。
下面是對sock結構的簡單注釋,有些沒有的待我弄懂后再來修改,^_^
struct sock {
/* Socket demultiplex comparisons on incoming packets. */
__u32 daddr; /* 外部IP地址 */
__u32 rcv_saddr; /* 綁定的本地IP地址 */
__u16 dport; /* 目標端口 */
unsigned short num; /* 本地端口 */
int bound_dev_if; /* 綁定網卡接口的索引 不為0 */
/* 用于各種協議查找表的哈希鏈表 */
struct sock *next;
struct sock **pprev;
struct sock *bind_next;
struct sock **bind_pprev;
volatile unsigned char state, /* 連接狀態 */
zapped; /* In ax25 & ipx means not linked */
__u16 sport; /* 源端口 */
unsigned short family; /* 地址簇 */
unsigned char reuse; /* SO_REUSEADDR設置,表示地址重用 */
unsigned char shutdown; /* 半關閉標志 SEND_SHUTDOWN和RCV_SHUTDOWN掩碼 */
atomic_t refcnt; /* 引用計數 */
socket_lock_t lock; /* 同步鎖. */
int rcvbuf; /* 接受緩沖區長度(單位:字節) */
wait_queue_head_t *sleep; /* 等待隊列,通常指向socket的wait域 */
struct dst_entry *dst_cache; /* 目的地緩存,用于路由查找結果 */
rwlock_t dst_lock; /* 目的地緩存鎖*/
atomic_t rmem_alloc; /* 已接收隊列字節數 */
struct sk_buff_head receive_queue; /* 接收包隊列 */
atomic_t wmem_alloc; /* 已發送字節數 */
struct sk_buff_head write_queue; /* 包發送隊列 */
atomic_t omem_alloc; /* "o"是"option"(選項)或"other"(其它)的意思 */
int wmem_queued; /* 穩定的隊列長度 */
int forward_alloc; /* 提前分配的空間 */
__u32 saddr; /* 發送源地址 */
unsigned int allocation; /* 分配模式 */
int sndbuf; /* 發送緩沖區長度(單位:字節) */
struct sock *prev;
/* Not all are volatile, but some are, so we might as well say they all are.
* XXX Make this a flag word -DaveM
*/
volatile char dead,
done,
urginline,
keepopen,
linger,
destroy,
no_check, /*SO_NO_CHECK標志,表示是否檢查包*/
broadcast,
bsdism;
unsigned char debug;
unsigned char rcvtstamp;
unsigned char userlocks;/*SO_SNDBUF和SO_RCVBUF標志 */
int proc;
unsigned long lingertime;
int hashent;
struct sock *pair;
/* The backlog queue is special, it is always used with
* the per-socket spinlock held and requires low latency
* access. Therefore we special case it's implementation.
*/
struct {
struct sk_buff *head;
struct sk_buff *tail;
} backlog;
rwlock_t callback_lock;/*用于結構末尾的回調函數互斥*/
/* 錯誤隊列,很少使用 */
struct sk_buff_head error_queue;
struct proto *prot;/*網絡地址簇內部協議處理*/
#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
union {
struct ipv6_pinfo af_inet6;
} net_pinfo;
#endif
union {
struct tcp_opt af_tcp;
#if defined(CONFIG_INET) || defined (CONFIG_INET_MODULE)
struct raw_opt tp_raw4;
#endif
#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
struct raw6_opt tp_raw;
#endif /* CONFIG_IPV6 */
#if defined(CONFIG_SPX) || defined (CONFIG_SPX_MODULE)
struct spx_opt af_spx;
#endif /* CONFIG_SPX */
} tp_pinfo;
int err, err_soft; /* Soft holds errors that don't
cause failure but are the cause
of a persistent failure not just
'timed out' */
unsigned short ack_backlog;
unsigned short max_ack_backlog;
__u32 priority;
unsigned short type; /*socket類型,如:SOCK_STREAM*/
unsigned char localroute; /* 僅用于本地路由 */
unsigned char protocol; /*socket 所屬網絡地址簇的協議*/
struct ucred peercred; /*對端sock信息 SO_PEERCRED標志設置*/
int rcvlowat; /*SO_RCVLOWAT標志位*/
long rcvtimeo; /*SO_RCVTIMEO標志位*/
long sndtimeo; /*SO_SNDTIMEO標志位*/
#ifdef CONFIG_FILTER
/* Socket過濾指令 */
struct sk_filter *filter;
#endif /* CONFIG_FILTER */
/* This is where all the private (optional) areas that don't
* overlap will eventually live.
*/
union {
void *destruct_hook;
struct unix_opt af_unix;
#if defined(CONFIG_INET) || defined (CONFIG_INET_MODULE)
struct inet_opt af_inet;
#endif
#if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
struct atalk_sock af_at;
#endif
#if defined(CONFIG_IPX) || defined(CONFIG_IPX_MODULE)
struct ipx_opt af_ipx;
#endif
#if defined (CONFIG_DECNET) || defined(CONFIG_DECNET_MODULE)
struct dn_scp dn;
#endif
#if defined (CONFIG_PACKET) || defined(CONFIG_PACKET_MODULE)
struct packet_opt *af_packet;
#endif
#if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
x25_cb *x25;
#endif
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
ax25_cb *ax25;
#endif
#if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
nr_cb *nr;
#endif
#if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
rose_cb *rose;
#endif
#if defined(CONFIG_PPPOE) || defined(CONFIG_PPPOE_MODULE)
struct pppox_opt *pppox;
#endif
#ifdef CONFIG_NETLINK
struct netlink_opt *af_netlink;
#endif
#if defined(CONFIG_ECONET) || defined(CONFIG_ECONET_MODULE)
struct econet_opt *af_econet;
#endif
#if defined(CONFIG_ATM) || defined(CONFIG_ATM_MODULE)
struct atm_vcc *af_atm;
#endif
#if defined(CONFIG_IRDA) || defined(CONFIG_IRDA_MODULE)
struct irda_sock *irda;
#endif
} protinfo; /*私有數據區,網絡地址簇規范*/
/* This part is used for the timeout functions. */
struct timer_list timer; /* sock清除定時器. */
struct timeval stamp; /*最后到達的包的時間戳*/
/* Identd and reporting IO signals */
struct socket *socket; /*BSD socket層套接字*/
/* RPC層私有數據 */
void *user_data;
/* 回調函數 */
void (*state_change)(struct sock *sk);
void (*data_ready)(struct sock *sk,int bytes);
void (*write_space)(struct sock *sk);
void (*error_report)(struct sock *sk);
int (*backlog_rcv) (struct sock *sk,
struct sk_buff *skb);
void (*destruct)(struct sock *sk);
};