command.c 10.4 KB
Newer Older
1
2
3
/* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
/* See LICENSE for licensing information */
/* $Id$ */
Roger Dingledine's avatar
Roger Dingledine committed
4
5
6

#include "or.h"

7
8
extern or_options_t options; /* command-line and config-file options */

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
void command_time_process_cell(cell_t *cell, connection_t *conn,
                               int *num, int *time,
                               void (*func)(cell_t *, connection_t *)) {
  struct timeval start, end;
  int time_passed; 

  *num += 1;

  if(gettimeofday(&start,NULL) < 0) {
    log(LOG_ERR,"command_time_process_cell(): gettimeofday failed.");
    return;
  }

  (*func)(cell, conn);

  if(gettimeofday(&end,NULL) < 0) {
    log(LOG_ERR,"command_time_process_cell(): gettimeofday failed.");
    return;
  }

  if(end.tv_usec < start.tv_usec) {
    end.tv_sec--;
    end.tv_usec += 1000000;
  }
  time_passed = ((end.tv_sec - start.tv_sec)*1000000) + (end.tv_usec - start.tv_usec);
  if(time_passed > 5000) { /* more than 5ms */
    log(LOG_INFO,"command_time_process_cell(): That call just took %d ms.",time_passed/1000);
  }
  *time += time_passed;
}

Roger Dingledine's avatar
Roger Dingledine committed
40
void command_process_cell(cell_t *cell, connection_t *conn) {
41
42
  static int num_create=0, num_data=0, num_destroy=0, num_sendme=0;
  static int create_time=0, data_time=0, destroy_time=0, sendme_time=0;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  static long current_second = 0; /* from previous calls to gettimeofday */
  struct timeval now;

  if(gettimeofday(&now,NULL) < 0) {
    log(LOG_ERR,"command_process_cell(): gettimeofday failed.");
    return;
  }

  if(now.tv_sec > current_second) { /* the second has rolled over */
    /* print stats */
    log(LOG_INFO,"At end of second:"); 
    log(LOG_INFO,"Create:    %d (%d ms)", num_create, create_time/1000);
    log(LOG_INFO,"Data:      %d (%d ms)", num_data, data_time/1000);
    log(LOG_INFO,"Destroy:   %d (%d ms)", num_destroy, destroy_time/1000);
    log(LOG_INFO,"Sendme:    %d (%d ms)", num_sendme, sendme_time/1000);

    /* zero out stats */
60
61
    num_create = num_data = num_destroy = num_sendme = 0;
    create_time = data_time = destroy_time = sendme_time = 0;
62
63
64
65

    /* remember which second it is, for next time */
    current_second = now.tv_sec; 
  }
Roger Dingledine's avatar
Roger Dingledine committed
66
67
68
69
70
71

  switch(cell->command) {
    case CELL_PADDING:
      /* do nothing */
      break;
    case CELL_CREATE:
72
73
      command_time_process_cell(cell, conn, &num_create, &create_time,
                                command_process_create_cell);
Roger Dingledine's avatar
Roger Dingledine committed
74
75
      break;
    case CELL_DATA:
76
77
      command_time_process_cell(cell, conn, &num_data, &data_time,
                                command_process_data_cell);
Roger Dingledine's avatar
Roger Dingledine committed
78
79
      break;
    case CELL_DESTROY:
80
81
      command_time_process_cell(cell, conn, &num_destroy, &destroy_time,
                                command_process_destroy_cell);
Roger Dingledine's avatar
Roger Dingledine committed
82
      break;
83
    case CELL_SENDME:
84
85
      command_time_process_cell(cell, conn, &num_sendme, &sendme_time,
                                command_process_sendme_cell);
86
87
88
89
      break;
    default:
      log(LOG_DEBUG,"Cell of unknown type (%d) received. Dropping.", cell->command);
      break;
Roger Dingledine's avatar
Roger Dingledine committed
90
91
92
93
94
95
96
97
  }
}

