blob: 45909aeb55d50f6e2186bb60796cdc81c365f8fb [file] [log] [blame]
b.liud440f9f2025-04-18 10:44:31 +08001/*-----------------------------------------------------------------------------------------------*/
2/**
3 @file NULL
4 @brief libmbedtls.so.3.6.2 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 20241022 yq.wang Created .
21-------------------------------------------------------------------------------------------------*/
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <stdbool.h>
27#include <arpa/inet.h>
28#include <sys/socket.h>
29
30#ifdef MBTK_MBEDTLS_V3_6_2_SUPPORT
31#include "mbtk_mbedtls.h"
32
33#define BUFFER_SIZE 1024
34
35static int tcp_connect_init(int *client_fd, int port, char *ip)
36{
37 int ret = -1;
38 struct sockaddr_in server_addr;
39
40 if(port < 1 || port > 65535)
41 {
42 printf("[%s] Invalid port number\n", __func__);
43 goto error;
44 }
45
46 *client_fd = socket(AF_INET, SOCK_STREAM, 0);
47 if(*client_fd < 0)
48 {
49 printf("[%s] socket creation failed\n", __func__);
50 goto error;
51 }
52
53 server_addr.sin_family = AF_INET;
54 server_addr.sin_port = htons(port);
55 ret = inet_pton(AF_INET, ip, &server_addr.sin_addr);
56 if(ret <= 0)
57 {
58 perror("invalid address");
59 goto error;
60 }
61
62 ret = connect(*client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
63 if(ret< 0)
64 {
65 perror("connection failed");
66 goto error;
67 }
68
69 printf("[%s] Connected to %s:%d\n", __func__, ip, port);
70 return 0;
71error:
72 if(*client_fd >= 0)
73 {
74 close(*client_fd);
75 *client_fd = -1;
76 }
77 return -1;
78}
79
80int main(int argc, char *argv[])
81{
82 if(argc != 3)
83 {
84 printf("Usage: %s <IP> <PORT>\n", argv[0]);
85 exit(EXIT_FAILURE);
86 }
87
88 int ret = -1;
89 int client_fd = -1;
90 ssize_t bytes_recv = 0;
91 char buffer[BUFFER_SIZE] = {0};
92 mbtk_mbedtls_ssl_result_e mbtk_ssl_ret = MBTK_MBEDTLS_SSL_RESULT_SUCCESS;
93 mbtk_mbedtls_ssl_info_s inter_info = {0};
94 mbtk_mbedtls_ssl_options_s opt = {0};
95
96 ret = tcp_connect_init(&client_fd, atoi(argv[2]), argv[1]);
97 if(ret < 0)
98 {
99 printf("tcp_connect_init() fail\n");
100 exit(EXIT_FAILURE);
101 }
102
103 mbtk_mbedtls_ssl_options_default(&opt);
104 opt.load_cert = true;
105 opt.ca_file = "/ca.crt";
106 opt.crt_file = "/client.crt";
107 opt.key_file = "/client.key";
108 opt.auth_mode = MBTK_MBEDTLS_SSL_VERIFY_REQUIRED;
109 opt.allowed_mds |= MBTK_MBEDTLS_SSL_MD_SHA1;
110 memset(&inter_info, 0x00, sizeof(mbtk_mbedtls_ssl_info_s));
111 mbtk_ssl_ret = mbtk_mbedtls_ssl_init(client_fd, &opt, &inter_info);
112 if(mbtk_ssl_ret != MBTK_MBEDTLS_SSL_RESULT_SUCCESS)
113 {
114 printf("mbtk_mbedtls_ssl_init() fail\n");
115 close(client_fd);
116 client_fd = -1;
117 exit(EXIT_FAILURE);
118 }
119
120 while(1)
121 {
122 printf("Enter message: \n");
123 fgets(buffer, BUFFER_SIZE, stdin);
124
125 if(memcmp(buffer, "exit", 4) == 0)
126 {
127 printf("process exit\n");
128 break;
129 }
130
131 ret = mbtk_mbedtls_ssl_write(inter_info.ssl, (const unsigned char*)buffer, strlen(buffer));
132 if(ret < 0)
133 {
134 perror("send failed");
135 break;
136 }
137
138 bytes_recv = mbtk_mbedtls_ssl_read(inter_info.ssl, (unsigned char*)buffer, BUFFER_SIZE-1);
139 if (bytes_recv <= 0)
140 {
141 bytes_recv == 0 ? printf("Connection closed\n") : perror("recv failed");
142 break;
143 }
144 buffer[bytes_recv] = '\0';
145 printf("Server response: %s\n", buffer);
146 }
147
148 mbtk_mbedtls_ssl_deinit(&inter_info);
149 close(client_fd);
150 client_fd = -1;
151
152 return 0;
153}
154
155#else
156int main(int argc, char *argv[])
157{
158 printf("No support polarssl.\n");
159 return 0;
160}
161#endif
162