1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
45 #include "socket-util.h"
48 #include "vconn-provider.h"
49 #include "vconn-stream.h"
52 #define THIS_MODULE VLM_vconn_tcp
57 new_tcp_vconn(const char *name, int fd, int connect_status,
58 const struct sockaddr_in *sin, struct vconn **vconnp)
63 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
65 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
70 return new_stream_vconn(name, fd, connect_status, sin->sin_addr.s_addr,
75 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
78 const char *host_name;
79 const char *port_string;
80 struct sockaddr_in sin;
84 /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
85 * can cause segfaults here:
86 * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
87 * Using "::" instead of the obvious ":" works around it. */
88 host_name = strtok_r(suffix, "::", &save_ptr);
89 port_string = strtok_r(NULL, "::", &save_ptr);
91 ofp_error(0, "%s: bad peer name format", name);
95 memset(&sin, 0, sizeof sin);
96 sin.sin_family = AF_INET;
97 if (lookup_ip(host_name, &sin.sin_addr)) {
100 sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
102 fd = socket(AF_INET, SOCK_STREAM, 0);
104 VLOG_ERR("%s: socket: %s", name, strerror(errno));
108 retval = set_nonblocking(fd);
114 retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
116 if (errno == EINPROGRESS) {
117 return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp);
120 VLOG_ERR("%s: connect: %s", name, strerror(error));
125 return new_tcp_vconn(name, fd, 0, &sin, vconnp);
129 struct vconn_class tcp_vconn_class = {
141 static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
142 struct vconn **vconnp);
145 ptcp_open(const char *name, char *suffix, struct pvconn **pvconnp)
147 struct sockaddr_in sin;
150 unsigned int yes = 1;
152 fd = socket(AF_INET, SOCK_STREAM, 0);
154 VLOG_ERR("%s: socket: %s", name, strerror(errno));
158 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
159 VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", name, strerror(errno));
163 memset(&sin, 0, sizeof sin);
164 sin.sin_family = AF_INET;
165 sin.sin_addr.s_addr = htonl(INADDR_ANY);
166 sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
167 retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
170 VLOG_ERR("%s: bind: %s", name, strerror(error));
175 return new_pstream_pvconn("ptcp", fd, ptcp_accept, pvconnp);
179 ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
180 struct vconn **vconnp)
182 const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
185 if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
186 sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin->sin_addr));
187 if (sin->sin_port != htons(OFP_TCP_PORT)) {
188 sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
193 return new_tcp_vconn(name, fd, 0, sin, vconnp);
196 struct pvconn_class ptcp_pvconn_class = {