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 *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)
60 struct sockaddr_in sin;
63 error = tcp_open_active(suffix, OFP_TCP_PORT, NULL, &fd);
65 return new_tcp_vconn(name, fd, error, &sin, vconnp);
67 VLOG_ERR("%s: connect: %s", name, strerror(error));
72 struct vconn_class tcp_vconn_class = {
84 static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
85 struct vconn **vconnp);
88 ptcp_open(const char *name UNUSED, char *suffix, struct pvconn **pvconnp)
92 fd = tcp_open_passive(suffix, OFP_TCP_PORT);
96 return new_pstream_pvconn("ptcp", fd, ptcp_accept, pvconnp);
101 ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
102 struct vconn **vconnp)
104 const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
107 if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
108 sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin->sin_addr));
109 if (sin->sin_port != htons(OFP_TCP_PORT)) {
110 sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
115 return new_tcp_vconn(name, fd, 0, sin, vconnp);
118 struct pvconn_class ptcp_pvconn_class = {