<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>JH</title>
    <description>My personal blog</description>
    <link>https://johnhx.github.io/</link>
    <atom:link href="https://johnhx.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Wed, 20 Sep 2017 15:35:26 +0000</pubDate>
    <lastBuildDate>Wed, 20 Sep 2017 15:35:26 +0000</lastBuildDate>
    <generator>Jekyll v3.5.2</generator>
    
      <item>
        <title>Https的SSL/TLS协议分析</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#背景&quot; id=&quot;markdown-toc-背景&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#第一阶段-tcp的三路握手&quot; id=&quot;markdown-toc-第一阶段-tcp的三路握手&quot;&gt;第一阶段 TCP的三路握手&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#第二阶段-tlsv12协议握手&quot; id=&quot;markdown-toc-第二阶段-tlsv12协议握手&quot;&gt;第二阶段 TLSv1.2协议握手&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#总结&quot; id=&quot;markdown-toc-总结&quot;&gt;总结&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

本文基于访问 https://mail.google.com 的WireShark抓包结果分析Https和SSL/TLS协议握手过程, 操作过程为:

&lt;ol&gt;
  &lt;li&gt;
    打开chrome浏览器并访问 https://mail.google.com 首页
  &lt;/li&gt;
  &lt;li&gt;
    关闭chrome
  &lt;/li&gt;
&lt;/ol&gt;

这个过程客户端chrome和mail.google.com之间新建了3个TCP连接.

为便于描述, 通过Wireshark筛选了其中一个连接的数据做分析.

总体上讲，经历了一下几个大的阶段:

&lt;ol&gt;
  &lt;li&gt;
    TCP的三路握手
  &lt;/li&gt;
  &lt;li&gt;
    TLSv1.2协议握手
  &lt;/li&gt;
  &lt;li&gt;
    应用数据传输
  &lt;/li&gt;
  &lt;li&gt;
    断连
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;第一阶段-tcp的三路握手&quot;&gt;第一阶段 TCP的三路握手&lt;/h2&gt;

没什么好说的，见图:

&lt;img src=&quot;http://johnhx.github.io/img/https/tcp_handshake.png&quot; alt=&quot;Image of TCP Handshake&quot; /&gt;

&lt;h2 id=&quot;第二阶段-tlsv12协议握手&quot;&gt;第二阶段 TLSv1.2协议握手&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    第一步 客户端往服务端443端口发一个Client Hello报文

    应用层协议为Secure Socket Layer, 如下图所示:

    &lt;img src=&quot;http://johnhx.github.io/img/https/client_hello.png&quot; alt=&quot;Image of Client Hello&quot; /&gt;

    Secure Socket Layer的具体内容如下图所示, 包括：

    &lt;ul&gt;
      &lt;li&gt;
        Content Type为Handshake
      &lt;/li&gt;
      &lt;li&gt;
        Version为TLS v1.0
      &lt;/li&gt;
      &lt;li&gt;
        Length表示Secure Socket Layer的数据长度
      &lt;/li&gt;
      &lt;li&gt;
        Handshake Protocol的详细内容:

        &lt;ul&gt;
          &lt;li&gt;
            Handshake Type为Client Hello, 值为1
          &lt;/li&gt;
          &lt;li&gt;
            Length表示Handshare Protocol的数据长度
          &lt;/li&gt;
          &lt;li&gt;
            Version为TLS 1.2
          &lt;/li&gt;
          &lt;li&gt;
            随机数: 这是客户端生成的随机数, 稍后用于生成”对话密钥”
          &lt;/li&gt;
          &lt;li&gt;
            Session ID Length为0(还没有Session ID生成, 故Length为0)
          &lt;/li&gt;
          &lt;li&gt;
            Cipher Suites: 客户端支持的加密方法
          &lt;/li&gt;
          &lt;li&gt;
            Compression Methods: 客户端支持的压缩方法
          &lt;/li&gt;
          &lt;li&gt;
            Extension部分(略)
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;

    &lt;img src=&quot;http://johnhx.github.io/img/https/client_hello_ssl.png&quot; alt=&quot;Image of Client Hello SSL&quot; /&gt;
  &lt;/li&gt;
  &lt;li&gt;
    第二步 服务端首次回应Server Hello报文

    应用层协议同样为Secure Sockets Layer, 具体内容如下图所示:

    &lt;ul&gt;
      &lt;li&gt;
        Content Type为Handshake
      &lt;/li&gt;
      &lt;li&gt;
        Version为TLS v1.2
      &lt;/li&gt;
      &lt;li&gt;
        Length表示Secure Socket Layer的数据长度
      &lt;/li&gt;
      &lt;li&gt;
        Handshake Protocol的详细内容:

        &lt;ul&gt;
          &lt;li&gt;
            Handshake Type为Server Hello, 值为2
          &lt;/li&gt;
          &lt;li&gt;
            Length表示Handshare Protocol的数据长度
          &lt;/li&gt;
          &lt;li&gt;
            Version为TLS 1.2
          &lt;/li&gt;
          &lt;li&gt;
            随机数: 这是服务端生成的随机数 ( 这是Https协议握手过程的第二个随机数 )
          &lt;/li&gt;
          &lt;li&gt;
            Session ID Length为0(还没有Session ID生成, 故Length为0)
          &lt;/li&gt;
          &lt;li&gt;
            Cipher Suite: 服务端决定的加密算法
          &lt;/li&gt;
          &lt;li&gt;
            Compression Methods: 服务端决定的压缩方法, 此处为null
          &lt;/li&gt;
          &lt;li&gt;
            Extension部分
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;

    需要注意的是, 在协议的Extension部分, 服务端将后面步骤用到的证书的时间戳放在signed_certificate_timestamp字段发给了客户端.

    &lt;img src=&quot;http://johnhx.github.io/img/https/server_hello.png&quot; alt=&quot;Image of Server Hello&quot; /&gt;
  &lt;/li&gt;
  &lt;li&gt;
    第三步 服务端将证书发给客户端

    这个报文比较长, 是3个TCP分节的combine.

    逻辑上包含三个TLSv1.2 Record Layer的报文:

    &lt;ul&gt;
      &lt;li&gt;
        Handshake Protocol为”Certificate(11)”: 包含证书
      &lt;/li&gt;
      &lt;li&gt;
        Handshake Protocol为”Server Key Exchange(12)”: 包含Diffie-Hellman算法的服务端证书公钥, 证书本身的数字签名
      &lt;/li&gt;
      &lt;li&gt;
        Handshake Protocol位”Server Hello Done(14)”: 表示服务端的Server Hello完成
      &lt;/li&gt;
    &lt;/ul&gt;

    &lt;img src=&quot;http://johnhx.github.io/img/https/certificates.png&quot; alt=&quot;Image of Certificates&quot; /&gt;
  &lt;/li&gt;
  &lt;li&gt;
    第四步 客户端回应服务端

    逻辑上包含如下几个TLSv1.2 Record Layer的报文:

    &lt;ul&gt;
      &lt;li&gt;
        Handshake Protocol为”Client Key Exchange(16)”: 包括Diffie-Hellman算法的客户端公钥
      &lt;/li&gt;
      &lt;li&gt;
        Change Cipher Spec报文: 告知服务端, 客户端已经切换到之前的Cipher Suite来加密数据并传输
      &lt;/li&gt;
      &lt;li&gt;
        第三个随机数的加密数据
      &lt;/li&gt;
    &lt;/ul&gt;

    此外, 客户端使用前面的两个随机数, 已经刚刚生成的第三个随机数, 使用之前与服务器确定的加密算法, 在客户端生成一个Session Secret.

    &lt;img src=&quot;http://johnhx.github.io/img/https/client_key_exchange.png&quot; alt=&quot;Image of Client Key Exchange&quot; /&gt;
  &lt;/li&gt;
  &lt;li&gt;
    第五步 服务端再次响应客户端

    逻辑上包含如下几个TLSv1.2 Record Layer报文:

    &lt;ul&gt;
      &lt;li&gt;
        Handshake Protocol为”New Session Ticket(4)”的报文: 包含Session Ticket
      &lt;/li&gt;
      &lt;li&gt;
        Change Cipher Spec报文: 告知客户端, 服务端已经切换到之前的Cipher Suite来加密数据并传输
      &lt;/li&gt;
      &lt;li&gt;
        加密的Finish消息 ( 用于验证之前通过握手建立起来的加解密通道是否成功 )
      &lt;/li&gt;
    &lt;/ul&gt;

    &lt;img src=&quot;http://johnhx.github.io/img/https/new_session_ticket.png&quot; alt=&quot;Image of new session ticket&quot; /&gt;
  &lt;/li&gt;
&lt;/ul&gt;

至此, TLSv1.2的握手过程完成。

往后, 服务端和客户端就可以通过TLSv1.2传输加密数据了, 加密数据的TLSv1.2 Record Layer的Content Type为Application Data(23).

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

SSL/TLS在握手阶段使用的是非对称加密, 在传输阶段使用的是对称加密.

传输阶段的对称加密的密钥是基于非对称算法在不安全的网络上让会话双方在握手阶段生成的。

兼顾了安全和性能。
</description>
        <pubDate>Wed, 16 Aug 2017 00:00:00 +0000</pubDate>
        <link>https://johnhx.github.io/2017/08/16/https-protocol/</link>
        <guid isPermaLink="true">https://johnhx.github.io/2017/08/16/https-protocol/</guid>
        
        <category>Networking</category>
        
        <category>Https</category>
        
        
        <category>Https</category>
        
      </item>
    
      <item>
        <title>记录一个的Spring Boot Maven Plugin的issue</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#背景&quot; id=&quot;markdown-toc-背景&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#问题介绍&quot; id=&quot;markdown-toc-问题介绍&quot;&gt;问题介绍&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#问题的分析和解决&quot; id=&quot;markdown-toc-问题的分析和解决&quot;&gt;问题的分析和解决&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

最近几天在把一个Restful后台项目从Spring MVC移植到Spring Boot.

移植的目的有两个:

一是趁机会学习一下Spring Boot;

二是Spring Boot Secutity提供了很好的Restful API的鉴权接口.

移植的过程遇到不少问题, 尤以本文打算记叙的这个问题最为奇葩.

&lt;h2 id=&quot;问题介绍&quot;&gt;问题介绍&lt;/h2&gt;

这个Restful后台项目所用的数据库是MS SQL Server, SQL Server的JDBC Driver, 是在微软官网上提供的jar包下载, 似乎并没有任何Maven Repository对此jar包做官方提供.

在这样的背景下, 我能想到的最佳引用方式就是在pom.xml引用本地lib目录的jar包, 如下代码:

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;com.microsoft.sqlserver&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;sqljdbc42&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;4.2&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;scrope&amp;gt;&lt;/span&gt;system&lt;span class=&quot;nt&quot;&gt;&amp;lt;/scrope&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;systemPath&amp;gt;&lt;/span&gt;${project.basedir}/lib/sqljdbc42-4.2jar&lt;span class=&quot;nt&quot;&gt;&amp;lt;/systemPath&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

然后再在spring-boot-maven-plugin的配置中将includeSystemScope设置为true:

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.springframework.boot&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;spring-boot-maven-plugin&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
        ...
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;includeSystemScope&amp;gt;&lt;/span&gt;true&lt;span class=&quot;nt&quot;&gt;&amp;lt;/includeSystemScope&amp;gt;&lt;/span&gt;
        ...
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

这样在编译jar包的时候, sqljdbc42-4.2.jar被打在了BOOT-INF\lib里面.

用mvn spring-boot:run可以正确加载并且运行.

但是该项目是一个Web应用, 最终是要发布在tomcat里面的, 所以在发布之前是要被打成war包的.

问题来了, 当在pom.xml里加上如下选项, 将其打为war包时, sqljdbc42-4.2.jar被打进了WEB-INF\lib-provided里面, 而不是期望的WEB-INF\lib.

&lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;packaging&amp;gt;&lt;/span&gt;war&lt;span class=&quot;nt&quot;&gt;&amp;lt;/packaging&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

maven的scope分为如下几种:

&lt;ul&gt;
  &lt;li&gt;
    compile

    默认Scope. 设置为compile scope的依赖, 在编译, 测试, 运行阶段, 这个artifact对应的包都会在classpath中, 意味着, 设置为compile scope的依赖, 会被打入发布的jar, war包之中.
  &lt;/li&gt;
  &lt;li&gt;
    provided

    provided指的是目标容器已经provide了这个依赖包/artifact. 所以设置为provided scope的依赖, 只在编译, 测试阶段会出现在classpath中.

    而在运行阶段, 由于我们假定目标容器会provide这个依赖包/artifact, 所以这个依赖包不会被打包进入发布的jar, war之中.
  &lt;/li&gt;
  &lt;li&gt;
    runtime

    运行时scope. 只是在测试, 运行阶段会出现在classpath中.
  &lt;/li&gt;
  &lt;li&gt;
    test

    测试scope, 只有在测试编译和测试运行阶段可用.
  &lt;/li&gt;
  &lt;li&gt;
    system

    system与provided类似, 但是必须显式的提供一个本地文件系统中jar文件的路径. 将一个依赖设置为system的同时, 必须提供一个systemPath, 指明本地文件系统中jar文件的路径.
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;问题的分析和解决&quot;&gt;问题的分析和解决&lt;/h2&gt;

从maven的scope的说明文档上, system这个scope是不被推荐使用的.

而且”system和provided类似”, 似乎本问题(sqljdbc42-4.2.jar被打进war包的WEB-INF\lib-provided而不是WEB-INF\lib)的存在也是合理的.

所以解决这个问题无外乎几个办法:

