2014-08-31 50 views
0
之後啓動SSL

我需要設置一個TCP套接字並向服務器發送消息,然後與服務器進行SSL協商並在SSL中進行後續數據交換。我需要使用libevent來實現這個功能,因爲它沒有阻塞,所以我可以同時有很多連接。libevent,在

我可以得到的連接設置和清晰的文本交換一些數據:

struct bufferevent *bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE); 
struct sockaddr_in addr; 
//fill the addr with ip and port 
bufferevent_socket_connect(bev, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)); 
//send data 
evbuffer_add(bufferevent_get_output(bev), data, len); 

的問題是,libevent的似乎只有一個API來完成SSL,但它假定bufferevent對象尚未創建。

bufferevent_openssl_socket_new 

感謝您的任何建議。

回答

2

找到解決方法,釋放現有的(明文)並創建一個新的。訣竅是在破壞bufferevent之前保存舊的fd。

//save the fd in the old bufferevent (cleartext) 
fd = bufferevent_getfd(cb->bev); 
bufferevent_setfd(cb->bev, -1); 
bufferevent_free(cb->bev); 

bev = bufferevent_openssl_socket_new(base, fd, ssl, 
    BUFFEREVENT_SSL_CONNECTING, 
    BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS); 
0

您可以嘗試使用bufferevent_openssl_filter_new()直接包裝已有bufferevent,而不是創建原始套接字fd

/** * Create a new SSL bufferevent to send its data over another bufferevent. * * @param base An event_base to use to detect reading and writing. It * must also be the base for the underlying bufferevent. * @param underlying A socket to use for this SSL * @param ssl A SSL* object from openssl. * @param state The current state of the SSL connection * @param options One or more bufferevent_options * @return A new bufferevent on success, or NULL on failure */ struct bufferevent * bufferevent_openssl_filter_new(struct event_base *base, struct bufferevent *underlying, struct ssl_st *ssl, enum bufferevent_ssl_state state, int options);

另一個bufferevent