circuit.c 18 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
7
8

#include "or.h"

/********* START VARIABLES **********/

Roger Dingledine's avatar
Roger Dingledine committed
9
static circuit_t *global_circuitlist=NULL;
Roger Dingledine's avatar
Roger Dingledine committed
10

11
12
char *circuit_state_to_string[] = {
  "receiving the onion",    /* 0 */
13
14
15
  "waiting to process create", /* 1 */
  "connecting to firsthop", /* 2 */
  "open"                    /* 3 */
16
17
};

Roger Dingledine's avatar
Roger Dingledine committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/********* END VARIABLES ************/

void circuit_add(circuit_t *circ) {

  if(!global_circuitlist) { /* first one */
    global_circuitlist = circ;
    circ->next = NULL;
  } else {
    circ->next = global_circuitlist;
    global_circuitlist = circ;
  }

}

void circuit_remove(circuit_t *circ) {
  circuit_t *tmpcirc;

Roger Dingledine's avatar
Roger Dingledine committed
35
  assert(circ && global_circuitlist);
Roger Dingledine's avatar
Roger Dingledine committed
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

  if(global_circuitlist == circ) {
    global_circuitlist = global_circuitlist->next;
    return;
  }

  for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
    if(tmpcirc->next == circ) {
      tmpcirc->next = circ->next;
      return;
    }
  }
}

circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn) {
  circuit_t *circ; 

  circ = (circuit_t *)malloc(sizeof(circuit_t));
  if(!circ)
    return NULL;
  memset(circ,0,sizeof(circuit_t)); /* zero it out */

  circ->p_aci = p_aci;
  circ->p_conn = p_conn;

61
  circ->state = CIRCUIT_STATE_ONION_WAIT;
Roger Dingledine's avatar
Roger Dingledine committed
62
63
64

  /* ACIs */
  circ->p_aci = p_aci;
Roger Dingledine's avatar
Roger Dingledine committed
65
  /* circ->n_aci remains 0 because we haven't identified the next hop yet */
Roger Dingledine's avatar
Roger Dingledine committed
66

67
68
  circ->n_receive_circwindow = CIRCWINDOW_START;
  circ->p_receive_circwindow = CIRCWINDOW_START;
69

Roger Dingledine's avatar
Roger Dingledine committed
70
71
72
73
74
75
  circuit_add(circ);

  return circ;
}

void circuit_free(circuit_t *circ) {
76
  struct data_queue_t *tmpd;
77
78
79
80
81
  
  if (circ->n_crypto)
    crypto_free_cipher_env(circ->n_crypto);
  if (circ->p_crypto)
    crypto_free_cipher_env(circ->p_crypto);
Roger Dingledine's avatar
Roger Dingledine committed
82
83
84

  if(circ->onion)
    free(circ->onion);
85
86
  if(circ->cpath)
    circuit_free_cpath(circ->cpath, circ->cpathlen);
87
88
89
90
91
92
  while(circ->data_queue) {
    tmpd = circ->data_queue;
    circ->data_queue = tmpd->next;
    free(tmpd->cell);
    free(tmpd);
  }
Roger Dingledine's avatar
Roger Dingledine committed
93
94

  free(circ);
95
96
}

97
void circuit_free_cpath(crypt_path_t **cpath, int cpathlen) {
98
99
100
101
  int i;

  for(i=0;i<cpathlen;i++)
    free(cpath[i]);
Roger Dingledine's avatar
Roger Dingledine committed
102

103
  free(cpath);
Roger Dingledine's avatar
Roger Dingledine committed
104
105
}

