关闭

Linux内核--网络栈实现分析(八)--应用层发送数据(下)

发表于:2013-1-04 09:39

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:闫明    来源:51Testing软件测试网采编

  注:标题中的”(上)“,”(下)“表示分析过程基于数据包的传递方向:”(上)“表示分析是从底层向上分析、”(下)“表示分析是从上向下分析。

  下面是发送数据的流程:

  应用层发送数据包的入口函数是BSD socket层的sock_write()函数,在分析该函数之前,先分析下socket的创建,系统调用sys_socket()对应的BSD socket层函数为sock_socket()

  sock_socket()函数

/*
 * Perform the socket system call. we locate the appropriate
 * family, then create a fresh socket.
 */

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 = 0; 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 < 0)
   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())) //获取一个socket,已经完成了socket部分初始化设置
 {
  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)) < 0) //调用INET层函数,inet_create()函数,创建inet层的socket,即sock结构
 {
  sock_release(sock);
  return(i);
 }

 if ((fd = get_fd(SOCK_INODE(sock))) < 0) //根据sock结构中的inode,分配文件描述符
 {
  sock_release(sock);
  return(-EINVAL);
 }

 return(fd);
}

41/41234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号