1 /** @addtogroup MCD_MCDIMPL_DAEMON_SRV 2 * @{ 3 * @file 4 * 5 * Connection server. 6 * 7 * Handles incoming socket connections from clients using the MobiCore driver. 8 * 9 * Iterative socket server using Netlink dgram protocol. 10 * 11 * <!-- Copyright Giesecke & Devrient GmbH 2009 - 2012 --> 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. The name of the author may not be used to endorse or promote 22 * products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 26 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 29 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 31 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 #ifndef NETLINKSERVER_H_ 38 #define NETLINKSERVER_H_ 39 40 #include <sys/types.h> 41 #include <sys/socket.h> 42 #include <sys/un.h> 43 #include <linux/netlink.h> 44 #include <cstdio> 45 #include <map> 46 47 #include "NetlinkConnection.h" 48 #include "ConnectionHandler.h" 49 #include "Server.h" 50 51 class NetlinkServer: public Server, public NetlinkConnectionManager 52 { 53 public: 54 /** 55 * Server contructor. 56 * 57 * @param connectionHanler Connection handler to pass incoming connections to. 58 */ 59 NetlinkServer( 60 ConnectionHandler *connectionHandler 61 ); 62 63 /** 64 * Server destructor. 65 * All available connections will be terminated. Resources will be freed. 66 */ 67 virtual ~NetlinkServer( 68 void 69 ); 70 71 /** 72 * Start server and listen for incoming connections. 73 * Implements the central socket server loop. Incoming connections will be stored. 74 */ 75 virtual void run( 76 void 77 ); 78 79 /** 80 * Remove a connection object from the list of available connections. 81 * Detaching is required for notification connections wich are never used to transfer command 82 * data from TLCs to the driver. If the function succeeds, freeing the connection will no longer 83 * be the server's responsability. 84 * 85 * @param connection The connection object to remove. 86 */ 87 virtual void detachConnection( 88 Connection *connection 89 ); 90 91 private: 92 /** 93 * Handle incomming Netlink message. 94 * It routes the incomming packet to the apropriate connection based on the packet's 95 * session magic. 96 * 97 * @param nlh The netlink message's header + payload 98 */ 99 void handleMessage( 100 struct nlmsghdr *nlh 101 ); 102 103 /** 104 * Retreive connection based on hash. 105 * Search the peer connections hashmap for a hash and return 106 * the associated Connection object 107 * 108 * @param seq The seq to search 109 * @return The NetlinkConnection object if found or NULL if not found 110 */ 111 NetlinkConnection *findConnection( 112 uint64_t hash 113 ); 114 115 /** 116 * Insert a connection in the peer connection hashmap 117 * Insert a new connection in the peer connections hashmap. If there is 118 * already such a connection it will be overriden! 119 * 120 * @param seq The seq to use 121 * @param connection The connection object to insert 122 */ 123 void insertConnection( 124 uint64_t hash, 125 NetlinkConnection *connection 126 ); 127 128 /** 129 * Remove a connection from the peer connections 130 * Remove the connection associated with seq from the peer list. 131 * This doesn't actually free the connection object! 132 * If the seq is invalid nothing happens. 133 * 134 * @param seq The seq to use 135 */ 136 void removeConnection( 137 uint64_t hash 138 ); 139 140 141 /** 142 * Check for sessions started by applications that died(exited) 143 * Remove the connections to applications that are not active anymore 144 * If the application has died then all the sessions associated with it 145 * should be closed! 146 * 147 */ 148 void cleanupConnections( 149 void 150 ); 151 152 connectionMap_t peerConnections; /**< Hashmap with connections to clients */ 153 }; 154 155 #endif /* SERVER_H_ */ 156 157 /** @} */ 158