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-stream.h"
51 #define THIS_MODULE VLM_vconn_tcp
56 new_tcp_vconn(const char *name, int fd, int connect_status,
57 const struct sockaddr_in *sin, struct vconn **vconnp)
62 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
64 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
69 return new_stream_vconn(name, fd, connect_status, sin->sin_addr.s_addr,
74 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
77 const char *host_name;
78 const char *port_string;
79 struct sockaddr_in sin;
83 /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
84 * can cause segfaults here:
85 * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
86 * Using "::" instead of the obvious ":" works around it. */
87 host_name = strtok_r(suffix, "::", &save_ptr);
88 port_string = strtok_r(NULL, "::", &save_ptr);
90 error(0, "%s: bad peer name format", name);
94 memset(&sin, 0, sizeof sin);
95 sin.sin_family = AF_INET;
96 if (lookup_ip(host_name, &sin.sin_addr)) {
99 sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
101 fd = socket(AF_INET, SOCK_STREAM, 0);
103 VLOG_ERR("%s: socket: %s", name, strerror(errno));
107 retval = set_nonblocking(fd);
113 retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
115 if (errno == EINPROGRESS) {
116 return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp);
119 VLOG_ERR("%s: connect: %s", name, strerror(error));
124 return new_tcp_vconn(name, fd, 0, &sin, vconnp);
128 struct vconn_class tcp_vconn_class = {
135 static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
136 struct vconn **vconnp);
139 ptcp_open(const char *name, char *suffix, struct vconn **vconnp)
141 struct sockaddr_in sin;
144 unsigned int yes = 1;
146 fd = socket(AF_INET, SOCK_STREAM, 0);
148 VLOG_ERR("%s: socket: %s", name, strerror(errno));
152 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
153 VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", name, strerror(errno));
157 memset(&sin, 0, sizeof sin);
158 sin.sin_family = AF_INET;
159 sin.sin_addr.s_addr = htonl(INADDR_ANY);
160 sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
161 retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
164 VLOG_ERR("%s: bind: %s", name, strerror(error));
169 return new_pstream_vconn("ptcp", fd, ptcp_accept, vconnp);
173 ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
174 struct vconn **vconnp)
176 const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
179 if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
180 sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin->sin_addr));
181 if (sin->sin_port != htons(OFP_TCP_PORT)) {
182 sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
187 return new_tcp_vconn(name, fd, 0, sin, vconnp);
190 struct vconn_class ptcp_vconn_class = {