2 * Copyright (c) 2008, 2009 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 *remote, struct vconn **vconnp)
43 struct sockaddr_in local;
44 socklen_t local_len = sizeof local;
48 /* Get the local IP and port information */
49 retval = getsockname(fd, (struct sockaddr *)&local, &local_len);
51 memset(&local, 0, sizeof local);
54 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
56 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
61 retval = new_stream_vconn(name, fd, connect_status, true, vconnp);
63 struct vconn *vconn = *vconnp;
64 vconn_set_remote_ip(vconn, remote->sin_addr.s_addr);
65 vconn_set_remote_port(vconn, remote->sin_port);
66 vconn_set_local_ip(vconn, local.sin_addr.s_addr);
67 vconn_set_local_port(vconn, local.sin_port);
73 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
76 const char *host_name;
77 const char *port_string;
78 struct sockaddr_in sin;
82 host_name = strtok_r(suffix, ":", &save_ptr);
83 port_string = strtok_r(NULL, ":", &save_ptr);
85 ovs_error(0, "%s: bad peer name format", name);
89 memset(&sin, 0, sizeof sin);
90 sin.sin_family = AF_INET;
91 if (lookup_ip(host_name, &sin.sin_addr)) {
94 sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
96 fd = socket(AF_INET, SOCK_STREAM, 0);
98 VLOG_ERR("%s: socket: %s", name, strerror(errno));
102 retval = set_nonblocking(fd);
108 retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
110 if (errno == EINPROGRESS) {
111 return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp);
114 VLOG_ERR("%s: connect: %s", name, strerror(error));
119 return new_tcp_vconn(name, fd, 0, &sin, vconnp);
123 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 pvconn **pvconnp)
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_pvconn("ptcp", fd, ptcp_accept, pvconnp);
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 pvconn_class ptcp_pvconn_class = {