]> git.proxmox.com Git - mirror_ovs.git/blob - lib/vconn-unix.c
Merge citrix branch into master.
[mirror_ovs.git] / lib / vconn-unix.c
1 /*
2 * Copyright (c) 2008, 2009 Nicira Networks.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include "vconn.h"
19 #include <assert.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <netdb.h>
23 #include <poll.h>
24 #include <sys/types.h>
25 #include <sys/un.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include "ofpbuf.h"
30 #include "openflow/openflow.h"
31 #include "packets.h"
32 #include "poll-loop.h"
33 #include "socket-util.h"
34 #include "util.h"
35 #include "vconn-provider.h"
36 #include "vconn-stream.h"
37
38 #include "vlog.h"
39 #define THIS_MODULE VLM_vconn_unix
40
41 /* Active UNIX socket. */
42
43 /* Number of unix sockets created so far, to ensure binding path uniqueness. */
44 static int n_unix_sockets;
45
46 static int
47 unix_open(const char *name, char *suffix, struct vconn **vconnp)
48 {
49 const char *connect_path = suffix;
50 char *bind_path;
51 int fd;
52
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);
56 if (fd < 0) {
57 VLOG_ERR("%s: connection to %s failed: %s",
58 bind_path, connect_path, strerror(-fd));
59 free(bind_path);
60 return -fd;
61 }
62
63 return new_stream_vconn(name, fd, check_connection_completion(fd),
64 bind_path, vconnp);
65 }
66
67 struct vconn_class unix_vconn_class = {
68 "unix", /* name */
69 unix_open, /* open */
70 NULL, /* close */
71 NULL, /* connect */
72 NULL, /* recv */
73 NULL, /* send */
74 NULL, /* wait */
75 };
76 \f
77 /* Passive UNIX socket. */
78
79 static int punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
80 struct vconn **vconnp);
81
82 static int
83 punix_open(const char *name UNUSED, char *suffix, struct pvconn **pvconnp)
84 {
85 int fd, error;
86
87 fd = make_unix_socket(SOCK_STREAM, true, true, suffix, NULL);
88 if (fd < 0) {
89 VLOG_ERR("%s: binding failed: %s", suffix, strerror(errno));
90 return errno;
91 }
92
93 error = set_nonblocking(fd);
94 if (error) {
95 close(fd);
96 return error;
97 }
98
99 if (listen(fd, 10) < 0) {
100 error = errno;
101 VLOG_ERR("%s: listen: %s", name, strerror(error));
102 close(fd);
103 return error;
104 }
105
106 return new_pstream_pvconn("punix", fd, punix_accept,
107 xstrdup(suffix), pvconnp);
108 }
109
110 static int
111 punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
112 struct vconn **vconnp)
113 {
114 const struct sockaddr_un *sun = (const struct sockaddr_un *) sa;
115 int name_len = get_unix_name_len(sa_len);
116 char name[128];
117
118 if (name_len > 0) {
119 snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path);
120 } else {
121 strcpy(name, "unix");
122 }
123 return new_stream_vconn(name, fd, 0, NULL, vconnp);
124 }
125
126 struct pvconn_class punix_pvconn_class = {
127 "punix",
128 punix_open,
129 NULL,
130 NULL,
131 NULL
132 };
133