Merge citrix branch into master.
[sliver-openvswitch.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[128];
51     int fd;
52
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);
56     if (fd < 0) {
57         VLOG_ERR("%s: connection to %s failed: %s",
58                  bind_path, connect_path, strerror(-fd));
59         return -fd;
60     }
61
62     return new_stream_vconn(name, fd, check_connection_completion(fd),
63                             true, vconnp);
64 }
65
66 struct vconn_class unix_vconn_class = {
67     "unix",                     /* name */
68     unix_open,                  /* open */
69     NULL,                       /* close */
70     NULL,                       /* connect */
71     NULL,                       /* recv */
72     NULL,                       /* send */
73     NULL,                       /* wait */
74 };
75 \f
76 /* Passive UNIX socket. */
77
78 static int punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
79                         struct vconn **vconnp);
80
81 static int
82 punix_open(const char *name UNUSED, char *suffix, struct pvconn **pvconnp)
83 {
84     int fd, error;
85
86     fd = make_unix_socket(SOCK_STREAM, true, true, suffix, NULL);
87     if (fd < 0) {
88         VLOG_ERR("%s: binding failed: %s", suffix, strerror(errno));
89         return errno;
90     }
91
92     error = set_nonblocking(fd);
93     if (error) {
94         close(fd);
95         return error;
96     }
97
98     if (listen(fd, 10) < 0) {
99         error = errno;
100         VLOG_ERR("%s: listen: %s", name, strerror(error));
101         close(fd);
102         return error;
103     }
104
105     return new_pstream_pvconn("punix", fd, punix_accept, pvconnp);
106 }
107
108 static int
109 punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
110              struct vconn **vconnp)
111 {
112     const struct sockaddr_un *sun = (const struct sockaddr_un *) sa;
113     int name_len = get_unix_name_len(sa_len);
114     char name[128];
115
116     if (name_len > 0) {
117         snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path);
118     } else {
119         strcpy(name, "unix");
120     }
121     return new_stream_vconn(name, fd, 0, true, vconnp);
122 }
123
124 struct pvconn_class punix_pvconn_class = {
125     "punix",
126     punix_open,
127     NULL,
128     NULL,
129     NULL
130 };
131