linux下c语言实现pop3,C语言ssl 连接pop3登录收信模拟

/* OpenSSL headers */

#include #include #include #include #include #include #include #include

int main()

{

/* Initializing OpenSSL */

SSL_load_error_strings();

ERR_load_BIO_strings();

OpenSSL_add_all_algorithms();

SSL_library_init();

int len;

BIO *bio;

char *buf = "user lailaigq\n";

char *buf1 = "pass XXXX\n";

char *buf3 ="list\n";

char *buf4 ="uidl 1\n";

len = strlen(buf);

char buf2[1024];

char buf11[1024];

SSL_CTX  *ctx=SSL_CTX_new(TLSv1_client_method());    //新建ssl认证方法

SSL *ssl;

if(!SSL_CTX_load_verify_locations(ctx,"/etc/pki/tls/certs/ca-bundle.crt",NULL))  //加载所有证书进行验证

{

/* Handle failed load here */

printf("Error: %s\n",ERR_reason_error_string(ERR_get_error()));   //加载所有证书是否正常

}

bio = BIO_new_ssl_connect(ctx);     //设置BIO 对像

BIO_get_ssl(bio, &ssl);

SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);

/* Attempt to connect */

//设置连接

BIO_set_conn_hostname(bio,"72.14.213.109:995");

/* Verify the connection opened and perform the handshake */

//验证连接是否打开,而且执行握手情况

if(BIO_do_connect(bio) <= 0)   //连接失败

{

/* Handle failed connection */

printf("Error: %s\n",ERR_reason_error_string(ERR_get_error()));   //加载所有证书是否正常

return 0;

}

//检查证书是否有效

if(SSL_get_verify_result(ssl) != X509_V_OK)

{

/* Handle the failed verification */

printf("Error: %s\n",ERR_reason_error_string(ERR_get_error()));   //加载所有证书是否正常

return 0;

}

//写入到连接

if(BIO_write(bio, buf, len) <= 0)

{

if(! BIO_should_retry(bio))

{

/* Handle failed write here */

printf("Handle failed write here");

}

/* Do something to handle the retry */

}

//从连接读取

int x = BIO_read(bio, buf2, 1024);

if(x == 0)

{

/* Handle closed connection */

return 0;

}

else if(x < 0)

{

if(! BIO_should_retry(bio))

{

/* Handle failed read here */

printf("Handle failed read here ");

}

/* Do something to handle the retry */

}else

{

printf("result:%s\n",buf2);

}

//写入到连接

if(BIO_write(bio, buf1, strlen(buf1)) <= 0)

{

if(! BIO_should_retry(bio))

{

/* Handle failed write here */

printf(" Handle failed write here");

}

/* Do something to handle the retry */

}

//从连接读取

x = BIO_read(bio, buf2, 1024);

if(x == 0)

{

/* Handle closed connection */

return 0;

}

else if(x < 0)

{

if(! BIO_should_retry(bio))

{

/* Handle failed read here */

printf("Handle failed read  here ");

}

/* Do something to handle the retry */

return 0;

}

else

{

printf("result:%s\n",buf2);

}

//写入到连接

if(BIO_write(bio,buf3, strlen(buf3)) <= 0)

{

if(! BIO_should_retry(bio))

{

/* Handle failed write here */

printf(" Handle failed write here");

}

/* Do something to handle the retry */

}

//从连接读取

x = BIO_read(bio, buf11,1024);

if(x == 0)

{

/* Handle closed connection */

return 0;

}

else if(x < 0)

{

if(! BIO_should_retry(bio))

{

/* Handle failed read here */

}

/* Do something to handle the retry */

return 0;

}

else

{

printf("result:%s\n",buf11);

}

//写入到连接

if(BIO_write(bio,buf4, strlen(buf4)) <= 0)

{

if(! BIO_should_retry(bio))

{

/* Handle failed write here */

printf(" Handle failed write here");

}

/* Do something to handle the retry */

}

//从连接读取

x = BIO_read(bio, buf11,1024);

if(x == 0)

{

/* Handle closed connection */

return 0;

}

else if(x < 0)

{

if(! BIO_should_retry(bio))

{

/* Handle failed read here */

}

/* Do something to handle the retry */

return 0;

}

else

{

printf("result:%s\n",buf11);

}

SSL_CTX_free(ctx);

BIO_free_all(bio);

return 1;

}

Makefile 如下:

project = testssl

$(project):$(project).c

gcc -Wall -o $@ -lssl  $<

编译

make

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值