内核网络四层结构: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);
};