A super trimmed down TLS stack, GPL licensed

Dependents:   MiniTLS-HTTPS-Example

MiniTLS - A super trimmed down TLS/SSL Library for embedded devices Author: Donatien Garnier Copyright (C) 2013-2014 AppNearMe Ltd

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Committer:
MiniTLS
Date:
Mon Jun 09 14:57:54 2014 +0000
Revision:
2:527a66d0a1a9
Child:
3:eb324ffffd2b
Change name to MiniTLS and added doc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MiniTLS 2:527a66d0a1a9 1 /*
MiniTLS 2:527a66d0a1a9 2 MiniTLS - A super trimmed down TLS/SSL Library for embedded devices
MiniTLS 2:527a66d0a1a9 3 Author: Donatien Garnier
MiniTLS 2:527a66d0a1a9 4 Copyright (C) 2013-2014 AppNearMe Ltd
MiniTLS 2:527a66d0a1a9 5
MiniTLS 2:527a66d0a1a9 6 This program is free software; you can redistribute it and/or
MiniTLS 2:527a66d0a1a9 7 modify it under the terms of the GNU General Public License
MiniTLS 2:527a66d0a1a9 8 as published by the Free Software Foundation; either version 2
MiniTLS 2:527a66d0a1a9 9 of the License, or (at your option) any later version.
MiniTLS 2:527a66d0a1a9 10
MiniTLS 2:527a66d0a1a9 11 This program is distributed in the hope that it will be useful,
MiniTLS 2:527a66d0a1a9 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
MiniTLS 2:527a66d0a1a9 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MiniTLS 2:527a66d0a1a9 14 GNU General Public License for more details.
MiniTLS 2:527a66d0a1a9 15
MiniTLS 2:527a66d0a1a9 16 You should have received a copy of the GNU General Public License
MiniTLS 2:527a66d0a1a9 17 along with this program; if not, write to the Free Software
MiniTLS 2:527a66d0a1a9 18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
MiniTLS 2:527a66d0a1a9 19 *//**
MiniTLS 2:527a66d0a1a9 20 * \file socket_lwip.c
MiniTLS 2:527a66d0a1a9 21 * \copyright Copyright (c) AppNearMe Ltd 2013
MiniTLS 2:527a66d0a1a9 22 * \author Donatien Garnier
MiniTLS 2:527a66d0a1a9 23 */
MiniTLS 2:527a66d0a1a9 24
MiniTLS 2:527a66d0a1a9 25 #include "core/fwk.h"
MiniTLS 2:527a66d0a1a9 26
MiniTLS 2:527a66d0a1a9 27 #include "socket.h"
MiniTLS 2:527a66d0a1a9 28
MiniTLS 2:527a66d0a1a9 29 #include "lwip/sockets.h"
MiniTLS 2:527a66d0a1a9 30 #include "lwip/inet.h"
MiniTLS 2:527a66d0a1a9 31 #include "lwip/netdb.h"
MiniTLS 2:527a66d0a1a9 32
MiniTLS 2:527a66d0a1a9 33 int socket_socket()
MiniTLS 2:527a66d0a1a9 34 {
MiniTLS 2:527a66d0a1a9 35 return lwip_socket(AF_INET, SOCK_STREAM, 0);
MiniTLS 2:527a66d0a1a9 36 }
MiniTLS 2:527a66d0a1a9 37
MiniTLS 2:527a66d0a1a9 38
MiniTLS 2:527a66d0a1a9 39 int socket_connect(int fd, const char* hostname, uint16_t port)
MiniTLS 2:527a66d0a1a9 40 {
MiniTLS 2:527a66d0a1a9 41 /*struct hostent* hostent = gethostbyname(hostname);
MiniTLS 2:527a66d0a1a9 42 if(hostent == NULL)
MiniTLS 2:527a66d0a1a9 43 {
MiniTLS 2:527a66d0a1a9 44 return MINITLS_ERR_SOCKET_ERROR;
MiniTLS 2:527a66d0a1a9 45 }*/
MiniTLS 2:527a66d0a1a9 46 struct sockaddr_in server;
MiniTLS 2:527a66d0a1a9 47 server.sin_family = AF_INET;
MiniTLS 2:527a66d0a1a9 48 //memcpy(&server.sin_addr.s_addr, &hostent->h_addr_list[0], hostent->h_length);
MiniTLS 2:527a66d0a1a9 49 server.sin_addr.s_addr = inet_addr(hostname);
MiniTLS 2:527a66d0a1a9 50 server.sin_port = htons(port);
MiniTLS 2:527a66d0a1a9 51
MiniTLS 2:527a66d0a1a9 52 DBG("Trying to connect to %s:%d", hostname, port);
MiniTLS 2:527a66d0a1a9 53
MiniTLS 2:527a66d0a1a9 54 return lwip_connect(fd, (struct sockaddr*)&server, sizeof(struct sockaddr_in));
MiniTLS 2:527a66d0a1a9 55 }
MiniTLS 2:527a66d0a1a9 56
MiniTLS 2:527a66d0a1a9 57 int socket_wait_readable(int fd, int timeout_ms)
MiniTLS 2:527a66d0a1a9 58 {
MiniTLS 2:527a66d0a1a9 59 //Wait for record to be readable
MiniTLS 2:527a66d0a1a9 60 fd_set set;
MiniTLS 2:527a66d0a1a9 61 FD_ZERO(&set);
MiniTLS 2:527a66d0a1a9 62 FD_SET(fd, &set);
MiniTLS 2:527a66d0a1a9 63
MiniTLS 2:527a66d0a1a9 64 struct timeval timeout;
MiniTLS 2:527a66d0a1a9 65 timeout.tv_sec = timeout_ms / 1000;
MiniTLS 2:527a66d0a1a9 66 timeout.tv_usec = (timeout_ms - (timeout.tv_sec * 1000)) * 1000;
MiniTLS 2:527a66d0a1a9 67
MiniTLS 2:527a66d0a1a9 68 int r = lwip_select(fd + 1, &set, NULL, NULL, &timeout);
MiniTLS 2:527a66d0a1a9 69 if( (r <= 0) || !FD_ISSET(fd, &set) )
MiniTLS 2:527a66d0a1a9 70 {
MiniTLS 2:527a66d0a1a9 71 return -1;
MiniTLS 2:527a66d0a1a9 72 }
MiniTLS 2:527a66d0a1a9 73 return 0;
MiniTLS 2:527a66d0a1a9 74 }
MiniTLS 2:527a66d0a1a9 75
MiniTLS 2:527a66d0a1a9 76 int socket_wait_writeable(int fd, int timeout_ms)
MiniTLS 2:527a66d0a1a9 77 {
MiniTLS 2:527a66d0a1a9 78 //Wait for record to be writeable
MiniTLS 2:527a66d0a1a9 79 fd_set set;
MiniTLS 2:527a66d0a1a9 80 FD_ZERO(&set);
MiniTLS 2:527a66d0a1a9 81 FD_SET(fd, &set);
MiniTLS 2:527a66d0a1a9 82
MiniTLS 2:527a66d0a1a9 83 struct timeval timeout;
MiniTLS 2:527a66d0a1a9 84 timeout.tv_sec = timeout_ms / 1000;
MiniTLS 2:527a66d0a1a9 85 timeout.tv_usec = (timeout_ms - (timeout.tv_sec * 1000)) * 1000;
MiniTLS 2:527a66d0a1a9 86
MiniTLS 2:527a66d0a1a9 87 int r = lwip_select(fd + 1, NULL, &set, NULL, &timeout);
MiniTLS 2:527a66d0a1a9 88 if( (r <= 0) || !FD_ISSET(fd, &set) )
MiniTLS 2:527a66d0a1a9 89 {
MiniTLS 2:527a66d0a1a9 90 return -1;
MiniTLS 2:527a66d0a1a9 91 }
MiniTLS 2:527a66d0a1a9 92 return 0;
MiniTLS 2:527a66d0a1a9 93 }
MiniTLS 2:527a66d0a1a9 94
MiniTLS 2:527a66d0a1a9 95 int socket_recv(int fd, void* buf, size_t size)
MiniTLS 2:527a66d0a1a9 96 {
MiniTLS 2:527a66d0a1a9 97 return lwip_recv(fd, buf, size, 0);
MiniTLS 2:527a66d0a1a9 98 }
MiniTLS 2:527a66d0a1a9 99
MiniTLS 2:527a66d0a1a9 100 int socket_send(int fd, void* buf, size_t size)
MiniTLS 2:527a66d0a1a9 101 {
MiniTLS 2:527a66d0a1a9 102 return lwip_send(fd, buf, size, 0);
MiniTLS 2:527a66d0a1a9 103 }
MiniTLS 2:527a66d0a1a9 104
MiniTLS 2:527a66d0a1a9 105 int socket_close(int fd)
MiniTLS 2:527a66d0a1a9 106 {
MiniTLS 2:527a66d0a1a9 107 return lwip_close(fd);
MiniTLS 2:527a66d0a1a9 108 }