linux1.2.13源码中,管理sock结构体的数据结构及操作函数

2024-03-27 21:48

本文主要是介绍linux1.2.13源码中,管理sock结构体的数据结构及操作函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

tcp和udp等协议在传输层都对应一个sock结构,该结构是实现协议的重要结构体,而传输层实现的就是对该结构体的管理。利用一个哈希链表根据端口号保存sock结构体。有了保存sock结构的数据结构后,还需要一系列的操作函数。代码如下。

/**	See if a socket number is in use.*/
// 看socket的端口是否在使用 
static int sk_inuse(struct proto *prot, int num)
{struct sock *sk;// 根据端口号取得哈希链表中的一个链表for(sk = prot->sock_array[num & (SOCK_ARRAY_SIZE -1 )];sk != NULL;  sk=sk->next) {if (sk->num == num) return(1);}return(0);
}/**	Pick a new socket number*/
// 随机获取一个端口
unsigned short get_new_socknum(struct proto *prot, unsigned short base)
{static int start=0;/** Used to cycle through the port numbers so the* chances of a confused connection drop.*/int i, j;int best = 0;int size = 32767; /* a big num. */struct sock *sk;// 大于1024if (base == 0) base = PROT_SOCK+1+(start % 1024);if (base <= PROT_SOCK) {base += PROT_SOCK+(start % 1024);}/* Now look through the entire array and try to find an empty ptr. */for(i=0; i < SOCK_ARRAY_SIZE; i++) {j = 0;// 找到一条链表sk = prot->sock_array[(i+base+1) &(SOCK_ARRAY_SIZE -1)];// 找到链表中的最后一个节点while(sk != NULL) {sk = sk->next;j++;}// 该链表上还没有节点,说明这个端口还没有被使用过,返回该端口号,更新start变量if (j == 0) {start =(i+1+start )%1024;return(i+base+1);}/*j为本次循环的队列的节点数,best记录新端口所属队列的索引,size为本次循环为止节点数最少的队列的节点数,为了避免单个队列过长,找可用端口的时候,不仅要找到一个可用的端口,而且尽量保证端口所对应的队列不会过长,避免查找的时候比较慢,所以for循环是为了找出哈希链表中节点数最少的队列对应的索引。然后往该队列插入一个新的端口节点*/if (j < size) {best = i;size = j;}}/* Now make sure the one we want is not in use. */// 在一条队列中找到一个未使用的端口号,SOCK_ARRAY_SIZE保证哈希后对应的是同一个队列while(sk_inuse(prot, base +best+1)) {best += SOCK_ARRAY_SIZE;}return(best+base+1);
}/**	Add a socket into the socket tables by number.*/void put_sock(unsigned short num, struct sock *sk)
{struct sock *sk1;struct sock *sk2;int mask;unsigned long flags;sk->num = num;sk->next = NULL;num = num &(SOCK_ARRAY_SIZE -1);/* We can't have an interrupt re-enter here. */save_flags(flags);cli();// 使用的socket数sk->prot->inuse += 1;// 最多使用的socket数if (sk->prot->highestinuse < sk->prot->inuse)sk->prot->highestinuse = sk->prot->inuse;// 链表为空,sk成为第一个节点if (sk->prot->sock_array[num] == NULL) {sk->prot->sock_array[num] = sk;restore_flags(flags);return;}restore_flags(flags);// mask为0xff000000 => 0xffff0000 => 0xffffff00 => 0xfffffffffor(mask = 0xff000000; mask != 0xffffffff; mask = (mask >> 8) | mask) {if ((mask & sk->saddr) &&(mask & sk->saddr) != (mask & 0xffffffff)) {mask = mask << 8;break;}}cli();// 根据端口找到对应的链表,找到对应的位置插入队列sk1 = sk->prot->sock_array[num];for(sk2 = sk1; sk2 != NULL; sk2=sk2->next) {if (!(sk2->saddr & mask)) {if (sk2 == sk1) {sk->next = sk->prot->sock_array[num];sk->prot->sock_array[num] = sk;sti();return;}sk->next = sk2;sk1->next= sk;sti();return;}sk1 = sk2;}/* Goes at the end. */sk->next = NULL;sk1->next = sk;sti();
}/**	Remove a socket from the socket tables.*/static void remove_sock(struct sock *sk1)
{struct sock *sk2;unsigned long flags;if (!sk1->prot) {printk("sock.c: remove_sock: sk1->prot == NULL\n");return;}/* We can't have this changing out from under us. */save_flags(flags);cli();sk2 = sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)];// 是队列的第一个节点if (sk2 == sk1) {sk1->prot->inuse -= 1;sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)] = sk1->next;restore_flags(flags);return;}// 找sk1while(sk2 && sk2->next != sk1) {sk2 = sk2->next;}// 找到if (sk2) {sk1->prot->inuse -= 1;sk2->next = sk1->next;restore_flags(flags);return;}restore_flags(flags);
}/**	Destroy an AF_INET socket*/void destroy_sock(struct sock *sk)
{struct sk_buff *skb;sk->inuse = 1;			/* just to be safe. *//* In case it's sleeping somewhere. */if (!sk->dead) sk->write_space(sk);remove_sock(sk);/* Now we can no longer get new packets. */delete_timer(sk);/* Nor send them */del_timer(&sk->retransmit_timer);while ((skb = tcp_dequeue_partial(sk)) != NULL) {IS_SKB(skb);kfree_skb(skb, FREE_WRITE);}/* Cleanup up the write buffer. */while((skb = skb_dequeue(&sk->write_queue)) != NULL) {IS_SKB(skb);kfree_skb(skb, FREE_WRITE);}/**	Don't discard received data until the user side kills its*	half of the socket.*/if (sk->dead) {while((skb=skb_dequeue(&sk->receive_queue))!=NULL) {/** This will take care of closing sockets that were* listening and didn't accept everything.*/// 处理listen型的socket,监听套接字接收队列里的skb关联的sock结构是一个新建的而不是skif (skb->sk != NULL && skb->sk != sk) {IS_SKB(skb);skb->sk->dead = 1;// 关闭连接skb->sk->prot->close(skb->sk, 0);}IS_SKB(skb);kfree_skb(skb, FREE_READ);}}	/* Now we need to clean up the send head. */cli();// 清空为了重传而缓存的数据包for(skb = sk->send_head; skb != NULL; ){struct sk_buff *skb2;/** We need to remove skb from the transmit queue,* or maybe the arp queue.*/if (skb->next  && skb->prev) {
/*			printk("destroy_sock: unlinked skb\n");*/IS_SKB(skb);skb_unlink(skb);}skb->dev = NULL;// unlink后link3指针仍然指向下一个skb节点skb2 = skb->link3;kfree_skb(skb, FREE_WRITE);skb = skb2;}sk->send_head = NULL;sti();/* And now the backlog. */// 还没来得及移到receive_queue队列的而缓存在back_log队列的skbwhile((skb=skb_dequeue(&sk->back_log))!=NULL) {/* this should never happen. */
/*		printk("cleaning back_log\n");*/kfree_skb(skb, FREE_READ);}/* Now if it has a half accepted/ closed socket. */if (sk->pair) {sk->pair->dead = 1;sk->pair->prot->close(sk->pair, 0);sk->pair = NULL;}/** Now if everything is gone we can free the socket* structure, otherwise we need to keep it around until* everything is gone.*/if (sk->dead && sk->rmem_alloc == 0 && sk->wmem_alloc == 0) {kfree_s((void *)sk,sizeof(*sk));} else {/* this should never happen. *//* actually it can if an ack has just been sent. */sk->destroy = 1;sk->ack_backlog = 0;sk->inuse = 0;reset_timer(sk, TIME_DESTROY, SOCK_DESTROY_TIME);}
}struct sock *get_sock(struct proto *prot, unsigned short num,unsigned long raddr,unsigned short rnum, unsigned long laddr)
{struct sock *s;struct sock *result = NULL;int badness = -1;unsigned short hnum;hnum = ntohs(num);/** SOCK_ARRAY_SIZE must be a power of two.  This will work better* than a prime unless 3 or more sockets end up using the same* array entry.  This should not be a problem because most* well known sockets don't overlap that much, and for* the other ones, we can just be careful about picking our* socket number when we choose an arbitrary one.*/for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];s != NULL; s = s->next) {int score = 0;if (s->num != hnum) continue;if(s->dead && (s->state == TCP_CLOSE))continue;/* local address matches? */if (s->saddr) {if (s->saddr != laddr)continue;score++;}/* remote address matches? */if (s->daddr) {if (s->daddr != raddr)continue;score++;}/* remote port matches? */if (s->dummy_th.dest) {if (s->dummy_th.dest != rnum)continue;score++;}/* perfect match? */// 全匹配,直接返回if (score == 3)return s;/* no, check if this is the best so far.. */if (score <= badness)continue;// 记录最好的匹配项result = s;badness = score;}return result;
}

协议每次新建一个socket的时候就会在底层生成一个sock结构体,然后插入大到哈希链表中,收到数据时候根据ip和端口从哈希链表中找到对应的sock结构体。

这篇关于linux1.2.13源码中,管理sock结构体的数据结构及操作函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/853488

相关文章

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

Java Multimap实现类与操作的具体示例

《JavaMultimap实现类与操作的具体示例》Multimap出现在Google的Guava库中,它为Java提供了更加灵活的集合操作,:本文主要介绍JavaMultimap实现类与操作的... 目录一、Multimap 概述Multimap 主要特点:二、Multimap 实现类1. ListMult

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与

java中BigDecimal里面的subtract函数介绍及实现方法

《java中BigDecimal里面的subtract函数介绍及实现方法》在Java中实现减法操作需要根据数据类型选择不同方法,主要分为数值型减法和字符串减法两种场景,本文给大家介绍java中BigD... 目录Java中BigDecimal里面的subtract函数的意思?一、数值型减法(高精度计算)1.

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示