Roger Dingledine's avatar
Roger Dingledine committed
106
/* return 0 if can't get a unique aci. */
Roger Dingledine's avatar
Roger Dingledine committed
107
108
109
110
aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type) {
  aci_t test_aci;
  connection_t *conn;

Roger Dingledine's avatar
Roger Dingledine committed
111
try_again:
Roger Dingledine's avatar
Roger Dingledine committed
112
113
  log(LOG_DEBUG,"get_unique_aci_by_addr_port() trying to get a unique aci");

114
  crypto_pseudo_rand(2, (unsigned char *)&test_aci);
Roger Dingledine's avatar
Roger Dingledine committed
115

Roger Dingledine's avatar
Roger Dingledine committed
116
117
118
119
  if(aci_type == ACI_TYPE_LOWER && test_aci >= (2<<15))
    test_aci -= (2<<15);
  if(aci_type == ACI_TYPE_HIGHER && test_aci < (2<<15))
    test_aci += (2<<15);
120
  /* if aci_type == ACI_BOTH, don't filter any of it */
Roger Dingledine's avatar
Roger Dingledine committed
121
122

  if(test_aci == 0)
Roger Dingledine's avatar
Roger Dingledine committed
123
    goto try_again;
Roger Dingledine's avatar
Roger Dingledine committed
124

125
  conn = connection_exact_get_by_addr_port(addr,port);
Roger Dingledine's avatar
Roger Dingledine committed
126
127
128
129
  if(!conn) /* there can't be a conflict -- no connection of that sort yet */
    return test_aci;

  if(circuit_get_by_aci_conn(test_aci, conn))
Roger Dingledine's avatar
Roger Dingledine committed
130
    goto try_again;
Roger Dingledine's avatar
Roger Dingledine committed
131
132
133
134
135

  return test_aci;
}

int circuit_init(circuit_t *circ, int aci_type) {
136
  unsigned char iv[16];
Roger Dingledine's avatar
Roger Dingledine committed
137
138
  unsigned char digest1[20];
  unsigned char digest2[20];
139
140
141
  struct timeval start, end;
  int time_passed; 

142
  assert(circ && circ->onion);
Roger Dingledine's avatar
Roger Dingledine committed
143
144

  log(LOG_DEBUG,"circuit_init(): starting");
145
146
147
148
149
150
151
  circ->n_port = ntohs(*(uint16_t *)(circ->onion+2));
  log(LOG_DEBUG,"circuit_init(): Set port to %u.",circ->n_port);
  circ->n_addr = ntohl(*(uint32_t *)(circ->onion+4));
  circ->p_f = *(circ->onion+1) >> 4; /* backf */
  log(LOG_DEBUG,"circuit_init(): Set BACKF to %u.",circ->p_f);
  circ->n_f = *(circ->onion+1) & 0x0f; /* forwf */
  log(LOG_DEBUG,"circuit_init(): Set FORWF to %u.",circ->n_f);
Roger Dingledine's avatar
Roger Dingledine committed
152
153
154
155
  circ->state = CIRCUIT_STATE_OPEN;

  log(LOG_DEBUG,"circuit_init(): aci_type = %u.",aci_type);

156
157
158
159


  gettimeofday(&start,NULL);

Roger Dingledine's avatar
Roger Dingledine committed
160
  circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, aci_type);
Roger Dingledine's avatar
Roger Dingledine committed
161
162
163
164
  if(!circ->n_aci) {
    log(LOG_ERR,"circuit_init(): failed to get unique aci.");
    return -1;
  }
Roger Dingledine's avatar
Roger Dingledine committed
165

166
167
168
169
170
171
172
173
174
175
176
177
178
  gettimeofday(&end,NULL);

  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 > 1000) { /* more than 1ms */
    log(LOG_NOTICE,"circuit_init(): get_unique_aci just took %d us!",time_passed);
  }



Roger Dingledine's avatar
Roger Dingledine committed
179
180
181
  log(LOG_DEBUG,"circuit_init(): Chosen ACI %u.",circ->n_aci);

  /* keys */
182
183
  memset(iv, 0, 16);
  crypto_SHA_digest(circ->onion+12,16,digest1);
184
185
  crypto_SHA_digest(digest1,20,digest2);
  crypto_SHA_digest(digest2,20,digest1);
Roger Dingledine's avatar
Roger Dingledine committed
186
187
  log(LOG_DEBUG,"circuit_init(): Computed keys.");

188
  if (!(circ->p_crypto = create_onion_cipher(circ->p_f,digest2,iv,1))) {
Roger Dingledine's avatar
Roger Dingledine committed
189
190
191
    log(LOG_ERR,"Cipher initialization failed (ACI %u).",circ->n_aci);
    return -1;
  }
192
  
193
  if (!(circ->n_crypto = create_onion_cipher(circ->n_f, digest1, iv, 0))) {
Roger Dingledine's avatar
Roger Dingledine committed
194
195
196
    log(LOG_ERR,"Cipher initialization failed (ACI %u).",circ->n_aci);
    return -1;
  }
