2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/tcp.h>
28 #include "socket-util.h"
30 #include "openflow/openflow.h"
31 #include "vconn-provider.h"
32 #include "vconn-stream.h"
35 #define THIS_MODULE VLM_vconn_tcp
40 new_tcp_vconn(const char *name, int fd, int connect_status,
41 const struct sockaddr_in *sin, struct vconn **vconnp)
46 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
48 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
53 return new_stream_vconn(name, fd, connect_status, sin->sin_addr.s_addr,
58 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
61 const char *host_name;
62 const char *port_string;
63 struct sockaddr_in sin;
67 /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
68 * can cause segfaults here:
69 * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
70 * Using "::" instead of the obvious ":" works around it. */
71 host_name = strtok_r(suffix, "::", &save_ptr);
72 port_string = strtok_r(NULL, "::", &save_ptr);
74 ovs_error(0, "%s: bad peer name format", name);
78 memset(&sin, 0, sizeof sin);
79 sin.sin_family = AF_INET;
80 if (lookup_ip(host_name, &sin.sin_addr)) {
83 sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
85 fd = socket(AF_INET, SOCK_STREAM, 0);
87 VLOG_ERR("%s: socket: %s", name, strerror(errno));
91 retval = set_nonblocking(fd);
97 retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
99 if (errno == EINPROGRESS) {
100 return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp);
103 VLOG_ERR("%s: connect: %s", name, strerror(error));
108 return new_tcp_vconn(name, fd, 0, &sin, vconnp);
112 struct vconn_class tcp_vconn_class = {
124 static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
125 struct vconn **vconnp);
128 ptcp_open(const char *name, char *suffix, struct pvconn **pvconnp)
130 struct sockaddr_in sin;
133 unsigned int yes = 1;
135 fd = socket(AF_INET, SOCK_STREAM, 0);
137 VLOG_ERR("%s: socket: %s", name, strerror(errno));
141 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
142 VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", name, strerror(errno));
146 memset(&sin, 0, sizeof sin);
147 sin.sin_family = AF_INET;
148 sin.sin_addr.s_addr = htonl(INADDR_ANY);
149 sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
150 retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
153 VLOG_ERR("%s: bind: %s", name, strerror(error));
158 return new_pstream_pvconn("ptcp", fd, ptcp_accept, pvconnp);
162 ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
163 struct vconn **vconnp)
165 const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
168 if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
169 sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin->sin_addr));
170 if (sin->sin_port != htons(OFP_TCP_PORT)) {
171 sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
176 return new_tcp_vconn(name, fd, 0, sin, vconnp);
179 struct pvconn_class ptcp_pvconn_class = {