2 * Copyright 2001-2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * Unix Domain-based SocketListener implementation.
24 #include "remoting/impl/SocketListener.h"
26 #include <xercesc/util/XMLUniDefs.hpp>
27 #include <xmltooling/XMLToolingConfig.h>
28 #include <xmltooling/unicode.h>
29 #include <xmltooling/util/PathResolver.h>
30 #include <xmltooling/util/XMLHelper.h>
33 # include <sys/socket.h>
36 # include <arpa/inet.h>
39 #include <sys/types.h>
40 #include <sys/stat.h> /* for chmod() */
45 using namespace shibsp;
46 using namespace xmltooling;
47 using namespace xercesc;
52 class UnixListener : virtual public SocketListener
55 UnixListener(const DOMElement* e);
56 ~UnixListener() {if (m_bound) unlink(m_address.c_str());}
58 bool create(ShibSocket& s) const;
59 bool bind(ShibSocket& s, bool force=false) const;
60 bool connect(ShibSocket& s) const;
61 bool close(ShibSocket& s) const;
62 bool accept(ShibSocket& listener, ShibSocket& s) const;
64 int send(ShibSocket& s, const char* buf, int len) const {
65 return ::send(s, buf, len, 0);
68 int recv(ShibSocket& s, char* buf, int buflen) const {
69 return ::recv(s, buf, buflen, 0);
77 ListenerService* SHIBSP_DLLLOCAL UnixListenerServiceFactory(const DOMElement* const & e)
79 return new UnixListener(e);
82 static const XMLCh address[] = UNICODE_LITERAL_7(a,d,d,r,e,s,s);
85 UnixListener::UnixListener(const DOMElement* e)
86 : SocketListener(e), m_address(XMLHelper::getAttrString(e, "shibd.sock", address)), m_bound(false)
88 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_address, PathResolver::XMLTOOLING_RUN_FILE);
92 #define UNIX_PATH_MAX 100
95 bool UnixListener::create(ShibSocket& sock) const
97 sock = socket(PF_UNIX, SOCK_STREAM, 0);
103 bool UnixListener::bind(ShibSocket& s, bool force) const
105 struct sockaddr_un addr;
106 memset(&addr, 0, sizeof (addr));
107 addr.sun_family = AF_UNIX;
108 strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
111 unlink(m_address.c_str());
113 if (::bind(s, (struct sockaddr *)&addr, sizeof (addr)) < 0) {
119 // Make sure that only the creator can read -- we don't want just
120 // anyone connecting, do we?
121 if (chmod(m_address.c_str(),0777) < 0) {
124 unlink(m_address.c_str());
132 bool UnixListener::connect(ShibSocket& s) const
134 struct sockaddr_un addr;
135 memset(&addr, 0, sizeof (addr));
136 addr.sun_family = AF_UNIX;
137 strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
139 if (::connect(s, (struct sockaddr *)&addr, sizeof (addr)) < 0)
144 bool UnixListener::close(ShibSocket& s) const
150 bool UnixListener::accept(ShibSocket& listener, ShibSocket& s) const
152 s=::accept(listener,nullptr,nullptr);