EMANE  1.0.1
controlportservice.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 - 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  makeException<SocketException>("setsockopt SO_REUSEADDR: %s",
70  strerror(errno));
71 
72  }
73 
74  if(bind(iSock_,endpoint.getSockAddr(),endpoint.getAddrLength()) < 0)
75  {
76  makeException<SocketException>("bind: %s",
77  strerror(errno));
78  }
79 
80 
81  if(listen(iSock_,10) < 0)
82  {
83  makeException<SocketException>("listen: %s",
84  strerror(errno));
85  }
86 
87  thread_ = std::thread(&Service::process,this);
88 }
89 
90 
92 {
93  eventfd_write(iSignalEvent_,1);
94  thread_.join();
95  ::close(iSock_);
96  ::close(iSignalEvent_);
97 }
98 
99 void EMANE::ControlPort::Service::process()
100 {
101  std::map<int,std::unique_ptr<Session>> sessionMap;
102 
103  while(1)
104  {
105  int nfds{iSignalEvent_};
106 
107  fd_set rfds;
108 
109  FD_ZERO(&rfds);
110  FD_SET(iSignalEvent_,&rfds);
111  FD_SET(iSock_,&rfds);
112 
113  nfds = std::max(iSock_,nfds);
114 
115  for(const auto & entry : sessionMap)
116  {
117  FD_SET(entry.first,&rfds);
118  nfds = std::max(entry.first,nfds);
119  }
120 
121  auto result = select(nfds+1,&rfds,nullptr,nullptr,nullptr);
122 
123  if(result == -1)
124  {
125  if(errno == EINTR)
126  {
127  continue;
128  }
129  else
130  {
131  break;
132  }
133  }
134 
135  if(FD_ISSET(iSignalEvent_,&rfds))
136  {
137  break;
138  }
139 
140  if(FD_ISSET(iSock_,&rfds))
141  {
142  int iNewFd{};
143 
144  if((iNewFd = accept(iSock_,nullptr,nullptr)) > 0)
145  {
146  sessionMap.insert(std::make_pair(iNewFd,std::unique_ptr<Session>{new Session{}}));
147  }
148  }
149 
150  auto iter = sessionMap.begin();
151 
152  while(iter != sessionMap.end())
153  {
154  if(FD_ISSET(iter->first,&rfds))
155  {
156  // process the session data
157  if(iter->second->process(iter->first))
158  {
159  sessionMap.erase(iter++);
160  }
161  else
162  {
163  ++iter;
164  }
165  }
166  else
167  {
168  ++iter;
169  }
170  }
171  }
172 
173  for(const auto & entry : sessionMap)
174  {
175  ::close(entry.first);
176  //delete entry.second;
177  }
178 }
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