2 * Copyright 2001-2007 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
20 * Unix Domain-based SocketListener implementation
\r
23 #include "internal.h"
\r
24 #include "remoting/impl/SocketListener.h"
\r
26 #include <xercesc/util/XMLUniDefs.hpp>
\r
27 #include <xmltooling/unicode.h>
\r
29 #ifdef HAVE_UNISTD_H
\r
30 # include <sys/socket.h>
\r
31 # include <sys/un.h>
\r
32 # include <unistd.h>
\r
33 # include <arpa/inet.h>
\r
36 #include <sys/types.h>
\r
37 #include <sys/stat.h> /* for chmod() */
\r
42 using namespace shibsp;
\r
43 using namespace xmltooling;
\r
44 using namespace xercesc;
\r
45 using namespace std;
\r
49 static const XMLCh address[] = UNICODE_LITERAL_7(a,d,d,r,e,s,s);
\r
51 class UnixListener : virtual public SocketListener
\r
54 UnixListener(const DOMElement* e);
\r
55 ~UnixListener() {if (m_bound) unlink(m_address.c_str());}
\r
57 bool create(ShibSocket& s) const;
\r
58 bool bind(ShibSocket& s, bool force=false) const;
\r
59 bool connect(ShibSocket& s) const;
\r
60 bool close(ShibSocket& s) const;
\r
61 bool accept(ShibSocket& listener, ShibSocket& s) const;
\r
63 int send(ShibSocket& s, const char* buf, int len) const {
\r
64 return ::send(s, buf, len, 0);
\r
67 int recv(ShibSocket& s, char* buf, int buflen) const {
\r
68 return ::recv(s, buf, buflen, 0);
\r
73 mutable bool m_bound;
\r
76 ListenerService* SHIBSP_DLLLOCAL UnixListenerServiceFactory(const DOMElement* const & e)
\r
78 return new UnixListener(e);
\r
82 UnixListener::UnixListener(const DOMElement* e) : SocketListener(e), m_address("/var/run/shar-socket"), m_bound(false)
\r
84 // We're stateless, but we need to load the configuration.
\r
85 const XMLCh* tag=e->getAttributeNS(NULL,address);
\r
87 auto_ptr_char a(tag);
\r
92 #ifndef UNIX_PATH_MAX
\r
93 #define UNIX_PATH_MAX 100
\r
96 bool UnixListener::create(ShibSocket& sock) const
\r
98 sock = socket(PF_UNIX, SOCK_STREAM, 0);
\r
100 return log_error();
\r
104 bool UnixListener::bind(ShibSocket& s, bool force) const
\r
106 struct sockaddr_un addr;
\r
107 memset(&addr, 0, sizeof (addr));
\r
108 addr.sun_family = AF_UNIX;
\r
109 strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
\r
112 unlink(m_address.c_str());
\r
114 if (::bind(s, (struct sockaddr *)&addr, sizeof (addr)) < 0) {
\r
120 // Make sure that only the creator can read -- we don't want just
\r
121 // anyone connecting, do we?
\r
122 if (chmod(m_address.c_str(),0777) < 0) {
\r
125 unlink(m_address.c_str());
\r
130 return m_bound=true;
\r
133 bool UnixListener::connect(ShibSocket& s) const
\r
135 struct sockaddr_un addr;
\r
136 memset(&addr, 0, sizeof (addr));
\r
137 addr.sun_family = AF_UNIX;
\r
138 strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
\r
140 if (::connect(s, (struct sockaddr *)&addr, sizeof (addr)) < 0)
\r
141 return log_error();
\r
145 bool UnixListener::close(ShibSocket& s) const
\r
151 bool UnixListener::accept(ShibSocket& listener, ShibSocket& s) const
\r
153 s=::accept(listener,NULL,NULL);
\r
155 return log_error();
\r