Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / stream-unix.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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 "stream.h"
19 #include <assert.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <netdb.h>
23 #include <poll.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <sys/un.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include "packets.h"
31 #include "poll-loop.h"
32 #include "socket-util.h"
33 #include "util.h"
34 #include "stream-provider.h"
35 #include "stream-fd.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(stream_unix);
39
40 /* Active UNIX socket. */
41
42 static int
43 unix_open(const char *name, char *suffix, struct stream **streamp,
44           uint8_t dscp OVS_UNUSED)
45 {
46     const char *connect_path = suffix;
47     int fd;
48
49     fd = make_unix_socket(SOCK_STREAM, true, false, NULL, connect_path);
50     if (fd < 0) {
51         VLOG_ERR("%s: connection failed (%s)", connect_path, strerror(-fd));
52         return -fd;
53     }
54
55     return new_fd_stream(name, fd, check_connection_completion(fd), streamp);
56 }
57
58 const struct stream_class unix_stream_class = {
59     "unix",                     /* name */
60     false,                      /* needs_probes */
61     unix_open,                  /* open */
62     NULL,                       /* close */
63     NULL,                       /* connect */
64     NULL,                       /* recv */
65     NULL,                       /* send */
66     NULL,                       /* run */
67     NULL,                       /* run_wait */
68     NULL,                       /* wait */
69 };
70 \f
71 /* Passive UNIX socket. */
72
73 static int punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
74                         struct stream **streamp);
75
76 static int
77 punix_open(const char *name OVS_UNUSED, char *suffix,
78            struct pstream **pstreamp, uint8_t dscp OVS_UNUSED)
79 {
80     int fd, error;
81
82     fd = make_unix_socket(SOCK_STREAM, true, true, suffix, NULL);
83     if (fd < 0) {
84         VLOG_ERR("%s: binding failed: %s", suffix, strerror(errno));
85         return errno;
86     }
87
88     if (listen(fd, 10) < 0) {
89         error = errno;
90         VLOG_ERR("%s: listen: %s", name, strerror(error));
91         close(fd);
92         return error;
93     }
94
95     return new_fd_pstream(name, fd, punix_accept,
96                           xstrdup(suffix), pstreamp);
97 }
98
99 static int
100 punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
101              struct stream **streamp)
102 {
103     const struct sockaddr_un *sun = (const struct sockaddr_un *) sa;
104     int name_len = get_unix_name_len(sa_len);
105     char name[128];
106
107     if (name_len > 0) {
108         snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path);
109     } else {
110         strcpy(name, "unix");
111     }
112     return new_fd_stream(name, fd, 0, streamp);
113 }
114
115 const struct pstream_class punix_pstream_class = {
116     "punix",
117     false,
118     punix_open,
119     NULL,
120     NULL,
121     NULL
122 };
123