&lt;ul&gt;
  &lt;li&gt;
    引用公共或自己定制的Maven Repository中的sqljdbc42-4.2.jar
  &lt;/li&gt;
  &lt;li&gt;
    看spring-boot-maven-plugin是否有提供选项将system scope的jar打包路径从WEB-INF\lib-provided变为WEB-INF\lib
  &lt;/li&gt;
&lt;/ul&gt;

自己定制Maven Repository这个办法太麻烦了.

考虑到发布的便利, 我决定优先采用第二种办法.

下了一份spring-boot的源码(包含spring-boot-maven-plugin的源码), 读了war包打包部分的逻辑, 发现并没有办法动态配置指定scope或特定jar包的打包路径.

maven本身有提供&lt;resources&gt;这样的标签, 但是这个是用于定制target/目标目录结构的.&lt;/resources&gt;

本文问题是出在war包的打包结构上, 为打war包负责的模块应该是spring-boot-maven-plugin.

居然没有提供这样的动态配置接口…

</description>
        <pubDate>Sun, 06 Aug 2017 00:00:00 +0000</pubDate>
        <link>https://johnhx.github.io/2017/08/06/spring-boot-maven-plugin-issue/</link>
        <guid isPermaLink="true">https://johnhx.github.io/2017/08/06/spring-boot-maven-plugin-issue/</guid>
        
        <category>Java</category>
        
        <category>Spring</category>
        
        <category>Maven</category>
        
        
        <category>Java</category>
        
      </item>
    
      <item>
        <title>apache tomcat 8源码学习(1) - start流程</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#背景&quot; id=&quot;markdown-toc-背景&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tomcat的start&quot; id=&quot;markdown-toc-tomcat的start&quot;&gt;tomcat的start&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#orgapachecatalinastartupbootstrap&quot; id=&quot;markdown-toc-orgapachecatalinastartupbootstrap&quot;&gt;org.apache.catalina.startup.Bootstrap&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#静态区域&quot; id=&quot;markdown-toc-静态区域&quot;&gt;静态区域&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#start方法&quot; id=&quot;markdown-toc-start方法&quot;&gt;start方法&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#init获取catalinadaemon&quot; id=&quot;markdown-toc-init获取catalinadaemon&quot;&gt;&lt;b&gt;init()获取catalinaDaemon&lt;/b&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#通过反射调用catalinadaemon的start方法&quot; id=&quot;markdown-toc-通过反射调用catalinadaemon的start方法&quot;&gt;&lt;b&gt;通过反射调用catalinaDaemon的start方法:&lt;/b&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

在改进一个基于一个Servlet Web框架(Draco)的时候, 对Servlet标准产生了些许兴趣(现在已经出到Servlet 4, 支持HTTP/2.0), 接下来便想看一下Servlet的实现。

便翻出了apache tomcat 8的源码, 这一看, 就有点停不下来, 于是从tomcat instance的start流程入手, 对tomcat从上层到底层的源码和机制都梳理一下.

&lt;h2 id=&quot;tomcat的start&quot;&gt;tomcat的start&lt;/h2&gt;

tomcat下载后, 我们启动都是通过bin\startup.sh来启动.

startup.sh是一个shell脚本, 调用到的是bin\catalina.sh start, 后跟其他参数.

catalina.sh也是一个shell脚本, 这个脚本稍微复杂了一些, 不过主要都是在处理start, stop, run, debug以及其他参数, 对于start流程, 其核心是调用到下面的Java命令:

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$JRE_HOME&lt;/span&gt;/bin/java ... org.apache.catalina.startup.Bootstrap ... start
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

可见, 对于start的过程, 其入口类是org.apache.catalina.startup.Bootstrap类

&lt;h2 id=&quot;orgapachecatalinastartupbootstrap&quot;&gt;org.apache.catalina.startup.Bootstrap&lt;/h2&gt;

&lt;h3 id=&quot;静态区域&quot;&gt;静态区域&lt;/h3&gt;

主要做了以下几件事情:

&lt;ul&gt;
  &lt;li&gt;
    获取catalinaHomeFile ( 可通过Globals.CATALINA_HOME_PROP获取，默认为bootstrap.jar )
  &lt;/li&gt;
  &lt;li&gt;
    获取catalinaBaseFile ( 可通过Globals.CATALINA_BASE_PROP获取 )
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;start方法&quot;&gt;start方法&lt;/h3&gt;

&lt;h4 id=&quot;init获取catalinadaemon&quot;&gt;&lt;b&gt;init()获取catalinaDaemon&lt;/b&gt;&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;
    initClassLoaders()

    &lt;ul&gt;
      &lt;li&gt;
        创建commonLoader
      &lt;/li&gt;
      &lt;li&gt;
        创建catalinaLoader
      &lt;/li&gt;
      &lt;li&gt;
        创建sharedLoader
      &lt;/li&gt;
    &lt;/ul&gt;

    注意: commonLoader是catalinaLoader和sharedLoader的父Loader.

    &lt;blockquote&gt;
      以上创建loader都是通过调用createClassLoader方法, createClassLoader方法流程如下:
      &lt;ol&gt;
        &lt;li&gt;获取name + “.loader”配置属性, 例如对于commonLoader, 就是common.loader&lt;/li&gt;
        &lt;li&gt;根据*.loader生成repository列表, 稍后找ClassLoader需要用到&lt;/li&gt;
        &lt;li&gt;调用ClassLoaderFactory.createClassLoader方法&lt;/li&gt;
        &lt;li&gt;生成java.net.URLClassLoader对象&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/blockquote&gt;
  &lt;/li&gt;
  &lt;li&gt;
    将catalinaLoader设为currentThread的contextClassLoader
  &lt;/li&gt;
  &lt;li&gt;
    加载org.apache.catalina.startup.Catalina类, 并newInstance
  &lt;/li&gt;
  &lt;li&gt;
    调用org.apache.catalina.startup.Catalina类的setParentClassLoader方法, 将其parent ClassLoader设置为sharedLoader
  &lt;/li&gt;
  &lt;li&gt;
    catalinaDaemon赋值为org.apache.catalina.startup.Catalina的instance
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;通过反射调用catalinadaemon的start方法&quot;&gt;&lt;b&gt;通过反射调用catalinaDaemon的start方法:&lt;/b&gt;&lt;/h4&gt;

org.apache.catalina.startup.Catalina类startup方法流程:

&lt;ul&gt;
  &lt;li&gt;
    &lt;b&gt;load()&lt;/b&gt;

    &lt;ul&gt;
      &lt;li&gt;
        initDirs(): 检查系统配置java.io.tmpdir
      &lt;/li&gt;
      &lt;li&gt;
        createStarterDigest

        创建org.apache.tomcat.util.digest实例

        注意: Digester是个维护XML和Java对象的库( 见 &lt;a href=&quot;https://commons.apache.org/proper/commons-digester/guide/core.html&quot;&gt;Apache Digester&lt;/a&gt; ), 动态添加了很多规则, 如:

        &lt;ul&gt;
          &lt;li&gt;
            当XML Parser扫描到&amp;lt;Server&amp;gt;时, 创建org.apache.catalina.core.StandardServer实例, 并保持其生命周期到&amp;lt;/Server&amp;gt;
          &lt;/li&gt;
          &lt;li&gt;
            当XML Parser扫描到&amp;lt;Server&amp;gt;&amp;lt;GlobalNamingResources&amp;gt;时, 创建org.apache.catalina.deploy.NamingResourceImpl实例

            …

            ( 详见Catalina.java\createStarterDigester方法 )

            个人感受: Digester是一个另类的依赖注入模型
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;
        取conf/server.xml, 并由digest进行解析

        &lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;digest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;// 将Catalina.java类实例作为Digest的object stack的root&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;digest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputSource&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// 解析&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
        &lt;/div&gt;

        这样Catalina.java的getServer()返回的便是Digestor的规则所创建的Server实例( org.apache.catalina.core.StandardServer )
      &lt;/li&gt;
      &lt;li&gt;
        调用StandardServer的init

        附: StandardServer的继承关系如下图:

        &lt;img src=&quot;https://raw.githubusercontent.com/johnhx/johnhx.github.io/master/img/tomcat_1_standardserver.png&quot; alt=&quot;Image of StandardServer&quot; /&gt;

        附: Lifecycle的状态机如下:

        &lt;img src=&quot;https://raw.githubusercontent.com/johnhx/johnhx.github.io/master/img/tomcat_1_lifecyclestate.png&quot; alt=&quot;Image of Lifecycle State Machine&quot; /&gt;

        &lt;b&gt; 调用StandardServer的init, 但是因为StandardServer没有init方法, 实际调用到的是LifecycleBase的init: &lt;/b&gt;

        &lt;u&gt;LifecycleBase的init()流程:&lt;/u&gt;

        &lt;ul&gt;
          &lt;li&gt;
            Lifecycle状态迁移为INITIALIZING
          &lt;/li&gt;
          &lt;li&gt;
            调用initInternal(), 调用到的是StandardServer的initInternal:

            &lt;ul&gt;
              &lt;li&gt;
                新建StringCache对象, 注册Catalina:type=StringCache到MBean Server;
              &lt;/li&gt;
              &lt;li&gt;
                新建MBeanFactor对象, 将container设置为当前的StandardServer实例, 注册Catalina:type=MBeanFactory到MBean Server;
              &lt;/li&gt;
              &lt;li&gt;
                调用NamingResourceImpl的initInternal:

                &lt;ul&gt;
                  &lt;li&gt;为resources, env, resourceLinks调用createMBean&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;
                把common和shared的classes添加到ExtensionValidator
              &lt;/li&gt;
              &lt;li&gt;
                调用services的init() ( services是由conf\server.xml的&lt;Service&gt;节点添加 )&lt;/Service&gt;

                &lt;u&gt;StandardService的init()流程:&lt;/u&gt;

                &lt;ul&gt;
                  &lt;li&gt;
                    调用Executor的init

                    注意: Executor是在被Digestor在解析conf\server.xml时, 解析到&lt;Server&gt;&lt;Service&gt;&lt;Executor&gt;时添加, &lt;Executor&gt;用于定义线程池.&lt;/Executor&gt;&lt;/Executor&gt;&lt;/Service&gt;&lt;/Server&gt;
                  &lt;/li&gt;
                  &lt;li&gt;
                    初始化MapperListener实例 ( 用于listen virtualhosts的配置改变 )
                  &lt;/li&gt;
                  &lt;li&gt;
                    调用Connector的init

                    注意: Connector是在被Digestor在解析conf\server.xml时, 解析到&amp;lt;Server&amp;gt;&amp;lt;Service&amp;gt;&amp;lt;Connector&amp;gt;时添加.

                    Digester在XML Parser到如下节点时:

                    &lt;div class=&quot;language-xml highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;...
&lt;span class=&quot;nt&quot;&gt;&amp;lt;Connector&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;port=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;8080&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;protocol=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;HTTP/1.1&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;connectionTimeout=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;20000&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;redirectPort=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;8443&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
...
&lt;/code&gt;&lt;/pre&gt;
                    &lt;/div&gt;

                    调用ConnectorCreateRule的begin()方法, 创建Connector对象, 放入digester的object stack栈顶.

                    &lt;u&gt;创建Connector对象流程:&lt;/u&gt;

                    &lt;ul&gt;
                      &lt;li&gt;
                        获取传入的protocol ( “HTTP/1.1”, “AJP/1.3” )
                      &lt;/li&gt;
                      &lt;li&gt;
                        加载protocolHandlerClassName类 ( org.apache.coyote.http11.Http11NioProtocol )
                      &lt;/li&gt;
                      &lt;li&gt;
                        newInstance创建Http11NioProtocol类实例, 赋值给this.protocolHandler
                      &lt;/li&gt;
                    &lt;/ul&gt;

                    &lt;u&gt;/创建Connector对象流程&lt;/u&gt;

                    &lt;u&gt;Connector的init流程:&lt;/u&gt;

                    &lt;ul&gt;
                      &lt;li&gt;
                        创建CoyoteAdapter类实例, 赋值给protocolHandler的Adapter；
                      &lt;/li&gt;
                      &lt;li&gt;
                        初始化protocolHandler ( protocolHandler.init, 即调到Http11NioProtocol的init )

                        &lt;u&gt;Http11NioProtocol的init流程:&lt;/u&gt;

                        Http11NioProtocol的类结构如下:

                        &lt;img src=&quot;https://raw.githubusercontent.com/johnhx/johnhx.github.io/master/img/tomcat_1_nioprotocol.png&quot; alt=&quot;Image of NioProtocol&quot; /&gt;

                        &lt;strong&gt;Http11NioProtocol类自身没有init方法, 故init方法调到的是父类AbstractHttp11JsseProtocol的init:&lt;/strong&gt;

                        &lt;u&gt;AbstractHttp11JsseProtocol的init()流程:&lt;/u&gt;

                        &lt;ul&gt;
                          &lt;li&gt;
                            获取SSLImplementation类实例
                          &lt;/li&gt;
                          &lt;li&gt;
                            AbstractProtocol的init方法:

                            &lt;u&gt;AbstractProtocol的init()流程:&lt;/u&gt;

                            &lt;ul&gt;
                              &lt;li&gt;
                                生成Catalina:type=ProtocolHandler,port=8080,address=xxx的ObjectName实例, 注册到MBean Server
                              &lt;/li&gt;
                              &lt;li&gt;
                                生成Catalina:type=ThreadPool,name=xxx的ObjectName实例, 注册到MBean Server
                              &lt;/li&gt;
                              &lt;li&gt;
                                生成Catalina:type=GlobalRequestProcessor,name=xxx的ObjectName实例, 注册到MBean Server
                              &lt;/li&gt;
                              &lt;li&gt;
                                调用endpoint的init()方法, 这里endpoint指向NioEndpoint实例:

                                &lt;u&gt;NioEndpoin的init()流程&lt;/u&gt;

                                &lt;ul&gt;
                                  &lt;li&gt;
                                    调用bind()方法 ( 调到NioEndpoint的bind方法 ):

                                    &lt;ul&gt;
                                      &lt;li&gt;
                                        java.nio的调用:

                                        ServerSocketChannel.open() -&amp;gt; bind, configureBlocking, setSoTimeout
                                      &lt;/li&gt;
                                      &lt;li&gt;
                                        org.apache.tomcat.util.net.NioSelectorPool的open调用

                                        &lt;ul&gt;
                                          &lt;li&gt;
                                            java.nio.channels.Selector的open调用
                                          &lt;/li&gt;
                                          &lt;li&gt;
                                            新的org.apache.tomcat.util.net.NioBlockingSelector实例
                                          &lt;/li&gt;
                                          &lt;li&gt;
                                            NioBlockingSelector的open调用:

                                            &lt;ul&gt;
                                              &lt;li&gt;
                                                创建新的BlockPoller线程实例 ( NioBlockingSelector.BlockPoller-线程计数 )
                                              &lt;/li&gt;
                                              &lt;li&gt;
                                                BlockPoller的start调用 ( run方法 ):

                                                &lt;ul&gt;
                                                  &lt;li&gt;
                                                    while循环
                                                  &lt;/li&gt;
                                                  &lt;li&gt;
                                                    先处理events( 读, 写事件 )
                                                  &lt;/li&gt;
                                                  &lt;li&gt;
                                                    select
                                                  &lt;/li&gt;
                                                  &lt;li&gt;
                                                    dispatch select出来的事件
                                                  &lt;/li&gt;
                                                &lt;/ul&gt;
                                              &lt;/li&gt;
                                            &lt;/ul&gt;
                                          &lt;/li&gt;
                                        &lt;/ul&gt;
                                      &lt;/li&gt;
                                    &lt;/ul&gt;
                                  &lt;/li&gt;
                                  &lt;li&gt;
                                    bindState置为BOUND_ON_INIT;
                                  &lt;/li&gt;
                                &lt;/ul&gt;

                                &lt;u&gt;/NioEndpoin的init()流程&lt;/u&gt;
                              &lt;/li&gt;
                            &lt;/ul&gt;

                            &lt;u&gt;/AbstractProtocol的init()流程&lt;/u&gt;
                          &lt;/li&gt;
                        &lt;/ul&gt;

                        &lt;u&gt;/AbstractHttp11JsseProtocol的init()流程&lt;/u&gt;

                        &lt;u&gt;/Http11NioProtocol的init流程&lt;/u&gt;
                      &lt;/li&gt;
                    &lt;/ul&gt;

                    &lt;u&gt;/Connector的init流程&lt;/u&gt;
                  &lt;/li&gt;
                &lt;/ul&gt;

                &lt;u&gt;/StandardService的init()流程&lt;/u&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;
            Lifecycle状态迁移为INITIALIZED
          &lt;/li&gt;
        &lt;/ul&gt;

        &lt;u&gt;/LifecycleBase的init()流程&lt;/u&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;b&gt;Server的start()方法&lt;/b&gt;

    &lt;strong&gt;调用StandardServer的start, 但是由于StandardServer没有start方法, 实际调用到的是LifecycleBase的start:&lt;/strong&gt;

    &lt;u&gt;LifecycleBase的start()流程:&lt;/u&gt;

    &lt;ul&gt;
      &lt;li&gt;
        Lifecycle状态迁移为STARTING_PREP
      &lt;/li&gt;
      &lt;li&gt;
        调用startInternal ( 这里调到StandardServer的startInternal方法 )

        &lt;u&gt;StandardServer的startInteral()流程&lt;/u&gt;

        &lt;ul&gt;
          &lt;li&gt;
            触发Lifecycle的事件CONFIGURE_START_EVENT
          &lt;/li&gt;
          &lt;li&gt;
            Lifecycle状态设为STARTING
          &lt;/li&gt;
          &lt;li&gt;
            调用service的start

            &lt;strong&gt;调用StandardService的start，但是由于StandardService没有start方法, 实际调到LifecycleBase的start, 再调到StandardService的startInternal&lt;/strong&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Mon, 31 Jul 2017 00:00:00 +0000</pubDate>
        <link>https://johnhx.github.io/2017/07/31/tomcat-source-code-start/</link>
        <guid isPermaLink="true">https://johnhx.github.io/2017/07/31/tomcat-source-code-start/</guid>
        
        <category>Java</category>
        
        <category>Tomcat</category>
        
        <category>源码</category>
        
        
        <category>Java</category>
        
      </item>
    
      <item>
        <title>一个Java Memory Leak的跟踪处理过程</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#背景&quot; id=&quot;markdown-toc-背景&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#问题描述&quot; id=&quot;markdown-toc-问题描述&quot;&gt;问题描述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#跟踪和分析过程&quot; id=&quot;markdown-toc-跟踪和分析过程&quot;&gt;跟踪和分析过程&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

项目中有一个工作流模型，如下图所示:

&lt;img src=&quot;https://raw.githubusercontent.com/johnhx/johnhx.github.io/master/img/java_oom_work_flow_2.png&quot; alt=&quot;Image of job work flow&quot; /&gt;

其中:

&lt;ul&gt;
  &lt;li&gt;
    绿色椭圆框体为业务状态;
  &lt;/li&gt;
  &lt;li&gt;
    黑色长方框体为业务的作业状态;
  &lt;/li&gt;
  &lt;li&gt;
    每个业务状态都会把作业状态从NEW -&amp;gt; STARTED -&amp;gt; IN_QUEUE -&amp;gt; IN_PROGREE -&amp;gt; COMPLETED转一遍.
  &lt;/li&gt;
  &lt;li&gt;
    每一个业务状态都会有一个对应的CHECK状态. 在CHECK状态时, 如果SUCCESS检查没过, 则直接退回STARTED状态, 重走一遍CHECK(类似于轮询).
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;问题描述&quot;&gt;问题描述&lt;/h2&gt;

当某个业务状态所对应的CHECK状态, 轮询次数过多时, 会发现运行jetty的java进程占用的虚拟内存容量过大, 造成页面加载极其缓慢.

ec2的2GB内存所剩无几:

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;ec2-user@ip-172-31-25-152 ~]&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;free -m
             total       used       free     shared    buffers     cached
