2 * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014 Nicira, Inc.
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>
27 #include <sys/socket.h>
29 #include "dynamic-string.h"
31 #include "socket-util.h"
33 #include "stream-provider.h"
34 #include "stream-fd.h"
37 VLOG_DEFINE_THIS_MODULE(stream_tcp);
42 new_tcp_stream(const char *name, int fd, int connect_status,
43 struct stream **streamp)
48 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
50 int error = sock_errno();
51 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s",
52 name, sock_strerror(error));
57 return new_fd_stream(name, fd, connect_status, streamp);
61 tcp_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp)
65 error = inet_open_active(SOCK_STREAM, suffix, 0, NULL, &fd, dscp);
67 return new_tcp_stream(name, fd, error, streamp);
69 VLOG_ERR("%s: connect: %s", name, ovs_strerror(error));
74 const struct stream_class tcp_stream_class = {
76 true, /* needs_probes */
89 windows_open(const char *name, char *suffix, struct stream **streamp,
94 char *suffix_new, *path;
96 /* If the path does not contain a ':', assume it is relative to
98 if (!strchr(suffix, ':')) {
99 path = xasprintf("%s/%s", ovs_rundir(), suffix);
101 path = xstrdup(suffix);
104 file = fopen(path, "r");
107 VLOG_DBG("%s: could not open %s (%s)", name, suffix,
108 ovs_strerror(error));
112 error = fscanf(file, "%d", &port);
114 VLOG_ERR("failed to read port from %s", suffix);
120 suffix_new = xasprintf("127.0.0.1:%d", port);
122 error = tcp_open(name, suffix_new, streamp, dscp);
129 const struct stream_class windows_stream_class = {
131 false, /* needs_probes */
132 windows_open, /* open */
145 static int ptcp_accept(int fd, const struct sockaddr_storage *,
146 size_t, struct stream **streamp);
149 new_pstream(char *suffix, const char *name, struct pstream **pstreamp,
150 int dscp, char *unlink_path, bool kernel_print_port)
152 char bound_name[SS_NTOP_BUFSIZE + 16];
153 char addrbuf[SS_NTOP_BUFSIZE];
154 struct sockaddr_storage ss;
158 char *conn_name = CONST_CAST(char *, name);
160 fd = inet_open_passive(SOCK_STREAM, suffix, -1, &ss, dscp,
166 port = ss_get_port(&ss);
168 snprintf(bound_name, sizeof bound_name, "ptcp:%"PRIu16":%s",
169 port, ss_format_address(&ss, addrbuf, sizeof addrbuf));
170 conn_name = bound_name;
173 error = new_fd_pstream(conn_name, fd, ptcp_accept, set_dscp, unlink_path,
176 pstream_set_bound_port(*pstreamp, htons(port));
182 ptcp_open(const char *name OVS_UNUSED, char *suffix, struct pstream **pstreamp,
185 return new_pstream(suffix, NULL, pstreamp, dscp, NULL, true);
189 ptcp_accept(int fd, const struct sockaddr_storage *ss,
190 size_t ss_len OVS_UNUSED, struct stream **streamp)
192 char name[SS_NTOP_BUFSIZE + 16];
193 char addrbuf[SS_NTOP_BUFSIZE];
195 snprintf(name, sizeof name, "tcp:%s:%"PRIu16,
196 ss_format_address(ss, addrbuf, sizeof addrbuf),
198 return new_tcp_stream(name, fd, 0, streamp);
201 const struct pstream_class ptcp_pstream_class = {
213 pwindows_open(const char *name, char *suffix, struct pstream **pstreamp,
217 char *suffix_new, *path;
219 struct pstream *listener;
221 suffix_new = xstrdup("0:127.0.0.1");
223 /* If the path does not contain a ':', assume it is relative to
225 if (!strchr(suffix, ':')) {
226 path = xasprintf("%s/%s", ovs_rundir(), suffix);
228 path = xstrdup(suffix);
231 error = new_pstream(suffix_new, name, pstreamp, dscp, path, false);
235 listener = *pstreamp;
237 file = fopen(path, "w");
240 VLOG_DBG("could not open %s (%s)", path, ovs_strerror(error));
244 fprintf(file, "%d\n", ntohs(listener->bound_port));
245 if (fflush(file) == EOF) {
247 VLOG_ERR("write failed for %s", path);
258 const struct pstream_class pwindows_pstream_class = {