197

Roger Dingledine's avatar
Roger Dingledine committed
198
199
  log(LOG_DEBUG,"circuit_init(): Cipher initialization complete.");

200
  circ->expire = ntohl(*(uint32_t *)(circ->onion+8));
Roger Dingledine's avatar
Roger Dingledine committed
201
202
203
204

  return 0;
}

205
circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *circ, uint32_t naddr, uint16_t nport) {
206

207
208
209
210
211
  if(!circ) /* use circ if it's defined, else start from the beginning */
    circ = global_circuitlist; 
  else
    circ = circ->next;

212
  for( ; circ; circ = circ->next) {
213
214
215
216
217
218
    if(circ->n_addr == naddr && circ->n_port == nport)
       return circ;
  }
  return NULL;
}

Roger Dingledine's avatar
Roger Dingledine committed
219
220
circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn) {
  circuit_t *circ;
221
  connection_t *tmpconn;
Roger Dingledine's avatar
Roger Dingledine committed
222
223

  for(circ=global_circuitlist;circ;circ = circ->next) {
224
225
226
227
228
229
230
231
232
233
234
235
    if(circ->p_aci == aci) {
      for(tmpconn = circ->p_conn; tmpconn; tmpconn = tmpconn->next_topic) {
        if(tmpconn == conn)
          return circ;
      }
    }
    if(circ->n_aci == aci) {
      for(tmpconn = circ->n_conn; tmpconn; tmpconn = tmpconn->next_topic) {
        if(tmpconn == conn)
          return circ;
      }
    }
Roger Dingledine's avatar
Roger Dingledine committed
236
237
238
239
240
241
  }
  return NULL;
}

circuit_t *circuit_get_by_conn(connection_t *conn) {
  circuit_t *circ;
242
  connection_t *tmpconn;
Roger Dingledine's avatar
Roger Dingledine committed
243
244

  for(circ=global_circuitlist;circ;circ = circ->next) {
245
246
247
248
249
250
    for(tmpconn = circ->p_conn; tmpconn; tmpconn=tmpconn->next_topic)
      if(tmpconn == conn)
        return circ;
    for(tmpconn = circ->n_conn; tmpconn; tmpconn=tmpconn->next_topic)
      if(tmpconn == conn)
        return circ;
Roger Dingledine's avatar
Roger Dingledine committed
251
252
253
254
  }
  return NULL;
}

255
256
257
258
int circuit_deliver_data_cell_from_edge(cell_t *cell, circuit_t *circ, char edge_type) {
  int cell_direction;

  log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): called, edge_type %d.", edge_type);
Roger Dingledine's avatar
Roger Dingledine committed
259

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  if(edge_type == EDGE_AP) { /* i'm the AP */
    cell_direction = CELL_DIRECTION_OUT;
    if(circ->p_receive_circwindow <= 0) {
      log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): window 0, queueing for later.");
      circ->data_queue = data_queue_add(circ->data_queue, cell);
      return 0;
    }
    circ->p_receive_circwindow--;
  } else { /* i'm the exit */
    cell_direction = CELL_DIRECTION_IN;
    if(circ->n_receive_circwindow <= 0) {
      log(LOG_DEBUG,"circuit_deliver_data_cell_from_edge(): window 0, queueing for later.");
      circ->data_queue = data_queue_add(circ->data_queue, cell);
      return 0;
    }
    circ->n_receive_circwindow--;
  }

  if(circuit_deliver_data_cell(cell, circ, cell_direction) < 0) {
    return -1;
  }
    
  circuit_consider_stop_edge_reading(circ, edge_type); /* has window reached 0? */
  return 0;
}

int circuit_deliver_data_cell(cell_t *cell, circuit_t *circ, int cell_direction) {
  connection_t *conn;

  assert(cell && circ);
  assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN); 
  if(cell_direction == CELL_DIRECTION_OUT)
    conn = circ->n_conn;
  else
    conn = circ->p_conn;

  /* first crypt cell->length */
  if(circuit_crypt(circ, &(cell->length), 1, cell_direction) < 0) {
    log(LOG_DEBUG,"circuit_deliver_data_cell(): length crypt failed. Dropping connection.");
Roger Dingledine's avatar
Roger Dingledine committed
299
300
301
    return -1;
  }