Mem:          2003       1706        296         20        107        405
-/+ buffers/cache:       1193        809
Swap:            0          0          0
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

top命令发现某java进程的虚拟内存达到了6GB之多, 驻留内存站到了700+MB, 对于2GB的ec2来说比例相当高了.

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;top - 09:19:19 up 82 days, 15:01,  1 user,  load average: 0.58, 0.56, 0.35
Tasks:  89 total,   1 running,  88 sleeping,   0 stopped,   0 zombie
Cpu&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: 15.7%us,  0.7%sy,  0.0%ni, 83.3%id,  0.0%wa,  0.0%hi,  0.0%si,  0.3%st
Mem:   2051472k total,  1748936k used,   302536k free,   110560k buffers
Swap:        0k total,        0k used,        0k free,   415216k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 8478 ec2-user  20   0 5791m 776m  17m S 12.6 38.7 163:28.09 java
25888 postgres  20   0  329m  16m  13m S  1.3  0.8   0:10.20 postmaster
25911 postgres  20   0  329m  15m  13m S  1.3  0.8   0:08.96 postmaster
25996 postgres  20   0  330m  17m  13m S  1.0  0.9   0:02.05 postmaster
25983 postgres  20   0  330m  17m  13m S  0.7  0.9   0:02.68 postmaster
 8537 ec2-user  20   0 2493m 154m  17m S  0.3  7.7   4:14.04 java
    1 root      20   0 19636 2408 2084 S  0.0  0.1   0:05.12 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
    3 root      20   0     0    0    0 S  0.0  0.0   0:51.00 ksoftirqd/0
    4 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kworker/0:0
    5 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kworker/0:0H
    7 root      20   0     0    0    0 S  0.0  0.0  11:31.83 rcu_sched
    8 root      20   0     0    0    0 S  0.0  0.0   0:00.00 rcu_bh
    9 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0
   10 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kdevtmpfs
   11 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 netns
   12 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 perf
   15 root      20   0     0    0    0 S  0.0  0.0   0:00.02 xenwatch
   20 root      20   0     0    0    0 S  0.0  0.0   0:00.00 xenbus
   21 root      20   0     0    0    0 S  0.0  0.0   2:49.92 kworker/0:1
  129 root      20   0     0    0    0 S  0.0  0.0   0:02.30 khungtaskd
  130 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 writeback
  132 root      25   5     0    0    0 S  0.0  0.0   0:00.00 ksmd
  133 root      39  19     0    0    0 S  0.0  0.0   0:00.00 khugepaged
  134 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 crypto
  135 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kintegrityd
  136 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 bioset
  138 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 kblockd
  488 root       0 -20     0    0    0 S  0.0  0.0   0:00.00 md
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

查看对应进程, 发现正是运行jetty容器提供HTTP服务的进程:

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;ec2-user@ip-172-31-25-152 ~]&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ps -ef| grep 8478
ec2-user  8478     1 11 May10 ?        02:43:37 java -jar /home/ec2-user/jetty-latest/start.jar
ec2-user 26202 26167  0 09:19 pts/0    00:00:00 grep --color&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;auto 8478
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;跟踪和分析过程&quot;&gt;跟踪和分析过程&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    用jmap把问题进程的heap抓下来:

    &lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;jmap -heap:format&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;b 8478
&lt;/code&gt;&lt;/pre&gt;
    &lt;/div&gt;

    jmap是JDK附带的工具, 可以通过指定PID把对应java进程的heap和memory dump出来.

    上面的jmap命令运行结束后, 会在当前目录生成heap.bin文件.
  &lt;/li&gt;
  &lt;li&gt;
    将heap.bin下载到安装有Eclipse Memory Analysis Tool的机器上, 直接打开heap.bin
  &lt;/li&gt;
  &lt;li&gt;
    Eclipse Memory Analysis Tool提供了强大的图形化分析功能.
  &lt;/li&gt;
  &lt;li&gt;
    分析后发现大量的ChannelSftp对象存放在一个vector里, 被强引用, 占据了堆空间, 没有及时释放, 也无法被GC回收.
  &lt;/li&gt;
&lt;/ul&gt;

跟踪代码, 发现在业务状态所对应的CHECK状态时的动作是创建一个ssh Session连到ec2上, 检查对应业务状态的log文件是否包含有”SUCCESS”字串.

创建ssh Session的动作, 是通过com.jcraft.jsch包来做的, 流程如下:

&lt;ul&gt;
  &lt;li&gt;
    创建com.jscraft.jsch.Session实例;
  &lt;/li&gt;
  &lt;li&gt;
    通过Session创建com.jscraft.jsch.ChannelSftp实例;
  &lt;/li&gt;
  &lt;li&gt;
    通过ChannelSftp获取远端ec2上文件的内容;
  &lt;/li&gt;
&lt;/ul&gt;

代码逻辑如下:

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;readTextFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SshInfo&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sshInfo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getSessionConnected&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sshInfo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ChannelSftp&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sftpChannel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ChannelSftp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;openChannel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;sftp&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sftpChannel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;OutputStream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outputStream&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OutputStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sftpChannel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outputStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AppException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(...);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

com.jscraft.jsch是开源的第三方包, 下载了一份源码.

跟踪到ChannelSftp类, 发现:

&lt;ul&gt;
  &lt;li&gt;
    在ChannelSftp的Channel基类的构造函数中, 将Channel对象自身加到一个static Vector pool中去;
  &lt;/li&gt;
  &lt;li&gt;
    在Channel基类的del方法中, 才将该Channel对象从pool中移除; ( 移除后强引用关系丢失, GC才会自动回收Channel对象 )
  &lt;/li&gt;
&lt;/ul&gt;

继续跟踪在何处调到del, 发现在Channel的disconnect方法会调到del方法.

逻辑和rootcause很清晰了, &lt;em&gt;ChannelSftp用完后要显式的调用disconnect, ChannelSftp对象才能被GC回收.&lt;/em&gt;

