]>
Commit | Line | Data |
---|---|---|
064af421 BP |
1 | /* |
2 | * Copyright (c) 2008, 2009 Nicira Networks. | |
3 | * | |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
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. | |
064af421 BP |
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), | |
d7cca867 | 63 | true, vconnp); |
064af421 BP |
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; | |
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 | return new_pstream_pvconn("punix", fd, punix_accept, pvconnp); | |
93 | } | |
94 | ||
95 | static int | |
96 | punix_accept(int fd, const struct sockaddr *sa, size_t sa_len, | |
97 | struct vconn **vconnp) | |
98 | { | |
99 | const struct sockaddr_un *sun = (const struct sockaddr_un *) sa; | |
100 | int name_len = get_unix_name_len(sa_len); | |
101 | char name[128]; | |
102 | ||
103 | if (name_len > 0) { | |
104 | snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path); | |
105 | } else { | |
106 | strcpy(name, "unix"); | |
107 | } | |
d7cca867 | 108 | return new_stream_vconn(name, fd, 0, true, vconnp); |
064af421 BP |
109 | } |
110 | ||
111 | struct pvconn_class punix_pvconn_class = { | |
112 | "punix", | |
113 | punix_open, | |
114 | NULL, | |
115 | NULL, | |
116 | NULL | |
117 | }; | |
118 |