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>
26 #include <sys/socket.h>
28 #include "dynamic-string.h"
30 #include "socket-util.h"
32 #include "stream-provider.h"
33 #include "stream-fd.h"
36 VLOG_DEFINE_THIS_MODULE(stream_tcp
);
41 new_tcp_stream(const char *name
, int fd
, int connect_status
,
42 struct stream
**streamp
)
44 if (connect_status
== 0) {
45 setsockopt_tcp_nodelay(fd
);
48 return new_fd_stream(name
, fd
, connect_status
, AF_INET
, streamp
);
52 tcp_open(const char *name
, char *suffix
, struct stream
**streamp
, uint8_t dscp
)
56 error
= inet_open_active(SOCK_STREAM
, suffix
, 0, NULL
, &fd
, dscp
);
58 return new_tcp_stream(name
, fd
, error
, streamp
);
60 VLOG_ERR("%s: connect: %s", name
, ovs_strerror(error
));
65 const struct stream_class tcp_stream_class
= {
67 true, /* needs_probes */
80 windows_open(const char *name
, char *suffix
, struct stream
**streamp
,
85 char *suffix_new
, *path
;
87 /* If the path does not contain a ':', assume it is relative to
89 if (!strchr(suffix
, ':')) {
90 path
= xasprintf("%s/%s", ovs_rundir(), suffix
);
92 path
= xstrdup(suffix
);
95 file
= fopen(path
, "r");
98 VLOG_DBG("%s: could not open %s (%s)", name
, suffix
,
103 error
= fscanf(file
, "%d", &port
);
105 VLOG_ERR("failed to read port from %s", suffix
);
111 suffix_new
= xasprintf("127.0.0.1:%d", port
);
113 error
= tcp_open(name
, suffix_new
, streamp
, dscp
);
120 const struct stream_class windows_stream_class
= {
122 false, /* needs_probes */
123 windows_open
, /* open */
136 static int ptcp_accept(int fd
, const struct sockaddr_storage
*,
137 size_t, struct stream
**streamp
);
140 new_pstream(char *suffix
, const char *name
, struct pstream
**pstreamp
,
141 int dscp
, char *unlink_path
, bool kernel_print_port
)
143 char bound_name
[SS_NTOP_BUFSIZE
+ 16];
144 char addrbuf
[SS_NTOP_BUFSIZE
];
145 struct sockaddr_storage ss
;
149 char *conn_name
= CONST_CAST(char *, name
);
151 fd
= inet_open_passive(SOCK_STREAM
, suffix
, -1, &ss
, dscp
,
157 port
= ss_get_port(&ss
);
159 snprintf(bound_name
, sizeof bound_name
, "ptcp:%"PRIu16
":%s",
160 port
, ss_format_address(&ss
, addrbuf
, sizeof addrbuf
));
161 conn_name
= bound_name
;
164 error
= new_fd_pstream(conn_name
, fd
, ptcp_accept
, set_dscp
, unlink_path
,
167 pstream_set_bound_port(*pstreamp
, htons(port
));
173 ptcp_open(const char *name OVS_UNUSED
, char *suffix
, struct pstream
**pstreamp
,
176 return new_pstream(suffix
, NULL
, pstreamp
, dscp
, NULL
, true);
180 ptcp_accept(int fd
, const struct sockaddr_storage
*ss
,
181 size_t ss_len OVS_UNUSED
, struct stream
**streamp
)
183 char name
[SS_NTOP_BUFSIZE
+ 16];
184 char addrbuf
[SS_NTOP_BUFSIZE
];
186 snprintf(name
, sizeof name
, "tcp:%s:%"PRIu16
,
187 ss_format_address(ss
, addrbuf
, sizeof addrbuf
),
189 return new_tcp_stream(name
, fd
, 0, streamp
);
192 const struct pstream_class ptcp_pstream_class
= {
204 pwindows_open(const char *name
, char *suffix
, struct pstream
**pstreamp
,
208 char *suffix_new
, *path
;
210 struct pstream
*listener
;
212 suffix_new
= xstrdup("0:127.0.0.1");
214 /* If the path does not contain a ':', assume it is relative to
216 if (!strchr(suffix
, ':')) {
217 path
= xasprintf("%s/%s", ovs_rundir(), suffix
);
219 path
= xstrdup(suffix
);
222 error
= new_pstream(suffix_new
, name
, pstreamp
, dscp
, path
, false);
226 listener
= *pstreamp
;
228 file
= fopen(path
, "w");
231 VLOG_DBG("could not open %s (%s)", path
, ovs_strerror(error
));
235 fprintf(file
, "%d\n", ntohs(listener
->bound_port
));
236 if (fflush(file
) == EOF
) {
238 VLOG_ERR("write failed for %s", path
);
249 const struct pstream_class pwindows_pstream_class
= {