真是一个低级错误.
</description>
        <pubDate>Tue, 16 May 2017 00:00:00 +0000</pubDate>
        <link>https://johnhx.github.io/2017/05/16/Java-Memory-Leak/</link>
        <guid isPermaLink="true">https://johnhx.github.io/2017/05/16/Java-Memory-Leak/</guid>
        
        <category>Java</category>
        
        <category>Memory</category>
        
        <category>Leak</category>
        
        
        <category>Java</category>
        
      </item>
    
      <item>
        <title>Linux内核网络子系统源码分析(2) -- connect系统调用</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#connect系统调用&quot; id=&quot;markdown-toc-connect系统调用&quot;&gt;connect系统调用&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#inet_stream_connect流程&quot; id=&quot;markdown-toc-inet_stream_connect流程&quot;&gt;inet_stream_connect流程&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#1-取struct-socket内部的struct-sock成员指针指向的struct-sock-见文首图很清晰&quot; id=&quot;markdown-toc-1-取struct-socket内部的struct-sock成员指针指向的struct-sock-见文首图很清晰&quot;&gt;1. 取struct socket内部的struct sock成员指针指向的struct sock; (见文首图很清晰)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2-根据struct-socket的socket_state类型字段&quot; id=&quot;markdown-toc-2-根据struct-socket的socket_state类型字段&quot;&gt;2. 根据struct socket的socket_state类型字段:&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tcp_v4_connect流程&quot; id=&quot;markdown-toc-tcp_v4_connect流程&quot;&gt;tcp_v4_connect流程&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#1-从connect系统调用传入的struct-sockaddr取出destination-address-destination-port-并取得nexthop如果struct-inet_sock的struct-ip_options_rcu有设置-nexthop为struct-ip_option的faddr-然后调用ip_route_connect进行寻路-取得struct-rtable&quot; id=&quot;markdown-toc-1-从connect系统调用传入的struct-sockaddr取出destination-address-destination-port-并取得nexthop如果struct-inet_sock的struct-ip_options_rcu有设置-nexthop为struct-ip_option的faddr-然后调用ip_route_connect进行寻路-取得struct-rtable&quot;&gt;1. 从connect系统调用传入的struct sockaddr取出destination address, destination port, 并取得nexthop(如果struct inet_sock的struct ip_options_rcu有设置, nexthop为struct ip_option的faddr), 然后调用ip_route_connect进行寻路, 取得struct rtable;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2-根据ip寻路的结果-确定destination-address-destination-port&quot; id=&quot;markdown-toc-2-根据ip寻路的结果-确定destination-address-destination-port&quot;&gt;2. 根据IP寻路的结果, 确定destination address, destination port&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#3-设置默认tcp_mss---ipv4默认为536-&quot; id=&quot;markdown-toc-3-设置默认tcp_mss---ipv4默认为536-&quot;&gt;3. 设置默认TCP_MSS  ( IPv4默认为536 )&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#4-tcp状态设为tcp_syn_sent&quot; id=&quot;markdown-toc-4-tcp状态设为tcp_syn_sent&quot;&gt;4. tcp状态设为TCP_SYN_SENT&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#5-将struct-sock插入bind链表中&quot; id=&quot;markdown-toc-5-将struct-sock插入bind链表中&quot;&gt;5. 将struct sock插入bind链表中&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#6-调用ip_route_newports&quot; id=&quot;markdown-toc-6-调用ip_route_newports&quot;&gt;6. 调用ip_route_newports;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#7-为tcp生成sequence-number-赋给struct-sock的write_seq成员&quot; id=&quot;markdown-toc-7-为tcp生成sequence-number-赋给struct-sock的write_seq成员&quot;&gt;7. 为tcp生成sequence number, 赋给struct sock的write_seq成员;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#8-调用tcp_connect方法&quot; id=&quot;markdown-toc-8-调用tcp_connect方法&quot;&gt;8. 调用tcp_connect方法&lt;/a&gt;            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#81-分配struct-sk_buff&quot; id=&quot;markdown-toc-81-分配struct-sk_buff&quot;&gt;8.1 分配struct sk_buff&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#82-在struct-sk_buff中的tcp_skb_cb部分的tcp_flags字段-设置syn标志-设置seq-end_seq-gso_-什么是gso&quot; id=&quot;markdown-toc-82-在struct-sk_buff中的tcp_skb_cb部分的tcp_flags字段-设置syn标志-设置seq-end_seq-gso_-什么是gso&quot;&gt;8.2 在struct sk_buff中的tcp_skb_cb部分的tcp_flags字段, 设置SYN标志, 设置seq, end_seq, gso_* (什么是gso?)&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#83-根据sysctl_tcp_ecn-设置tcp_flags字段中的ecn-cwr标志&quot; id=&quot;markdown-toc-83-根据sysctl_tcp_ecn-设置tcp_flags字段中的ecn-cwr标志&quot;&gt;8.3 根据sysctl_tcp_ecn, 设置tcp_flags字段中的ECN, CWR标志&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#84-给struct-sk_buff打上时间戳&quot; id=&quot;markdown-toc-84-给struct-sk_buff打上时间戳&quot;&gt;8.4 给struct sk_buff打上时间戳&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#85-减struct-sk_buff引用&quot; id=&quot;markdown-toc-85-减struct-sk_buff引用&quot;&gt;8.5 减struct sk_buff引用&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#86-调用__skb_queue_tail-将struct-sk_buff挂到struct-sock的sk_write_queue&quot; id=&quot;markdown-toc-86-调用__skb_queue_tail-将struct-sk_buff挂到struct-sock的sk_write_queue&quot;&gt;8.6 调用__skb_queue_tail, 将struct sk_buff挂到struct sock的sk_write_queue&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#87-调用tcp_transmit_skb方法进入tcp层实际的处理和传输过程&quot; id=&quot;markdown-toc-87-调用tcp_transmit_skb方法进入tcp层实际的处理和传输过程&quot;&gt;8.7 调用tcp_transmit_skb方法进入&lt;strong&gt;tcp层实际的处理和传输过程&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tcp_transmit_skb流程&quot; id=&quot;markdown-toc-tcp_transmit_skb流程&quot;&gt;tcp_transmit_skb流程&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#1-如果clone_it为1调用skb_clone方法clone一个sk_buff-注意此处只是克隆数据结构packet-data只是加引用计数&quot; id=&quot;markdown-toc-1-如果clone_it为1调用skb_clone方法clone一个sk_buff-注意此处只是克隆数据结构packet-data只是加引用计数&quot;&gt;1. 如果clone_it为1，调用skb_clone方法clone一个sk_buff, 注意此处只是克隆数据结构，packet data只是加引用计数;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2-计算tcp头部空间大小tcp_header_size-sizeofstruct-tcphdr--tcp_options_size&quot; id=&quot;markdown-toc-2-计算tcp头部空间大小tcp_header_size-sizeofstruct-tcphdr--tcp_options_size&quot;&gt;2. 计算tcp头部空间大小tcp_header_size: sizeof(struct tcphdr) + tcp_options_size&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#3-使用tcp_header_size来构建tcp头部-包括设置source-port-dest-port-seq-ack_seq-window等&quot; id=&quot;markdown-toc-3-使用tcp_header_size来构建tcp头部-包括设置source-port-dest-port-seq-ack_seq-window等&quot;&gt;3. 使用tcp_header_size来构建tcp头部, 包括设置source port, dest port, seq, ack_seq, window等&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#4-调用struct-inet_connection_sock的icsk_af_ops的queue_xmit方法--该函数指针在tcp_v4_init_sock方法中初始化-指向ip_outputc的ip_queue_xmit-&quot; id=&quot;markdown-toc-4-调用struct-inet_connection_sock的icsk_af_ops的queue_xmit方法--该函数指针在tcp_v4_init_sock方法中初始化-指向ip_outputc的ip_queue_xmit-&quot;&gt;4. 调用struct inet_connection_sock的icsk_af_ops的queue_xmit方法 ( 该函数指针在tcp_v4_init_sock方法中初始化, 指向ip_output.c的ip_queue_xmit )&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ip_queue_xmit方法-ip层实际的处理和传输过程&quot; id=&quot;markdown-toc-ip_queue_xmit方法-ip层实际的处理和传输过程&quot;&gt;ip_queue_xmit方法 (IP层实际的处理和传输过程)&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#1-加ip头部&quot; id=&quot;markdown-toc-1-加ip头部&quot;&gt;1. 加ip头部&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2-ip_local_out---__ip_local_out---nf_hookipv4-local_out--此处进入netfilter子系统-若允许pass-返回1-调用dst_output&quot; id=&quot;markdown-toc-2-ip_local_out---__ip_local_out---nf_hookipv4-local_out--此处进入netfilter子系统-若允许pass-返回1-调用dst_output&quot;&gt;2. ip_local_out -&amp;gt; __ip_local_out -&amp;gt; nf_hook(IPV4, LOCAL_OUT, …), 此处进入netfilter子系统, 若允许pass, 返回1, 调用dst_output&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#3-dst_output方法&quot; id=&quot;markdown-toc-3-dst_output方法&quot;&gt;3. dst_output方法:&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ip_output方法&quot; id=&quot;markdown-toc-ip_output方法&quot;&gt;ip_output方法&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#1-获取struct-net_device&quot; id=&quot;markdown-toc-1-获取struct-net_device&quot;&gt;1. 获取struct net_device&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2-再次调用netfilter-进行post_routing的filter-filter的结果为pass则调用ip_finish_output&quot; id=&quot;markdown-toc-2-再次调用netfilter-进行post_routing的filter-filter的结果为pass则调用ip_finish_output&quot;&gt;2. 再次调用netfilter, 进行POST_ROUTING的filter. filter的结果为pass则调用ip_finish_output.&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#3-获取struct-dst的struct-neighbour成员&quot; id=&quot;markdown-toc-3-获取struct-dst的struct-neighbour成员&quot;&gt;3. 获取struct dst的struct neighbour成员&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#dev_queue_xmit方法&quot; id=&quot;markdown-toc-dev_queue_xmit方法&quot;&gt;dev_queue_xmit方法&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

接&lt;a href=&quot;&amp;quot;http://johnhx.github.io/2017/05/01/Socket-flow/&amp;quot;&quot;&gt;《Linux内核网络子系统源码分析(1) – socket系统调用和关键数据结构》&lt;/a&gt;一文, 继续往下, 阅读Linux内核子系统connect系统调用流程源码.

由于网络子系统数据结构交错复杂, 先上一张数据结构关系图, 便于后文描述:

&lt;img src=&quot;https://raw.githubusercontent.com/johnhx/johnhx.github.io/master/img/socket_struct_init.png&quot; alt=&quot;Image of struct socket initialization&quot; /&gt;

&lt;h2 id=&quot;connect系统调用&quot;&gt;connect系统调用&lt;/h2&gt;
connect系统调用的作用为连接server端正在listen的socket, connect系统调用由client端调用, 接口原型为:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockfd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                   &lt;span class=&quot;n&quot;&gt;socklen_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addrlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

connect系统调用实现入口位于Linux内核的网络子系统顶层代码中(net\socket.c).

暴露出来的connect接口指向函数为kernel_connect:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;kernel_connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addrlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
           &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addrlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;EXPORT_SYMBOL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernel_connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

对于net\ipv4\这个family来说, 结合文首图得知, sock-&amp;gt;ops指向inetsw_array对应type的ops.

type为SOCK_STREAM, prot指向tcp_prot, ops指向inet_stream_ops

sock-&amp;gt;ops-&amp;gt;connect指向inet_stream_ops的connect, 即inet_stream_connect.

&lt;h3 id=&quot;inet_stream_connect流程&quot;&gt;inet_stream_connect流程&lt;/h3&gt;

&lt;h4 id=&quot;1-取struct-socket内部的struct-sock成员指针指向的struct-sock-见文首图很清晰&quot;&gt;1. 取struct socket内部的struct sock成员指针指向的struct sock; (见文首图很清晰)&lt;/h4&gt;

&lt;h4 id=&quot;2-根据struct-socket的socket_state类型字段&quot;&gt;2. 根据struct socket的socket_state类型字段:&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;
    SS_CONNECTED/SS_CONNECTING(略过)
  &lt;/li&gt;
  &lt;li&gt;
    SS_UNCONNECTED: 调用struct sock的sk_prot的connect函数( 这里即指向tcp_proc的connect, 方法名为tcp_v4_connect )
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;tcp_v4_connect流程&quot;&gt;tcp_v4_connect流程&lt;/h3&gt;

&lt;h4 id=&quot;1-从connect系统调用传入的struct-sockaddr取出destination-address-destination-port-并取得nexthop如果struct-inet_sock的struct-ip_options_rcu有设置-nexthop为struct-ip_option的faddr-然后调用ip_route_connect进行寻路-取得struct-rtable&quot;&gt;1. 从connect系统调用传入的struct sockaddr取出destination address, destination port, 并取得nexthop(如果struct inet_sock的struct ip_options_rcu有设置, nexthop为struct ip_option的faddr), 然后调用ip_route_connect进行寻路, 取得struct rtable;&lt;/h4&gt;

问题: struct inet_sock的struct ip_options_rcu *inet_opt在何处设置?

&lt;h4 id=&quot;2-根据ip寻路的结果-确定destination-address-destination-port&quot;&gt;2. 根据IP寻路的结果, 确定destination address, destination port&lt;/h4&gt;

&lt;h4 id=&quot;3-设置默认tcp_mss---ipv4默认为536-&quot;&gt;3. 设置默认TCP_MSS  ( IPv4默认为536 )&lt;/h4&gt;

&lt;h4 id=&quot;4-tcp状态设为tcp_syn_sent&quot;&gt;4. tcp状态设为TCP_SYN_SENT&lt;/h4&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;tcp_set_state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TCP_SYN_SENT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h4 id=&quot;5-将struct-sock插入bind链表中&quot;&gt;5. 将struct sock插入bind链表中&lt;/h4&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inet_hash_connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tcp_death_row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

*在这一步之前source port可能还为0(对于client端调用socket后直接调用connect不经过bind的情况, source port为0).

所以inet_hash_connect方法会生成一个随机的source port, 赋给struct inet_sk的inet_sport成员.*

&lt;h4 id=&quot;6-调用ip_route_newports&quot;&gt;6. 调用ip_route_newports;&lt;/h4&gt;

&lt;h4 id=&quot;7-为tcp生成sequence-number-赋给struct-sock的write_seq成员&quot;&gt;7. 为tcp生成sequence number, 赋给struct sock的write_seq成员;&lt;/h4&gt;

&lt;h4 id=&quot;8-调用tcp_connect方法&quot;&gt;8. 调用tcp_connect方法&lt;/h4&gt;

