summaryrefslogtreecommitdiff
path: root/server/src/queryhandler.cc
blob: 309c0147bb3ce62905256d922b9c8e8a78dde1c6 (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            queryhandler.cc
 *
 *  Tue May  6 14:50:56 CEST 2008
 *  Copyright 2008 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Pracro.
 *
 *  Pracro 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.
 *
 *  Pracro 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 Pracro; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "queryhandler.h"

// For time
#include <time.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// For getpid
#include <unistd.h>
#include <sys/types.h>

// For time
#include <time.h>

// For strerror and errno
#include <errno.h>

// For socket and friends
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>

// For ioctl
#include <sys/ioctl.h>

typedef struct {
  in_addr_t ip;
  time_t time;
  pid_t pid;
  unsigned short int count;
} UID;


#define SIOCGIFCONF 0x8912 // get iface list

static in_addr_t getIP(const char *interface)
{
  in_addr_t ret = 0;
  int numreqs = 30, sd, n;
  struct ifconf ifc;
  struct ifreq *ifr;
  struct in_addr *ia;

  sd = socket(AF_INET, SOCK_STREAM, 0);
  if(sd == -1) {
    //    throw Pentominos::UIDCouldNotConnectException(strerror(errno));
  }

  ifc.ifc_buf = NULL;
  ifc.ifc_len = sizeof(struct ifreq) * numreqs;

  ifc.ifc_buf = (char*)malloc(ifc.ifc_len);
  if(ifc.ifc_buf == NULL) {
    //    throw Pentominos::UIDOutOfMemoryException();
  }

  if (ioctl(sd, SIOCGIFCONF, &ifc) < 0) {
    //    throw Pentominos::UIDInterfaceListException(strerror(errno));
  } 

  ifr = ifc.ifc_req; 
  for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
    ia = (struct in_addr *)((ifr->ifr_ifru.ifru_addr.sa_data)+2);
    if(!strcmp(ifr->ifr_ifrn.ifrn_name, interface)) {
      ret = *(in_addr_t*)ia;
    }
    ifr++; 
  }

  if(!ret) { // Still no interface... We're fucked!
    //    throw Pentominos::UIDInterfaceException(interface);
  }

  free(ifc.ifc_buf);
  return ret;
}


static unsigned short counter = 0;
static unsigned short getCounter()
{
  return counter++;
}

static UID uid = {0,0,0,0};
static std::string getUID(const char *interface)
{
  if(!uid.ip) uid.ip = getIP(interface);

  time_t t = time(NULL);
  if(uid.time != t) counter = 0; // If time differes, reset the counter
  uid.time = t; // We need this value every time.

  if(!uid.pid) uid.pid = getpid();

  uid.count = getCounter();

  char buf[32];
  sprintf(buf, "%08x%08x%04x%04x", uid.ip, (unsigned int)uid.time, uid.pid, uid.count);
  return std::string(buf);
}


QueryHandler::QueryHandler(TCPSocket *socket, std::string cpr)
{
  this->cpr = cpr;
  this->socket = socket;
}

void QueryHandler::addQuery(Query &query)
{
  queries.push_back(query);
}

std::string QueryHandler::exec()
{
  time_t timestamp = time(NULL);
  std::string uid = getUID("eth0");

  char buf[512];
  char header[] =
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    "<artefact xmlns=\"http://www.aasimon.org/pentominos\"\n"
    "          xmlns:pentominos=\"http://www.aasimon.org/pentominos\"\n"
    "          xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
    "          xsi:schemaLocation=\"http://www.aasimon.org/pentominos schema.xsd\">\n";
  socket->write(header, strlen(header));

  sprintf(buf, "  <pentominos:entry cpr=\"%s\"\n"
          "                    src_addr=\"%s\"\n"
          "                    dst_addr=\"%s\"\n"
          "                    timestamp=\"%d\"\n"
          "                    uid=\"%s\"/>\n",
          cpr.c_str(),
          socket->srcaddr().c_str(),
          socket->dstaddr().c_str(),
          (unsigned int)timestamp,
          uid.c_str());
  socket->write(buf, strlen(buf));

  std::vector< Query >::iterator j = queries.begin();
  while(j != queries.end()) {
    Query query = *j;

    sprintf(buf, "  <pentominos:query format=\"pracroxml\"\n"
            "                    device_id=\"%s\"\n"
            "                    device_type=\"%s\"\n"
            "                    filter=\"latest\"\n"
            "                    location=\"all\"/>\n",
            query.attributes["device_id"].c_str(),
            query.attributes["device_type"].c_str());

    socket->write(buf, strlen(buf));

    j++;
  }

  sprintf(buf, "</artefact>\n");
  socket->write(buf, strlen(buf));

  // Terminate
  char term[] = "\0";
  socket->write(term, 1);

  // Wait for answer
  char abuf[64];
  int res;
  std::string answer;
  do {
    memset(abuf, 0, sizeof(abuf));
    res = socket->read(abuf, sizeof(abuf) - 1);
    answer += abuf;
  } while(res);

  return answer;
}

#ifdef TEST_QUERYHANDLER

int main()
{
  TCPSocket s;
  s.connect("localhost", 11108);

  QueryHandler qh(&s, "2003791613");

  Query q1;
  q1.attributes["device_id"] = "lensmeter";
  q1.attributes["device_type"] = "lensmeter";
  qh.addQuery(q1);

  std::string res = qh.exec();
  
  printf("%s\n", res.c_str());

  return 0;
}

#endif/*TEST_QUERYHANDLER*/