ARM Shanghai IoT Team (Internal) / newMiniTLS-GPL

Fork of MiniTLS-GPL by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers socket_lwip.c Source File

socket_lwip.c

Go to the documentation of this file.
00001 /*
00002 MiniTLS - A super trimmed down TLS/SSL Library for embedded devices
00003 Author: Donatien Garnier
00004 Copyright (C) 2013-2014 AppNearMe Ltd
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program; if not, write to the Free Software
00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 *//**
00020  * \file socket_lwip.c
00021  * \copyright Copyright (c) AppNearMe Ltd 2013
00022  * \author Donatien Garnier
00023  */
00024 
00025 #define __DEBUG__ 4
00026 #ifndef __MODULE__
00027 #define __MODULE__ "socket_lwip.c"
00028 #endif
00029 
00030 #include "core/fwk.h"
00031 
00032 #include "socket.h"
00033 
00034 #include "lwip/sockets.h"
00035 #include "lwip/inet.h"
00036 #include "lwip/netdb.h"
00037 
00038 int socket_socket()
00039 {
00040   return lwip_socket(AF_INET, SOCK_STREAM, 0);
00041 }
00042 
00043 
00044 int socket_connect(int fd, const char* hostname, uint16_t port)
00045 {
00046   struct hostent* hostent = lwip_gethostbyname(hostname);
00047   if(hostent == NULL)
00048   { 
00049     WARN("Could not resolve %s\r\n", hostname);
00050     return -1;
00051   }
00052   struct sockaddr_in server;
00053   memset(&server, 0, sizeof(struct sockaddr_in));
00054   server.sin_family = AF_INET;
00055   memcpy(&server.sin_addr.s_addr, hostent->h_addr_list[0], hostent->h_length);
00056   //server.sin_addr.s_addr = inet_addr(hostname);
00057   server.sin_port = htons(port);
00058 
00059   DBG("Trying to connect to %s:%d", inet_ntoa(server.sin_addr.s_addr), port);
00060 
00061   return lwip_connect(fd, (const struct sockaddr*)&server, sizeof(struct sockaddr_in));
00062 }
00063 
00064 int socket_wait_readable(int fd, int timeout_ms)
00065 {
00066   //Wait for record to be readable
00067   fd_set set;
00068   FD_ZERO(&set);
00069   FD_SET(fd, &set);
00070 
00071   struct timeval timeout;
00072   timeout.tv_sec = timeout_ms / 1000;
00073   timeout.tv_usec = (timeout_ms - (timeout.tv_sec * 1000)) * 1000;
00074 
00075   int r = lwip_select(fd + 1, &set, NULL, NULL, &timeout);
00076   if( (r <= 0) || !FD_ISSET(fd, &set) )
00077   {
00078     return -1;
00079   }
00080   return 0;
00081 }
00082 
00083 int socket_wait_writeable(int fd, int timeout_ms)
00084 {
00085   //Wait for record to be writeable
00086   fd_set set;
00087   FD_ZERO(&set);
00088   FD_SET(fd, &set);
00089 
00090   struct timeval timeout;
00091   timeout.tv_sec = timeout_ms / 1000;
00092   timeout.tv_usec = (timeout_ms - (timeout.tv_sec * 1000)) * 1000;
00093 
00094   int r = lwip_select(fd + 1, NULL, &set, NULL, &timeout);
00095   if( (r <= 0) || !FD_ISSET(fd, &set) )
00096   {
00097     return -1;
00098   }
00099   return 0;
00100 }
00101 
00102 int socket_recv(int fd, void* buf, size_t size)
00103 {
00104   return lwip_recv(fd, buf, size, 0);
00105 }
00106 
00107 int socket_send(int fd, void* buf, size_t size)
00108 {
00109   return lwip_send(fd, buf, size, 0);
00110 }
00111 
00112 int socket_close(int fd)
00113 {
00114   return lwip_close(fd);
00115 }