void command_process_create_cell(cell_t *cell, connection_t *conn) {
  circuit_t *circ;

  circ = circuit_get_by_aci_conn(cell->aci, conn);

98
99
  if(circ && circ->state != CIRCUIT_STATE_ONION_WAIT) {
    log(LOG_DEBUG,"command_process_create_cell(): received CREATE cell, not in onion_wait. Dropping.");
Roger Dingledine's avatar
Roger Dingledine committed
100
101
102
103
104
    return;
  }

  if(!circ) { /* if it's not there, create it */
    circ = circuit_new(cell->aci, conn);
105
    circ->state = CIRCUIT_STATE_ONION_WAIT;
106
    circ->onionlen = ntohl(*(int*)cell->payload);
Roger Dingledine's avatar
Roger Dingledine committed
107
    log(LOG_DEBUG,"command_process_create_cell():  Onion length is %u.",circ->onionlen);
108
    if(circ->onionlen > 50000 || circ->onionlen < 1) { /* too big or too small */
Roger Dingledine's avatar
Roger Dingledine committed
109
110
111
112
      log(LOG_DEBUG,"That's ludicrous. Closing.");
      circuit_close(circ);
      return;
    }
113
    circ->onion = malloc(circ->onionlen);
Roger Dingledine's avatar
Roger Dingledine committed
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
    if(!circ->onion) { 
      log(LOG_DEBUG,"command_process_create_cell(): Out of memory. Closing.");
      circuit_close(circ);
      return;
    }
    if(circ->onionlen < cell->length-4) { /* protect from buffer overflow */
      log(LOG_DEBUG,"command_process_create_cell(): Onion too small. Closing.");
      circuit_close(circ);
      return;
    }
    memcpy((void *)circ->onion,(void *)(cell->payload+4),cell->length-4);
    circ->recvlen = cell->length-4;
    log(LOG_DEBUG,"command_process_create_cell(): Primary create cell handled, have received %d of %d onion bytes.",
        circ->recvlen,circ->onionlen);

  } else { /* pull over as much of the onion as we can */
    if(cell->length + circ->recvlen > circ->onionlen) { /* protect from buffer overflow */
      log(LOG_DEBUG,"command_process_create_cell(): payload too big for onion. Closing.");
      circuit_close(circ);
      return;
    }
    memcpy((void *)(circ->onion+circ->recvlen),(void *)cell->payload,cell->length);
    circ->recvlen += cell->length;
    log(LOG_DEBUG,"command_process_create_cell(): Secondary create cell handled, have received %d of %d onion bytes.",
        circ->recvlen,circ->onionlen);
  }

  if(circ->recvlen != circ->onionlen) {
    log(LOG_DEBUG,"command_process_create_cell(): Onion not all here yet. Ok.");
    return;
  }

146
147
148
149
150
151
152
153
154
  /* add it to the pending onions queue, and then return */
  circ->state = CIRCUIT_STATE_ONION_PENDING;

  if(onion_pending_add(circ) < 0) {
    log(LOG_DEBUG,"command_process_create_cell(): Failed to queue onion. Closing.");
    circuit_close(circ);
  }
  return;
}
Roger Dingledine's avatar
Roger Dingledine committed
155

156
void command_process_sendme_cell(cell_t *cell, connection_t *conn) {
Roger Dingledine's avatar
Roger Dingledine committed
157
158
  circuit_t *circ;

159
160
161
162
163
164
165
  circ = circuit_get_by_aci_conn(cell->aci, conn);

  if(!circ) {
    log(LOG_DEBUG,"command_process_sendme_cell(): unknown circuit %d. Dropping.", cell->aci);
    return;
  }

166
167
  if(circ->state == CIRCUIT_STATE_ONION_WAIT) {
    log(LOG_DEBUG,"command_process_sendme_cell(): circuit in onion_wait. Dropping.");
168
169
170
171
172
173
174
175
176
    return;
  }
  if(circ->state == CIRCUIT_STATE_OR_WAIT) {
    log(LOG_DEBUG,"command_process_sendme_cell(): circuit in or_wait. Dropping.");
    return;
  }

  /* at this point both circ->n_conn and circ->p_conn are guaranteed to be set */

177
  if(cell->length != CIRCWINDOW_INCREMENT) {
178
179
    log(LOG_WARNING,"command_process_sendme_cell(): non-standard sendme value %d.",cell->length);
  }
180
181

  if(cell->aci == circ->p_aci) { /* it's an outgoing cell */
182
183
184
185
186
    circ->n_receive_circwindow += cell->length;
    assert(circ->n_receive_circwindow <= CIRCWINDOW_START);
    log(LOG_DEBUG,"connection_process_sendme_cell(): n_receive_circwindow for aci %d is %d.",circ->n_aci,circ->n_receive_circwindow);
    if(!circ->n_conn || circ->n_conn->type == CONN_TYPE_EXIT) {
      circuit_resume_edge_reading(circ, EDGE_EXIT);
187
188
189
190
191
192
193
194
    } else {
      cell->aci = circ->n_aci; /* switch it */
      if(connection_write_cell_to_buf(cell, circ->n_conn) < 0) { /* (clobbers cell) */
        circuit_close(circ);
        return;
      }
    }
  } else { /* it's an ingoing cell */
195
196
197
198
199
    circ->p_receive_circwindow += cell->length;
    log(LOG_DEBUG,"connection_process_sendme_cell(): p_receive_circwindow for aci %d is %d.",circ->p_aci,circ->p_receive_circwindow);
    assert(circ->p_receive_circwindow <= CIRCWINDOW_START);
    if(!circ->p_conn || circ->p_conn->type == CONN_TYPE_AP) {
      circuit_resume_edge_reading(circ, EDGE_AP);
200
201
202
203
204
205
206
207
208
209
210
211
    } else {
      cell->aci = circ->p_aci; /* switch it */
      if(connection_write_cell_to_buf(cell, circ->p_conn) < 0) { /* (clobbers cell) */
        circuit_close(circ);
        return;
      }
    }
  } 
}

