1924a60495b1d48777cac0588b1633866541f2d5
[bertos.git] / bertos / net / tcp_socket.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2012 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief TCP sockect with kfile interface.
34  *
35  * \author Luca Ottaviano <lottaviano@develer.com>
36  * \author Daniele Basile <asterix@develer.com>
37  */
38
39 #include "tcp_socket.h"
40
41 #define LOG_LEVEL   LOG_LVL_WARN
42 #define LOG_FORMAT  LOG_FMT_TERSE
43 #include <cfg/log.h>
44 #include <cpu/byteorder.h>
45
46 #include <lwip/ip_addr.h>
47 #include <lwip/netif.h>
48 #include <lwip/netbuf.h>
49 #include <lwip/tcpip.h>
50
51 static int tcpConnect(TcpSocket *socket)
52 {
53         socket->sock = netconn_new(NETCONN_TCP);
54         ASSERT(socket->sock);
55
56         if(netconn_bind(socket->sock, socket->local_addr, socket->port) != ERR_OK)
57         {
58                 LOG_ERR("Connection error\n");
59                 goto error;
60         }
61
62         if(netconn_connect(socket->sock, socket->remote_addr, socket->port) != ERR_OK)
63         {
64                 LOG_ERR("Cannot create socket\n");
65                 goto error;
66         }
67
68         LOG_INFO("connected ip=%d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(socket->local_addr->addr));
69         return 0;
70
71 error:
72         netconn_delete(socket->sock);
73         socket->sock = NULL;
74         return -1;
75 }
76
77
78 static bool reconnect(TcpSocket *socket)
79 {
80         LOG_INFO("Reconnecting...\n");
81         // Release old socket if needed
82         if (socket->sock)
83         {
84                 if (netconn_delete(socket->sock) != ERR_OK)
85                         LOG_ERR("Error closing socket\n");
86
87                 socket->sock = NULL;
88         }
89
90         // Connect to our peer peer
91         if (tcpConnect(socket) < 0)
92         {
93                 LOG_ERR("Reconnect error!\n");
94                 socket->error |= ERR_TCP_NOTCONN;
95                 return false;
96         }
97
98         LOG_INFO("Reconnecting DONE!\n");
99
100         return true;
101 }
102
103 static int tcpsocket_close(KFile *fd)
104 {
105         TcpSocket *socket = TCPSOCKET_CAST(fd);
106         int ret = netconn_delete(socket->sock);
107         socket->sock = NULL;
108
109         if (ret)
110         {
111                 LOG_ERR("Close error\n");
112                 socket->error |= ERR_CONN_CLOSE;
113                 return EOF;
114         }
115         return 0;
116 }
117
118 static size_t tcpsocket_read(KFile *fd, void *buf, size_t len)
119 {
120         TcpSocket *socket = TCPSOCKET_CAST(fd);
121         uint16_t recv_len = 0;
122
123         // Try reconnecting if our socket isn't valid
124         if (!socket->sock)
125         {
126                 if (!reconnect(socket))
127                         return 0;
128         }
129
130         while (len)
131         {
132                 struct netbuf *rx_buf_conn = netconn_recv(socket->sock);
133                 if (rx_buf_conn)
134                 {
135                         netbuf_data(rx_buf_conn, (void **)&buf, &recv_len);
136
137                         socket->error |= ERR_RECV_DATA;
138                         return recv_len;
139                 }
140
141                 // Do we have an EOF condition? If so, bailout.
142                 if (recv_len == 0 && len != 0)
143                 {
144                         LOG_INFO("Connection reset by peer\n");
145                         socket->error = 0;
146
147                         if (tcpsocket_close(fd) == EOF)
148                                 LOG_ERR("Error closing socket, leak detected\n");
149
150                         return recv_len;
151                 }
152                 len -= recv_len;
153         }
154
155         return recv_len;
156 }
157
158 static size_t tcpsocket_write(KFile *fd, const void *buf, size_t len)
159 {
160         TcpSocket *socket = TCPSOCKET_CAST(fd);
161         ssize_t result;
162
163         // Try reconnecting if our socket isn't valid
164         if (!socket->sock)
165         {
166                 if (!reconnect(socket))
167                         return 0;
168         }
169
170         result = netconn_write(socket->sock, buf, len, NETCONN_COPY);
171         if (result != ERR_OK)
172         {
173                 LOG_ERR("While writing %d\n", result);
174                 if (result == ERR_RST)
175                 {
176                         LOG_INFO("Connection close\n");
177
178                         if (tcpsocket_close(fd) == EOF)
179                                 LOG_ERR("Error closing socket, leak detected\n");
180                         return 0;
181                 }
182                 return 0;
183         }
184
185         return len;
186 }
187
188 static int tcpsocket_error(KFile *fd)
189 {
190         TcpSocket *socket = TCPSOCKET_CAST(fd);
191         return socket->error;
192 }
193
194 static void tcpsocket_clearerr(KFile *fd)
195 {
196         TcpSocket *socket = TCPSOCKET_CAST(fd);
197         socket->error = 0;
198 }
199
200 void tcpsocket_init(TcpSocket *socket, struct ip_addr *local_addr, struct ip_addr *remote_addr, uint16_t port)
201 {
202         socket->sock = NULL;
203         socket->local_addr = local_addr;
204         socket->remote_addr = remote_addr;
205         socket->port = port;
206
207         socket->fd._type = KFT_TCPSOCKET;
208         socket->fd.read = tcpsocket_read;
209         socket->fd.error = tcpsocket_error;
210         socket->fd.close = tcpsocket_close;
211         socket->fd.write = tcpsocket_write;
212         socket->fd.clearerr = tcpsocket_clearerr;
213
214 }