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:
Fri Jun 06 10:49:02 2014 +0000
Revision:
0:35aa5be3b78d
Initial commit

Who changed what in which revision?

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