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.
socket/socket_lwip.c@4:cbaf466d717d, 2014-06-10 (annotated)
- Committer:
- MiniTLS
- Date:
- Tue Jun 10 14:23:09 2014 +0000
- Revision:
- 4:cbaf466d717d
- Parent:
- 3:eb324ffffd2b
Fixes for mbed
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:eb324ffffd2b | 25 | #define __DEBUG__ 4 |
MiniTLS | 3:eb324ffffd2b | 26 | #ifndef __MODULE__ |
MiniTLS | 3:eb324ffffd2b | 27 | #define __MODULE__ "socket_lwip.c" |
MiniTLS | 3:eb324ffffd2b | 28 | #endif |
MiniTLS | 3:eb324ffffd2b | 29 | |
MiniTLS | 2:527a66d0a1a9 | 30 | #include "core/fwk.h" |
MiniTLS | 2:527a66d0a1a9 | 31 | |
MiniTLS | 2:527a66d0a1a9 | 32 | #include "socket.h" |
MiniTLS | 2:527a66d0a1a9 | 33 | |
MiniTLS | 2:527a66d0a1a9 | 34 | #include "lwip/sockets.h" |
MiniTLS | 2:527a66d0a1a9 | 35 | #include "lwip/inet.h" |
MiniTLS | 2:527a66d0a1a9 | 36 | #include "lwip/netdb.h" |
MiniTLS | 2:527a66d0a1a9 | 37 | |
MiniTLS | 2:527a66d0a1a9 | 38 | int socket_socket() |
MiniTLS | 2:527a66d0a1a9 | 39 | { |
MiniTLS | 2:527a66d0a1a9 | 40 | return lwip_socket(AF_INET, SOCK_STREAM, 0); |
MiniTLS | 2:527a66d0a1a9 | 41 | } |
MiniTLS | 2:527a66d0a1a9 | 42 | |
MiniTLS | 2:527a66d0a1a9 | 43 | |
MiniTLS | 2:527a66d0a1a9 | 44 | int socket_connect(int fd, const char* hostname, uint16_t port) |
MiniTLS | 2:527a66d0a1a9 | 45 | { |
MiniTLS | 3:eb324ffffd2b | 46 | struct hostent* hostent = lwip_gethostbyname(hostname); |
MiniTLS | 2:527a66d0a1a9 | 47 | if(hostent == NULL) |
MiniTLS | 3:eb324ffffd2b | 48 | { |
MiniTLS | 3:eb324ffffd2b | 49 | WARN("Could not resolve %s\r\n", hostname); |
MiniTLS | 3:eb324ffffd2b | 50 | return -1; |
MiniTLS | 3:eb324ffffd2b | 51 | } |
MiniTLS | 2:527a66d0a1a9 | 52 | struct sockaddr_in server; |
MiniTLS | 3:eb324ffffd2b | 53 | memset(&server, 0, sizeof(struct sockaddr_in)); |
MiniTLS | 2:527a66d0a1a9 | 54 | server.sin_family = AF_INET; |
MiniTLS | 3:eb324ffffd2b | 55 | memcpy(&server.sin_addr.s_addr, hostent->h_addr_list[0], hostent->h_length); |
MiniTLS | 3:eb324ffffd2b | 56 | //server.sin_addr.s_addr = inet_addr(hostname); |
MiniTLS | 2:527a66d0a1a9 | 57 | server.sin_port = htons(port); |
MiniTLS | 2:527a66d0a1a9 | 58 | |
MiniTLS | 3:eb324ffffd2b | 59 | DBG("Trying to connect to %s:%d", inet_ntoa(server.sin_addr.s_addr), port); |
MiniTLS | 2:527a66d0a1a9 | 60 | |
MiniTLS | 3:eb324ffffd2b | 61 | return lwip_connect(fd, (const struct sockaddr*)&server, sizeof(struct sockaddr_in)); |
MiniTLS | 2:527a66d0a1a9 | 62 | } |
MiniTLS | 2:527a66d0a1a9 | 63 | |
MiniTLS | 2:527a66d0a1a9 | 64 | int socket_wait_readable(int fd, int timeout_ms) |
MiniTLS | 2:527a66d0a1a9 | 65 | { |
MiniTLS | 2:527a66d0a1a9 | 66 | //Wait for record to be readable |
MiniTLS | 2:527a66d0a1a9 | 67 | fd_set set; |
MiniTLS | 2:527a66d0a1a9 | 68 | FD_ZERO(&set); |
MiniTLS | 2:527a66d0a1a9 | 69 | FD_SET(fd, &set); |
MiniTLS | 2:527a66d0a1a9 | 70 | |
MiniTLS | 2:527a66d0a1a9 | 71 | struct timeval timeout; |
MiniTLS | 2:527a66d0a1a9 | 72 | timeout.tv_sec = timeout_ms / 1000; |
MiniTLS | 2:527a66d0a1a9 | 73 | timeout.tv_usec = (timeout_ms - (timeout.tv_sec * 1000)) * 1000; |
MiniTLS | 2:527a66d0a1a9 | 74 | |
MiniTLS | 2:527a66d0a1a9 | 75 | int r = lwip_select(fd + 1, &set, NULL, NULL, &timeout); |
MiniTLS | 2:527a66d0a1a9 | 76 | if( (r <= 0) || !FD_ISSET(fd, &set) ) |
MiniTLS | 2:527a66d0a1a9 | 77 | { |
MiniTLS | 2:527a66d0a1a9 | 78 | return -1; |
MiniTLS | 2:527a66d0a1a9 | 79 | } |
MiniTLS | 2:527a66d0a1a9 | 80 | return 0; |
MiniTLS | 2:527a66d0a1a9 | 81 | } |
MiniTLS | 2:527a66d0a1a9 | 82 | |
MiniTLS | 2:527a66d0a1a9 | 83 | int socket_wait_writeable(int fd, int timeout_ms) |
MiniTLS | 2:527a66d0a1a9 | 84 | { |
MiniTLS | 2:527a66d0a1a9 | 85 | //Wait for record to be writeable |
MiniTLS | 2:527a66d0a1a9 | 86 | fd_set set; |
MiniTLS | 2:527a66d0a1a9 | 87 | FD_ZERO(&set); |
MiniTLS | 2:527a66d0a1a9 | 88 | FD_SET(fd, &set); |
MiniTLS | 2:527a66d0a1a9 | 89 | |
MiniTLS | 2:527a66d0a1a9 | 90 | struct timeval timeout; |
MiniTLS | 2:527a66d0a1a9 | 91 | timeout.tv_sec = timeout_ms / 1000; |
MiniTLS | 2:527a66d0a1a9 | 92 | timeout.tv_usec = (timeout_ms - (timeout.tv_sec * 1000)) * 1000; |
MiniTLS | 2:527a66d0a1a9 | 93 | |
MiniTLS | 2:527a66d0a1a9 | 94 | int r = lwip_select(fd + 1, NULL, &set, NULL, &timeout); |
MiniTLS | 2:527a66d0a1a9 | 95 | if( (r <= 0) || !FD_ISSET(fd, &set) ) |
MiniTLS | 2:527a66d0a1a9 | 96 | { |
MiniTLS | 2:527a66d0a1a9 | 97 | return -1; |
MiniTLS | 2:527a66d0a1a9 | 98 | } |
MiniTLS | 2:527a66d0a1a9 | 99 | return 0; |
MiniTLS | 2:527a66d0a1a9 | 100 | } |
MiniTLS | 2:527a66d0a1a9 | 101 | |
MiniTLS | 2:527a66d0a1a9 | 102 | int socket_recv(int fd, void* buf, size_t size) |
MiniTLS | 2:527a66d0a1a9 | 103 | { |
MiniTLS | 2:527a66d0a1a9 | 104 | return lwip_recv(fd, buf, size, 0); |
MiniTLS | 2:527a66d0a1a9 | 105 | } |
MiniTLS | 2:527a66d0a1a9 | 106 | |
MiniTLS | 2:527a66d0a1a9 | 107 | int socket_send(int fd, void* buf, size_t size) |
MiniTLS | 2:527a66d0a1a9 | 108 | { |
MiniTLS | 2:527a66d0a1a9 | 109 | return lwip_send(fd, buf, size, 0); |
MiniTLS | 2:527a66d0a1a9 | 110 | } |
MiniTLS | 2:527a66d0a1a9 | 111 | |
MiniTLS | 2:527a66d0a1a9 | 112 | int socket_close(int fd) |
MiniTLS | 2:527a66d0a1a9 | 113 | { |
MiniTLS | 2:527a66d0a1a9 | 114 | return lwip_close(fd); |
MiniTLS | 2:527a66d0a1a9 | 115 | } |