mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-07 00:46:17 +08:00
Add socket wrapper
This commit is contained in:
70
include/gmssl/socket.h
Normal file
70
include/gmssl/socket.h
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
#ifndef GMSSL_SOCKET_H
|
||||
#define GMSSL_SOCKET_H
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
#pragma comment (lib, "Ws2_32.lib")
|
||||
#pragma comment (lib, "Mswsock.lib")
|
||||
#pragma comment (lib, "AdvApi32.lib")
|
||||
|
||||
#include <winsock2.h>
|
||||
|
||||
typedef SOCKET tls_socket_t;
|
||||
typedef int tls_ret_t;
|
||||
typedef int tls_socklen_t;
|
||||
|
||||
|
||||
#define tls_socket_send(sock,buf,len,flags) send(sock,buf,(int)(len),flags)
|
||||
#define tls_socket_recv(sock,buf,len,flags) recv(sock,buf,(int)(len),flags)
|
||||
#define tls_socket_close(sock) closesocket(sock)
|
||||
|
||||
|
||||
#else
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef int tls_socket_t;
|
||||
typedef ssize_t tls_ret_t;
|
||||
typedef socklen_t tls_socklen_t;
|
||||
|
||||
|
||||
#define tls_socket_send(sock,buf,len,flags) send(sock,buf,len,flags)
|
||||
#define tls_socket_recv(sock,buf,len,flags) recv(sock,buf,len,flags)
|
||||
#define tls_socket_close(sock) close(sock)
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -12,13 +12,6 @@
|
||||
#ifndef GMSSL_TLS_H
|
||||
#define GMSSL_TLS_H
|
||||
|
||||
#ifdef WIN32
|
||||
#pragma comment (lib, "Ws2_32.lib")
|
||||
#pragma comment (lib, "Mswsock.lib")
|
||||
#pragma comment (lib, "AdvApi32.lib")
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sm2.h>
|
||||
@@ -26,6 +19,7 @@
|
||||
#include <gmssl/sm4.h>
|
||||
#include <gmssl/digest.h>
|
||||
#include <gmssl/block_cipher.h>
|
||||
#include <gmssl/socket.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -458,15 +452,9 @@ int tls_record_set_data(uint8_t *record, const uint8_t *data, size_t datalen);
|
||||
int tls_record_print(FILE *fp, const uint8_t *record, size_t recordlen, int format, int indent);
|
||||
int tlcp_record_print(FILE *fp, const uint8_t *record, size_t recordlen, int format, int indent);
|
||||
|
||||
#ifdef WIN32
|
||||
int tls_record_send(const uint8_t* record, size_t recordlen, SOCKET sock);
|
||||
int tls_record_recv(uint8_t* record, size_t* recordlen, SOCKET sock);
|
||||
int tls12_record_recv(uint8_t* record, size_t* recordlen, SOCKET sock);
|
||||
#else
|
||||
int tls_record_send(const uint8_t *record, size_t recordlen, int sock);
|
||||
int tls_record_recv(uint8_t *record, size_t *recordlen, int sock);
|
||||
int tls12_record_recv(uint8_t *record, size_t *recordlen, int sock);
|
||||
#endif
|
||||
int tls_record_send(const uint8_t *record, size_t recordlen, tls_socket_t sock);
|
||||
int tls_record_recv(uint8_t *record, size_t *recordlen, tls_socket_t sock);
|
||||
int tls12_record_recv(uint8_t *record, size_t *recordlen, tls_socket_t sock);
|
||||
|
||||
|
||||
// Handshake
|
||||
@@ -752,12 +740,7 @@ typedef struct {
|
||||
int is_client;
|
||||
int cipher_suites[TLS_MAX_CIPHER_SUITES_COUNT];
|
||||
size_t cipher_suites_cnt;
|
||||
|
||||
#ifdef WIN32
|
||||
SOCKET sock;
|
||||
#else
|
||||
int sock;
|
||||
#endif
|
||||
tls_socket_t sock;
|
||||
|
||||
uint8_t enced_record[TLS_MAX_RECORD_SIZE];
|
||||
size_t enced_record_len;
|
||||
@@ -807,11 +790,7 @@ typedef struct {
|
||||
|
||||
|
||||
int tls_init(TLS_CONNECT *conn, const TLS_CTX *ctx);
|
||||
#ifdef WIN32
|
||||
int tls_set_socket(TLS_CONNECT* conn, SOCKET sock);
|
||||
#else
|
||||
int tls_set_socket(TLS_CONNECT *conn, int sock);
|
||||
#endif
|
||||
int tls_set_socket(TLS_CONNECT *conn, tls_socket_t sock);
|
||||
int tls_do_handshake(TLS_CONNECT *conn);
|
||||
int tls_send(TLS_CONNECT *conn, const uint8_t *in, size_t inlen, size_t *sentlen);
|
||||
int tls_recv(TLS_CONNECT *conn, uint8_t *out, size_t outlen, size_t *recvlen);
|
||||
|
||||
Reference in New Issue
Block a user