]> git.proxmox.com Git - mirror_ovs.git/blob - lib/vconn-tcp.c
Update primary code license to Apache 2.0.
[mirror_ovs.git] / lib / vconn-tcp.c
1 /*
2 * Copyright (c) 2008, 2009 Nicira Networks.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include "vconn.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/tcp.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include "packets.h"
28 #include "socket-util.h"
29 #include "util.h"
30 #include "openflow/openflow.h"
31 #include "vconn-provider.h"
32 #include "vconn-stream.h"
33
34 #include "vlog.h"
35 #define THIS_MODULE VLM_vconn_tcp
36
37 /* Active TCP. */
38
39 static int
40 new_tcp_vconn(const char *name, int fd, int connect_status,
41 const struct sockaddr_in *sin, struct vconn **vconnp)
42 {
43 int on = 1;
44 int retval;
45
46 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
47 if (retval) {
48 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
49 close(fd);
50 return errno;
51 }
52
53 return new_stream_vconn(name, fd, connect_status, sin->sin_addr.s_addr,
54 true, vconnp);
55 }
56
57 static int
58 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
59 {
60 char *save_ptr;
61 const char *host_name;
62 const char *port_string;
63 struct sockaddr_in sin;
64 int retval;
65 int fd;
66
67 host_name = strtok_r(suffix, ":", &save_ptr);
68 port_string = strtok_r(NULL, ":", &save_ptr);
69 if (!host_name) {
70 ovs_error(0, "%s: bad peer name format", name);
71 return EAFNOSUPPORT;
72 }
73
74 memset(&sin, 0, sizeof sin);
75 sin.sin_family = AF_INET;
76 if (lookup_ip(host_name, &sin.sin_addr)) {
77 return ENOENT;
78 }
79 sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
80
81 fd = socket(AF_INET, SOCK_STREAM, 0);
82 if (fd < 0) {
83 VLOG_ERR("%s: socket: %s", name, strerror(errno));
84 return errno;
85 }
86
87 retval = set_nonblocking(fd);
88 if (retval) {
89 close(fd);
90 return retval;
91 }
92
93 retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
94 if (retval < 0) {
95 if (errno == EINPROGRESS) {
96 return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp);
97 } else {
98 int error = errno;
99 VLOG_ERR("%s: connect: %s", name, strerror(error));
100 close(fd);
101 return error;
102 }
103 } else {
104 return new_tcp_vconn(name, fd, 0, &sin, vconnp);
105 }
106 }
107
108 struct vconn_class tcp_vconn_class = {
109 "tcp", /* name */
110 tcp_open, /* open */
111 NULL, /* close */
112 NULL, /* connect */
113 NULL, /* recv */
114 NULL, /* send */
115 NULL, /* wait */
116 };
117 \f
118 /* Passive TCP. */
119
120 static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
121 struct vconn **vconnp);
122
123 static int
124 ptcp_open(const char *name, char *suffix, struct pvconn **pvconnp)
125 {
126 struct sockaddr_in sin;
127 int retval;
128 int fd;
129 unsigned int yes = 1;
130
131 fd = socket(AF_INET, SOCK_STREAM, 0);
132 if (fd < 0) {
133 VLOG_ERR("%s: socket: %s", name, strerror(errno));
134 return errno;
135 }
136
137 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
138 VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", name, strerror(errno));
139 return errno;
140 }
141
142 memset(&sin, 0, sizeof sin);
143 sin.sin_family = AF_INET;
144 sin.sin_addr.s_addr = htonl(INADDR_ANY);
145 sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
146 retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
147 if (retval < 0) {
148 int error = errno;
149 VLOG_ERR("%s: bind: %s", name, strerror(error));
150 close(fd);
151 return error;
152 }
153
154 return new_pstream_pvconn("ptcp", fd, ptcp_accept, pvconnp);
155 }
156
157 static int
158 ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
159 struct vconn **vconnp)
160 {
161 const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
162 char name[128];
163
164 if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
165 sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin->sin_addr));
166 if (sin->sin_port != htons(OFP_TCP_PORT)) {
167 sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
168 }
169 } else {
170 strcpy(name, "tcp");
171 }
172 return new_tcp_vconn(name, fd, 0, sin, vconnp);
173 }
174
175 struct pvconn_class ptcp_pvconn_class = {
176 "ptcp",
177 ptcp_open,
178 NULL,
179 NULL,
180 NULL
181 };
182