TCP/IP socket.c

本文深入解析了Linux Socket编程的关键概念和技术细节,包括socket结构、地址结构、协议注册过程及socket创建流程等。通过分析核心代码,揭示了网络编程在Linux内核中的实现机制。

linux/net/socket.c

1. pops is inited in the sock_register() function, and sock_register is called in inet_proto_init/ipx_proto_init, and inet_proto_init/ipx_proto_init is used to initialize the global protocol table.

/*
* The protocol list. Each protocol is registered in here.
*/
static struct proto_ops *pops[NPROTO];

/*
* Protocol Table
*/
struct net_proto protocols[] = {
#ifdef CONFIG_IPX
{ "IPX", ipx_proto_init },

#endif

...

}

and the protocols init is like following,

void proto_init(void)
{
extern struct net_proto protocols[]; /* Network protocols */
struct net_proto *pro;

/* Kick all configured protocols. */
pro = protocols;
while (pro->name != NULL)
{
(*pro->init_func)(pro);
pro++;
}
/* We're all done... */
}

and start_kernel -> sock_init -> proto_init

2. definition of sockaddr

struct sockaddr {
unsigned short sa_family; /* address family, AF_xxx */
char sa_data[14]; /* 14 bytes of protocol address */
};

3. __asm__ and __volatile__ 内嵌汇编用法

例如: # define barrier() _asm__volatile_("": : :"memory")

refer to : http://www.dzsc.com/data/html/2009-5-31/76505.html


4. the struct of socket is below,

/*
* Internal representation of a socket. not all the fields are used by
* all configurations:
*
* server client
* conn client connected to server connected to
* iconn list of clients -unused-
* awaiting connections
* wait sleep for clients, sleep for connection,
* sleep for i/o sleep for i/o
*/
struct socket {
short type; /* SOCK_STREAM, ... */
socket_state state;
long flags;
struct proto_ops *ops; /* protocols do most everything */
void *data; /* protocol data */
struct socket *conn; /* server socket connected to */
struct socket *iconn; /* incomplete client conn.s */
struct socket *next;
struct wait_queue **wait; /* ptr to place to wait on */
struct inode *inode;
struct fasync_struct *fasync_list; /* Asynchronous wake up list */
};

when creating a new socket using function sock_socket(), it will call sock_alloc() and get_fd(), return the file descriptor.

5. sys_socketcall 是所有sock_*函数的入口。

6. socket.c 文件中定义的函数作为BSD 层的函数集,对于INET 正常网络操作而言,其下层即对应inet/af_inet.c 文件中定义的函数。从对af_inet.c 文件的分析来看,其中定义的函数实际上也没有进行具体功能的实现,而是将请求继续传递给下一层(此时即传输层)。

reference: LINUX-2.4.0内核网络栈实现源代码分析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值