Home >Backend Development >PHP Tutorial >'In-depth understanding of Nginx' notes: ngx_mail related structures

'In-depth understanding of Nginx' notes: ngx_mail related structures

WBOY
WBOYOriginal
2016-07-30 13:30:331203browse

Nginx mail related module structure

ngx_mail_module_t

This is the abstract interface of the mail module, that is, the mail module-specific interface pointed to by the ctx member in ngx_module_t. Each mail module implements its own ngx_mail_module_t structure.

<code><span>typedef</span><span>struct</span> {
     <span>// POP3 STMP IMAP邮件模块提取出的通用接口</span>
    ngx_mail_protocol_t        *protocol;

     <span>// 用于创建main级别配置项的结构体</span><span>void</span>                       *(*create_main_conf)(ngx_conf_t *cf);

    <span>// 解析完main级别配置项后被回调的函数</span><span>char</span>                       *(*init_main_conf)(ngx_conf_t *cf, <span>void</span> *conf);

     <span>// 用于创建srv级别配置项的结构体</span><span>void</span>                       *(*create_srv_conf)(ngx_conf_t *cf);

    <span>// 根据具体模块处理srv下和main下同名的配置项</span><span>char</span>                       *(*merge_srv_conf)(ngx_conf_t *cf, <span>void</span> *prev,
                                      <span>void</span> *conf);
} ngx_mail_module_t;</code>

ngx_protocol_s

<code><span>typedef</span><span>struct</span> ngx_mail_protocol_s ngx_mail_protocol_t;

<span>// 四个POP3 SMTP IMAP等应用级别的邮件模块所需要实现的接口方法</span><span>typedef</span><span>void</span> (*ngx_mail_init_session_pt)(ngx_mail_session_t *s,
    ngx_connection_t *c);
<span>typedef</span><span>void</span> (*ngx_mail_init_protocol_pt)(ngx_event_t *rev);
<span>typedef</span><span>void</span> (*ngx_mail_auth_state_pt)(ngx_event_t *rev);
<span>typedef</span> ngx_int_t (*ngx_mail_parse_command_pt)(ngx_mail_session_t *s);


<span>struct</span> ngx_mail_protocol_s {
     <span>// 邮件模块名称</span>
    ngx_str_t                   name;

    <span>// 当前邮件模块中所要监听的最常用4个端口</span>
    in_port_t                   port[<span>4</span>];

    <span>// 邮件模块类型</span>
    ngx_uint_t                  type;

     <span>// 与客户端建立起TCP连接后的初始化方法</span>
    ngx_mail_init_session_pt    init_session;

    <span>// 接收、解析客户端请求的方法</span>
    ngx_mail_init_protocol_pt   init_protocol;

    <span>// 解析客户端邮件协议的接口方法</span>
    ngx_mail_parse_command_pt   parse_command;
    ngx_mail_auth_state_pt      auth_state;

     <span>// 当处理中没有遇到错误时,返回internal_server_error指定的响应给客户端</span>
    ngx_str_t                   internal_server_error;
    ngx_str_t                   cert_error;
    ngx_str_t                   no_cert;
};</code>

ngx_mail_session_t

After Nginx establishes a TCP connection with the client, it will call back the ngx_mail_init_connection function to initialize the email protocol. At this time, a core structure similar to ngx_http_request_t in HTTP requests will be created: ngx_mail_session_s.

<code><span>typedef</span><span>struct</span> {
    uint32_t                signature;         <span>/* "MAIL" */</span><span>// 下游客户端和Nginx之间的连接</span>
    ngx_connection_t       *connection;

     <span>// 可存需要向下游客户端发送的内容</span>
    ngx_str_t               out;

    <span>// 用于接收来自客户端的请求</span>
    ngx_buf_t              *buffer;

     <span>// 指向一个指针数组,保存着这个请求中各个邮件模块的上下文建构体指针</span><span>void</span>                  **ctx;

    <span>// main级别配置结构体组成的指针数组</span><span>void</span>                  **main_conf;

    <span>// srv级别配置结构体组成的指针数组</span><span>void</span>                  **srv_conf;

     <span>// 解析主机域名</span>
    ngx_resolver_ctx_t     *resolver_ctx;

     <span>// proxy上下文,用于Nginx双向透传客户端与邮件服务器间的通信</span>
    ngx_mail_proxy_ctx_t   *proxy;

     <span>// 表示与邮件服务器交互时,当前处于哪种状态</span>
    ngx_uint_t              mail_state;

     <span>// 邮件协议类型</span><span>unsigned</span>                protocol:<span>3</span>;

    <span>// 1:表示当前读或写操作需要被阻塞</span><span>unsigned</span>                blocked:<span>1</span>;

    <span>// 1:请求需要结束</span><span>unsigned</span>                quit:<span>1</span>;

    <span>// 一下三个标志位仅在解析具体邮件协议时由邮件框架使用</span><span>unsigned</span>                quoted:<span>1</span>;
    <span>unsigned</span>                backslash:<span>1</span>;
    <span>unsigned</span>                no_sync_literal:<span>1</span>;

    <span>// 当使用SSL协议时才有意义</span><span>unsigned</span>                starttls:<span>1</span>;
    <span>unsigned</span>                esmtp:<span>1</span>;

    <span>// 表示与认证服务器交互时的记录认证方式</span><span>unsigned</span>                auth_method:<span>3</span>;

    <span>// 1:表示认证服务器要求暂缓接收响应,Nginx会继续等待认证服务器的后续响应</span><span>unsigned</span>                auth_wait:<span>1</span>;

     <span>// 验证时的用户名</span>
    ngx_str_t               login;

    <span>// 验证时的密码</span>
    ngx_str_t               passwd;

     <span>// 作为Auth-Salt验证的信息</span>
    ngx_str_t               salt;

    <span>// 一下三个成员仅用于IMAP通信</span>
    ngx_str_t               tag;
    ngx_str_t               tagged_line;
    ngx_str_t               text;

     <span>// 当前连接上对应的Nginx服务器地址</span>
    ngx_str_t              *addr_text;

    <span>// 主机地址</span>
    ngx_str_t               host;

    <span>//一下四个成员仅用于SMTP通信</span>
    ngx_str_t               smtp_helo;
    ngx_str_t               smtp_from;
    ngx_str_t               smtp_to;

    ngx_str_t               cmd;

     <span>// 在于邮件服务器交互时,表示解析自邮件服务器的消息类型</span>
    ngx_uint_t              command;

    <span>// 存放来自下游客户端的邮件协议中的参数</span>
    ngx_array_t             args;

     <span>// 当前请求尝试访问服务器验证的次数</span>
    ngx_uint_t              login_attempt;

    <span>/* used to parse POP3/IMAP/SMTP command */</span>    ngx_uint_t              state;
    u_char                 *cmd_start;
    u_char                 *arg_start;
    u_char                 *arg_end;
    ngx_uint_t              literal_len;
} ngx_mail_session_t;</code>

Copyright Statement: Pain is just in your mind.

The above introduces the ngx_mail related structure in the notes of "In-depth Understanding of Nginx", including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP generate PDF fileNext article:PHP generate PDF file