&lt;h5 id=&quot;81-分配struct-sk_buff&quot;&gt;8.1 分配struct sk_buff&lt;/h5&gt;

&lt;h5 id=&quot;82-在struct-sk_buff中的tcp_skb_cb部分的tcp_flags字段-设置syn标志-设置seq-end_seq-gso_-什么是gso&quot;&gt;8.2 在struct sk_buff中的tcp_skb_cb部分的tcp_flags字段, 设置SYN标志, 设置seq, end_seq, gso_* (什么是gso?)&lt;/h5&gt;

&lt;h5 id=&quot;83-根据sysctl_tcp_ecn-设置tcp_flags字段中的ecn-cwr标志&quot;&gt;8.3 根据sysctl_tcp_ecn, 设置tcp_flags字段中的ECN, CWR标志&lt;/h5&gt;

&lt;h5 id=&quot;84-给struct-sk_buff打上时间戳&quot;&gt;8.4 给struct sk_buff打上时间戳&lt;/h5&gt;

&lt;h5 id=&quot;85-减struct-sk_buff引用&quot;&gt;8.5 减struct sk_buff引用&lt;/h5&gt;

&lt;h5 id=&quot;86-调用__skb_queue_tail-将struct-sk_buff挂到struct-sock的sk_write_queue&quot;&gt;8.6 调用__skb_queue_tail, 将struct sk_buff挂到struct sock的sk_write_queue&lt;/h5&gt;

&lt;h5 id=&quot;87-调用tcp_transmit_skb方法进入tcp层实际的处理和传输过程&quot;&gt;8.7 调用tcp_transmit_skb方法进入&lt;strong&gt;tcp层实际的处理和传输过程&lt;/strong&gt;&lt;/h5&gt;

&lt;h3 id=&quot;tcp_transmit_skb流程&quot;&gt;tcp_transmit_skb流程&lt;/h3&gt;

&lt;strong&gt;此方法开始TCP层实际的头部数据处理和往下层传输.&lt;/strong&gt;

tcp_transmit_skb方法接口如下:

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tcp_transmit_skb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sk_buff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;skb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clone_it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gfp_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gfp_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h4 id=&quot;1-如果clone_it为1调用skb_clone方法clone一个sk_buff-注意此处只是克隆数据结构packet-data只是加引用计数&quot;&gt;1. 如果clone_it为1，调用skb_clone方法clone一个sk_buff, 注意此处只是克隆数据结构，packet data只是加引用计数;&lt;/h4&gt;

skb_clone方法克隆的struct sk_buff不属于任何socket.

&lt;h4 id=&quot;2-计算tcp头部空间大小tcp_header_size-sizeofstruct-tcphdr--tcp_options_size&quot;&gt;2. 计算tcp头部空间大小tcp_header_size: sizeof(struct tcphdr) + tcp_options_size&lt;/h4&gt;

&lt;h4 id=&quot;3-使用tcp_header_size来构建tcp头部-包括设置source-port-dest-port-seq-ack_seq-window等&quot;&gt;3. 使用tcp_header_size来构建tcp头部, 包括设置source port, dest port, seq, ack_seq, window等&lt;/h4&gt;

&lt;h4 id=&quot;4-调用struct-inet_connection_sock的icsk_af_ops的queue_xmit方法--该函数指针在tcp_v4_init_sock方法中初始化-指向ip_outputc的ip_queue_xmit-&quot;&gt;4. 调用struct inet_connection_sock的icsk_af_ops的queue_xmit方法 ( 该函数指针在tcp_v4_init_sock方法中初始化, 指向ip_output.c的ip_queue_xmit )&lt;/h4&gt;

&lt;h3 id=&quot;ip_queue_xmit方法-ip层实际的处理和传输过程&quot;&gt;ip_queue_xmit方法 (IP层实际的处理和传输过程)&lt;/h3&gt;

&lt;h4 id=&quot;1-加ip头部&quot;&gt;1. 加ip头部&lt;/h4&gt;

&lt;h4 id=&quot;2-ip_local_out---__ip_local_out---nf_hookipv4-local_out--此处进入netfilter子系统-若允许pass-返回1-调用dst_output&quot;&gt;2. ip_local_out -&amp;gt; __ip_local_out -&amp;gt; nf_hook(IPV4, LOCAL_OUT, …), 此处进入netfilter子系统, 若允许pass, 返回1, 调用dst_output&lt;/h4&gt;

此处是第一次碰到netfilter子系统, 在Linux内核网络子系统中, Netfilter相当于一个hook系统, 在不同的时间点进行filter, 有如下几个大的时间点:

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nf_inet_hooks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;NF_INET_PRE_ROUTING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;NF_INET_LOCAL_IN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;NF_INET_FORWARD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;NF_INET_LOCAL_OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;NF_INET_POST_ROUTING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;NF_INET_NUMHOOK&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h4 id=&quot;3-dst_output方法&quot;&gt;3. dst_output方法:&lt;/h4&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skb_dst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;skb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;skb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

struct dst_entry的output方法函数指针在net\ipv4\route.c的__mkroute_output得到赋值, 指向ip_output方法.

&lt;h3 id=&quot;ip_output方法&quot;&gt;ip_output方法&lt;/h3&gt;

&lt;h4 id=&quot;1-获取struct-net_device&quot;&gt;1. 获取struct net_device&lt;/h4&gt;

&lt;h4 id=&quot;2-再次调用netfilter-进行post_routing的filter-filter的结果为pass则调用ip_finish_output&quot;&gt;2. 再次调用netfilter, 进行POST_ROUTING的filter. filter的结果为pass则调用ip_finish_output.&lt;/h4&gt;

ip_finish_output -&amp;gt; 是否需要分片? ip_fragment() : ip_finish_output2()

先描述ip_finish_output2()这条线的流程.

&lt;h4 id=&quot;3-获取struct-dst的struct-neighbour成员&quot;&gt;3. 获取struct dst的struct neighbour成员&lt;/h4&gt;

struct neighbour有一个NUD信息, 全称为“neighbour unreachability detection”;
(此处应该补一张structure neighbour的结构图)

依次调用neigh_output -&amp;gt; neigh_hh_output, 最终调用到dev_queue_xmit方法

&lt;h3 id=&quot;dev_queue_xmit方法&quot;&gt;dev_queue_xmit方法&lt;/h3&gt;

struct net_device -&amp;gt; struct netdev_queue -&amp;gt; struct Qdisc

调用struct Qdisc的enqueue方法将struct sk_buff挂入队列中.

struct Qdisc有多种实现, 实现在net\sched\sch_*.c中, 有fifo, generic, dsmark, cbq, choke等(留待日后细分析).

一个connect过程的调用框图如下:

&lt;img src=&quot;https://raw.githubusercontent.com/johnhx/johnhx.github.io/master/img/connect_sequence.png&quot; alt=&quot;Image of connect sequence&quot; /&gt;
</description>
        <pubDate>Thu, 11 May 2017 00:00:00 +0000</pubDate>
        <link>https://johnhx.github.io/2017/05/11/LinuxKernel-Connect-flow/</link>
        <guid isPermaLink="true">https://johnhx.github.io/2017/05/11/LinuxKernel-Connect-flow/</guid>
        
        <category>Linux内核</category>
        
        <category>Network</category>
        
        <category>Socket</category>
        
        
        <category>Linux内核</category>
        
      </item>
    
      <item>
        <title>对j8ql的一个enhancement</title>
        <description>
最近使用&lt;a href=&quot;https://github.com/BriteSnow/j8ql&quot; title=&quot;j8ql&quot;&gt;j8ql&lt;/a&gt;做ORM搭建了一套内部的Web Service/Web UI.

j8ql是比较小众的一个ORM类库, 主打轻量.

为什么不用Hibernate, MyBatis? 以前用过这两者, MyBatis是使用xml做Mapper, 实在不喜欢，配置也过于复杂, 造成本来就很小的Web Service应用太重.

这套内部的Web Service有批量插入的需求, 大概是数万条记录的批量插入.

为了提高性能, 采取batch insert的方式.

由于batch insert后, 需要更新另外一张关系表, 故需要取得batch insert生成的键值.

j8ql对java.sql的PreparedStatement和Statement类做了封装, 将部分Statement的接口作为API提供给应用使用.

j8ql的executeBatch接口封装了Statement的executeBatch接口, 返回的是int[], 代表insert的结果成功还是失败.

如果需要取得batch insert的键值, j8ql没有提供方式.

遍寻无解的时候, 发现java.sql的Statement接口有一个getGeneratedKeys的方法, 用于获取该Statement运行时auto-generated keys, 正是我所需要的方法.

问题是j8ql的Runner接口和RunnerImpl类并未暴露这个getGeneratedKeys.

只能自己搞了, 于是对j8ql做了如下改动:

&lt;ul&gt;
  &lt;li&gt;为RunnerImpl新增了一个List&lt;Record&gt;成员, 用于保存PreparedStatement/Statement对象执行完executeBatch后调用getGeneratedKeys获取的结果;&lt;/Record&gt;&lt;/li&gt;
  &lt;li&gt;为Runner新增了一个getGeneratedKeys方法, 返回这个List&lt;Record&gt;成员.&lt;/Record&gt;&lt;/li&gt;
&lt;/ul&gt;

代码改完, 测试过程中, 发现在PreparedStatement/Statement在executeBatch后调用PreparedStatement/Statement的getGeneratedKeys依然取不到生成的键值.

Google后发现, Statement在prepateStatement时, 需要设置第二个参数autoGeneratedKeys为1, 方能使getGeneratedKeys返回结果.

UT完成后, 大功告成.

生成pull request提交给j8ql的owner做review和merge:

https://github.com/BriteSnow/j8ql/pull/12

