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

#include "or.h"

7
static int directory_send_command(connection_t *conn, int command);
Roger Dingledine's avatar
Roger Dingledine committed
8
9
static int directory_handle_command(connection_t *conn);

10
11
12
13
/********* START VARIABLES **********/

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

14
static char fetchstring[] = "GET / HTTP/1.0\r\n\r\n";
15
static char answerstring[] = "HTTP/1.0 200 OK\r\n\r\n";
Roger Dingledine's avatar
Roger Dingledine committed
16
17
/* XXX the_directory is the same name as a different variable in
 * dirserv.c, are you crazy?? */
18
19
static char the_directory[MAX_DIR_SIZE+1];
static int directorylen=0;
20
21
22

/********* END VARIABLES ************/

23
void directory_initiate_command(routerinfo_t *router, int command) {
24
25
  connection_t *conn;

26
  if (command == DIR_CONN_STATE_CONNECTING_FETCH)
27
    log_fn(LOG_DEBUG,"initiating directory fetch");
28
  else
29
    log_fn(LOG_DEBUG,"initiating directory upload");
30

31
32
33
34
35
  if (!router) { /* i guess they didn't have one in mind for me to use */
    log_fn(LOG_WARN,"No running dirservers known. Not trying.");
    return;
  }

36
37
38
  conn = connection_new(CONN_TYPE_DIR);

  /* set up conn so it's got all the data we need to remember */
Roger Dingledine's avatar
Roger Dingledine committed
39
40
  conn->addr = router->addr;
  conn->port = router->dir_port;
41
42
  conn->address = tor_strdup(router->address);
  conn->nickname = tor_strdup(router->nickname);
43
44
  assert(router->identity_pkey);
  conn->identity_pkey = crypto_pk_dup_key(router->identity_pkey);
45

46
  if(connection_add(conn) < 0) { /* no space, forget it */
47
48
49
50
    connection_free(conn);
    return;
  }

51
52
  switch(connection_connect(conn, router->address, router->addr, router->dir_port)) {
    case -1:
53
      router_mark_as_down(conn->nickname); /* don't try him again */
54
      connection_remove(conn);
55
56
      connection_free(conn);
      return;
57
58
    case 0:
      connection_set_poll_socket(conn);
59
60
61
      connection_watch_events(conn, POLLIN | POLLOUT | POLLERR);
      /* writable indicates finish, readable indicates broken link,
         error indicates broken link in windowsland. */
62
      conn->state = command;
63
      return;
64
    /* case 1: fall through */
65
66
  }

67
  connection_set_poll_socket(conn);
68
  if(directory_send_command(conn, command) < 0) {
69
70
71
72
73
    connection_remove(conn);
    connection_free(conn);
  }
}

74
static int directory_send_command(connection_t *conn, int command) {
75
76
  const char *s;
  char tmp[8192];
77
78
79

  assert(conn && conn->type == CONN_TYPE_DIR);

80
  switch(command) {
81
    case DIR_CONN_STATE_CONNECTING_FETCH:
82
      connection_write_to_buf(fetchstring, strlen(fetchstring), conn);
83
      conn->state = DIR_CONN_STATE_CLIENT_SENDING_FETCH;
84
      break;
85
    case DIR_CONN_STATE_CONNECTING_UPLOAD:
86
87
      s = router_get_my_descriptor();
      if(!s) {
Roger Dingledine's avatar
Roger Dingledine committed
88
        log_fn(LOG_WARN,"Failed to get my descriptor.");
89
90
        return -1;
      }
91
92
      snprintf(tmp, sizeof(tmp), "POST / HTTP/1.0\r\nContent-Length: %d\r\n\r\n%s",
               strlen(s), s);
93
      connection_write_to_buf(tmp, strlen(tmp), conn);
94
      conn->state = DIR_CONN_STATE_CLIENT_SENDING_UPLOAD;
95
      break;
96
97
98
99
100
101
102
103
104
  }
  return 0;
}

