Support SSL in secchan and controller.
[openvswitch] / lib / vconn-tcp.c
1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21
22 #include "vconn.h"
23 #include <assert.h>
24 #include <errno.h>
25 #include <netdb.h>
26 #include <poll.h>
27 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <netinet/tcp.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include "buffer.h"
34 #include "socket-util.h"
35 #include "util.h"
36 #include "openflow.h"
37 #include "ofp-print.h"
38
39 #include "vlog.h"
40 #define THIS_MODULE VLM_vconn_tcp
41
42 /* Active TCP. */
43
44 struct tcp_vconn
45 {
46     struct vconn vconn;
47     int fd;
48     struct buffer *rxbuf;
49     struct buffer *txbuf;
50 };
51
52 static int
53 new_tcp_vconn(const char *name, int fd, struct vconn **vconnp) 
54 {
55     struct tcp_vconn *tcp;
56     int on = 1;
57     int retval;
58
59     retval = set_nonblocking(fd);
60     if (retval) {
61         VLOG_ERR("%s: set_nonblocking: %s", name, strerror(retval));
62         close(fd);
63         return retval;
64     }
65
66     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
67     if (retval) {
68         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
69         close(fd);
70         return errno;
71     }
72
73     tcp = xmalloc(sizeof *tcp);
74     tcp->vconn.class = &tcp_vconn_class;
75     tcp->fd = fd;
76     tcp->txbuf = NULL;
77     tcp->rxbuf = NULL;
78     *vconnp = &tcp->vconn;
79     return 0;
80 }
81
82 static struct tcp_vconn *
83 tcp_vconn_cast(struct vconn *vconn) 
84 {
85     assert(vconn->class == &tcp_vconn_class);
86     return CONTAINER_OF(vconn, struct tcp_vconn, vconn); 
87 }
88
89
90 static int
91 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
92 {
93     char *save_ptr;
94     const char *host_name;
95     const char *port_string;
96     struct sockaddr_in sin;
97     int retval;
98     int fd;
99
100     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
101      * can cause segfaults here:
102      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
103      * Using "::" instead of the obvious ":" works around it. */
104     host_name = strtok_r(suffix, "::", &save_ptr);
105     port_string = strtok_r(NULL, "::", &save_ptr);
106     if (!host_name) {
107         fatal(0, "%s: bad peer name format", name);
108     }
109
110     memset(&sin, 0, sizeof sin);
111     sin.sin_family = AF_INET;
112     if (lookup_ip(host_name, &sin.sin_addr)) {
113         return ENOENT;
114     }
115     sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
116
117     fd = socket(AF_INET, SOCK_STREAM, 0);
118     if (fd < 0) {
119         VLOG_ERR("%s: socket: %s", name, strerror(errno));
120         return errno;
121     }
122
123     retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
124     if (retval < 0) {
125         int error = errno;
126         VLOG_ERR("%s: connect: %s", name, strerror(error));
127         close(fd);
128         return error;
129     }
130
131     return new_tcp_vconn(name, fd, vconnp);
132 }
133
134 static void
135 tcp_close(struct vconn *vconn) 
136 {
137     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
138     close(tcp->fd);
139     free(tcp);
140 }
141
142 static bool
143 tcp_prepoll(struct vconn *vconn, int want, struct pollfd *pfd) 
144 {
145     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
146     pfd->fd = tcp->fd;
147     if (want & WANT_RECV) {
148         pfd->events |= POLLIN;
149     }
150     if (want & WANT_SEND || tcp->txbuf) {
151         pfd->events |= POLLOUT;
152     }
153     return false;
154 }
155
156 static void
157 tcp_postpoll(struct vconn *vconn, short int *revents)
158 {
159     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
160     if (*revents & POLLOUT && tcp->txbuf) {
161         ssize_t n = write(tcp->fd, tcp->txbuf->data, tcp->txbuf->size);
162         if (n < 0) {
163             if (errno != EAGAIN) {
164                 VLOG_ERR("send: %s", strerror(errno));
165                 *revents |= POLLERR;
166             }
167         } else if (n > 0) {
168             buffer_pull(tcp->txbuf, n);
169             if (tcp->txbuf->size == 0) {
170                 buffer_delete(tcp->txbuf);
171                 tcp->txbuf = NULL;
172             }
173         }
174         if (tcp->txbuf) {
175             *revents &= ~POLLOUT;
176         }
177     }
178 }
179
180 static int
181 tcp_recv(struct vconn *vconn, struct buffer **bufferp)
182 {
183     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
184     struct buffer *rx;
185     size_t want_bytes;
186     ssize_t retval;
187
188     if (tcp->rxbuf == NULL) {
189         tcp->rxbuf = buffer_new(1564);
190     }
191     rx = tcp->rxbuf;
192
193 again:
194     if (sizeof(struct ofp_header) > rx->size) {
195         want_bytes = sizeof(struct ofp_header) - rx->size;
196     } else {
197         struct ofp_header *oh = rx->data;
198         size_t length = ntohs(oh->length);
199         if (length < sizeof(struct ofp_header)) {
200             VLOG_ERR("received too-short ofp_header (%zu bytes)", length);
201             return EPROTO;
202         }
203         want_bytes = length - rx->size;
204     }
205     buffer_reserve_tailroom(rx, want_bytes);
206
207     retval = read(tcp->fd, buffer_tail(rx), want_bytes);
208     if (retval > 0) {
209         rx->size += retval;
210         if (retval == want_bytes) {
211             if (rx->size > sizeof(struct ofp_header)) {
212                 *bufferp = rx;
213                 tcp->rxbuf = NULL;
214                 return 0;
215             } else {
216                 goto again;
217             }
218         }
219         return EAGAIN;
220     } else if (retval == 0) {
221         return rx->size ? EPROTO : EOF;
222     } else {
223         return retval ? errno : EAGAIN;
224     }
225 }
226
227 static int
228 tcp_send(struct vconn *vconn, struct buffer *buffer) 
229 {
230     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
231     ssize_t retval;
232
233     if (tcp->txbuf) {
234         return EAGAIN;
235     }
236
237     retval = write(tcp->fd, buffer->data, buffer->size);
238     if (retval == buffer->size) {
239         buffer_delete(buffer);
240         return 0;
241     } else if (retval >= 0 || errno == EAGAIN) {
242         tcp->txbuf = buffer;
243         if (retval > 0) {
244             buffer_pull(buffer, retval);
245         }
246         return 0;
247     } else {
248         return errno;
249     }
250 }
251
252 struct vconn_class tcp_vconn_class = {
253     .name = "tcp",
254     .open = tcp_open,
255     .close = tcp_close,
256     .prepoll = tcp_prepoll,
257     .postpoll = tcp_postpoll,
258     .recv = tcp_recv,
259     .send = tcp_send,
260 };
261 \f
262 /* Passive TCP. */
263
264 struct ptcp_vconn
265 {
266     struct vconn vconn;
267     int fd;
268 };
269
270 static struct ptcp_vconn *
271 ptcp_vconn_cast(struct vconn *vconn) 
272 {
273     assert(vconn->class == &ptcp_vconn_class);
274     return CONTAINER_OF(vconn, struct ptcp_vconn, vconn); 
275 }
276
277 static int
278 ptcp_open(const char *name, char *suffix, struct vconn **vconnp)
279 {
280     struct sockaddr_in sin;
281     struct ptcp_vconn *ptcp;
282     int retval;
283     int fd;
284     unsigned int yes  = 1;
285
286     fd = socket(AF_INET, SOCK_STREAM, 0);
287     if (fd < 0) {
288         VLOG_ERR("%s: socket: %s", name, strerror(errno));
289         return errno;
290     }
291
292     if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
293         VLOG_ERR("%s: setsockopt::SO_REUSEADDR: %s", name, strerror(errno));
294         return errno;
295     }
296
297
298     memset(&sin, 0, sizeof sin);
299     sin.sin_family = AF_INET;
300     sin.sin_addr.s_addr = htonl(INADDR_ANY);
301     sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
302     retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
303     if (retval < 0) {
304         int error = errno;
305         VLOG_ERR("%s: bind: %s", name, strerror(error));
306         close(fd);
307         return error;
308     }
309
310     retval = listen(fd, 10);
311     if (retval < 0) {
312         int error = errno;
313         VLOG_ERR("%s: listen: %s", name, strerror(error));
314         close(fd);
315         return error;
316     }
317
318     retval = set_nonblocking(fd);
319     if (retval) {
320         VLOG_ERR("%s: set_nonblocking: %s", name, strerror(retval));
321         close(fd);
322         return retval;
323     }
324
325     ptcp = xmalloc(sizeof *ptcp);
326     ptcp->vconn.class = &ptcp_vconn_class;
327     ptcp->fd = fd;
328     *vconnp = &ptcp->vconn;
329     return 0;
330 }
331
332 static void
333 ptcp_close(struct vconn *vconn) 
334 {
335     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
336     close(ptcp->fd);
337     free(ptcp);
338 }
339
340 static bool
341 ptcp_prepoll(struct vconn *vconn, int want, struct pollfd *pfd) 
342 {
343     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
344     pfd->fd = ptcp->fd;
345     if (want & WANT_ACCEPT) {
346         pfd->events |= POLLIN;
347     }
348     return false;
349 }
350
351 static int
352 ptcp_accept(struct vconn *vconn, struct vconn **new_vconnp) 
353 {
354     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
355     int new_fd;
356
357     new_fd = accept(ptcp->fd, NULL, NULL);
358     if (new_fd < 0) {
359         return errno;
360     }
361
362     return new_tcp_vconn("tcp" /* FIXME */, new_fd, new_vconnp);
363 }
364
365 struct vconn_class ptcp_vconn_class = {
366     .name = "ptcp",
367     .open = ptcp_open,
368     .close = ptcp_close,
369     .prepoll = ptcp_prepoll,
370     .accept = ptcp_accept,
371 };
372