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>
32 # include <sys/socket.h>
35 # include <arpa/inet.h>
38 #include <sys/types.h>
39 #include <sys/stat.h> /* for chmod() */
44 using namespace shibsp;
45 using namespace xmltooling;
46 using namespace xercesc;
51 static const XMLCh address[] = UNICODE_LITERAL_7(a,d,d,r,e,s,s);
53 class UnixListener : virtual public SocketListener
56 UnixListener(const DOMElement* e);
57 ~UnixListener() {if (m_bound) unlink(m_address.c_str());}
59 bool create(ShibSocket& s) const;
60 bool bind(ShibSocket& s, bool force=false) const;
61 bool connect(ShibSocket& s) const;
62 bool close(ShibSocket& s) const;
63 bool accept(ShibSocket& listener, ShibSocket& s) const;
65 int send(ShibSocket& s, const char* buf, int len) const {
66 return ::send(s, buf, len, 0);
69 int recv(ShibSocket& s, char* buf, int buflen) const {
70 return ::recv(s, buf, buflen, 0);
78 ListenerService* SHIBSP_DLLLOCAL UnixListenerServiceFactory(const DOMElement* const & e)
80 return new UnixListener(e);
84 UnixListener::UnixListener(const DOMElement* e) : SocketListener(e), m_address("/var/run/shar-socket"), m_bound(false)
86 const XMLCh* tag=e->getAttributeNS(nullptr,address);
90 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_address, PathResolver::XMLTOOLING_RUN_FILE);
95 #define UNIX_PATH_MAX 100
98 bool UnixListener::create(ShibSocket& sock) const
100 sock = socket(PF_UNIX, SOCK_STREAM, 0);
106 bool UnixListener::bind(ShibSocket& s, bool force) const
108 struct sockaddr_un addr;
109 memset(&addr, 0, sizeof (addr));
110 addr.sun_family = AF_UNIX;
111 strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
114 unlink(m_address.c_str());
116 if (::bind(s, (struct sockaddr *)&addr, sizeof (addr)) < 0) {
122 // Make sure that only the creator can read -- we don't want just
123 // anyone connecting, do we?
124 if (chmod(m_address.c_str(),0777) < 0) {
127 unlink(m_address.c_str());
135 bool UnixListener::connect(ShibSocket& s) const
137 struct sockaddr_un addr;
138 memset(&addr, 0, sizeof (addr));
139 addr.sun_family = AF_UNIX;
140 strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
142 if (::connect(s, (struct sockaddr *)&addr, sizeof (addr)) < 0)
147 bool UnixListener::close(ShibSocket& s) const
153 bool UnixListener::accept(ShibSocket& listener, ShibSocket& s) const
155 s=::accept(listener,nullptr,nullptr);