302
303
304
  /* then crypt the payload */
  if(circuit_crypt(circ, (char *)&(cell->payload), CELL_PAYLOAD_SIZE, cell_direction) < 0) {
    log(LOG_DEBUG,"circuit_deliver_data_cell(): payload crypt failed. Dropping connection.");
Roger Dingledine's avatar
Roger Dingledine committed
305
306
307
    return -1;
  }

308
309
310
  if((!conn && cell_direction == CELL_DIRECTION_OUT) || (conn && conn->type == CONN_TYPE_EXIT)) {
    log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to exit.");
    return connection_exit_process_data_cell(cell, circ);
Roger Dingledine's avatar
Roger Dingledine committed
311
  }
312
313
314
  if((!conn && cell_direction == CELL_DIRECTION_IN) || (conn && conn->type == CONN_TYPE_AP)) {
    log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to AP.");
    return connection_ap_process_data_cell(cell, circ);
315
316
  }
  /* else send it as a cell */
317
318
  assert(conn);
  //log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to connection.");
319
  return connection_write_cell_to_buf(cell, conn);
Roger Dingledine's avatar
Roger Dingledine committed
320
321
}

322
int circuit_crypt(circuit_t *circ, char *in, int inlen, char cell_direction) {
Roger Dingledine's avatar
Roger Dingledine committed
323
  char *out;
324
325
  int i;
  crypt_path_t *thishop;
Roger Dingledine's avatar
Roger Dingledine committed
326

327
  assert(circ && in);
Roger Dingledine's avatar
Roger Dingledine committed
328

329
  out = (char *)malloc(inlen);
Roger Dingledine's avatar
Roger Dingledine committed
330
331
332
  if(!out)
    return -1;

333
  if(cell_direction == CELL_DIRECTION_IN) { //crypt_type == 'e') {
334
    if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
335
336
337
      for (i=circ->cpathlen-1; i >= 0; i--) /* moving from first to last hop 
                                       * Remember : cpath is in reverse order, i.e. last hop first
                                       */
338
339
      { 
        thishop = circ->cpath[i];
340
341
342
343

        /* decrypt */
        if(crypto_cipher_decrypt(thishop->b_crypto, in, inlen, out)) {
          log(LOG_ERR,"Error performing decryption:%s",crypto_perror());
344
345
346
347
348
349
350
351
          free(out);
          return -1;
        }

        /* copy ciphertext back to buf */
        memcpy(in,out,inlen);
      }
    } else { /* we're in the middle. Just one crypt. */
352
      if(crypto_cipher_encrypt(circ->p_crypto,in, inlen, out)) {
353
        log(LOG_ERR,"circuit_encrypt(): Encryption failed for ACI : %u (%s).",
354
            circ->p_aci, crypto_perror());
355
356
357
358
        free(out);
        return -1;
      }
      memcpy(in,out,inlen);
Roger Dingledine's avatar
Roger Dingledine committed
359
    }
360
  } else if(cell_direction == CELL_DIRECTION_OUT) { //crypt_type == 'd') {
361
    if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
362
363
364
      for (i=0; i < circ->cpathlen; i++) /* moving from last to first hop 
                                          * Remember : cpath is in reverse order, i.e. last hop first
                                          */
365
366
      { 
        thishop = circ->cpath[i];
367
    
368
        /* encrypt */
369
370
        if(crypto_cipher_encrypt(thishop->f_crypto, in, inlen, (unsigned char *)out)) {
          log(LOG_ERR,"Error performing encryption:%s",crypto_perror());
371
372
373
374
375
376
377
378
          free(out);
          return -1;
        }

        /* copy ciphertext back to buf */
        memcpy(in,out,inlen);
      }
    } else { /* we're in the middle. Just one crypt. */
379
      if(crypto_cipher_decrypt(circ->n_crypto,in, inlen, out)) {
380
        log(LOG_ERR,"circuit_crypt(): Decryption failed for ACI : %u (%s).",
381
            circ->n_aci, crypto_perror());
382
383
384
385
        free(out);
        return -1;
      }
      memcpy(in,out,inlen);
Roger Dingledine's avatar
Roger Dingledine committed
386
    }
387
388
389
  } else {
    log(LOG_ERR,"circuit_crypt(): unknown cell direction %d.", cell_direction);
    assert(0);
Roger Dingledine's avatar
Roger Dingledine committed
390
391
392
  }

  free(out);
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
  return 0;
}