int connection_dir_process_inbuf(connection_t *conn) {

  assert(conn && conn->type == CONN_TYPE_DIR);

  if(conn->inbuf_reached_eof) {
105
    switch(conn->state) {
106
      case DIR_CONN_STATE_CLIENT_READING_FETCH:
107
        /* kill it, but first process the_directory and learn about new routers. */
108
        switch(fetch_from_buf_http(conn->inbuf,
109
                                   NULL, 0, the_directory, MAX_DIR_SIZE)) {
110
          case -1: /* overflow */
Roger Dingledine's avatar
Roger Dingledine committed
111
            log_fn(LOG_WARN,"'fetch' response too large. Failing.");
112
113
            return -1;
          case 0:
114
            log_fn(LOG_INFO,"'fetch' response not all here, but we're at eof. Closing.");
115
116
117
            return -1;
          /* case 1, fall through */
        }
118
        /* XXX check headers, at least make sure returned 2xx */
119
        directorylen = strlen(the_directory);
120
        log_fn(LOG_INFO,"Received directory (size %d):\n%s", directorylen, the_directory);
121
        if(directorylen == 0) {
122
          log_fn(LOG_INFO,"Empty directory. Ignoring.");
123
124
          return -1;
        }
Roger Dingledine's avatar
Roger Dingledine committed
125
        if(router_set_routerlist_from_directory(the_directory, conn->identity_pkey) < 0){
126
          log_fn(LOG_INFO,"...but parsing failed. Ignoring.");
127
        } else {
Roger Dingledine's avatar
Roger Dingledine committed
128
          log_fn(LOG_INFO,"updated routers.");
129
        }
130
        if(options.ORPort) { /* connect to them all */
131
132
133
          router_retry_connections();
        }
        return -1;
134
      case DIR_CONN_STATE_CLIENT_READING_UPLOAD:
135
        /* XXX make sure there's a 200 OK on the buffer */
136
        log_fn(LOG_INFO,"eof while reading upload response. Finished.");
137
138
        return -1;
      default:
139
        log_fn(LOG_INFO,"conn reached eof, not reading. Closing.");
140
        return -1;
Roger Dingledine's avatar
cleanup    
Roger Dingledine committed
141
    }
142
143
  }

144
145
146
147
  if(conn->state == DIR_CONN_STATE_SERVER_COMMAND_WAIT)
    return directory_handle_command(conn);

  /* XXX for READ states, might want to make sure inbuf isn't too big */
148

149
  log_fn(LOG_DEBUG,"Got data, not eof. Leaving on inbuf.");
150
151
152
  return 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
153
static int directory_handle_command(connection_t *conn) {
154
  char headers[2048];
155
  char body[50000]; /* XXX */
Roger Dingledine's avatar
Roger Dingledine committed
156
  size_t dlen;
157
  const char *cp;
158
159
160

  assert(conn && conn->type == CONN_TYPE_DIR);

161
  switch(fetch_from_buf_http(conn->inbuf,
162
                             headers, sizeof(headers), body, sizeof(body))) {
163
    case -1: /* overflow */
Roger Dingledine's avatar
Roger Dingledine committed
164
      log_fn(LOG_WARN,"input too large. Failing.");
165
166
167
168
169
      return -1;
    case 0:
      log_fn(LOG_DEBUG,"command not all here yet.");
      return 0;
    /* case 1, fall through */
170
171
  }

172
173
  log_fn(LOG_DEBUG,"headers '%s', body '%s'.",headers,body);
  if(!strncasecmp(headers,"GET",3)) {
174
    /* XXX should check url and http version */
175
    log_fn(LOG_DEBUG,"Received GET command.");
176

Roger Dingledine's avatar
Roger Dingledine committed
177
    dlen = dirserv_get_directory(&cp);
178

Roger Dingledine's avatar
Roger Dingledine committed
179
    if(dlen == 0) {
Roger Dingledine's avatar
Roger Dingledine committed
180
      log_fn(LOG_WARN,"My directory is empty. Closing.");
181
      return -1; /* XXX send some helpful http error code */
182
    }
183

184
    log_fn(LOG_DEBUG,"Dumping directory to client."); 
185
186
    connection_write_to_buf(answerstring, strlen(answerstring), conn);
    connection_write_to_buf(cp, dlen, conn);
187
188
    conn->state = DIR_CONN_STATE_SERVER_WRITING;
    return 0;
189
190
  }

191
  if(!strncasecmp(headers,"POST",4)) {
192
    /* XXX should check url and http version */
193
    log_fn(LOG_DEBUG,"Received POST command.");
194
195
    cp = body;
    if(dirserv_add_descriptor(&cp) < 0) {
Roger Dingledine's avatar
Roger Dingledine committed
196
      log_fn(LOG_WARN,"dirserv_add_descriptor() failed. Dropping.");
197
198
      return -1; /* XXX should write an http failed code */
    }
199
    dirserv_get_directory(&cp); /* rebuild and write to disk */
200
    connection_write_to_buf(answerstring, strlen(answerstring), conn);
201
202
    conn->state = DIR_CONN_STATE_SERVER_WRITING;
    return 0;
203
204
  }

Roger Dingledine's avatar
Roger Dingledine committed
205
  log_fn(LOG_WARN,"Got headers with unknown command. Closing.");
206
  return -1;
207
208
209
210
211
212
213
214
}

int connection_dir_finished_flushing(connection_t *conn) {
  int e, len=sizeof(e);

  assert(conn && conn->type == CONN_TYPE_DIR);

  switch(conn->state) {
215
216
    case DIR_CONN_STATE_CONNECTING_FETCH:
    case DIR_CONN_STATE_CONNECTING_UPLOAD:
217
      if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0)  { /* not yet */
218
        if(!ERRNO_CONN_EINPROGRESS(errno)) {
219
          log_fn(LOG_DEBUG,"in-progress connect failed. Removing.");
220
          router_mark_as_down(conn->nickname); /* don't try him again */
221
222
223
224
225
226
227
          return -1;
        } else {
          return 0; /* no change, see if next time is better */
        }
      }
      /* the connect has finished. */

228
      log_fn(LOG_INFO,"Dir connection to router %s:%u established.",
229
230
          conn->address,conn->port);

231
      return directory_send_command(conn, conn->state);
232
233
234
    case DIR_CONN_STATE_CLIENT_SENDING_FETCH:
      log_fn(LOG_DEBUG,"client finished sending fetch command.");
      conn->state = DIR_CONN_STATE_CLIENT_READING_FETCH;
235
236
      connection_watch_events(conn, POLLIN);
      return 0;
237
238
239
    case DIR_CONN_STATE_CLIENT_SENDING_UPLOAD:
      log_fn(LOG_DEBUG,"client finished sending upload command.");
      conn->state = DIR_CONN_STATE_CLIENT_READING_UPLOAD;
240
241
      connection_watch_events(conn, POLLIN);
      return 0;
242
    case DIR_CONN_STATE_SERVER_WRITING:
243
      log_fn(LOG_INFO,"Finished writing server response. Closing.");
244
245
      return -1; /* kill it */
    default:
Roger Dingledine's avatar
Roger Dingledine committed
246
      log_fn(LOG_WARN,"BUG: called in unexpected state.");
247
      return -1;
248
249
250
251
  }
  return 0;
}

252
253
254
255
256
257
258
/*
  Local Variables:
  mode:c
  indent-tabs-mode:nil
  c-basic-offset:2
  End:
*/