void command_process_data_cell(cell_t *cell, connection_t *conn) {
  circuit_t *circ;
Roger Dingledine's avatar
Roger Dingledine committed
212
213
214
215

  circ = circuit_get_by_aci_conn(cell->aci, conn);

  if(!circ) {
216
    log(LOG_DEBUG,"command_process_data_cell(): unknown circuit %d. Dropping.", cell->aci);
Roger Dingledine's avatar
Roger Dingledine committed
217
218
219
    return;
  }

220
221
  if(circ->state == CIRCUIT_STATE_ONION_WAIT) {
    log(LOG_DEBUG,"command_process_data_cell(): circuit in onion_wait. Dropping data cell.");
Roger Dingledine's avatar
Roger Dingledine committed
222
223
    return;
  }
224
225
226
227
  if(circ->state == CIRCUIT_STATE_OR_WAIT) {
    log(LOG_DEBUG,"command_process_data_cell(): circuit in or_wait. Dropping data cell.");
    return;
  }
228
229
230
231
232
  if(circ->state == CIRCUIT_STATE_ONION_PENDING) {
    log(LOG_DEBUG,"command_process_data_cell(): circuit in create_wait. Queueing data cell.");
    onion_pending_data_add(circ, cell);
    return;
  }
233
234

  /* circ->p_conn and n_conn are only null if we're at an edge point with no connections yet */
Roger Dingledine's avatar
Roger Dingledine committed
235
236
237

  if(cell->aci == circ->p_aci) { /* it's an outgoing cell */
    cell->aci = circ->n_aci; /* switch it */
238
239
    if(--circ->p_receive_circwindow < 0) { /* is it less than 0 after decrement? */
      log(LOG_INFO,"connection_process_data_cell(): Too many data cells for circuit (aci %d). Closing.", circ->p_aci);
240
241
242
      circuit_close(circ);
      return;
    }
243
244
245
    log(LOG_DEBUG,"connection_process_data_cell(): p_receive_circwindow for aci %d is %d.",circ->p_aci,circ->p_receive_circwindow);
    if(circuit_deliver_data_cell(cell, circ, CELL_DIRECTION_OUT) < 0) {
      log(LOG_INFO,"command_process_data_cell(): circuit_deliver_data_cell (forward) failed. Closing.");
Roger Dingledine's avatar
Roger Dingledine committed
246
247
248
249
250
      circuit_close(circ);
      return;
    }
  } else { /* it's an ingoing cell */
    cell->aci = circ->p_aci; /* switch it */
251
252
    if(--circ->n_receive_circwindow < 0) { /* is it less than 0 after decrement? */
      log(LOG_DEBUG,"connection_process_data_cell(): Too many data cells for circuit (aci %d). Closing.", circ->n_aci);
253
254
255
      circuit_close(circ);
      return;
    }
256
257
258
259
260
    log(LOG_DEBUG,"connection_process_data_cell(): n_receive_circwindow for aci %d is %d.",circ->n_aci,circ->n_receive_circwindow);
    if(circuit_deliver_data_cell(cell, circ, CELL_DIRECTION_IN) < 0) {
      log(LOG_DEBUG,"command_process_data_cell(): circuit_deliver_data_cell (backward to AP) failed. Closing.");
      circuit_close(circ);
      return;
Roger Dingledine's avatar
Roger Dingledine committed
261
262
263
264
265
266
    }
  }
}

void command_process_destroy_cell(cell_t *cell, connection_t *conn) {
  circuit_t *circ;
267
  connection_t *tmpconn;
Roger Dingledine's avatar
Roger Dingledine committed
268
269
270
271

  circ = circuit_get_by_aci_conn(cell->aci, conn);

  if(!circ) {
272
    log(LOG_DEBUG,"command_process_destroy_cell(): unknown circuit %d. Dropping.", cell->aci);
Roger Dingledine's avatar
Roger Dingledine committed
273
274
275
276
    return;
  }

  log(LOG_DEBUG,"command_process_destroy_cell(): Received for aci %d.",cell->aci);
277
278
279
  if(circ->state == CIRCUIT_STATE_ONION_PENDING) {
    onion_pending_remove(circ);
  }
280

Roger Dingledine's avatar
Roger Dingledine committed
281
  circuit_remove(circ);
282

283
  if(cell->aci == circ->p_aci) { /* the destroy came from behind */
284
285
286
    for(tmpconn = circ->n_conn; tmpconn; tmpconn=tmpconn->next_topic) {
      connection_send_destroy(circ->n_aci, tmpconn);
    }
287
288
  }
  if(cell->aci == circ->n_aci) { /* the destroy came from ahead */
289
290
291
    for(tmpconn = circ->p_conn; tmpconn; tmpconn=tmpconn->next_topic) {
      connection_send_destroy(circ->p_aci, tmpconn);
    }
292
  }
Roger Dingledine's avatar
Roger Dingledine committed
293
294
295
  circuit_free(circ);
}