</description>
        <pubDate>Mon, 08 May 2017 00:00:00 +0000</pubDate>
        <link>https://johnhx.github.io/2017/05/08/j8ql/</link>
        <guid isPermaLink="true">https://johnhx.github.io/2017/05/08/j8ql/</guid>
        
        <category>ORM</category>
        
        <category>j8ql</category>
        
        <category>databse</category>
        
        
        <category>Java</category>
        
      </item>
    
      <item>
        <title>Linux内核网络子系统源码分析(1) -- socket系统调用和关键数据结构</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#内核初始化相关网络子系统初始化&quot; id=&quot;markdown-toc-内核初始化相关网络子系统初始化&quot;&gt;内核初始化相关/网络子系统初始化&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#socket系统调用流程&quot; id=&quot;markdown-toc-socket系统调用流程&quot;&gt;Socket系统调用流程&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#一-首先调用到sock_create&quot; id=&quot;markdown-toc-一-首先调用到sock_create&quot;&gt;一. 首先调用到sock_create&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#1-__sock_create&quot; id=&quot;markdown-toc-1-__sock_create&quot;&gt;1. __sock_create&lt;/a&gt;            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#11-调用secutiry子系统的方法secutiry_ops-socket_create去security子系统折腾了一圈&quot; id=&quot;markdown-toc-11-调用secutiry子系统的方法secutiry_ops-socket_create去security子系统折腾了一圈&quot;&gt;1.1 调用secutiry子系统的方法secutiry_ops-&amp;gt;socket_create去security子系统折腾了一圈;&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#12-sock_alloc-分配struct-socket&quot; id=&quot;markdown-toc-12-sock_alloc-分配struct-socket&quot;&gt;1.2 sock_alloc: 分配struct socket&lt;/a&gt;                &lt;ul&gt;
                  &lt;li&gt;&lt;a href=&quot;#121-通过sockfs中分配一个inode--sockfs的初始化详见netsocketc的sock_init方法-&quot; id=&quot;markdown-toc-121-通过sockfs中分配一个inode--sockfs的初始化详见netsocketc的sock_init方法-&quot;&gt;1.2.1 通过sockfs中分配一个inode ( sockfs的初始化详见net/socket.c的sock_init方法 )&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#122-基于inode结构取到socket结构--怎么取到的-这里使用的是著名的container_of宏-&quot; id=&quot;markdown-toc-122-基于inode结构取到socket结构--怎么取到的-这里使用的是著名的container_of宏-&quot;&gt;1.2.2 基于inode结构取到socket结构 ( 怎么取到的? 这里使用的是著名的container_of宏 )&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#13-从全局net_families数组中根据下标family取到对应的struct-net_proto_family结构pf&quot; id=&quot;markdown-toc-13-从全局net_families数组中根据下标family取到对应的struct-net_proto_family结构pf&quot;&gt;1.3 从全局net_families数组中根据下标family取到对应的struct net_proto_family结构pf;&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#14-关键的一步调用到pf-create-按照上面的描述-调用到了netipv4af_inetc的inet_create方法&quot; id=&quot;markdown-toc-14-关键的一步调用到pf-create-按照上面的描述-调用到了netipv4af_inetc的inet_create方法&quot;&gt;1.4 关键的一步调用到pf-&amp;gt;create, 按照上面的描述, 调用到了net/ipv4/Af_inet.c的inet_create方法.&lt;/a&gt;                &lt;ul&gt;
                  &lt;li&gt;&lt;a href=&quot;#1-先将struct-socket的state设为ss_unconnected&quot; id=&quot;markdown-toc-1-先将struct-socket的state设为ss_unconnected&quot;&gt;1. 先将struct socket的state设为SS_UNCONNECTED;&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#2-根据struct-socket的typesock_stream之类-遍历inetswtype-找到对应到protocol的结构体&quot; id=&quot;markdown-toc-2-根据struct-socket的typesock_stream之类-遍历inetswtype-找到对应到protocol的结构体&quot;&gt;2. 根据struct socket的type(SOCK_STREAM之类), 遍历inetsw[type], 找到对应到protocol的结构体;&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#3-将对应到protocol的结构体的ops赋给struct-socket结构的ops&quot; id=&quot;markdown-toc-3-将对应到protocol的结构体的ops赋给struct-socket结构的ops&quot;&gt;3. 将”对应到protocol的结构体”的ops赋给struct socket结构的ops.&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#4-调用sk_alloc-分配网络子系统核心netcore的数据结构struct-sock--记录family-protocol到sk_family-sk_prot成员-&quot; id=&quot;markdown-toc-4-调用sk_alloc-分配网络子系统核心netcore的数据结构struct-sock--记录family-protocol到sk_family-sk_prot成员-&quot;&gt;4. 调用sk_alloc, 分配网络子系统核心(net/core)的数据结构struct sock ( 记录family, protocol到sk_family, sk_prot成员 )&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#5-然后直接将struct-sock强转为struct-inet_sk调用inet_sk-设置一些inet的参数&quot; id=&quot;markdown-toc-5-然后直接将struct-sock强转为struct-inet_sk调用inet_sk-设置一些inet的参数&quot;&gt;5. 然后直接将struct sock强转为struct inet_sk(调用inet_sk), 设置一些inet的参数.&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#6-根据ipv4_configno_pmtu_disc-设置struct-inet_sock的pmtudisc标志位dont或者want&quot; id=&quot;markdown-toc-6-根据ipv4_configno_pmtu_disc-设置struct-inet_sock的pmtudisc标志位dont或者want&quot;&gt;6. 根据ipv4_config.no_pmtu_disc, 设置struct inet_sock的pmtudisc标志位DONT或者WANT.&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#7-调用sock_init_datastruct-socket-struct-sock&quot; id=&quot;markdown-toc-7-调用sock_init_datastruct-socket-struct-sock&quot;&gt;7. 调用sock_init_data(struct socket, struct sock)&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#8-sk-sk_backlog_rcv--收到backlog的回调函数--初始化为sk-sk_prot-backlog_rcv&quot; id=&quot;markdown-toc-8-sk-sk_backlog_rcv--收到backlog的回调函数--初始化为sk-sk_prot-backlog_rcv&quot;&gt;8. sk-&amp;gt;sk_backlog_rcv ( 收到backlog的回调函数 ) 初始化为sk-&amp;gt;sk_prot-&amp;gt;backlog_rcv&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#9-inet-ut_ttl--unicast的ttl-为1&quot; id=&quot;markdown-toc-9-inet-ut_ttl--unicast的ttl-为1&quot;&gt;9. inet-&amp;gt;ut_ttl ( Unicast的TTL )为1&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#10-inet-mc_loop--multicast的loop--为1&quot; id=&quot;markdown-toc-10-inet-mc_loop--multicast的loop--为1&quot;&gt;10. inet-&amp;gt;mc_loop ( Multicast的loop ) 为1&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#11-inet-mc_ttl--multicast的ttl--mc_all为1&quot; id=&quot;markdown-toc-11-inet-mc_ttl--multicast的ttl--mc_all为1&quot;&gt;11. inet-&amp;gt;mc_ttl ( Multicast的TTL ), mc_all为1&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#12-inet-mc_index--multicast的device-index-为0&quot; id=&quot;markdown-toc-12-inet-mc_index--multicast的device-index-为0&quot;&gt;12. inet-&amp;gt;mc_index ( Multicast的device index )为0&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#13-如果inet-inet_num--local-port-不为空&quot; id=&quot;markdown-toc-13-如果inet-inet_num--local-port-不为空&quot;&gt;13. 如果inet-&amp;gt;inet_num ( local port? )不为空,&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#14-调用sk-sk_prot-init-例如对于tcp-指向netipv4tcp_ipv4c的全局结构体struct-proto-tcp_port中的tcp_v4_init_sock-此方法完成该socket在内核网络子系统tcp层的初始化&quot; id=&quot;markdown-toc-14-调用sk-sk_prot-init-例如对于tcp-指向netipv4tcp_ipv4c的全局结构体struct-proto-tcp_port中的tcp_v4_init_sock-此方法完成该socket在内核网络子系统tcp层的初始化&quot;&gt;14. 调用sk-&amp;gt;sk_prot-&amp;gt;init, 例如对于TCP, 指向net/ipv4/Tcp_ipv4.c的全局结构体struct proto tcp_port中的tcp_v4_init_sock, 此方法完成该socket在内核网络子系统TCP层的初始化:&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#15-调用secutiry子系统的方法secutiry_ops-socket_post_create去security子系统折腾了一圈&quot; id=&quot;markdown-toc-15-调用secutiry子系统的方法secutiry_ops-socket_post_create去security子系统折腾了一圈&quot;&gt;1.5 调用secutiry子系统的方法secutiry_ops-&amp;gt;socket_post_create去security子系统折腾了一圈;&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#二-调用sock_map_fd将struct-socket挂接到fd供进程使用&quot; id=&quot;markdown-toc-二-调用sock_map_fd将struct-socket挂接到fd供进程使用&quot;&gt;二. 调用sock_map_fd将struct socket挂接到fd供进程使用.&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#1-调用sock_alloc_file-传入struct-socket-struct-file和flag&quot; id=&quot;markdown-toc-1-调用sock_alloc_file-传入struct-socket-struct-file和flag&quot;&gt;1. 调用sock_alloc_file, 传入struct socket, struct file和flag&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2-调用fd_install将file-descriptor加入fd-array&quot; id=&quot;markdown-toc-2-调用fd_install将file-descriptor加入fd-array&quot;&gt;2. 调用fd_install将file descriptor加入fd array&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

这部分代码看了有几年了，归纳总结的想法也想了几年，刚好最近比较空，把这部分代码流程梳理了一下，总结成一篇blog，记录一下。

&lt;h2 id=&quot;内核初始化相关网络子系统初始化&quot;&gt;内核初始化相关/网络子系统初始化&lt;/h2&gt;
内核初始化过程中，依次调到arch/&lt;arch&gt;/boot -&amp;gt; arch/&lt;arch&gt;/kernel -&amp;gt; start_kernel&lt;/arch&gt;&lt;/arch&gt;

start_kernel最后调到do_basic_setup()(init/main.c):
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__init&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;do_basic_setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cpuset_init_smp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;usermodehelper_init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;shmem_init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;driver_init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;init_irq_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;do_ctors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;usermodehelper_enable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;do_initcalls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

do_initcalls()(init/main.c)方法如下:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__init&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;do_initcalls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;initcall_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__early_initcall_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__initcall_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;do_one_initcall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

其中__early_initcall_end, __initcall_end定义在Vmlinux.lds.h中:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#define INITCALLS                           \
    *(.initcallearly.init)                      \
    VMLINUX_SYMBOL(__early_initcall_end) = .;           \
    *(.initcall0.init)                      \
    *(.initcall0s.init)                     \
    *(.initcall1.init)                      \
    *(.initcall1s.init)                     \
    *(.initcall2.init)                      \
    *(.initcall2s.init)                     \
    *(.initcall3.init)                      \
    *(.initcall3s.init)                     \
    *(.initcall4.init)                      \
    *(.initcall4s.init)                     \
    *(.initcall5.init)                      \
    *(.initcall5s.init)                     \
    *(.initcallrootfs.init)                     \
    *(.initcall6.init)                      \
    *(.initcall6s.init)                     \
    *(.initcall7.init)                      \
    *(.initcall7s.init)
&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#define INIT_CALLS                          \
        VMLINUX_SYMBOL(__initcall_start) = .;           \
        INITCALLS                       \
        VMLINUX_SYMBOL(__initcall_end) = .;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

可见, 在__early_initcall_end和__initcall_end之间是从.initcall0.init到.initcall7s.init这几个段的函数指针.

这几个段里分别放的哪些函数指针呢?

在inlude\linux\init.h中, 有如下的宏定义:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#define pure_initcall(fn)       __define_initcall(&quot;0&quot;,fn,0)
&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#define core_initcall(fn)       __define_initcall(&quot;1&quot;,fn,1)
#define core_initcall_sync(fn)      __define_initcall(&quot;1s&quot;,fn,1s)
#define postcore_initcall(fn)       __define_initcall(&quot;2&quot;,fn,2)
#define postcore_initcall_sync(fn)  __define_initcall(&quot;2s&quot;,fn,2s)
#define arch_initcall(fn)       __define_initcall(&quot;3&quot;,fn,3)
#define arch_initcall_sync(fn)      __define_initcall(&quot;3s&quot;,fn,3s)
#define subsys_initcall(fn)     __define_initcall(&quot;4&quot;,fn,4)
#define subsys_initcall_sync(fn)    __define_initcall(&quot;4s&quot;,fn,4s)
#define fs_initcall(fn)         __define_initcall(&quot;5&quot;,fn,5)
#define fs_initcall_sync(fn)        __define_initcall(&quot;5s&quot;,fn,5s)
#define rootfs_initcall(fn)     __define_initcall(&quot;rootfs&quot;,fn,rootfs)
#define device_initcall(fn)     __define_initcall(&quot;6&quot;,fn,6)
#define device_initcall_sync(fn)    __define_initcall(&quot;6s&quot;,fn,6s)
#define late_initcall(fn)       __define_initcall(&quot;7&quot;,fn,7)
#define late_initcall_sync(fn)      __define_initcall(&quot;7s&quot;,fn,7s)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

可见, 在内核源码中，我们经常看到的诸如core_initcall, arch_initcall, subsys_initcall, fs_initcall宏调用，实际是将函数放到0 ~ 7s对应的段中, 以便于内核在初始化时候进行调用。

比如在内核的网络子系统部分(net/), 网络子系统sysctl interface的初始化逻辑放在net/sysctl_net.c中, 便调用了subsys_initcall宏:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__init&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sysctl_init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;register_pernet_subsys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sysctl_pernet_ops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;register_sysctl_root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net_sysctl_root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;setup_sysctl_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net_sysctl_ro_root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;register_sysctl_root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net_sysctl_ro_root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;out:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;subsys_initcall&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sysctl_init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;socket系统调用流程&quot;&gt;Socket系统调用流程&lt;/h2&gt;
Socket系统调用的作用是创建一个socket, 该系统调用传入的参数为:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;family&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;protocol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

Socket系统调用实现入口位于Linux内核的网络子系统顶层代码中(net\socket.c).

&lt;h3 id=&quot;一-首先调用到sock_create&quot;&gt;一. 首先调用到sock_create&lt;/h3&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;sock_create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;family&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;protocol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
在这个第一层函数sock_create里, 比起socket系统调用来多出来第四个参数&amp;amp;sock,

这个参数是一个结构体, 定义如下:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;socket_state&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kmemcheck_bitfield_begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;           &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;kmemcheck_bitfield_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;       &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket_wq&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__rcu&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;     &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt;     &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proto_ops&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

其中:
&lt;ul&gt;
  &lt;li&gt;state为枚举类型: SS_FREE, SS_UNCONNECTED, SS_CONNECTING, SS_CONNECTED, SS_DISCONNECTED. ( 比较好理解 );&lt;/li&gt;
  &lt;li&gt;type为诸如SOCK_STREAM之类的;&lt;/li&gt;
  &lt;li&gt;flags是标记;&lt;/li&gt;
  &lt;li&gt;wq是一个wait queue, 具体怎么用后面会提到;&lt;/li&gt;
  &lt;li&gt;file是供gc使用的文件描述符;&lt;/li&gt;
  &lt;li&gt;sk是比较重要的数据结构, 指向代表下层协议(network layer)数据的sock结构&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;1-__sock_create&quot;&gt;1. __sock_create&lt;/h4&gt;
sock_create向下调到第二层方法:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;__sock_create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nsproxy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net_ns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;family&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;protocol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
这一层函数引入了一个新的参数struct net, 后面再做分析。

在__sock_create这一层:

&lt;h5 id=&quot;11-调用secutiry子系统的方法secutiry_ops-socket_create去security子系统折腾了一圈&quot;&gt;1.1 调用secutiry子系统的方法secutiry_ops-&amp;gt;socket_create去security子系统折腾了一圈;&lt;/h5&gt;

&lt;h5 id=&quot;12-sock_alloc-分配struct-socket&quot;&gt;1.2 sock_alloc: 分配struct socket&lt;/h5&gt;

&lt;h6 id=&quot;121-通过sockfs中分配一个inode--sockfs的初始化详见netsocketc的sock_init方法-&quot;&gt;1.2.1 通过sockfs中分配一个inode ( sockfs的初始化详见net/socket.c的sock_init方法 )&lt;/h6&gt;

&lt;h6 id=&quot;122-基于inode结构取到socket结构--怎么取到的-这里使用的是著名的container_of宏-&quot;&gt;1.2.2 基于inode结构取到socket结构 ( 怎么取到的? 这里使用的是著名的container_of宏 )&lt;/h6&gt;

&lt;blockquote&gt;
  container_of宏实现如下:
  &lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#define container_of(ptr, type, member) ({          \
   const typeof( ((type *)0)-&amp;gt;member ) *__mptr = (ptr);    \
   (type *)( (char *)__mptr - offsetof(type,member) );})
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
  先new一个指针__mptr，指针所指类型和type内的member成员一样, 并指向传入的待求container数据ptr;

  该指针__mptr便存放着待求container的数据ptr的内存地址, 并且有正确的类型;

  然后用这个内存地址减去type内部的member成员的偏移量, 便是包含ptr这个类型为type的结构体的起始地址, 强转为type指针类型, 即为结果.
&lt;/blockquote&gt;

