各位前辈,本科三年级的一次有关 VPN 的 lab,下面是主函数
int main(int argc, char * argv[]) {
SSL_METHOD *meth;
SSL_CTX* ctx;
SSL *ssl;
int err;
// Step 0: OpenSSL library initialization
// This step is no longer needed as of version 1.1.0.
SSL_library_init();
SSL_load_error_strings();
SSLeay_add_ssl_algorithms();
// Step 1: SSL context initialization
meth = (SSL_METHOD *)TLSv1_2_method();
ctx = SSL_CTX_new(meth);
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
// Step 2: Set up the server certificate and private key
SSL_CTX_use_certificate_file(ctx, "./cert_server/server.crt", SSL_FILETYPE_PEM);
SSL_CTX_use_PrivateKey_file(ctx, "./cert_server/server.key", SSL_FILETYPE_PEM);
// Step 3: Create a new SSL structure for a connection
ssl = SSL_new(ctx);
int tunfd, sockfd;
tunfd = createTunDevice();
int listen_sock = initTCPServer();
struct sockaddr_in sa_client;
size_t client_len;
pthread_t t;
if(pthread_create(&t, NULL, get_tunfd, (void *)(&tunfd))==-1){
printf("pthread create dispath");
}
while (1) {
sockfd = accept(listen_sock, (struct sockaddr*)&sa_client, &client_len);
free_malloc();
*****************************************************
struct tun_route* temp=tun_route; //当注释掉这个语句后,
******************************************************
if (fork() == 0) { // The child process
close(listen_sock);
SSL_set_fd(ssl, sockfd);
********************************************************
err = SSL_accept(ssl); //返回正确连接,否则返回错误指令值-1
********************************************************
CHK_SSL(err);
printf("SSL connection established!\n");
// -----login-----
int login = Authentication(ssl);
// Enter the main loop
while (login & 1) {
fd_set readFDSet;
FD_ZERO(&readFDSet);
FD_SET(SSL_get_fd(ssl), &readFDSet);
FD_SET(tunfd, &readFDSet);
select(FD_SETSIZE, &readFDSet, NULL, NULL, NULL);
if (FD_ISSET(SSL_get_fd(ssl), &readFDSet)) socketSelected(tunfd, ssl);
if (FD_ISSET(tunfd, &readFDSet)) tunSelected(tunfd, ssl);
}
SSL_shutdown(ssl);
SSL_free(ssl);
close(sockfd);
return 0;
}
else {
close(sockfd);
}
}
return 0;
}
当我注释掉上面struct tun_route* temp=tun_route; 后,子进程中的 SSL_accept()返回正确值,当我加上这句后,SSL_accept 便返回一个错误指令-1。我不知道这是什么原因。其中 tun_route 的定义与其他的没有联系:
struct tun_route{
pid_t pid;
int client_ip;
int fd[2];
struct tun_route* next;
};
1
Erichailong OP ok,重启电脑后,就不报错,但不知道什么原理
|