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, getenv("SHIBSP_LISTENER_ADDRESS"), address)), m_bound(false)
88 if (m_address.empty())
89 m_address = "shibd.sock";
90 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_address, PathResolver::XMLTOOLING_RUN_FILE);
94 #define UNIX_PATH_MAX 100
97 bool UnixListener::create(ShibSocket& sock) const
99 sock = socket(PF_UNIX, SOCK_STREAM, 0);
105 bool UnixListener::bind(ShibSocket& s, bool force) const
107 struct sockaddr_un addr;
108 memset(&addr, 0, sizeof (addr));
109 addr.sun_family = AF_UNIX;
110 strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
113 unlink(m_address.c_str());
115 if (::bind(s, (struct sockaddr *)&addr, sizeof (addr)) < 0) {
121 // Make sure that only the creator can read -- we don't want just
122 // anyone connecting, do we?
123 if (chmod(m_address.c_str(),0777) < 0) {
126 unlink(m_address.c_str());
134 bool UnixListener::connect(ShibSocket& s) const
136 struct sockaddr_un addr;
137 memset(&addr, 0, sizeof (addr));
138 addr.sun_family = AF_UNIX;
139 strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
141 if (::connect(s, (struct sockaddr *)&addr, sizeof (addr)) < 0)
146 bool UnixListener::close(ShibSocket& s) const
152 bool UnixListener::accept(ShibSocket& listener, ShibSocket& s) const
154 s=::accept(listener,nullptr,nullptr);