b.liu | d440f9f | 2025-04-18 10:44:31 +0800 | [diff] [blame] | 1 | /*-----------------------------------------------------------------------------------------------*/
|
| 2 | /**
|
| 3 | @file NULL
|
| 4 | @brief libssl.so.3 function test
|
| 5 | */
|
| 6 | /*-----------------------------------------------------------------------------------------------*/
|
| 7 |
|
| 8 | /*-------------------------------------------------------------------------------------------------
|
| 9 | Copyright (c) 2024 mobiletek Wireless Solution, Co., Ltd. All Rights Reserved.
|
| 10 | mobiletek Wireless Solution Proprietary and Confidential.
|
| 11 | -------------------------------------------------------------------------------------------------*/
|
| 12 |
|
| 13 | /*-------------------------------------------------------------------------------------------------
|
| 14 | EDIT HISTORY
|
| 15 | This section contains comments describing changes made to the file.
|
| 16 | Notice that changes are listed in reverse chronological order.
|
| 17 | $Header: $
|
| 18 | when who what, where, why
|
| 19 | -------- --------- -----------------------------------------------------------------
|
| 20 | 20250409 yq.wang Created .
|
| 21 | -------------------------------------------------------------------------------------------------*/
|
| 22 | #include <stdio.h>
|
| 23 | #include <stdlib.h>
|
| 24 | #include <string.h>
|
| 25 | #include <unistd.h>
|
| 26 | #include <pthread.h>
|
| 27 | #include <sys/socket.h>
|
| 28 | #include <netinet/in.h>
|
| 29 | #include <arpa/inet.h>
|
| 30 |
|
| 31 | #ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
|
| 32 | #include "mbtk_openssl.h"
|
| 33 |
|
| 34 | #define BUFFER_SIZE 1024
|
| 35 |
|
| 36 | void* recv_thread(void *arg)
|
| 37 | {
|
| 38 | mbtk_openssl_info_s *inter_info = (mbtk_openssl_info_s *)(intptr_t)arg;
|
| 39 | char buffer[BUFFER_SIZE + 1] = {0};
|
| 40 |
|
| 41 | while(1)
|
| 42 | {
|
| 43 | memset(buffer, 0x00, BUFFER_SIZE + 1);
|
| 44 | ssize_t len = mbtk_openssl_read(inter_info->ssl, buffer, BUFFER_SIZE);
|
| 45 | if(len <= 0)
|
| 46 | {
|
| 47 | if(len == 0)
|
| 48 | {
|
| 49 | printf("Connection closed by server\n");
|
| 50 | }
|
| 51 | else
|
| 52 | {
|
| 53 | printf("mbtk_openssl_read() fail.[%d]\n", len);
|
| 54 | }
|
| 55 | break;
|
| 56 | }
|
| 57 | buffer[len] = '\0';
|
| 58 | printf("\nReceived: %s\n", buffer);
|
| 59 | }
|
| 60 |
|
| 61 | return NULL;
|
| 62 | }
|
| 63 |
|
| 64 | static int tcp_connect_init(int *client_fd, int port, char *ip)
|
| 65 | {
|
| 66 | int ret = -1;
|
| 67 | struct sockaddr_in server_addr;
|
| 68 |
|
| 69 | if(port < 1 || port > 65535)
|
| 70 | {
|
| 71 | printf("[%s] Invalid port number\n", __func__);
|
| 72 | goto error;
|
| 73 | }
|
| 74 |
|
| 75 | *client_fd = socket(AF_INET, SOCK_STREAM, 0);
|
| 76 | if(*client_fd < 0)
|
| 77 | {
|
| 78 | printf("[%s] socket creation failed\n", __func__);
|
| 79 | goto error;
|
| 80 | }
|
| 81 |
|
| 82 | server_addr.sin_family = AF_INET;
|
| 83 | server_addr.sin_port = htons(port);
|
| 84 | ret = inet_pton(AF_INET, ip, &server_addr.sin_addr);
|
| 85 | if(ret <= 0)
|
| 86 | {
|
| 87 | perror("invalid address");
|
| 88 | goto error;
|
| 89 | }
|
| 90 |
|
| 91 | ret = connect(*client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
| 92 | if(ret< 0)
|
| 93 | {
|
| 94 | perror("connection failed");
|
| 95 | goto error;
|
| 96 | }
|
| 97 |
|
| 98 | printf("[%s] Connected to %s:%d\n", __func__, ip, port);
|
| 99 | return 0;
|
| 100 | error:
|
| 101 | if(*client_fd >= 0)
|
| 102 | {
|
| 103 | close(*client_fd);
|
| 104 | *client_fd = -1;
|
| 105 | }
|
| 106 | return -1;
|
| 107 | }
|
| 108 |
|
| 109 | int main(int argc, char *argv[])
|
| 110 | {
|
| 111 | if(argc != 3)
|
| 112 | {
|
| 113 | printf("Usage: %s <IP> <PORT>\n", argv[0]);
|
| 114 | exit(EXIT_FAILURE);
|
| 115 | }
|
| 116 |
|
| 117 | int ret = -1;
|
| 118 | int client_fd = -1;
|
| 119 | ssize_t bytes_recv = 0;
|
| 120 | char buffer[BUFFER_SIZE + 1] = {0};
|
| 121 | mbtk_openssl_result_e mbtk_ret = MBTK_OPENSSL_RESULT_SUCCESS;
|
| 122 | mbtk_openssl_info_s inter_info = {0};
|
| 123 | mbtk_openssl_options_s opt = {0};
|
| 124 |
|
| 125 | pthread_t tid;
|
| 126 |
|
| 127 | ret = tcp_connect_init(&client_fd, atoi(argv[2]), argv[1]);
|
| 128 | if(ret < 0)
|
| 129 | {
|
| 130 | printf("tcp_connect_init() fail\n");
|
| 131 | exit(EXIT_FAILURE);
|
| 132 | }
|
| 133 |
|
| 134 | mbtk_openssl_options_default(&opt);
|
| 135 | opt.load_cert = true;
|
| 136 | opt.ca_file = "/ca.crt";
|
| 137 | opt.crt_file = "/client.crt";
|
| 138 | opt.key_file = "/client.key";
|
| 139 | opt.safety_level = MBTK_OPENSSL_SAFETY_LEVEL_0;//Use level 0 for testing only
|
| 140 | mbtk_ret = mbtk_openssl_init(client_fd, &opt, &inter_info);
|
| 141 | if(mbtk_ret != MBTK_OPENSSL_RESULT_SUCCESS)
|
| 142 | {
|
| 143 | printf("mbtk_openssl_init() fail\n");
|
| 144 | close(client_fd);
|
| 145 | client_fd =-1;
|
| 146 | exit(EXIT_FAILURE);
|
| 147 | }
|
| 148 |
|
| 149 | ret = pthread_create(&tid, NULL, recv_thread, (void*)(intptr_t)&inter_info);
|
| 150 | if(ret != 0)
|
| 151 | {
|
| 152 | perror("thread creation failed");
|
| 153 | close(client_fd);
|
| 154 | client_fd = -1;
|
| 155 | exit(EXIT_FAILURE);
|
| 156 | }
|
| 157 |
|
| 158 | while(1)
|
| 159 | {
|
| 160 | printf("Enter message: \n");
|
| 161 | memset(buffer, 0x00, BUFFER_SIZE);
|
| 162 | fgets(buffer, BUFFER_SIZE, stdin);
|
| 163 |
|
| 164 | if(memcmp(buffer, "exit", 4) == 0)
|
| 165 | {
|
| 166 | printf("process exit\n");
|
| 167 | break;
|
| 168 | }
|
| 169 |
|
| 170 | ret = mbtk_openssl_write(inter_info.ssl, buffer, strlen(buffer));
|
| 171 | if(ret < 0)
|
| 172 | {
|
| 173 | printf("mbtk_openssl_write() fail.[%d]\n", ret);
|
| 174 | break;
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | mbtk_openssl_deinit(&inter_info);
|
| 179 | close(client_fd);
|
| 180 | client_fd = -1;
|
| 181 |
|
| 182 | return 0;
|
| 183 | }
|
| 184 | #else
|
| 185 | int main(int argc, char *argv[])
|
| 186 | {
|
| 187 | printf("No support openssl.\n");
|
| 188 | return 0;
|
| 189 | }
|
| 190 | #endif |