2 * Copyright (c) 2008, 2009 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/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 bind_path
= xasprintf("/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
));
63 return new_stream_vconn(name
, fd
, check_connection_completion(fd
),
67 struct vconn_class unix_vconn_class
= {
77 /* Passive UNIX socket. */
79 static int punix_accept(int fd
, const struct sockaddr
*sa
, size_t sa_len
,
80 struct vconn
**vconnp
);
83 punix_open(const char *name UNUSED
, char *suffix
, struct pvconn
**pvconnp
)
87 fd
= make_unix_socket(SOCK_STREAM
, true, true, suffix
, NULL
);
89 VLOG_ERR("%s: binding failed: %s", suffix
, strerror(errno
));
93 error
= set_nonblocking(fd
);
99 if (listen(fd
, 10) < 0) {
101 VLOG_ERR("%s: listen: %s", name
, strerror(error
));
106 return new_pstream_pvconn("punix", fd
, punix_accept
,
107 xstrdup(suffix
), pvconnp
);
111 punix_accept(int fd
, const struct sockaddr
*sa
, size_t sa_len
,
112 struct vconn
**vconnp
)
114 const struct sockaddr_un
*sun
= (const struct sockaddr_un
*) sa
;
115 int name_len
= get_unix_name_len(sa_len
);
119 snprintf(name
, sizeof name
, "unix:%.*s", name_len
, sun
->sun_path
);
121 strcpy(name
, "unix");
123 return new_stream_vconn(name
, fd
, 0, NULL
, vconnp
);
126 struct pvconn_class punix_pvconn_class
= {