comparison seobeo/s_linux_network.c @ 67:6626ec933933

[Seobeo] Separated out Client Server logic. Created test tools.
author June Park <parkjune1995@gmail.com>
date Wed, 24 Dec 2025 09:15:55 -0800
parents a0f0ad5e42eb
children 75de5903355c
comparison
equal deleted inserted replaced
66:a0f0ad5e42eb 67:6626ec933933
103 { perror("connect"); return NULL; } 103 { perror("connect"); return NULL; }
104 freeaddrinfo(server_infos); 104 freeaddrinfo(server_infos);
105 105
106 p_handle->socket = socket_fd; 106 p_handle->socket = socket_fd;
107 p_handle->type = SEOBEO_STREAM_TYPE_CLIENT; 107 p_handle->type = SEOBEO_STREAM_TYPE_CLIENT;
108
109 p_handle->ssl_ctx = NULL;
110 p_handle->ssl = NULL;
111
108 if (use_tls) 112 if (use_tls)
109 { 113 {
110 printf("USE SSL\n\n"); 114 if (Seobeo_SSL_Setup_Client(p_handle, host, socket_fd) != 0)
111 Seobeo_Web_SSL_Init(); 115 {
112 p_handle->ssl_ctx = SSL_CTX_new(TLS_client_method()); 116 free(p_handle);
113 SSL_CTX_set_default_verify_paths(p_handle->ssl_ctx);
114
115 p_handle->ssl = SSL_new(p_handle->ssl_ctx);
116 SSL_set_fd(p_handle->ssl, p_handle->socket);
117
118 SSL_set_tlsext_host_name(p_handle->ssl, host);
119 // Blocking for TSL handshake
120 fcntl(socket_fd, F_SETFL, 0);
121
122 if (SSL_connect(p_handle->ssl) != 1)
123 {
124 fprintf(stderr, "SSL_connect failed\n");
125 ERR_print_errors_fp(stderr);
126 return NULL; 117 return NULL;
127 } 118 }
128 }else
129 {
130 p_handle->ssl_ctx = NULL;
131 p_handle->ssl = NULL;
132 } 119 }
133 p_handle->connected = true; 120 p_handle->connected = true;
134 121
135 p_handle->host = host != NULL ? strdup(host) : "localhost"; 122 p_handle->host = host != NULL ? strdup(host) : "localhost";
136 p_handle->port = strdup(port); 123 p_handle->port = strdup(port);
210 } 197 }
211 198
212 if (p_handle->host) Dowa_Free(p_handle->host); 199 if (p_handle->host) Dowa_Free(p_handle->host);
213 if (p_handle->port) Dowa_Free(p_handle->port); 200 if (p_handle->port) Dowa_Free(p_handle->port);
214 201
215 if (p_handle->ssl) 202 Seobeo_SSL_Cleanup(p_handle);
216 {
217 SSL_shutdown(p_handle->ssl);
218 SSL_free(p_handle->ssl);
219 p_handle->ssl = NULL;
220 }
221
222 if (p_handle->ssl_ctx)
223 {
224 SSL_CTX_free(p_handle->ssl_ctx);
225 p_handle->ssl_ctx = NULL;
226 }
227 203
228 if (p_handle->socket) { 204 if (p_handle->socket) {
229 printf("Closing: %d", p_handle->socket); 205 printf("Closing: %d", p_handle->socket);
230 close(p_handle->socket); 206 close(p_handle->socket);
231 } 207 }
246 222
247 while (sent < total) 223 while (sent < total)
248 { 224 {
249 if (p_handle->ssl) 225 if (p_handle->ssl)
250 { 226 {
251 int n = SSL_write(p_handle->ssl, p_handle->write_buffer + sent, total - sent); 227 int n = Seobeo_SSL_Write(p_handle, p_handle->write_buffer + sent, total - sent);
252 if (n < 0) 228 if (n < 0) return -1;
253 { 229 if (n == 0) return 0; // would block
254 int err = SSL_get_error(p_handle->ssl, n);
255 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
256 {
257 // caller must wait for socket readiness and retry
258 return 0;
259 }
260 ERR_print_errors_fp(stderr);
261 return -1;
262 }
263 sent += (uint32)n; 230 sent += (uint32)n;
264 }else 231 }else
265 { 232 {
266 printf("socket: %d\n", p_handle->socket); 233 printf("socket: %d\n", p_handle->socket);
267 ssize_t n = write( 234 ssize_t n = write(
362 if (free_space == 0) 329 if (free_space == 0)
363 return -1; 330 return -1;
364 331
365 if (p_handle->ssl) 332 if (p_handle->ssl)
366 { 333 {
367 read_size = (int32)SSL_read(p_handle->ssl, p_handle->read_buffer, 334 read_size = Seobeo_SSL_Read(p_handle, p_handle->read_buffer + p_handle->read_buffer_len, free_space);
368 free_space); 335 if (read_size < 0) return read_size; // -1 for error, -2 for closed
369 if (read_size <= 0) 336 if (read_size == 0) return 0; // would block
370 {
371 int err = SSL_get_error(p_handle->ssl, read_size);
372 switch (err)
373 {
374 case SSL_ERROR_WANT_READ:
375 case SSL_ERROR_WANT_WRITE:
376 return 0;
377 case SSL_ERROR_ZERO_RETURN:
378 default:
379 // TODO: Handle these errors
380 return -2;
381 }
382 }
383 }else 337 }else
384 { 338 {
385 read_size = (int32)read(p_handle->socket, 339 read_size = (int32)read(p_handle->socket,
386 p_handle->read_buffer + p_handle->read_buffer_len, 340 p_handle->read_buffer + p_handle->read_buffer_len,
387 free_space); 341 free_space);