EMANE  1.2.1
controlportservice.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015,2017 - Adjacent Link LLC, Bridgewater, New Jersey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of Adjacent Link LLC nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "controlportservice.h"
34 #include "controlportsession.h"
35 #include "socketexception.h"
36 
37 #include <cstring>
38 #include <map>
39 #include <algorithm>
40 #include <unistd.h>
41 #include <sys/socket.h>
42 #include <sys/eventfd.h>
43 
44 
46  iSignalEvent_{},
47  iSock_{},
48  thread_{}{}
49 
51 {
52  iSignalEvent_ = eventfd(0,0);
53 
54  if((iSock_ = socket(endpoint.getFamily(),
55  SOCK_STREAM,
56  0)) == -1)
57  {
58  throw SocketException(strerror(errno));
59  }
60 
61  int iOption{1};
62 
63  if(setsockopt(iSock_,
64  SOL_SOCKET,
65  SO_REUSEADDR,
66  reinterpret_cast<void*>(&iOption),
67  sizeof(iOption)) < 0)
68  {
69  throw makeException<SocketException>("setsockopt SO_REUSEADDR: %s",
70  strerror(errno));
71 
72  }
73 
74  if(bind(iSock_,endpoint.getSockAddr(),endpoint.getAddrLength()) < 0)
75  {
76  throw makeException<SocketException>("bind: %s",
77  strerror(errno));
78  }
79 
80 
81  if(listen(iSock_,10) < 0)
82  {
83  throw makeException<SocketException>("listen: %s",
84  strerror(errno));
85  }
86 
87  thread_ = std::thread(&Service::process,this);
88 }
89 
91 {
92  if(thread_.joinable())
93  {
94  close();
95  }
96 }
97 
99 {
100  eventfd_write(iSignalEvent_,1);
101  thread_.join();
102  ::close(iSock_);
103  ::close(iSignalEvent_);
104 }
105 
106 void EMANE::ControlPort::Service::process()
107 {
108  std::map<int,std::unique_ptr<Session>> sessionMap;
109 
110  while(1)
111  {
112  int nfds{iSignalEvent_};
113 
114  fd_set rfds;
115 
116  FD_ZERO(&rfds);
117  FD_SET(iSignalEvent_,&rfds);
118  FD_SET(iSock_,&rfds);
119 
120  nfds = std::max(iSock_,nfds);
121 
122  for(const auto & entry : sessionMap)
123  {
124  FD_SET(entry.first,&rfds);
125  nfds = std::max(entry.first,nfds);
126  }
127 
128  auto result = select(nfds+1,&rfds,nullptr,nullptr,nullptr);
129 
130  if(result == -1)
131  {
132  if(errno == EINTR)
133  {
134  continue;
135  }
136  else
137  {
138  break;
139  }
140  }
141 
142  if(FD_ISSET(iSignalEvent_,&rfds))
143  {
144  break;
145  }
146 
147  if(FD_ISSET(iSock_,&rfds))
148  {
149  int iNewFd{};
150 
151  if((iNewFd = accept(iSock_,nullptr,nullptr)) > 0)
152  {
153  sessionMap.insert(std::make_pair(iNewFd,std::unique_ptr<Session>{new Session{}}));
154  }
155  }
156 
157  auto iter = sessionMap.begin();
158 
159  while(iter != sessionMap.end())
160  {
161  if(FD_ISSET(iter->first,&rfds))
162  {
163  // process the session data
164  if(iter->second->process(iter->first))
165  {
166  ::close(iter->first);
167  sessionMap.erase(iter++);
168  }
169  else
170  {
171  ++iter;
172  }
173  }
174  else
175  {
176  ++iter;
177  }
178  }
179  }
180 
181  for(const auto & entry : sessionMap)
182  {
183  ::close(entry.first);
184  }
185 }
socklen_t getAddrLength() const
Definition: inetaddr.cc:404
void open(const INETAddr &endpoint)
sockaddr * getSockAddr() const
Definition: inetaddr.cc:399
int getFamily() const
Definition: inetaddr.cc:394