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)
45 struct sockaddr_storage local;
46 socklen_t local_len = sizeof local;
50 /* Get the local IP and port information */
51 retval = getsockname(fd, (struct sockaddr *) &local, &local_len);
53 memset(&local, 0, sizeof local);
56 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
58 int error = sock_errno();
59 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s",
60 name, sock_strerror(error));
65 return new_fd_stream(name, fd, connect_status, streamp);
69 tcp_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp)
73 error = inet_open_active(SOCK_STREAM, suffix, 0, NULL, &fd, dscp);
75 return new_tcp_stream(name, fd, error, streamp);
77 VLOG_ERR("%s: connect: %s", name, ovs_strerror(error));
82 const struct stream_class tcp_stream_class = {
84 true, /* needs_probes */
97 windows_open(const char *name, char *suffix, struct stream **streamp,
102 char *suffix_new, *path;
104 /* If the path does not contain a ':', assume it is relative to
106 if (!strchr(suffix, ':')) {
107 path = xasprintf("%s/%s", ovs_rundir(), suffix);
109 path = xstrdup(suffix);
112 file = fopen(path, "r");
115 VLOG_DBG("%s: could not open %s (%s)", name, suffix,
116 ovs_strerror(error));
120 error = fscanf(file, "%d", &port);
122 VLOG_ERR("failed to read port from %s", suffix);
128 suffix_new = xasprintf("127.0.0.1:%d", port);
130 error = tcp_open(name, suffix_new, streamp, dscp);
137 const struct stream_class windows_stream_class = {
139 false, /* needs_probes */
140 windows_open, /* open */
153 static int ptcp_accept(int fd, const struct sockaddr_storage *,
154 size_t, struct stream **streamp);
157 new_pstream(char *suffix, struct pstream **pstreamp, int dscp,
158 char *unlink_path, bool kernel_print_port)
160 char bound_name[SS_NTOP_BUFSIZE + 16];
161 char addrbuf[SS_NTOP_BUFSIZE];
162 struct sockaddr_storage ss;
167 fd = inet_open_passive(SOCK_STREAM, suffix, -1, &ss, dscp,
173 port = ss_get_port(&ss);
174 snprintf(bound_name, sizeof bound_name, "ptcp:%"PRIu16":%s",
175 port, ss_format_address(&ss, addrbuf, sizeof addrbuf));
177 error = new_fd_pstream(bound_name, fd, ptcp_accept, set_dscp, unlink_path,
180 pstream_set_bound_port(*pstreamp, htons(port));
186 ptcp_open(const char *name OVS_UNUSED, char *suffix, struct pstream **pstreamp,
189 return new_pstream(suffix, pstreamp, dscp, NULL, true);
193 ptcp_accept(int fd, const struct sockaddr_storage *ss,
194 size_t ss_len OVS_UNUSED, struct stream **streamp)
196 char name[SS_NTOP_BUFSIZE + 16];
197 char addrbuf[SS_NTOP_BUFSIZE];
199 snprintf(name, sizeof name, "tcp:%s:%"PRIu16,
200 ss_format_address(ss, addrbuf, sizeof addrbuf),
202 return new_tcp_stream(name, fd, 0, streamp);
205 const struct pstream_class ptcp_pstream_class = {
217 pwindows_open(const char *name OVS_UNUSED, char *suffix,
218 struct pstream **pstreamp, uint8_t dscp)
221 char *suffix_new, *path;
223 struct pstream *listener;
225 suffix_new = xstrdup("0:127.0.0.1");
227 /* If the path does not contain a ':', assume it is relative to
229 if (!strchr(suffix, ':')) {
230 path = xasprintf("%s/%s", ovs_rundir(), suffix);
232 path = xstrdup(suffix);
235 error = new_pstream(suffix_new, pstreamp, dscp, path, false);
239 listener = *pstreamp;
241 file = fopen(path, "w");
244 VLOG_DBG("could not open %s (%s)", path, ovs_strerror(error));
248 fprintf(file, "%d\n", ntohs(listener->bound_port));
249 if (fflush(file) == EOF) {
251 VLOG_ERR("write failed for %s", path);
262 const struct pstream_class pwindows_pstream_class = {