锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
銆銆銆榪欐牱浼拌鍙互鍦ㄤ竴瀹氱▼搴︿笂瑙e喅榪欎釜闂錛屼笉榪囪繖涔熸槸涓涓繪剰錛?br>銆銆浜岋紝媯嫻嬪強蹇呰鏃惰嚜鍔ㄥ悓姝ュ鎴風涓庢湇鍔″櫒鐨勬椂闂?br>銆銆閫氳繃鐢╳ireshake鎶撳寘鍒嗘瀽SSL寤虹珛榪炴帴鐨勮繃紼嬶紝鍙戠幇鍦⊿SL鎻℃墜榪囩▼涓紝浼氬悜瀵規柟浼犻佹湰鏈虹殑緋葷粺鏃墮棿錛庡洜姝や竴涓樉鑰屾槗瑙佺殑鍔炴硶灝辨槸鑾峰彇瀵規柟鐨勬椂闂達紝鐒跺悗鍦ㄥ繀瑕佹椂灝嗘湰鏈虹殑緋葷粺鏃墮棿鏀逛負瀵規柟鐨勭郴緇熸椂闂達紝澶辮觸鍚庡啀榪炰竴嬈★紟涓嬮潰鏄叿浣撶殑紺轟緥浠g爜錛?br>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <winsock2.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
typedef struct _TimeInfo
{
time_t client; /*瀹㈡埛绔殑鏃墮棿*/
time_t server; /*鏈嶅姟鍣ㄧ殑鏃墮棿*/
} TimeInfo;
/**
* 鍚屾緋葷粺鏃墮棿.
*/
BOOL syncSystemTime(time_t t)
{
SYSTEMTIME st;
FILETIME ft;
LONGLONG ll;
ll = Int32x32To64(t, 10000000) + 116444736000000000; //1970.01.01
ft.dwLowDateTime = (DWORD)ll;
ft.dwHighDateTime = (DWORD)(ll >> 32);
return FileTimeToSystemTime(&ft, &st) && SetSystemTime(&st);
}
/**
* 鑾峰彇SSL鎻℃墜榪囩▼涓湇鍔″櫒涓庡鎴風鍙屾柟鐨勭郴緇熸椂闂?
*/
void getSSLHandleShakeTimeInfo(int write_p,
int version,
int content_type,
const unsigned char* buf,
size_t len,
SSL *ssl,
TimeInfo *ti)
{
if(content_type != 22) //require handshake message
return;
if(len < 42)
return;
if(buf[0] == 1) //ClientHello Message send from client to server
ti->client = htonl(*((u_long*)(buf + 6)));
else if(buf[0] == 2) //ServerHello Message send from server to client
ti->server = htonl(*((u_long*)(buf + 6)));
else
return;
}
int main()
{
BIO * bio;
SSL * ssl;
SSL_CTX * ctx;
TimeInfo timeInfo = {-1, -1};
BOOL timeSynced = FALSE;
long result;
/* Set up the library */
SSL_library_init();
ERR_load_BIO_strings();
SSL_load_error_strings();
/* Set up the SSL context */
ctx = SSL_CTX_new(SSLv3_client_method());
if(ctx == NULL)
{
fprintf(stderr, "Error new SSL_CTX\n");
ERR_print_errors_fp(stderr);
SSL_CTX_free(ctx);
return 0;
}
/* Get Server and Client system time via SSL Handshake */
SSL_CTX_set_msg_callback(ctx, getSSLHandleShakeTimeInfo);
SSL_CTX_set_msg_callback_arg(ctx, &timeInfo);
/* Load the trust store */
if(! SSL_CTX_load_verify_locations(ctx, ".\\certs\\cacert.pem", NULL))
{
fprintf(stderr, "Error loading trust store\n");
ERR_print_errors_fp(stderr);
SSL_CTX_free(ctx);
return 0;
}
/* Setup the connection */
bio = BIO_new_ssl_connect(ctx);
/* Set the SSL_MODE_AUTO_RETRY flag */
BIO_get_ssl(bio, & ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
/* Create and setup the connection */
BIO_set_conn_hostname(bio, "192.168.1.5:5555");
if(BIO_do_connect(bio) <= 0)
{
fprintf(stderr, "Error attempting to connect\n");
ERR_print_errors_fp(stderr);
BIO_free_all(bio);
SSL_CTX_free(ctx);
return 0;
}
/* Check the certificate */
switch(SSL_get_verify_result(ssl))
{
case X509_V_OK:
break;
case X509_V_ERR_CERT_NOT_YET_VALID:
case X509_V_ERR_CERT_HAS_EXPIRED:
if(timeInfo.server != -1 && timeInfo.client != -1)
{
printf("褰撳墠瀹㈡埛绔椂闂? %s", ctime(&timeInfo.client));
printf("褰撳墠鏈嶅姟鍣ㄦ椂闂? %s", ctime(&timeInfo.server));
printf("灝濊瘯涓庢湇鍔″櫒鏃墮棿鍚屾");
if(syncSystemTime(timeInfo.server))
printf("鎴愬姛\n");
else
printf("澶辮觸\n");
printf("璇烽噸璇曡繛鎺ユ湇鍔″櫒錛乗n");
}
default:
fprintf(stderr, "Certificate verification error: %i\n", SSL_get_verify_result(ssl));
BIO_free_all(bio);
SSL_CTX_free(ctx);
return 0;
}
/* Close the connection and free the context */
BIO_free_all(bio);
SSL_CTX_free(ctx);
return 0;
}
]]>
;; c/c++ header include guard
(defun insert-include-guard ()
"insert include guard for c and c++ header file.
for file filename.ext will generate:
#ifndef FILENAME_EXT_
#define FILENAME_EXT_
original buffer content
#endif//FILENAME_EXT_
"
(interactive)
(setq file-macro
(concat (replace-regexp-in-string "\\." "_" (upcase (file-name-nondirectory buffer-file-name))) "_"))
(setq guard-begin (concat "#ifndef " file-macro "\n"
"#define " file-macro "\n\n"))
(setq guard-end
(concat "\n\n#endif//" file-macro "\n"))
(setq position (point))
(goto-char (point-min))
(insert guard-begin)
(goto-char (point-max))
(insert guard-end)
(goto-char (+ position (length guard-begin))))
]]>