网络协议栈学习(二)创建 socket
下面通过学习linux 1.2.13源码进一步理解socket通信机制。对该版本源码的学习主要参考《Linux内核网络栈源代码情景分析》(曹桂平 编著)。
要理解socket的本质,就要理解当调用socket函数时,该函数到底创建了什么?返回了什么?
- int socket(int family, int type, int protocol);
socket 函数为用户层函数,该函数对应的内核函数为sock_socket(socket.c文件),源码如下:
- static int sock_socket(int family, int type, int protocol)
- {
- int i, fd;
- struct socket *sock;
- struct proto_ops *ops;
- /* Locate the correct protocol family. */
- for (i = ; i < NPROTO; ++i)
- {
- if (pops[i] == NULL) continue;
- if (pops[i]->family == family)
- break;
- }
- if (i == NPROTO)
- {
- return -EINVAL;
- }
- ops = pops[i];
- /*
- * Check that this is a type that we know how to manipulate and
- * the protocol makes sense here. The family can still reject the
- * protocol later.
- */
- if ((type != SOCK_STREAM && type != SOCK_DGRAM &&
- type != SOCK_SEQPACKET && type != SOCK_RAW &&
- type != SOCK_PACKET) || protocol < )
- return(-EINVAL);
- /*
- * Allocate the socket and allow the family to set things up. if
- * the protocol is 0, the family is instructed to select an appropriate
- * default.
- */
- if (!(sock = sock_alloc()))
- {
- printk("NET: sock_socket: no more sockets\n");
- return(-ENOSR); /* Was: EAGAIN, but we are out of
- system resources! */
- }
- sock->type = type;
- sock->ops = ops;
- if ((i = sock->ops->create(sock, protocol)) < )
- {
- sock_release(sock);
- return(i);
- }
- if ((fd = get_fd(SOCK_INODE(sock))) < )
- {
- sock_release(sock);
- return(-EINVAL);
- }
- return(fd);
- }
sock_socket 函数完成如下工作:
(1)分配socket、sock结构,这两个结构在网络栈的不同层次表示一个套接字连接。
(2)分配inode、file结构用于普通文件操作。
(3)分配一个文件描述符并返回给应用程序作为以后的操作句柄。
sock_alloc 函数用于分配一个inode节点,并返回该节点的socket指针
- struct socket *sock_alloc(void)
- {
- struct inode * inode;
- struct socket * sock;
- inode = get_empty_inode();
- if (!inode)
- return NULL;
- inode->i_mode = S_IFSOCK;
- inode->i_sock = ;
- inode->i_uid = current->uid;
- inode->i_gid = current->gid;
- sock = &inode->u.socket_i;
- sock->state = SS_UNCONNECTED;
- sock->flags = ;
- sock->ops = NULL;
- sock->data = NULL;
- sock->conn = NULL;
- sock->iconn = NULL;
- sock->next = NULL;
- sock->wait = &inode->i_wait;
- sock->inode = inode; /* "backlink": we could use pointer arithmetic instead */
- sock->fasync_list = NULL;
- sockets_in_use++;
- return sock;
- }
inode的定义如下
- /* include/fs.h */
- struct inode {
- dev_t i_dev;
- unsigned long i_ino;
- umode_t i_mode;
- nlink_t i_nlink;
- uid_t i_uid;
- gid_t i_gid;
- dev_t i_rdev;
- off_t i_size;
- time_t i_atime;
- time_t i_mtime;
- time_t i_ctime;
- unsigned long i_blksize;
- unsigned long i_blocks;
- unsigned long i_version;
- struct semaphore i_sem;
- struct inode_operations * i_op;
- struct super_block * i_sb;
- struct wait_queue * i_wait;
- struct file_lock * i_flock;
- struct vm_area_struct * i_mmap;
- struct inode * i_next, * i_prev;
- struct inode * i_hash_next, * i_hash_prev;
- struct inode * i_bound_to, * i_bound_by;
- struct inode * i_mount;
- unsigned short i_count;
- unsigned short i_wcount;
- unsigned short i_flags;
- unsigned char i_lock;
- unsigned char i_dirt;
- unsigned char i_pipe;
- unsigned char i_sock;
- unsigned char i_seek;
- unsigned char i_update;
- union {
- struct pipe_inode_info pipe_i;
- struct minix_inode_info minix_i;
- struct ext_inode_info ext_i;
- struct ext2_inode_info ext2_i;
- struct hpfs_inode_info hpfs_i;
- struct msdos_inode_info msdos_i;
- struct umsdos_inode_info umsdos_i;
- struct iso_inode_info isofs_i;
- struct nfs_inode_info nfs_i;
- struct xiafs_inode_info xiafs_i;
- struct sysv_inode_info sysv_i;
- struct socket socket_i;
- void * generic_ip;
- } u;
- };
inode 结构是文件系统的一个结构体,该结构体中的成员变量u指明了该inode结构具体的文件类型,当inode是用于socket通信时,u的值就为socket_i。sock_alloc 的作用就是创建inode结构体,然后返回socket_i的地址。至于具体如何分配inode涉及到文件系统方面的知识,这里暂不讨论。
当协议族为AF_INET时,ops->create 将调用inet_create(struct socket*sock, int protocol)函数。该函数将创建一个sock结构体并使得socket的data指针指向该sock结构体。
- static int inet_create(struct socket *sock, int protocol)
- {
- struct sock *sk;
- struct proto *prot;
- int err;
- sk = (struct sock *) kmalloc(sizeof(*sk), GFP_KERNEL);
- if (sk == NULL)
- return(-ENOBUFS);
- sk->num = ;
- sk->reuse = ;
- switch(sock->type)
- {
- case SOCK_STREAM:
- case SOCK_SEQPACKET:
- if (protocol && protocol != IPPROTO_TCP)
- {
- kfree_s((void *)sk, sizeof(*sk));
- return(-EPROTONOSUPPORT);
- }
- protocol = IPPROTO_TCP;
- sk->no_check = TCP_NO_CHECK;
- prot = &tcp_prot;
- break;
- case SOCK_DGRAM:
- if (protocol && protocol != IPPROTO_UDP)
- {
- kfree_s((void *)sk, sizeof(*sk));
- return(-EPROTONOSUPPORT);
- }
- protocol = IPPROTO_UDP;
- sk->no_check = UDP_NO_CHECK;
- prot=&udp_prot;
- break;
- case SOCK_RAW:
- if (!suser())
- {
- kfree_s((void *)sk, sizeof(*sk));
- return(-EPERM);
- }
- if (!protocol)
- {
- kfree_s((void *)sk, sizeof(*sk));
- return(-EPROTONOSUPPORT);
- }
- prot = &raw_prot;
- sk->reuse = ;
- sk->no_check = ; /*
- * Doesn't matter no checksum is
- * performed anyway.
- */
- sk->num = protocol;
- break;
- case SOCK_PACKET:
- if (!suser())
- {
- kfree_s((void *)sk, sizeof(*sk));
- return(-EPERM);
- }
- if (!protocol)
- {
- kfree_s((void *)sk, sizeof(*sk));
- return(-EPROTONOSUPPORT);
- }
- prot = &packet_prot;
- sk->reuse = ;
- sk->no_check = ; /* Doesn't matter no checksum is
- * performed anyway.
- */
- sk->num = protocol;
- break;
- default:
- kfree_s((void *)sk, sizeof(*sk));
- return(-ESOCKTNOSUPPORT);
- }
- sk->socket = sock;
- #ifdef CONFIG_TCP_NAGLE_OFF
- sk->nonagle = ;
- #else
- sk->nonagle = ;
- #endif
- sk->type = sock->type;
- sk->stamp.tv_sec=;
- sk->protocol = protocol;
- ......
- sk->timer.function = &net_timer;
- skb_queue_head_init(&sk->back_log);
- sk->blog = ;
- sock->data =(void *) sk; //socket 指向 sock
- sk->dummy_th.doff = sizeof(sk->dummy_th)/;
- ......
- if (sk->prot->init)
- {
- err = sk->prot->init(sk);
- if (err != )
- {
- destroy_sock(sk);
- return(err);
- }
- }
- return();
- }
最后调用get_fd 返回一个文件描述符给上层应用。
- /* socket.c */
- static int get_fd(struct inode *inode)
- {
- int fd;
- struct file *file;
- /*
- * Find a file descriptor suitable for return to the user.
- */
- file = get_empty_filp(); // 获取一个闲置的file结构
- if (!file)
- return(-);
- for (fd = ; fd < NR_OPEN; ++fd)
- if (!current->files->fd[fd])
- break;
- if (fd == NR_OPEN)
- {
- file->f_count = ;
- return(-);
- }
- FD_CLR(fd, ¤t->files->close_on_exec);
- current->files->fd[fd] = file;
- file->f_op = &socket_file_ops; // socket 文件操作
- file->f_mode = ;
- file->f_flags = O_RDWR;
- file->f_count = ;
- file->f_inode = inode;
- if (inode)
- inode->i_count++;
- file->f_pos = ;
- return(fd);
- }
get_fd 用于为网络套接字分配一个文件描述符,分配描述符的同时需要一个file结构,每个file结构都需要一个inode结构对应。内核维护一个file结构数据,get_empty_filp 函数即通过检查该数组,获取一个闲置的成员。f_op 字段的赋值实现了网络操作的普通文件接口。如果调用write、read函数进行操作就会调用相应的sock_read 和 sock_write 函数。
如何根据文件描述如fd找到相应的sock?
网络协议栈学习(二)创建 socket的更多相关文章
- 网络协议栈学习(一)socket通信实例
网络协议栈学习(一)socket通信实例 该实例摘自<linux网络编程>(宋敬彬,孙海滨等著). 例子分为服务器端和客户端,客户端连接服务器后从标准输入读取输入的字符串,发送给服务器:服 ...
- 网络编程学习笔记-浅析socket
一.问题的引入——socket的引入是为了解决不同计算机间进程间通信的问题 .socket与进程的关系 ).socket与进程间的关系:socket 用来让一个进程和其他的进程互通信息(IPC),而S ...
- 网络编程学习笔记:Socket编程
文的主要内容如下: 1.网络中进程之间如何通信? 2.Socket是什么? 3.socket的基本操作 3.1.socket()函数 3.2.bind()函数 3.3.listen().connect ...
- 网络编程学习笔记--1.socket可读可写条件
转至 :http://blog.csdn.net/majianfei1023/article/details/45788591 socket可读可写条件,经常做为面试题被问,因为它考察被面试者对网络编 ...
- Linux网络编程学习(二) ----- 进程控制(第三章)
1.进程和程序 程序是一个可执行文件,而一个进程是一个执行中的程序实例.一个进程对应于一个程序的执行,进程是动态的,程序是静态的,多个进程可以并发执行同一个程序.比如几个用户可以同时运行一个编辑程序, ...
- 网络编程学习二(IP与端口)
InetAddress类 封装计算机的ip地址,没有端口 // 使用getLocalHost方法创建InetAddress对象 InetAddress addr = InetAddress.getLo ...
- pipelinewise 学习二 创建一个简单的pipeline
pipelinewise 提供了方便的创建简单pipeline的命令,可以简化pipeline 的创建,同时也可以帮我们学习 生成demo pipeline pipelinewise init --n ...
- sublime text 2学习(二):创建可复用的代码片段
对于前端工程师来讲,写一个html页面的基本结构是体力活,每次去拷贝一个也麻烦,sublime text 2 提供了一个很好的复用代码片段.下面介绍一下创建一个html5的代码片段的过程. 在菜单上点 ...
- micronaut 学习 二 创建一个简单的服务
micronaut 提供的cli 很方便,我们可以快速创建具有所需特性的应用,以下是一个简单的web server app 创建命令 mn create-app hello-world 效果 mn c ...
随机推荐
- 移动距离|2015年蓝桥杯B组题解析第八题-fishers
移动距离 X星球居民小区的楼房全是一样的,并且按矩阵样式排列.其楼房的编号为1,2,3... 当排满一行时,从下一行相邻的楼往反方向排号. 比如:当小区排号宽度为6时,开始情形如下: 1 2 3 4 ...
- Sql 最简单的Sqlserver连接
string name = txtUserName.Text.Trim();//移除用户名前部和后部的空格 string pwd = txtUserPwd.Text.Trim();//移除密码前部和后 ...
- 【Android实验】UI设计-Android计算器
目录 实验目的 实验要求 实验过程 1. 界面设计 2. 功能设计 3. 运算处理 实验目的 自主完成一个简单APP的设计工作,综合应用已经学到的Android UI设计技巧,重点注意合理使用布局 实 ...
- Network Simulator for P4(NSP4) src内容介绍
Structure What's NSP4? src source code introduction What's NSP4? NSP4是一个用于P4的网络仿真工具,旨在简化P4的环境部署和运行,将 ...
- FAST:通过Floodlight控制器下发流表
参考: Floodlight+Mininet搭建OpenFlow(四):流表操作 通过Floodlight控制器下发流表 下发流表的方式有两种: 1.借助Floodlight的北向API,利用curl ...
- Hibernate与iBastis 比较(转载)
Hibernate VS iBATIS 简介 Hibernate 是当前最流行的O/R mapping框架,当前版本是3.05.它出身于sf.net,现在已经成为Jboss的一部分了 iBATIS ...
- Cocos2d-x学习笔记(七)菜单
菜单类继承关系如下: 图1 菜单类继承关系 文本菜单只能显示文本,包括:MenuItemLabel.MenuItemFont和MenuItemAtlasFont: #include "Hel ...
- python 元组列表转为字典
#create a list l = [(), (), (), (), (), ()] d = {} for a, b in l: d.setdefault(a, []).append(b) prin ...
- c++ 容器元素填充指定数量的元素(generate_n)
#include <iostream> // cout #include <algorithm> // generate_n using namespace std; ; in ...
- js 基础数据类型和引用类型 ,深浅拷贝问题,以及内存分配问题
js 深浅拷贝问题 浅拷贝一般指的是基本类型的复制 深拷贝一般指引用类型的拷贝,把引用类型的值也拷贝出来 举例 h5的sessionStorage只能存放字符串,所以要存储json时就要把json使用 ...