3 * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
5 * This file is part of CyaSSL.
7 * CyaSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CyaSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
27 /* On WinCE winsock2.h must be included before windows.h for socket stuff */
31 #include <cyassl/internal.h>
33 /* if user writes own I/O callbacks they can define CYASSL_USER_IO to remove
34 automatic setting of default I/O functions EmbedSend() and EmbedReceive()
35 but they'll still need SetCallback xxx() at end of file
37 #ifndef CYASSL_USER_IO
43 #ifndef USE_WINDOWS_API
45 /* lwIP needs to be configured to use sockets API in this mode */
46 /* LWIP_SOCKET 1 && LWIP_COMPAT_SOCKETS 1 in lwip/opt.h or in build */
47 #define LWIP_PROVIDE_ERRNO 1
50 #include <sys/types.h>
54 #if !(defined(DEVKITPRO) || defined(THREADX))
55 #include <sys/socket.h>
56 #include <arpa/inet.h>
57 #include <netinet/in.h>
60 #include <netex/errno.h>
62 #include <sys/ioctl.h>
69 #endif /* USE_WINDOWS_API */
72 #include <sys/filio.h>
75 #ifdef USE_WINDOWS_API
78 #define WSAEPIPE -12345
80 #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
81 #define SOCKET_EAGAIN WSAEWOULDBLOCK
82 #define SOCKET_ECONNRESET WSAECONNRESET
83 #define SOCKET_EINTR WSAEINTR
84 #define SOCKET_EPIPE WSAEPIPE
86 #define SOCKET_EWOULDBLOCK SYS_NET_EWOULDBLOCK
87 #define SOCKET_EAGAIN SYS_NET_EAGAIN
88 #define SOCKET_ECONNRESET SYS_NET_ECONNRESET
89 #define SOCKET_EINTR SYS_NET_EINTR
90 #define SOCKET_EPIPE SYS_NET_EPIPE
92 #define SOCKET_EWOULDBLOCK EWOULDBLOCK
93 #define SOCKET_EAGAIN EAGAIN
94 #define SOCKET_ECONNRESET ECONNRESET
95 #define SOCKET_EINTR EINTR
96 #define SOCKET_EPIPE EPIPE
97 #endif /* USE_WINDOWS_API */
102 int net_send(int, const void*, int, unsigned int);
103 int net_recv(int, void*, int, unsigned int);
104 #define SEND_FUNCTION net_send
105 #define RECV_FUNCTION net_recv
107 #define SEND_FUNCTION send
108 #define RECV_FUNCTION recv
112 static INLINE int LastError(void)
114 #ifdef USE_WINDOWS_API
115 return WSAGetLastError();
121 /* The receive embedded callback
122 * return : nb bytes read, or error
124 int EmbedReceive(char *buf, int sz, void *ctx)
130 recvd = RECV_FUNCTION(sd, (char *)buf, sz, 0);
134 CYASSL_MSG("Embed Receive error");
136 if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
137 CYASSL_MSG(" Would block");
138 return IO_ERR_WANT_READ;
140 else if (err == SOCKET_ECONNRESET) {
141 CYASSL_MSG(" Connection reset");
142 return IO_ERR_CONN_RST;
144 else if (err == SOCKET_EINTR) {
145 CYASSL_MSG(" Socket interrupted");
149 CYASSL_MSG(" General error");
150 return IO_ERR_GENERAL;
153 else if (recvd == 0) {
154 CYASSL_MSG("Embed receive connection closed");
155 return IO_ERR_CONN_CLOSE;
161 /* The send embedded callback
162 * return : nb bytes sent, or error
164 int EmbedSend(char *buf, int sz, void *ctx)
171 sent = SEND_FUNCTION(sd, &buf[sz - len], len, 0);
175 CYASSL_MSG("Embed Send error");
177 if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
178 CYASSL_MSG(" Would Block");
179 return IO_ERR_WANT_WRITE;
181 else if (err == SOCKET_ECONNRESET) {
182 CYASSL_MSG(" Connection reset");
183 return IO_ERR_CONN_RST;
185 else if (err == SOCKET_EINTR) {
186 CYASSL_MSG(" Socket interrupted");
189 else if (err == SOCKET_EPIPE) {
190 CYASSL_MSG(" Socket EPIPE");
191 return IO_ERR_CONN_CLOSE;
194 CYASSL_MSG(" General error");
195 return IO_ERR_GENERAL;
205 #include <cyassl/ctaocrypt/sha.h>
207 /* The DTLS Generate Cookie callback
208 * return : number of bytes copied into buf, or error
210 int EmbedGenerateCookie(byte *buf, int sz, void *ctx)
212 CYASSL* ssl = (CYASSL*)ctx;
214 struct sockaddr_storage peer;
215 socklen_t peerSz = sizeof(peer);
216 byte cookieSrc[sizeof(struct in6_addr) + sizeof(int)];
220 getpeername(sd, (struct sockaddr*)&peer, &peerSz);
222 if (peer.ss_family == AF_INET) {
223 struct sockaddr_in *s = (struct sockaddr_in*)&peer;
225 cookieSrcSz = sizeof(struct in_addr) + sizeof(s->sin_port);
226 XMEMCPY(cookieSrc, &s->sin_port, sizeof(s->sin_port));
227 XMEMCPY(cookieSrc + sizeof(s->sin_port),
228 &s->sin_addr, sizeof(struct in_addr));
230 else if (peer.ss_family == AF_INET6) {
231 struct sockaddr_in6 *s = (struct sockaddr_in6*)&peer;
233 cookieSrcSz = sizeof(struct in6_addr) + sizeof(s->sin6_port);
234 XMEMCPY(cookieSrc, &s->sin6_port, sizeof(s->sin6_port));
235 XMEMCPY(cookieSrc + sizeof(s->sin6_port),
236 &s->sin6_addr, sizeof(struct in6_addr));
240 ShaUpdate(&sha, cookieSrc, cookieSrcSz);
243 return SHA_DIGEST_SIZE;
246 #endif /* CYASSL_DTLS */
249 #endif /* CYASSL_USER_IO */
251 CYASSL_API void CyaSSL_SetIORecv(CYASSL_CTX *ctx, CallbackIORecv CBIORecv)
253 ctx->CBIORecv = CBIORecv;
257 CYASSL_API void CyaSSL_SetIOSend(CYASSL_CTX *ctx, CallbackIOSend CBIOSend)
259 ctx->CBIOSend = CBIOSend;
263 CYASSL_API void CyaSSL_SetIOReadCtx(CYASSL* ssl, void *rctx)
265 ssl->IOCB_ReadCtx = rctx;
269 CYASSL_API void CyaSSL_SetIOWriteCtx(CYASSL* ssl, void *wctx)
271 ssl->IOCB_WriteCtx = wctx;