通过调用container_of宏, 使用inode结构取到了socket结构, 返回.

&lt;h5 id=&quot;13-从全局net_families数组中根据下标family取到对应的struct-net_proto_family结构pf&quot;&gt;1.3 从全局net_families数组中根据下标family取到对应的struct net_proto_family结构pf;&lt;/h5&gt;

全局net_families数组是一个维护着系统全局所有网络簇信息的一个数组结构.

net_families数组通过sock_register/sock_unregister添加/删除元素. ( 最多40个元素, 可以理解为下层协议的数据结构, 例如ipv4, ipv6, netlink, appletalk, bluetooth等, 本文后面部分采用ipv4作为示例. )

sock_register/sock_unregister通常在net/xxx/Af_xxx.c中调用.

例如对于INET, 在net/ipv4/Af_inet.c中将INET的struct net_proto_family结构添加到全局net_families数组中.

INET的struct net_proto_family定义如下:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;net_proto_family&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inet_family_ops&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;family&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PF_INET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inet_create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;owner&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;THIS_MODULE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h5 id=&quot;14-关键的一步调用到pf-create-按照上面的描述-调用到了netipv4af_inetc的inet_create方法&quot;&gt;1.4 关键的一步调用到pf-&amp;gt;create, 按照上面的描述, 调用到了net/ipv4/Af_inet.c的inet_create方法.&lt;/h5&gt;

&lt;h6 id=&quot;1-先将struct-socket的state设为ss_unconnected&quot;&gt;1. 先将struct socket的state设为SS_UNCONNECTED;&lt;/h6&gt;

&lt;h6 id=&quot;2-根据struct-socket的typesock_stream之类-遍历inetswtype-找到对应到protocol的结构体&quot;&gt;2. 根据struct socket的type(SOCK_STREAM之类), 遍历inetsw[type], 找到对应到protocol的结构体;&lt;/h6&gt;

注意:

inetsw是一个链表数组, key为SOCK_STREAM, SOCK_DGRAM, SOCK_RAW等等.

inetsw的初始化在net/ipv4/Af_inet.c的inet_init方法中:

&lt;ul&gt;
  &lt;li&gt;
    先初始化inesw为SOCK_STREAM/SOCK_DGRAM/SOCK_RAW等作为key的list_head数组;
  &lt;/li&gt;
  &lt;li&gt;
    遍历inetsw_array, 将其挂入inetsw中.
  &lt;/li&gt;
&lt;/ul&gt;

inetsw_array的元素封装了TCP, UDP, PING, RAW等协议, 即为上文中描述的”对应到protocol的结构体”.

inetsw_array的元素结构如下:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inet_protosw&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inetsw_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;       &lt;span class=&quot;n&quot;&gt;SOCK_STREAM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;protocol&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;IPPROTO_TCP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;       &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tcp_prot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;        &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inet_stream_ops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;no_check&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;   &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;INET_PROTOSW_PERMANENT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;INET_PROTOSW_ICSK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h6 id=&quot;3-将对应到protocol的结构体的ops赋给struct-socket结构的ops&quot;&gt;3. 将”对应到protocol的结构体”的ops赋给struct socket结构的ops.&lt;/h6&gt;

例如如果type是SOCK_STREAM, protocol是TCP, 将&amp;amp;inet_stream_ops赋给struct socket结构的ops.

各个数据结构关系如下图:

&lt;img src=&quot;https://raw.githubusercontent.com/johnhx/johnhx.github.io/master/img/socket_inet.png&quot; alt=&quot;Image of socket inet&quot; /&gt;

&lt;h6 id=&quot;4-调用sk_alloc-分配网络子系统核心netcore的数据结构struct-sock--记录family-protocol到sk_family-sk_prot成员-&quot;&gt;4. 调用sk_alloc, 分配网络子系统核心(net/core)的数据结构struct sock ( 记录family, protocol到sk_family, sk_prot成员 )&lt;/h6&gt;

这里所做的sk_alloc调用, 传入的answer_prot指向tcp_prot, 故分配的大小为struct tcp_sock.
(见net\ipv4\tcp_ipv4.c的struct proto tcp_prot结构体的obj_size字段):

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tcp_prot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tcp_sock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

调用sk_alloc的代码如下:

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;answer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;answer_prot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;answer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sk_alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PF_INET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GFP_KERNEL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;answer_prot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

struct sock的sk_prot指向“对应到protocol的结构体”中的prot, 即&amp;amp;tcp_prot;

&lt;h6 id=&quot;5-然后直接将struct-sock强转为struct-inet_sk调用inet_sk-设置一些inet的参数&quot;&gt;5. 然后直接将struct sock强转为struct inet_sk(调用inet_sk), 设置一些inet的参数.&lt;/h6&gt;

注意1:

之所以struct sock可以强转为struct inet_sk, 是因为在sk_alloc()时分配的是struct tcp_sock结构大小(但是返回的是struct sock).

tcp_sock, inet_sock, sock三个结构体的关系为:

&lt;strong&gt;tcp_sock 包含 inet_connection_sock 包含 inet_sock 包含 sock&lt;/strong&gt;

图示如下:

&lt;img src=&quot;https://raw.githubusercontent.com/johnhx/johnhx.github.io/master/img/sock_structure.png&quot; alt=&quot;Image of socket structure&quot; /&gt;

注意2:

如果是SOCK_RAW, 会把socket系统调用传入的protocol值直接存放在struct inet_sock的inet_num成员作为local port.

通常protocol值代表IP之上的协议代号, 定义在include\linux\ln.h里:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_IP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;cm&quot;&gt;/* Dummy protocol for TCP       */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_ICMP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* Internet Control Message Protocol    */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_IGMP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* Internet Group Management Protocol   */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_IPIP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* IPIP tunnels (older KA9Q tunnels use 94) */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_TCP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;      &lt;span class=&quot;cm&quot;&gt;/* Transmission Control Protocol    */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_EGP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;      &lt;span class=&quot;cm&quot;&gt;/* Exterior Gateway Protocol        */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_PUP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* PUP protocol             */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_UDP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* User Datagram Protocol       */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_IDP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* XNS IDP protocol         */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_DCCP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;        &lt;span class=&quot;cm&quot;&gt;/* Datagram Congestion Control Protocol */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_RSVP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;46&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;        &lt;span class=&quot;cm&quot;&gt;/* RSVP protocol            */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_GRE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;47&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* Cisco GRE tunnels (rfc 1701,1702)    */&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;IPPROTO_IPV6&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;      &lt;span class=&quot;cm&quot;&gt;/* IPv6-in-IPv4 tunnelling      */&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;IPPROTO_ESP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;            &lt;span class=&quot;cm&quot;&gt;/* Encapsulation Security Payload protocol */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_AH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;51&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;             &lt;span class=&quot;cm&quot;&gt;/* Authentication Header protocol       */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_BEETPH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;94&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;         &lt;span class=&quot;cm&quot;&gt;/* IP option pseudo header for BEET */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_PIM&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;103&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* Protocol Independent Multicast   */&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;IPPROTO_COMP&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;108&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;                &lt;span class=&quot;cm&quot;&gt;/* Compression Header protocol */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_SCTP&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;132&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* Stream Control Transport Protocol    */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_UDPLITE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;136&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;cm&quot;&gt;/* UDP-Lite (RFC 3828)          */&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;IPPROTO_RAW&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;cm&quot;&gt;/* Raw IP packets           */&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;IPPROTO_MAX&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

如下的代码没有使用系统预留的protocol, 而是利用RFC 3692定义的253端口做测试用:
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AF_INET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SOCK_RAW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;253&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h6 id=&quot;6-根据ipv4_configno_pmtu_disc-设置struct-inet_sock的pmtudisc标志位dont或者want&quot;&gt;6. 根据ipv4_config.no_pmtu_disc, 设置struct inet_sock的pmtudisc标志位DONT或者WANT.&lt;/h6&gt;

注意:

PMTUD全称Path MTU Discovery, 指的是在两个IP host间决定Maximum Transmission Unit ( MTU )的技术, 目的是避免IP分片.

由此可见, 这个PMTUD在内核里面是可配的!!!

&lt;h6 id=&quot;7-调用sock_init_datastruct-socket-struct-sock&quot;&gt;7. 调用sock_init_data(struct socket, struct sock)&lt;/h6&gt;

从函数原型上可以看出, 是借助struct socket来初始化网络层核心数据结构struct sock:

&lt;ul&gt;
  &lt;li&gt;sk-&amp;gt;sk_receive_queue, sk_write_queue, sk_error_queue ( 如果配了NET_DMA, 还有sk_async_wait_queue )&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_send_head&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_timer&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_rcvbuf ( 这里指的是rmem )&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_sndbuf ( 这里指的是wmem )&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_state ( 初始值为TCP_CLOSE )&lt;/li&gt;
  &lt;li&gt;接管struct socket的wq&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_dst_lock&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_callback_lock&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_rcvlowat ( SO_RCVLOWAT )&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_rcvtimeo ( SO_RCVTIMEO )&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_sndtimeo ( SO_SNDTIMEO )&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_stamp ( 上一个packet收到的timestamp )&lt;/li&gt;
&lt;/ul&gt;

&lt;h6 id=&quot;8-sk-sk_backlog_rcv--收到backlog的回调函数--初始化为sk-sk_prot-backlog_rcv&quot;&gt;8. sk-&amp;gt;sk_backlog_rcv ( 收到backlog的回调函数 ) 初始化为sk-&amp;gt;sk_prot-&amp;gt;backlog_rcv&lt;/h6&gt;

例如对于TCP, backlog_rcv指向net/ipv4/Tcp_ipv4.c的全局结构体struct proto tcp_prot中的tcp_v4_do_rcv

&lt;h6 id=&quot;9-inet-ut_ttl--unicast的ttl-为1&quot;&gt;9. inet-&amp;gt;ut_ttl ( Unicast的TTL )为1&lt;/h6&gt;

&lt;h6 id=&quot;10-inet-mc_loop--multicast的loop--为1&quot;&gt;10. inet-&amp;gt;mc_loop ( Multicast的loop ) 为1&lt;/h6&gt;

&lt;h6 id=&quot;11-inet-mc_ttl--multicast的ttl--mc_all为1&quot;&gt;11. inet-&amp;gt;mc_ttl ( Multicast的TTL ), mc_all为1&lt;/h6&gt;

&lt;h6 id=&quot;12-inet-mc_index--multicast的device-index-为0&quot;&gt;12. inet-&amp;gt;mc_index ( Multicast的device index )为0&lt;/h6&gt;

&lt;h6 id=&quot;13-如果inet-inet_num--local-port-不为空&quot;&gt;13. 如果inet-&amp;gt;inet_num ( local port? )不为空,&lt;/h6&gt;

意味着该protocol允许并且已经在socket创建时指定local Port, 于是调用sk-&amp;gt;sk_prot-&amp;gt;hash(sk).

例如对于TCP, hash()指向net/ipv4/Tcp_ipv4.c的全局结构体struct proto tcp_prot中的inet_hash:

该方法按local Port计算hash key, 在&amp;amp;tcp_hashinfo-&amp;gt;listening_hash按hash key, 将struct sock插入tcp的listening_hash链表.

但是似乎这个hashtable元素只有32个, 为什么这么小? (待看)

&lt;h6 id=&quot;14-调用sk-sk_prot-init-例如对于tcp-指向netipv4tcp_ipv4c的全局结构体struct-proto-tcp_port中的tcp_v4_init_sock-此方法完成该socket在内核网络子系统tcp层的初始化&quot;&gt;14. 调用sk-&amp;gt;sk_prot-&amp;gt;init, 例如对于TCP, 指向net/ipv4/Tcp_ipv4.c的全局结构体struct proto tcp_port中的tcp_v4_init_sock, 此方法完成该socket在内核网络子系统TCP层的初始化:&lt;/h6&gt;

注意: TCP层有自己的数据结构struct tcp_sock, 从struct sock强转而来(下图图示中”tcp_sock specified”部分为struct tcp_sock的专有数据, 内存布局于common字段的后面, 故可以使用强制类型转换于struct sock互转)

&lt;ul&gt;
  &lt;li&gt;out_of_order_queue初始化 ( 该queue用于存放Out of order的segment )&lt;/li&gt;
  &lt;li&gt;初始化三个timer: 
retransmit_timer &amp;lt;-&amp;gt; net/ipv4/tcp_timer.c的tcp_write_timer
delack &amp;lt;-&amp;gt; net/ipv4/tcp_timer.c的tcp_delack_timer
keepalive &amp;lt;-&amp;gt; net/ipv4/tcp_timer.c的tcp_keepalive_timer&lt;/li&gt;
  &lt;li&gt;“Data for direct copy to user” ( 称为prequeue )的init ( 机制待细看 )&lt;/li&gt;
  &lt;li&gt;RTO ( Retransmit Time Out ) 初始化为1*HZ&lt;/li&gt;
  &lt;li&gt;mdev 初始化为1*HZ ( mdev是什么待细看 )&lt;/li&gt;
  &lt;li&gt;snd_cwnd初始化为10 ( Sending Congestion Window )&lt;/li&gt;
  &lt;li&gt;snd_ssthresh初始化为0x7fffffff ( 慢启动size threshold )&lt;/li&gt;
  &lt;li&gt;snd_cwnd_clamp初始化为~0 ( snd_cwnd的上限 )&lt;/li&gt;
  &lt;li&gt;mss_cache初始化为536U&lt;/li&gt;
  &lt;li&gt;reordering初始化为sysctl_tcp_reordering ( packet reording metric )&lt;/li&gt;
  &lt;li&gt;icsk-&amp;gt;icsk_ca_ops ( pluggable congestion control hook 可插拔的拥塞控制hook )&lt;/li&gt;
  &lt;li&gt;sk-&amp;gt;sk_state设为TCP_CLOSE
