Add IPv6 support for OpenFlow, OVSDB, NetFlow, and sFlow.
[sliver-openvswitch.git] / lib / stream-tcp.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "stream.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/tcp.h>
24 #include <netdb.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/socket.h>
28 #include <unistd.h>
29 #include "dynamic-string.h"
30 #include "packets.h"
31 #include "socket-util.h"
32 #include "util.h"
33 #include "stream-provider.h"
34 #include "stream-fd.h"
35 #include "vlog.h"
36
37 VLOG_DEFINE_THIS_MODULE(stream_tcp);
38
39 /* Active TCP. */
40
41 static int
42 new_tcp_stream(const char *name, int fd, int connect_status,
43                struct stream **streamp)
44 {
45     struct sockaddr_storage local;
46     socklen_t local_len = sizeof local;
47     int on = 1;
48     int retval;
49
50     /* Get the local IP and port information */
51     retval = getsockname(fd, (struct sockaddr *) &local, &local_len);
52     if (retval) {
53         memset(&local, 0, sizeof local);
54     }
55
56     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
57     if (retval) {
58         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, ovs_strerror(errno));
59         close(fd);
60         return errno;
61     }
62
63     return new_fd_stream(name, fd, connect_status, streamp);
64 }
65
66 static int
67 tcp_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp)
68 {
69     int fd, error;
70
71     error = inet_open_active(SOCK_STREAM, suffix, 0, NULL, &fd, dscp);
72     if (fd >= 0) {
73         return new_tcp_stream(name, fd, error, streamp);
74     } else {
75         VLOG_ERR("%s: connect: %s", name, ovs_strerror(error));
76         return error;
77     }
78 }
79
80 const struct stream_class tcp_stream_class = {
81     "tcp",                      /* name */
82     true,                       /* needs_probes */
83     tcp_open,                   /* open */
84     NULL,                       /* close */
85     NULL,                       /* connect */
86     NULL,                       /* recv */
87     NULL,                       /* send */
88     NULL,                       /* run */
89     NULL,                       /* run_wait */
90     NULL,                       /* wait */
91 };
92 \f
93 /* Passive TCP. */
94
95 static int ptcp_accept(int fd, const struct sockaddr_storage *,
96                        size_t, struct stream **streamp);
97
98 static int
99 ptcp_open(const char *name OVS_UNUSED, char *suffix, struct pstream **pstreamp,
100           uint8_t dscp)
101 {
102     char bound_name[SS_NTOP_BUFSIZE + 16];
103     char addrbuf[SS_NTOP_BUFSIZE];
104     struct sockaddr_storage ss;
105     uint16_t port;
106     int error;
107     int fd;
108
109     fd = inet_open_passive(SOCK_STREAM, suffix, -1, &ss, dscp);
110     if (fd < 0) {
111         return -fd;
112     }
113
114     port = ss_get_port(&ss);
115     snprintf(bound_name, sizeof bound_name, "ptcp:%"PRIu16":%s",
116              port, ss_format_address(&ss, addrbuf, sizeof addrbuf));
117
118     error = new_fd_pstream(bound_name, fd, ptcp_accept, set_dscp, NULL,
119                            pstreamp);
120     if (!error) {
121         pstream_set_bound_port(*pstreamp, htons(port));
122     }
123     return error;
124 }
125
126 static int
127 ptcp_accept(int fd, const struct sockaddr_storage *ss,
128             size_t ss_len OVS_UNUSED, struct stream **streamp)
129 {
130     char name[SS_NTOP_BUFSIZE + 16];
131     char addrbuf[SS_NTOP_BUFSIZE];
132
133     snprintf(name, sizeof name, "tcp:%s:%"PRIu16,
134              ss_format_address(ss, addrbuf, sizeof addrbuf),
135              ss_get_port(ss));
136     return new_tcp_stream(name, fd, 0, streamp);
137 }
138
139 const struct pstream_class ptcp_pstream_class = {
140     "ptcp",
141     true,
142     ptcp_open,
143     NULL,
144     NULL,
145     NULL,
146     NULL,
147 };
148