lenga 发表于 2015-11-19 15:38:44

keepalived源码浅析——sock

  sock.h 源码
  

#ifndef _SOCK_H
#define _SOCK_H
/* system includes */
#include <openssl/ssl.h>//需包含openssl库
#include <openssl/md5.h>
/* Engine socket pool element structure *///sock 池 元素的结构体
typedef struct {
int fd;
SSL *ssl;
BIO *bio;
MD5_CTX context;
int status;
int lock;
char *buffer;
char *extracted;
int size;
int total_size;
} SOCK;
/* global vars exported *///全局变量
extern SOCK *sock;
/* Prototypes */
extern void free_sock(SOCK *);
extern void init_sock(void);
#endif


  


  sock.c源码

#include <string.h>
#include &quot;memory.h&quot;
#include &quot;utils.h&quot;
#include &quot;list.h&quot;
#include &quot;sock.h&quot;
#include &quot;layer4.h&quot;
#include &quot;ssl.h&quot;
#include &quot;main.h&quot;
/* global var */    //全局变量 SOCK指针
SOCK *sock = NULL;
/* Close the descriptor */ //关闭sock 如果有ssl句柄则关闭销毁
static void
close_sock(SOCK * sock_obj)
{
if (sock_obj->ssl) {
SSL_shutdown(sock_obj->ssl);
SSL_free(sock_obj->ssl);
}
close(sock_obj->fd);
}
/* Destroy the socket handler */ //销毁socket句柄
void
free_sock(SOCK * sock_obj)
{
DBG(&quot;Freeing fd:%d\n&quot;, sock_obj->fd);
close_sock(sock_obj);
FREE(sock_obj);
}
/* Init socket handler */ //sock句柄的初始化
void
init_sock(void)
{
sock = (SOCK *) MALLOC(sizeof (SOCK));
memset(sock, 0, sizeof (SOCK));
thread_add_event(master, tcp_connect_thread, sock, 0);
}

  

调用位置:
  main.c 中

/* Register the GET request */
init_sock();

  



Scheduler.c中
  



/* Add simple event thread. */

thread_t *
thread_add_event(thread_master_t * m, int (*func) (thread_t *)
, void *arg, int val)
{
thread_t *thread;
assert(m != NULL);
thread = thread_new(m);
thread->type = THREAD_EVENT;
thread->id = 0;
thread->master = m;
thread->func = func;
thread->arg = arg;
thread->u.val = val;
thread_list_add(&m->event, thread);
return thread;
}


其中 /* global vars */
thread_master_t *master = NULL;


  





版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: keepalived源码浅析——sock