void circuit_resume_edge_reading(circuit_t *circ, int edge_type) {
  connection_t *conn;
  struct data_queue_t *tmpd;

  assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);

  if(edge_type == EDGE_EXIT)
    conn = circ->n_conn;
  else
    conn = circ->p_conn;

  /* first, send the queue waiting at circ onto the circuit */
  while(circ->data_queue) {
    assert(circ->data_queue->cell);
    if(edge_type == EDGE_EXIT) {   
      circ->p_receive_circwindow--;
      assert(circ->p_receive_circwindow >= 0);
      
      if(circuit_deliver_data_cell(circ->data_queue->cell, circ, CELL_DIRECTION_IN) < 0) {
        circuit_close(circ);
        return;
      }
    } else { /* ap */
      circ->p_receive_circwindow--;
      assert(circ->p_receive_circwindow >= 0);
      
      if(circuit_deliver_data_cell(circ->data_queue->cell, circ, CELL_DIRECTION_IN) < 0) {
        circuit_close(circ);
        return;
      }
    }
    tmpd = circ->data_queue;
    circ->data_queue = tmpd->next;
    free(tmpd->cell);
    free(tmpd);

    if(circuit_consider_stop_edge_reading(circ, edge_type))
      return;
  }

  for( ; conn; conn=conn->next_topic) {
    if((edge_type == EDGE_EXIT && conn->n_receive_topicwindow > 0) ||
       (edge_type == EDGE_AP   && conn->p_receive_topicwindow > 0)) {
      connection_start_reading(conn);
      connection_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
    }
  }
  circuit_consider_stop_edge_reading(circ, edge_type);
}

/* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type) {
  connection_t *conn = NULL;

  assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);

  if(edge_type == EDGE_EXIT && circ->p_receive_circwindow <= 0)
    conn = circ->n_conn;      
  else if(edge_type == EDGE_AP && circ->n_receive_circwindow <= 0)
    conn = circ->p_conn;      
  else
    return 0;

  for( ; conn; conn=conn->next_topic)
    connection_stop_reading(conn);

  return 1;
}

int circuit_consider_sending_sendme(circuit_t *circ, int edge_type) {
  cell_t sendme;
Roger Dingledine's avatar
Roger Dingledine committed
467

468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
  assert(circ);

  sendme.command = CELL_SENDME;
  sendme.length = CIRCWINDOW_INCREMENT;

  if(edge_type == EDGE_AP) { /* i'm the AP */
    if(circ->n_receive_circwindow < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
      log(LOG_DEBUG,"circuit_consider_sending_sendme(): Queueing sendme forward.");
      circ->n_receive_circwindow += CIRCWINDOW_INCREMENT;
      sendme.aci = circ->n_aci;
      return connection_write_cell_to_buf(&sendme, circ->n_conn); /* (clobbers sendme) */
    }
  } else if(edge_type == EDGE_EXIT) { /* i'm the exit */
    if(circ->p_receive_circwindow < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
      log(LOG_DEBUG,"circuit_consider_sending_sendme(): Queueing sendme back.");
      circ->p_receive_circwindow += CIRCWINDOW_INCREMENT;
      sendme.aci = circ->p_aci;
      return connection_write_cell_to_buf(&sendme, circ->p_conn); /* (clobbers sendme) */
    }
  }
Roger Dingledine's avatar
Roger Dingledine committed
488
489
490
491
  return 0;
}

void circuit_close(circuit_t *circ) {
492
493
  connection_t *conn;

Roger Dingledine's avatar
Roger Dingledine committed
494
  circuit_remove(circ);
495
  for(conn=circ->n_conn; conn; conn=conn->next_topic) {
496
    connection_send_destroy(circ->n_aci, circ->n_conn); 
497
498
  }
  for(conn=circ->p_conn; conn; conn=conn->next_topic) {
499
    connection_send_destroy(circ->p_aci, circ->p_conn); 
500
  }
Roger Dingledine's avatar
Roger Dingledine committed
501
502
503
504
505
506
507
508
509
  circuit_free(circ);
}

