2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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.
24 #include <sys/socket.h>
25 #include <sys/types.h>
31 #include "poll-loop.h"
32 #include "socket-util.h"
34 #include "stream-provider.h"
35 #include "stream-fd.h"
38 VLOG_DEFINE_THIS_MODULE(stream_unix);
40 /* Active UNIX socket. */
43 unix_open(const char *name, char *suffix, struct stream **streamp)
45 const char *connect_path = suffix;
48 fd = make_unix_socket(SOCK_STREAM, true, false, NULL, connect_path);
50 VLOG_ERR("%s: connection failed (%s)", connect_path, strerror(-fd));
54 return new_fd_stream(name, fd, check_connection_completion(fd), streamp);
57 const struct stream_class unix_stream_class = {
69 /* Passive UNIX socket. */
71 static int punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
72 struct stream **streamp);
75 punix_open(const char *name OVS_UNUSED, char *suffix,
76 struct pstream **pstreamp)
80 fd = make_unix_socket(SOCK_STREAM, true, true, suffix, NULL);
82 VLOG_ERR("%s: binding failed: %s", suffix, strerror(errno));
86 if (listen(fd, 10) < 0) {
88 VLOG_ERR("%s: listen: %s", name, strerror(error));
93 return new_fd_pstream(name, fd, punix_accept,
94 xstrdup(suffix), pstreamp);
98 punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
99 struct stream **streamp)
101 const struct sockaddr_un *sun = (const struct sockaddr_un *) sa;
102 int name_len = get_unix_name_len(sa_len);
106 snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path);
108 strcpy(name, "unix");
110 return new_fd_stream(name, fd, 0, streamp);
113 const struct pstream_class punix_pstream_class = {