open62541 1.3.12
Open source implementation of OPC UA
Loading...
Searching...
No Matches
bearssl_sockets.h
Go to the documentation of this file.
1#if !defined(__BEARSSL_SOCKET_TEMPLATE_H__)
2#define __BEARSSL_SOCKET_TEMPLATE_H__
3
4#include <sys/socket.h>
5#include <netdb.h>
6#include <errno.h>
7#include <fcntl.h>
8
9#include <bearssl.h>
10
11//#include "mqtt_pal.h"
12
13/*
14 * Low-level data read callback for the simplified SSL I/O API.
15 */
16static int sock_read(void *ctx, unsigned char *buf, size_t len) {
17 ssize_t rlen;
18
19 for (;;) {
20 rlen = read(*(int *)ctx, buf, len);
21
22 if (rlen < 0) {
23 if (errno == EINTR) {
24 continue;
25 }
26 else if (errno == EWOULDBLOCK || errno == EAGAIN) {
27 rlen = 0;
28 break;
29 }
30 else {
31 break;
32 }
33 }
34 else {
35 break;
36 }
37 }
38
39 return (int)rlen;
40}
41
42/*
43 * Low-level data write callback for the simplified SSL I/O API.
44 */
45static int sock_write(void *ctx, const unsigned char *buf, size_t len) {
46 ssize_t wlen;
47
48 for (;;) {
49
50 wlen = write(*(int *)ctx, buf, len);
51 if (wlen <= 0 && errno == EINTR) {
52 continue;
53 }
54 return (int)wlen;
55 }
56}
57
58static int host_connect(const char *host, const char *port) {
59 struct hostent *he;
60 struct in_addr **addr_list;
61
62 if (NULL == (he = gethostbyname(host)))
63 {
64 fprintf(stderr, "Failed to resolve host name\n");
65 return -1;
66 }
67
68 addr_list = (struct in_addr **) he->h_addr_list;
69
70 int sockfd = -1;
71 int rv;
72 int i;
73
74 if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)))
75 return -1;
76
77 struct sockaddr_in server;
78
79 server.sin_family = AF_INET;
80 server.sin_port = htons(atoi(port));
81
82 /* open the first possible socket */
83 for (i = 0; addr_list[i] != NULL; i++)
84 {
85 memcpy(&server.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
86
87 if (0 > (rv = connect(sockfd, (struct sockaddr *)&server , sizeof(server)))) {
88 continue;
89 }
90 else {
91 int flags = fcntl(sockfd, F_GETFL, 0);
92 flags |= O_NONBLOCK;
93 fcntl(sockfd, F_SETFL, flags);
94 break;
95 }
96 }
97
98 if (addr_list[i] == NULL)
99 sockfd = -1;
100
101 /* return the new socket fd */
102 return sockfd;
103}
104
105int open_nb_socket(bearssl_context *ctx,
106 const char *hostname,
107 const char *port,
108 unsigned char *bearssl_iobuf,
109 size_t bearssl_iobuf_len) {
110
111 /* initialize the BearSSL engine */
112 br_ssl_client_init_full(&ctx->sc, &ctx->xc, ctx->anchOut, ctx->ta_count);
113 br_ssl_engine_set_buffer(&ctx->sc.eng, bearssl_iobuf, bearssl_iobuf_len, 1);
114 br_ssl_client_reset(&ctx->sc, hostname, 0);
115
116 if (-1 == (ctx->fd = host_connect(hostname, port)))
117 return -1;
118
119 ctx->low_read = sock_read;
120 ctx->low_write = sock_write;
121
122 return 0;
123}
124
125int close_socket(bearssl_context *ctx) {
126 int rc;
127
128 br_ssl_engine_close(&ctx->sc.eng);
129
130 if (ctx->fd != 0) {
131 shutdown(ctx->fd, SHUT_RDWR);
132 rc = close(ctx->fd);
133 ctx->fd = 0;
134 }
135
136 return rc;
137}
138
139#endif
int open_nb_socket(bearssl_context *ctx, const char *hostname, const char *port, unsigned char *bearssl_iobuf, size_t bearssl_iobuf_len)
int close_socket(bearssl_context *ctx)