2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <sys/types.h>
30 #include "openflow/openflow.h"
32 #include "poll-loop.h"
33 #include "socket-util.h"
35 #include "vconn-provider.h"
36 #include "vconn-stream.h"
39 #define THIS_MODULE VLM_vconn_unix
41 /* Active UNIX socket. */
43 /* Number of unix sockets created so far, to ensure binding path uniqueness. */
44 static int n_unix_sockets;
47 unix_open(const char *name, char *suffix, struct vconn **vconnp)
49 const char *connect_path = suffix;
53 sprintf(bind_path, "/tmp/vconn-unix.%ld.%d",
54 (long int) getpid(), n_unix_sockets++);
55 fd = make_unix_socket(SOCK_STREAM, true, false, bind_path, connect_path);
57 VLOG_ERR("%s: connection to %s failed: %s",
58 bind_path, connect_path, strerror(-fd));
62 return new_stream_vconn(name, fd, check_connection_completion(fd),
66 struct vconn_class unix_vconn_class = {
76 /* Passive UNIX socket. */
78 static int punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
79 struct vconn **vconnp);
82 punix_open(const char *name UNUSED, char *suffix, struct pvconn **pvconnp)
86 fd = make_unix_socket(SOCK_STREAM, true, true, suffix, NULL);
88 VLOG_ERR("%s: binding failed: %s", suffix, strerror(errno));
92 return new_pstream_pvconn("punix", fd, punix_accept, pvconnp);
96 punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
97 struct vconn **vconnp)
99 const struct sockaddr_un *sun = (const struct sockaddr_un *) sa;
100 int name_len = get_unix_name_len(sa_len);
104 snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path);
106 strcpy(name, "unix");
108 return new_stream_vconn(name, fd, 0, 0, true, vconnp);
111 struct pvconn_class punix_pvconn_class = {