1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*
This file is part of Kismet
Kismet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kismet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kismet; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __TCPSERVER_H__
#define __TCPSERVER_H__
#include "config.h"
#include <stdio.h>
#include <string>
#include <time.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#include <map>
#include <vector>
#include "configfile.h"
// Protocol parameters
#define PROTO_PARMS string& out_string, const vector<int> *field_vec, const void *data
#define TCP_SELECT_TIMEOUT 100
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif
/* A structure that holds a command from the client */
struct client_command {
int client_fd;
int stamp;
string cmd;
};
typedef struct server_protocol {
int ref_index;
string header;
int required;
// Double-listed (burns a little extra ram but not much) to make mapping requested
// fields fast.
map<string, int> field_map;
vector<string> field_vec;
int (*printer)(PROTO_PARMS);
void (*enable)(int);
};
// Client options
struct client_opt {
// Map of sentence references to field lists
map<int, vector<int> > protocols;
string wrbuf, cmdbuf;
};
// Allowed IP information
struct client_ipblock {
// Allowed network
in_addr network;
// Allowed mask
in_addr mask;
};
// TCP/IP server to push data to the frontend.
class TcpServer {
public:
TcpServer();
~TcpServer();
int Valid() { return sv_valid; };
int Setup(unsigned int in_max_clients, string bind_addr, short int in_port, vector<client_ipblock *> *in_ipb);
int MergeSet(fd_set in_set, int in_max, fd_set *out_set,
fd_set *outw_set);
int FetchDescriptor() { return serv_fd; }
void Kill(int in_fd);
int Poll(fd_set& in_rset, fd_set& in_wset);
// Send to a specific client, if they support that refnum
int SendToClient(int in_fd, int in_refnum, const void *in_data);
// Send to all clients that support the refnum
int SendToAll(int in_refnum, const void *in_data);
// Use a little bit of indirecton to allow kismet_server to trigger sending
// capabilities after it sends our KISMET headers
int SendMainProtocols(int in_fd, int proto_ref);
void Shutdown();
char *FetchError() { return errstr; }
inline int isClient(int fd) { return FD_ISSET(fd, &client_fds); }
int HandleClient(int fd, client_command *c, fd_set *rds, fd_set *wrs);
// Register an output sentence. This needs:
// * A header (ie, NETWORK)
// * A NULL-terminated array of fields
// * A pointer to a printer that takes a void * and a vector of field numbers
// and outputs a c++ string
// * An optional pointer to a function that takes the file descriptor of a client
// that triggers whatever events should happen the the client enables this kind
// of protocol. (ie, send all networks when the client enables the *NETWORK
// protocol)
// It returns the index number of the sentence added.
int RegisterProtocol(string in_header, int in_required, char **in_fields,
int (*in_printer)(PROTO_PARMS),
void (*in_enable)(int));
int FetchProtocolRef(string in_header);
// How many clients are using this protocol type?
int FetchNumClientRefs(int in_refnum);
// How many clients are connected?
int FetchNumClients();
protected:
void AddProtocolClient(int in_fd, int in_refnum, vector<int> in_fields);
void DelProtocolClient(int in_fd, int in_refnum);
int RawSend(int in_fd, const char *in_data);
int Accept();
int HandleInternalCommand(client_command *in_command);
// Map of reference numbers to sentences
map<int, server_protocol *> protocol_map;
// Map of headers to reference numbers
map<string, int> ref_map;
// Protocols clients are required to support
vector<int> required_protocols;
// Map of protocols to the number of clients using them
map<int, int> client_mapped_protocols;
char errstr[1024];
// Active server
int sv_valid;
unsigned int max_clients;
// Server info
short int port;
char hostname[MAXHOSTNAMELEN];
vector<client_ipblock *> *ipblock_vec;
// Socket items
int serv_fd;
struct sockaddr_in serv_sock;
// Master list of Fd's
fd_set server_fds;
fd_set client_fds;
int max_fd;
map<int, client_opt *> client_optmap;
};
#endif
|