该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
void *sock_read(int *client) //short event,void *arg
{
char buf[1024];
memset(buf,0,1024);
int d=SSL_read(*client,buf,1024);//接收客户端的数据包
perror("SSL_read");
printf("d=%d\n",d);
·····
}
static void sock_accept(int server_socket,short event,void *arg)
{
struct event *ev=arg;
int new_server_socket;
char sslbuff[MAXBUF +1];
SSL_CTX *ctx;
RSA *rsa;
X509 * client_cert;
char *str;
SSL_load_error_strings();//为打印调试信息做准备
SSL_library_init();//ssl初始化
OpenSSL_add_all_algorithms();//载入ssl算法
SSL_load_error_strings();//载入ssl错误信息
//这里不写了,太多字数限制,就是载入ssl算法和加入证书、验证私钥那些,这里应该没错的;
while(1)
{
SSL *ssl;
struct sockaddr_in addr;
socklen_t len=sizeof(addr);
//由于此结构要长期使用,所以rev必须动态分配,否则离开此函数后会自动释放,导致segment fault
struct event *rev=(struct event *)malloc(sizeof(struct event));
new_server_socket=accept(server_socket,(struct sockaddr *)&addr,&len);
ssl=SSL_new(ctx);
SSL_set_fd(ssl,new_server_socket);
// 建立 SSL 连接
if (SSL_accept(ssl)==-1)
{
close(new_server_socket);
break;
}
client_cert = SSL_get_peer_certificate (ssl); //得到证书并打印信息
if (client_cert != NULL)
{
str = X509_NAME_oneline (X509_get_subject_name (client_cert), 0, 0);
if(str==NULL)
{
exit(1);
}
free (str);
str = X509_NAME_oneline (X509_get_issuer_name (client_cert), 0, 0);
if(str==NULL)
{
exit(1);
}
free (str);
X509_free (client_cert);/*如不再需要,需将证书释放 */
}
else
{
printf ("client does not have certificate.\n");
}
pthread_t child_thread;
pthread_mutex_init(&mutex,NULL);//初始化
if((pthread_create(&child_thread,NULL,(void *)sock_read,(void *)&ssl))<0)//* talk_to_client
if(child_thread!=0)
{
pthread_join(child_thread,NULL);
}
//创建一个读事件,当有客户连接时,接收通知。
event_set(rev,new_server_socket,EV_READ,(void*)sock_read,rev);
event_add(rev,NULL);
event_add(ev,NULL);
printf("end sock_accept\n");
free(rev);
// 关闭 SSL 连接
SSL_shutdown(ssl);
SSL_free(ssl);
}
SSL_CTX_free (ctx);
close(new_server_socket);
}
程序中进行的是ssl accept之后参数的传递,但是在最上顶的
int d=SSL_read(*client,buf,1024);//接收客户端的数据包处总是提示:
warning: passing argument 1 of ‘SSL_write’ makes pointer from integer without a cast
类型不匹配,我将int d=SSL_read(*client,buf,1024);改成int d=SSL_read((SSL *)client,buf,1024);会收不到数据,d=-1,但是 perror显示的是succced,
请问,怎么改?谢谢!
我看过关于openssl不能用于多线程的资料,但是我能不能换种方式,将ssl指针换成其他指针来传递,然后将ssl加入到新指针里边,比如这样SSL_set_fd(ssl,new_server_socket);
参数传递的时候传递新的指针,这样还会起到加密的作用吗
我按照跟下面的方法试过了
int readFromSSLSocket(SSL *ssl,char *buf,int bufsize,int length_to_read)
{
int nread;
memset(buf,'\0',bufsize);
nread = SSL_read(ssl,buf,length_to_read);
return nread;
}
或者
void *sock_read(void *client)
int d=SSL_read((SSL *)client,buf,1024);
结果都一样,收不到数据,SSL_read的返回值都是-1
但是我用perror("SSL_read");打出结果,发现状态是Success,这是怎么回事?