…&lt;/li&gt;
&lt;/ul&gt;

至此pf-&amp;gt;create调用结束, 也就是inet_create方法调用结束.

图示如下:

&lt;img src=&quot;https://raw.githubusercontent.com/johnhx/johnhx.github.io/master/img/socket_struct_init.png&quot; alt=&quot;Image of struct socket initialization&quot; /&gt;

&lt;h5 id=&quot;15-调用secutiry子系统的方法secutiry_ops-socket_post_create去security子系统折腾了一圈&quot;&gt;1.5 调用secutiry子系统的方法secutiry_ops-&amp;gt;socket_post_create去security子系统折腾了一圈;&lt;/h5&gt;

至此__sock_create调用结束.

至此socket系统调用中sock_create调用结束.

&lt;h3 id=&quot;二-调用sock_map_fd将struct-socket挂接到fd供进程使用&quot;&gt;二. 调用sock_map_fd将struct socket挂接到fd供进程使用.&lt;/h3&gt;

&lt;h4 id=&quot;1-调用sock_alloc_file-传入struct-socket-struct-file和flag&quot;&gt;1. 调用sock_alloc_file, 传入struct socket, struct file和flag&lt;/h4&gt;

将struct socket的file设为struct file;

将struct file的private_data设为struct socket;

这样struct socket和struct file便互相关联起来了.

&lt;img src=&quot;https://raw.githubusercontent.com/johnhx/johnhx.github.io/master/img/socket_file.png&quot; alt=&quot;Image of socket file&quot; /&gt;

&lt;h4 id=&quot;2-调用fd_install将file-descriptor加入fd-array&quot;&gt;2. 调用fd_install将file descriptor加入fd array&lt;/h4&gt;

至此, 整个socket系统调用结束.
</description>
        <pubDate>Mon, 01 May 2017 00:00:00 +0000</pubDate>
        <link>https://johnhx.github.io/2017/05/01/Socket-flow/</link>
        <guid isPermaLink="true">https://johnhx.github.io/2017/05/01/Socket-flow/</guid>
        
        <category>Linux内核</category>
        
        <category>Network</category>
        
        <category>Socket</category>
        
        
        <category>Linux内核</category>
        
      </item>
    
      <item>
        <title>逆向wininet.dll的过程</title>
        <description>
wininet.dll是Windows提供的通过HTTP, FTP等应用层协议访问网络资源的一个库。

提供的API例如InternetOpen, InternetConnect, HttpOpenRequest, HttpSendRequest等。

HINTERNET作为wininet提供给用户应用的句柄, HttpOpenRequest, HttpSendRequest发起请求均是通过HINTERNET句柄来完成.

这个HINTERNET并不是raw socket handler，而是wininet封装raw socket handler提供给用户应用使用的一个数据结构。

这个HINTERNET带来了很多不便:
&lt;ul&gt;
  &lt;li&gt;HINTERNET并没有暴露足够多的接口去获取所有的TCP连接信息;&lt;/li&gt;
  &lt;li&gt;即便HINTERNET提供了足够的接口, 但是这些接口并不是socket标准接口, 获取不到raw socket handler, 很多标准socket方法无法使用。&lt;/li&gt;
&lt;/ul&gt;

于是便萌生了想法, 逆向一下wininet.dll看有没有办法挖出HINTERNET和raw socket handler之间的关系, 从而获取到raw socket handler.

用WinDbg抓下wininet.dll的symbol, 用Visual Studio反汇编, 单步跟进HttpSendRequest方法.

发现HttpSendRequest调用了一个_mapHandleToAddress的内部方法.

从名字上看，这个内部方法很像是我要找的方法。

继续用Visual Studio的Assembly模式，跟进_mapHandleToAddress内部, 从汇编代码逻辑可以看到, 该方法接受HINTERNET为参数, 在一个全局数据结构中以HINTERNET作为key查找, 辗转调用其他几个内部方法获取了一个value.

这个value就是我要找的raw socket handler.

大体逆向出来的流程和细节如下：

&lt;ul&gt;
  &lt;li&gt;
    其中_mapHandleToAddress用ecx, esi接收HINTERNET参数, 返回值放在edx指向的内存单元(这个不同于常规函数, 并未使用eax存返回值);
  &lt;/li&gt;
  &lt;li&gt;
    用_mapHandleToAddress的返回值, 调用另外几个内部方法GetSourcePort, GetDestPort, GetSocket等, 其中GetSocket返回的便是我要找的raw socket handler
  &lt;/li&gt;
&lt;/ul&gt;

那用GetSocket的返回值能否调用标准的socket函数呢?

用如下代码验证一下:

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;HANDLE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hProcess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetCurrentProcess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DWORD&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cbNeeded&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;HMODULE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hMods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EnumProcessModules&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hProcess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hMods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hMods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cbNeeded&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cbNeeded&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HMODULE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;TCHAR&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;szModName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAX_PATH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;MODULEINFO&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;modinfo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
 
        &lt;span class=&quot;c1&quot;&gt;// Get module’s name.
&lt;/span&gt; 
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetModuleBaseName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hProcess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hMods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;szModName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;szModName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TCHAR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pModName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;szModName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TCHAR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)];&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iLength&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WideCharToMultiByte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CP_ACP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;szModName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;WideCharToMultiByte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CP_ACP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;szModName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pModName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iLength&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strcmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pModName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;WININET.dll&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetModuleInformation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hProcess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hMods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;modinfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;modinfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;LPVOID&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseOfDll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;modinfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lpBaseOfDll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                    
                    &lt;span class=&quot;c1&quot;&gt;// TODO: get entry point of MapHandleToAddress dynamically rather than hardcoding offset.
&lt;/span&gt;                    &lt;span class=&quot;n&quot;&gt;ULONG64&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entryOfMapHandleToAddress&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ULONG64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hMods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xB08F0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;PDWORD&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdwRet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DWORD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
 
                    &lt;span class=&quot;c1&quot;&gt;// TODO: get entry point of GetSourcePort dynamically rather than hardcoding offset.
&lt;/span&gt;                    &lt;span class=&quot;n&quot;&gt;ULONG64&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entryOfGetSourcePort&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ULONG64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hMods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x16EA39&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;PDWORD&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdwSourcePort&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DWORD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
 
                    &lt;span class=&quot;c1&quot;&gt;// TODO: get entry point of GetDestPort dynamically rather than hardcoding offset.
&lt;/span&gt;                    &lt;span class=&quot;n&quot;&gt;ULONG64&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entryOfGetDestPort&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ULONG64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hMods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x16E965&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;PDWORD&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdwDestPort&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DWORD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
 
                    &lt;span class=&quot;c1&quot;&gt;// TODO: get entry point of GetSocket dynamically rather than hardcoding offset.
&lt;/span&gt;                    &lt;span class=&quot;n&quot;&gt;ULONG64&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entryOfGetSocket&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ULONG64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hMods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x16EA03&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;PDWORD&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdwSocket&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DWORD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
 
                    &lt;span class=&quot;kr&quot;&gt;__asm&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hFile&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;esi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hFile&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;edx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdwRet&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entryOfMapHandleToAddress&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;sub&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;esp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x04&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* Not sure why MapHandleToAddress doesn't restore esp correctly, manually restore it */&lt;/span&gt;
 
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdwRet&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entryOfGetDestPort&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdwDestPort&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dword&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;eax&lt;/span&gt;
 
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdwRet&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entryOfGetSourcePort&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdwSourcePort&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dword&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;eax&lt;/span&gt;
 
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdwRet&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ecx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entryOfGetSocket&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pdwSocket&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;mov&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dword&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ebx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;eax&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
                    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;source port: %d, destination port: %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pdwSourcePort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pdwDestPort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 
                    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr_in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;PWSTR&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serv_ip&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;wchar_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;PWSTR&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guest_ip&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;wchar_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
                    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serv_len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guest_len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;memset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serv_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;memset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guest_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getsockname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pdwSocket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serv_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getpeername&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pdwSocket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guest_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ret1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                        &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ServAddrName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NI_MAXHOST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
                        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getnameinfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LPSOCKADDR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guest_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ServAddrName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ServAddrName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NI_NUMERICHOST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;strcpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ServAddrName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;unknown&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
                        &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ClientAddrName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NI_MAXHOST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
                        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getnameinfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LPSOCKADDR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serv_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ClientAddrName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ClientAddrName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NI_NUMERICHOST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;strcpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ServAddrName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;unknown&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
                        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;server ip: %s, client ip: %s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ServAddrName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ClientAddrName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

可见, 通过调用GetSocket内部方法返回的pdwSocket, 用来调用getpeername, getsockname这样的标准socket函数, 能得到正确的结果.

调试汇编的过程有一些trick, 都在代码的注释中体现了.
</description>
        <pubDate>Sat, 29 Apr 2017 00:00:00 +0000</pubDate>
        <link>https://johnhx.github.io/2017/04/29/First-Reverse/</link>
        <guid isPermaLink="true">https://johnhx.github.io/2017/04/29/First-Reverse/</guid>
        
        <category>逆向</category>
        
        <category>开发</category>
        
        
        <category>开发</category>
        
      </item>
    
      <item>
        <title>一个基于github的远程团队协作开发模型</title>
        <description>
画这张图，一是想给新手们讲清楚git的一些基本操作，二是想描述一下我们现在正在用的基于github的协作开发模型。

“Local of $user”是某位成员的本地环境, 涉及到最基本的git操作, 无须赘述。

“Remote”是github上项目的布局情况, 包括主repo, 和每个成员各自fork的repo, 其中主repo的master随时处于shippable的状态, 通过github的release tag可以打”DROP-xxx”的tag生成release.

&lt;img src=&quot;https://raw.githubusercontent.com/johnhx/johnhx.github.io/master/img/github_base_model.png&quot; alt=&quot;Image of GitHub Dev Model&quot; /&gt;
</description>
        <pubDate>Fri, 28 Apr 2017 00:00:00 +0000</pubDate>
        <link>https://johnhx.github.io/2017/04/28/Classical-Git/</link>
        <guid isPermaLink="true">https://johnhx.github.io/2017/04/28/Classical-Git/</guid>
        
        <category>GitHub</category>
        
        <category>开发</category>
        
        <category>模型</category>
        
        
        <category>开发</category>
        
      </item>
    
      <item>
        <title>JavaScript的Generator和自动执行</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#thunk函数&quot; id=&quot;markdown-toc-thunk函数&quot;&gt;thunk函数&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#generator函数&quot; id=&quot;markdown-toc-generator函数&quot;&gt;Generator函数&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#generator自动执行&quot; id=&quot;markdown-toc-generator自动执行&quot;&gt;Generator自动执行&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#使用promise代替thunk函数&quot; id=&quot;markdown-toc-使用promise代替thunk函数&quot;&gt;使用Promise代替thunk函数&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;thunk函数&quot;&gt;thunk函数&lt;/h2&gt;

thunk函数的定义：能将执行结果传入回调函数，并将该回调函数返回的函数。(呃, 太抽象了…)

通常拿readFile来举例。下面是一个名为readFile的thunk函数:
&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

readFile如何调用和执行呢？如下:
&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'./package.json'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)((&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

可以看到, 在调用readFile的thunk函数后, 异步操作返回结果的获取权, 被交给了thunk函数的调用者。

这便是thunk函数的关键优势: 它将异步操作返回结果的获取权交给了thunk函数的返回值, 而不是把异步操作返回结果的获取权留在thunk函数本身的作用域内。

这一点优势很重要, 能结合Generator语法让Generator函数自动执行。

&lt;h2 id=&quot;generator函数&quot;&gt;Generator函数&lt;/h2&gt;

Generator是从ECMAScript6里引入的新概念。

一个Generator函数示例:

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;readFiles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;r1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'./package.json'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;r2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'./pom.xml'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

如果要调用这个Generator函数使其执行完毕并获取结果, 可以用下面的代码:

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;readFiles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;r1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;r2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;r2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;er&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

可以看到, 上面的代码有回调函数嵌套, 而且代码有很多重复.
Generator自动执行应运而生。

&lt;h2 id=&quot;generator自动执行&quot;&gt;Generator自动执行&lt;/h2&gt;

Generator自动执行器的核心代码如下:

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

其核心逻辑是一个递归, 最终是将Generator函数中所有的yield都执行完毕.

调用就是简单的一行:

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readFiles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

可见, 比起直接回调函数嵌套调用readFiles简单直观。

&lt;h3 id=&quot;使用promise代替thunk函数&quot;&gt;使用Promise代替thunk函数&lt;/h3&gt;

第一部分的readFile thunk函数可以改写为基于Promise的方式:

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fileName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;reject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fileName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;nx&quot;&gt;reject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;nx&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

和第一部分的thunk函数相比大同小异, 只是把函数返回值的获取权以Promise的方式交出.

https://github.com/BriteSnow/node-async6 实现了Generator自动执行器.
</description>
        <pubDate>Fri, 21 Apr 2017 00:00:00 +0000</pubDate>
        <link>https://johnhx.github.io/2017/04/21/Generator/</link>
        <guid isPermaLink="true">https://johnhx.github.io/2017/04/21/Generator/</guid>
        
        <category>JavaScript</category>
        
        <category>Generator</category>
        
        <category>自动执行</category>
        
        
        <category>JavaScript</category>
        
      </item>
    
  </channel>
</rss>