void circuit_about_to_close_connection(connection_t *conn) {
  /* send destroys for all circuits using conn */
  /* currently, we assume it's too late to flush conn's buf here.
   * down the road, maybe we'll consider that eof doesn't mean can't-write
   */
  circuit_t *circ;
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
  connection_t *prevconn, *tmpconn;
  cell_t cell;
  int edge_type;

  if(!connection_speaks_cells(conn)) {
    /* it's an edge conn. need to remove it from the linked list of
     * conn's for this circuit. Send an 'end' data topic.
     * But don't kill the circuit.
     */

    circ = circuit_get_by_conn(conn);
    if(!circ)
      return;

    memset(&cell, 0, sizeof(cell_t));
    cell.command = CELL_DATA;
    cell.length = TOPIC_HEADER_SIZE;
    *(uint32_t *)cell.payload = conn->topic_id;
    *cell.payload = TOPIC_COMMAND_END;
   
    if(conn == circ->p_conn) {
      circ->p_conn = conn->next_topic;
      edge_type = EDGE_AP;
      goto send_end;
    }
    if(conn == circ->n_conn) {
      circ->n_conn = conn->next_topic;
      edge_type = EDGE_EXIT;
      goto send_end;
    }
    for(prevconn = circ->p_conn; prevconn->next_topic && prevconn->next_topic != conn; prevconn = prevconn->next_topic) ;
    if(prevconn->next_topic) {
      prevconn->next_topic = conn->next_topic;
      edge_type = EDGE_AP;
      goto send_end;
    }
    for(prevconn = circ->n_conn; prevconn->next_topic && prevconn->next_topic != conn; prevconn = prevconn->next_topic) ;
    if(prevconn->next_topic) {
      prevconn->next_topic = conn->next_topic;
      edge_type = EDGE_EXIT;
      goto send_end;
    }
    log(LOG_ERR,"circuit_about_to_close_connection(): edge conn not in circuit's list?");
    assert(0); /* should never get here */
send_end:
    if(edge_type == EDGE_AP) { /* send to circ->n_conn */
      log(LOG_INFO,"circuit_about_to_close_connection(): send data end forward (aci %d).",circ->n_aci);
      cell.aci = circ->n_aci;
    } else { /* send to circ->p_conn */
      assert(edge_type == EDGE_EXIT);
      log(LOG_INFO,"circuit_about_to_close_connection(): send data end backward (aci %d).",circ->p_aci);
      cell.aci = circ->p_aci;
    }

    if(circuit_deliver_data_cell_from_edge(&cell, circ, edge_type) < 0) {
      log(LOG_DEBUG,"circuit_about_to_close_connection(): circuit_deliver_data_cell_from_edge (%d) failed. Closing.", edge_type);
      circuit_close(circ);
    }
    return;
  }
Roger Dingledine's avatar
Roger Dingledine committed
570
571
572
573

  while((circ = circuit_get_by_conn(conn))) {
    circuit_remove(circ);
    if(circ->n_conn == conn) /* it's closing in front of us */
574
575
576
      for(tmpconn=circ->p_conn; tmpconn; tmpconn=tmpconn->next_topic) {
        connection_send_destroy(circ->p_aci, tmpconn); 
      }
Roger Dingledine's avatar
Roger Dingledine committed
577
    if(circ->p_conn == conn) /* it's closing behind us */
578
579
580
      for(tmpconn=circ->n_conn; tmpconn; tmpconn=tmpconn->next_topic) {
        connection_send_destroy(circ->n_aci, tmpconn); 
      }
Roger Dingledine's avatar
Roger Dingledine committed
581
582
583
584
    circuit_free(circ);
  }  
}

585
/* FIXME this now leaves some out */
586
587
void circuit_dump_by_conn(connection_t *conn) {
  circuit_t *circ;
588
  connection_t *tmpconn;
589
590

  for(circ=global_circuitlist;circ;circ = circ->next) {
591
592
593
594
595
    for(tmpconn=circ->p_conn; tmpconn; tmpconn=tmpconn->next_topic) {
      if(tmpconn == conn) {
        printf("Conn %d has App-ward circuit:  aci %d (other side %d), state %d (%s)\n",
          conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
      }
596
    }
597
598
599
600
601
    for(tmpconn=circ->n_conn; tmpconn; tmpconn=tmpconn->next_topic) {
      if(tmpconn == conn) {
        printf("Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)\n",
          conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
      }
602
603
604
605
    }
  }
}