router.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /**
  6. * \file router.c
  7. * \brief OR functionality, including key maintenance, generating
  8. * and uploading server descriptors, retrying OR connections.
  9. **/
  10. extern or_options_t options; /* command-line and config-file options */
  11. /** Exposed for test.c. */ void get_platform_str(char *platform, int len);
  12. /************************************************************/
  13. /*****
  14. * Key management: ORs only.
  15. *****/
  16. /** Private keys for this OR. There is also an SSL key managed by tortls.c.
  17. */
  18. static tor_mutex_t *key_lock=NULL;
  19. static time_t onionkey_set_at=0; /* When was onionkey last changed? */
  20. static crypto_pk_env_t *onionkey=NULL;
  21. static crypto_pk_env_t *lastonionkey=NULL;
  22. static crypto_pk_env_t *identitykey=NULL;
  23. /** Replace the current onion key with <b>k</b>. Does not affect lastonionkey;
  24. * to update onionkey correctly, call rotate_onion_key().
  25. */
  26. void set_onion_key(crypto_pk_env_t *k) {
  27. tor_mutex_acquire(key_lock);
  28. onionkey = k;
  29. onionkey_set_at = time(NULL);
  30. tor_mutex_release(key_lock);
  31. }
  32. /** Return the current onion key. Requires that the onion key has been
  33. * loaded or generated. */
  34. crypto_pk_env_t *get_onion_key(void) {
  35. tor_assert(onionkey);
  36. return onionkey;
  37. }
  38. /** Return the onion key that was current before the most recent onion
  39. * key rotation. If no rotation has been performed since this process
  40. * started, return NULL.
  41. */
  42. crypto_pk_env_t *get_previous_onion_key(void) {
  43. return lastonionkey;
  44. }
  45. void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last)
  46. {
  47. tor_assert(key && last);
  48. tor_mutex_acquire(key_lock);
  49. *key = crypto_pk_dup_key(onionkey);
  50. if (lastonionkey)
  51. *last = crypto_pk_dup_key(lastonionkey);
  52. else
  53. *last = NULL;
  54. tor_mutex_release(key_lock);
  55. }
  56. /** Return the time when the onion key was last set. This is either the time
  57. * when the process launched, or the time of the most recent key rotation since
  58. * the process launched.
  59. */
  60. time_t get_onion_key_set_at(void) {
  61. return onionkey_set_at;
  62. }
  63. /** Set the current identity key to k.
  64. */
  65. void set_identity_key(crypto_pk_env_t *k) {
  66. identitykey = k;
  67. }
  68. /** Returns the current identity key; requires that the identity key has been
  69. * set.
  70. */
  71. crypto_pk_env_t *get_identity_key(void) {
  72. tor_assert(identitykey);
  73. return identitykey;
  74. }
  75. /** Replace the previous onion key with the current onion key, and generate
  76. * a new previous onion key. Immediately after calling this function,
  77. * the OR should:
  78. * - schedule all previous cpuworkers to shut down _after_ processing
  79. * pending work. (This will cause fresh cpuworkers to be generated.)
  80. * - generate and upload a fresh routerinfo.
  81. */
  82. void rotate_onion_key(void)
  83. {
  84. char fname[512];
  85. crypto_pk_env_t *prkey;
  86. sprintf(fname,"%s/keys/onion.key",options.DataDirectory);
  87. if (!(prkey = crypto_new_pk_env())) {
  88. log(LOG_ERR, "Error creating crypto environment.");
  89. goto error;
  90. }
  91. if (crypto_pk_generate_key(prkey)) {
  92. log(LOG_ERR, "Error generating onion key");
  93. goto error;
  94. }
  95. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  96. log(LOG_ERR, "Couldn't write generated key to %s.", fname);
  97. goto error;
  98. }
  99. tor_mutex_acquire(key_lock);
  100. if (lastonionkey)
  101. crypto_free_pk_env(lastonionkey);
  102. log_fn(LOG_INFO, "Rotating onion key");
  103. lastonionkey = onionkey;
  104. set_onion_key(prkey);
  105. tor_mutex_release(key_lock);
  106. return;
  107. error:
  108. log_fn(LOG_WARN, "Couldn't rotate onion key.");
  109. }
  110. /** Try to read an RSA key from <b>fname</b>. If <b>fname</b> doesn't exist,
  111. * create a new RSA key and save it in <b>fname</b>. Return the read/created
  112. * key, or NULL on error.
  113. */
  114. crypto_pk_env_t *init_key_from_file(const char *fname)
  115. {
  116. crypto_pk_env_t *prkey = NULL;
  117. FILE *file = NULL;
  118. if (!(prkey = crypto_new_pk_env())) {
  119. log(LOG_ERR, "Error creating crypto environment.");
  120. goto error;
  121. }
  122. switch(file_status(fname)) {
  123. case FN_DIR:
  124. case FN_ERROR:
  125. log(LOG_ERR, "Can't read key from %s", fname);
  126. goto error;
  127. case FN_NOENT:
  128. log(LOG_INFO, "No key found in %s; generating fresh key.", fname);
  129. if (crypto_pk_generate_key(prkey)) {
  130. log(LOG_ERR, "Error generating onion key");
  131. goto error;
  132. }
  133. if (crypto_pk_check_key(prkey) <= 0) {
  134. log(LOG_ERR, "Generated key seems invalid");
  135. goto error;
  136. }
  137. log(LOG_INFO, "Generated key seems valid");
  138. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  139. log(LOG_ERR, "Couldn't write generated key to %s.", fname);
  140. goto error;
  141. }
  142. return prkey;
  143. case FN_FILE:
  144. if (crypto_pk_read_private_key_from_filename(prkey, fname)) {
  145. log(LOG_ERR, "Error loading private key.");
  146. goto error;
  147. }
  148. return prkey;
  149. default:
  150. tor_assert(0);
  151. }
  152. error:
  153. if (prkey)
  154. crypto_free_pk_env(prkey);
  155. if (file)
  156. fclose(file);
  157. return NULL;
  158. }
  159. /** Initialize all OR private keys, and the TLS context, as necessary.
  160. * On OPs, this only initializes the tls context.
  161. */
  162. int init_keys(void) {
  163. char keydir[512];
  164. char fingerprint[FINGERPRINT_LEN+MAX_NICKNAME_LEN+3];
  165. char *cp;
  166. const char *tmp, *mydesc;
  167. crypto_pk_env_t *prkey;
  168. if (!key_lock)
  169. key_lock = tor_mutex_new();
  170. /* OP's don't need keys. Just initialize the TLS context.*/
  171. if (!options.ORPort) {
  172. tor_assert(!options.DirPort);
  173. if (tor_tls_context_new(NULL, 0, NULL, 0)<0) {
  174. log_fn(LOG_ERR, "Error creating TLS context for OP.");
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. /* Make sure DataDirectory exists, and is private. */
  180. tor_assert(options.DataDirectory);
  181. if (strlen(options.DataDirectory) > (512-128)) {
  182. log_fn(LOG_ERR, "DataDirectory is too long.");
  183. return -1;
  184. }
  185. if (check_private_dir(options.DataDirectory, 1)) {
  186. return -1;
  187. }
  188. /* Check the key directory. */
  189. sprintf(keydir,"%s/keys",options.DataDirectory);
  190. if (check_private_dir(keydir, 1)) {
  191. return -1;
  192. }
  193. cp = keydir + strlen(keydir); /* End of string. */
  194. /* 1. Read identity key. Make it if none is found. */
  195. strcpy(cp, "/identity.key");
  196. log_fn(LOG_INFO,"Reading/making identity key %s...",keydir);
  197. prkey = init_key_from_file(keydir);
  198. if (!prkey) return -1;
  199. set_identity_key(prkey);
  200. /* 2. Read onion key. Make it if none is found. */
  201. strcpy(cp, "/onion.key");
  202. log_fn(LOG_INFO,"Reading/making onion key %s...",keydir);
  203. prkey = init_key_from_file(keydir);
  204. if (!prkey) return -1;
  205. set_onion_key(prkey);
  206. /* 3. Initialize link key and TLS context. */
  207. if (tor_tls_context_new(get_identity_key(), 1, options.Nickname,
  208. MAX_SSL_KEY_LIFETIME) < 0) {
  209. log_fn(LOG_ERR, "Error initializing TLS context");
  210. return -1;
  211. }
  212. /* 4. Dump router descriptor to 'router.desc' */
  213. /* Must be called after keys are initialized. */
  214. if (!(router_get_my_descriptor())) {
  215. log_fn(LOG_ERR, "Error initializing descriptor.");
  216. return -1;
  217. }
  218. /* We need to add our own fingerprint so it gets recognized. */
  219. if (dirserv_add_own_fingerprint(options.Nickname, get_identity_key())) {
  220. log_fn(LOG_ERR, "Error adding own fingerprint to approved set");
  221. return -1;
  222. }
  223. tmp = mydesc = router_get_my_descriptor();
  224. if (dirserv_add_descriptor(&tmp) != 1) {
  225. log(LOG_ERR, "Unable to add own descriptor to directory.");
  226. return -1;
  227. }
  228. sprintf(keydir,"%s/router.desc", options.DataDirectory);
  229. log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
  230. if (write_str_to_file(keydir, mydesc)) {
  231. return -1;
  232. }
  233. /* 5. Dump fingerprint to 'fingerprint' */
  234. sprintf(keydir,"%s/fingerprint", options.DataDirectory);
  235. log_fn(LOG_INFO,"Dumping fingerprint to %s...",keydir);
  236. tor_assert(strlen(options.Nickname) <= MAX_NICKNAME_LEN);
  237. strcpy(fingerprint, options.Nickname);
  238. strcat(fingerprint, " ");
  239. if (crypto_pk_get_fingerprint(get_identity_key(),
  240. fingerprint+strlen(fingerprint))<0) {
  241. log_fn(LOG_ERR, "Error computing fingerprint");
  242. return -1;
  243. }
  244. strcat(fingerprint, "\n");
  245. if (write_str_to_file(keydir, fingerprint))
  246. return -1;
  247. if(!options.DirPort)
  248. return 0;
  249. /* 6. [dirserver only] load approved-routers file */
  250. sprintf(keydir,"%s/approved-routers", options.DataDirectory);
  251. log_fn(LOG_INFO,"Loading approved fingerprints from %s...",keydir);
  252. if(dirserv_parse_fingerprint_file(keydir) < 0) {
  253. log_fn(LOG_ERR, "Error loading fingerprints");
  254. return -1;
  255. }
  256. /* 7. [dirserver only] load old directory, if it's there */
  257. sprintf(keydir,"%s/cached-directory", options.DataDirectory);
  258. log_fn(LOG_INFO,"Loading cached directory from %s...",keydir);
  259. cp = read_file_to_str(keydir);
  260. if(!cp) {
  261. log_fn(LOG_INFO,"Cached directory %s not present. Ok.",keydir);
  262. } else {
  263. if(options.AuthoritativeDir && dirserv_load_from_directory_string(cp) < 0){
  264. log_fn(LOG_ERR, "Cached directory %s is corrupt", keydir);
  265. free(cp);
  266. return -1;
  267. }
  268. /* set time to 1 so it will be replaced on first download.
  269. */
  270. dirserv_set_cached_directory(cp, 1);
  271. }
  272. /* success */
  273. return 0;
  274. }
  275. /*
  276. * Clique maintenance
  277. */
  278. /** OR only: try to open connections to all of the other ORs we know about.
  279. */
  280. void router_retry_connections(void) {
  281. int i;
  282. routerinfo_t *router;
  283. routerlist_t *rl;
  284. router_get_routerlist(&rl);
  285. for (i=0;i < smartlist_len(rl->routers);i++) {
  286. router = smartlist_get(rl->routers, i);
  287. if(router_is_me(router))
  288. continue;
  289. if(!connection_exact_get_by_addr_port(router->addr,router->or_port)) {
  290. /* not in the list */
  291. log_fn(LOG_DEBUG,"connecting to OR %s:%u.",router->address,router->or_port);
  292. connection_or_connect(router);
  293. }
  294. }
  295. }
  296. /*
  297. * OR descriptor generation.
  298. */
  299. /** My routerinfo. */
  300. static routerinfo_t *desc_routerinfo = NULL;
  301. /** String representation of my descriptor, signed by me. */
  302. static char descriptor[8192];
  303. /** OR only: try to upload our signed descriptor to all the directory servers
  304. * we know about.
  305. */
  306. void router_upload_dir_desc_to_dirservers(void) {
  307. const char *s;
  308. s = router_get_my_descriptor();
  309. if (!s) {
  310. log_fn(LOG_WARN, "No descriptor; skipping upload");
  311. return;
  312. }
  313. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_DIR, s, strlen(s));
  314. }
  315. #define DEFAULT_EXIT_POLICY "reject 0.0.0.0/8,reject 169.254.0.0/16,reject 127.0.0.0/8,reject 192.168.0.0/16,reject 10.0.0.0/8,reject 172.16.0.0/12,accept *:20-22,accept *:53,accept *:79-81,accept *:110,accept *:143,accept *:443,accept *:873,accept *:993,accept *:995,accept *:1024-65535,reject *:*"
  316. /** Set the exit policy on <b>router</b> to match the exit policy in the
  317. * current configuration file. If the exit policy doesn't have a catch-all
  318. * rule, then append the default exit policy as well.
  319. */
  320. static void router_add_exit_policy_from_config(routerinfo_t *router) {
  321. struct exit_policy_t *ep;
  322. struct config_line_t default_policy;
  323. config_parse_exit_policy(options.ExitPolicy, &router->exit_policy);
  324. for (ep = router->exit_policy; ep; ep = ep->next) {
  325. if (ep->msk == 0 && ep->prt_min <= 1 && ep->prt_max >= 65535) {
  326. /* if exitpolicy includes a *:* line, then we're done. */
  327. return;
  328. }
  329. }
  330. /* Else, append the default exitpolicy. */
  331. default_policy.key = NULL;
  332. default_policy.value = DEFAULT_EXIT_POLICY;
  333. default_policy.next = NULL;
  334. config_parse_exit_policy(&default_policy, &router->exit_policy);
  335. }
  336. /** OR only: Return false if my exit policy says to allow connection to
  337. * conn. Else return true.
  338. */
  339. int router_compare_to_my_exit_policy(connection_t *conn)
  340. {
  341. tor_assert(desc_routerinfo);
  342. tor_assert(conn->addr); /* make sure it's resolved to something. this
  343. way we can't get a 'maybe' below. */
  344. return router_compare_addr_to_exit_policy(conn->addr, conn->port,
  345. desc_routerinfo->exit_policy);
  346. }
  347. /** Return true iff <b>router</b> has the same nickname as this OR. (For an
  348. * OP, always returns false.)
  349. */
  350. int router_is_me(routerinfo_t *router)
  351. {
  352. tor_assert(router);
  353. return options.Nickname && !strcasecmp(router->nickname, options.Nickname);
  354. }
  355. /** Return a routerinfo for this OR, rebuilding a fresh one if
  356. * necessary. Return NULL on error, or if called on an OP. */
  357. routerinfo_t *router_get_my_routerinfo(void)
  358. {
  359. if (!options.ORPort)
  360. return NULL;
  361. if (!desc_routerinfo) {
  362. if (router_rebuild_descriptor())
  363. return NULL;
  364. }
  365. return desc_routerinfo;
  366. }
  367. /** OR only: Return a signed server descriptor for this OR, rebuilding a fresh
  368. * one if necessary. Return NULL on error.
  369. */
  370. const char *router_get_my_descriptor(void) {
  371. if (!desc_routerinfo) {
  372. if (router_rebuild_descriptor())
  373. return NULL;
  374. }
  375. log_fn(LOG_DEBUG,"my desc is '%s'",descriptor);
  376. return descriptor;
  377. }
  378. /** Rebuild a fresh routerinfo and signed server descriptor for this
  379. * OR. Return 0 on success, -1 on error.
  380. */
  381. int router_rebuild_descriptor(void) {
  382. routerinfo_t *ri;
  383. struct in_addr addr;
  384. char platform[256];
  385. if (!tor_inet_aton(options.Address, &addr)) {
  386. log_fn(LOG_ERR, "options.Address didn't hold an IP.");
  387. return -1;
  388. }
  389. ri = tor_malloc_zero(sizeof(routerinfo_t));
  390. ri->address = tor_strdup(options.Address);
  391. ri->nickname = tor_strdup(options.Nickname);
  392. ri->addr = (uint32_t) ntohl(addr.s_addr);
  393. ri->or_port = options.ORPort;
  394. ri->socks_port = options.SocksPort;
  395. ri->dir_port = options.DirPort;
  396. ri->published_on = time(NULL);
  397. ri->onion_pkey = crypto_pk_dup_key(get_onion_key()); /* must invoke from main thread */
  398. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  399. get_platform_str(platform, sizeof(platform));
  400. ri->platform = tor_strdup(platform);
  401. ri->bandwidthrate = options.BandwidthRate;
  402. ri->bandwidthburst = options.BandwidthBurst;
  403. ri->exit_policy = NULL; /* zero it out first */
  404. router_add_exit_policy_from_config(ri);
  405. if (desc_routerinfo)
  406. routerinfo_free(desc_routerinfo);
  407. desc_routerinfo = ri;
  408. if (router_dump_router_to_string(descriptor, 8192, ri, get_identity_key())<0) {
  409. log_fn(LOG_WARN, "Couldn't dump router to string.");
  410. return -1;
  411. }
  412. if (ri->dir_port)
  413. ri->is_trusted_dir = 1;
  414. return 0;
  415. }
  416. /** Set <b>platform</b> (max length <b>len</b>) to a NUL-terminated short
  417. * string describing the version of Tor and the operating system we're
  418. * currently running on.
  419. */
  420. void get_platform_str(char *platform, int len)
  421. {
  422. snprintf(platform, len-1, "Tor %s on %s", VERSION, get_uname());
  423. platform[len-1] = '\0';
  424. return;
  425. }
  426. /* XXX need to audit this thing and count fenceposts. maybe
  427. * refactor so we don't have to keep asking if we're
  428. * near the end of maxlen?
  429. */
  430. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  431. /** OR only: Given a routerinfo for this router, and an identity key to sign
  432. * with, encode the routerinfo as a signed server descriptor and write the
  433. * result into <b>s</b>, using at most <b>maxlen</b> bytes. Return -1 on
  434. * failure, and the number of bytes used on success.
  435. */
  436. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  437. crypto_pk_env_t *ident_key) {
  438. char *onion_pkey; /* Onion key, PEM-encoded. */
  439. char *identity_pkey; /* Identity key, PEM-encoded. */
  440. char digest[20];
  441. char signature[128];
  442. char published[32];
  443. struct in_addr in;
  444. int onion_pkeylen, identity_pkeylen;
  445. int written;
  446. int result=0;
  447. struct exit_policy_t *tmpe;
  448. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  449. char *s_tmp, *s_dup;
  450. const char *cp;
  451. routerinfo_t *ri_tmp;
  452. #endif
  453. /* Make sure the identity key matches the one in the routerinfo. */
  454. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  455. log_fn(LOG_WARN,"Tried to sign a router with a private key that didn't match router's public key!");
  456. return -1;
  457. }
  458. /* PEM-encode the onion key */
  459. if(crypto_pk_write_public_key_to_string(router->onion_pkey,
  460. &onion_pkey,&onion_pkeylen)<0) {
  461. log_fn(LOG_WARN,"write onion_pkey to string failed!");
  462. return -1;
  463. }
  464. /* PEM-encode the identity key key */
  465. if(crypto_pk_write_public_key_to_string(router->identity_pkey,
  466. &identity_pkey,&identity_pkeylen)<0) {
  467. log_fn(LOG_WARN,"write identity_pkey to string failed!");
  468. tor_free(onion_pkey);
  469. return -1;
  470. }
  471. /* Encode the publication time. */
  472. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&router->published_on));
  473. /* Generate the easy portion of the router descriptor. */
  474. result = snprintf(s, maxlen,
  475. "router %s %s %d %d %d\n"
  476. "platform %s\n"
  477. "published %s\n"
  478. "bandwidth %d %d\n"
  479. "onion-key\n%s"
  480. "signing-key\n%s",
  481. router->nickname,
  482. router->address,
  483. router->or_port,
  484. router->socks_port,
  485. /* Due to an 0.0.7 bug, we can't actually say that we have a dirport unles
  486. * we're an authoritative directory.
  487. */
  488. router->is_trusted_dir ? router->dir_port : 0,
  489. router->platform,
  490. published,
  491. (int) router->bandwidthrate,
  492. (int) router->bandwidthburst,
  493. onion_pkey, identity_pkey);
  494. tor_free(onion_pkey);
  495. tor_free(identity_pkey);
  496. if(result < 0 || result >= maxlen) {
  497. /* apparently different glibcs do different things on snprintf error.. so check both */
  498. return -1;
  499. }
  500. /* From now on, we use 'written' to remember the current length of 's'. */
  501. written = result;
  502. if (router->dir_port && !router->is_trusted_dir) {
  503. /* dircacheport wasn't recognized before 0.0.8pre. (When 0.0.7 is gone,
  504. * we can fold this back into dirport anyway.) */
  505. result = snprintf(s+written,maxlen-written, "opt dircacheport %d\n",
  506. router->dir_port);
  507. if (result<0 || result+written > maxlen)
  508. return -1;
  509. written += result;
  510. }
  511. if (options.ContactInfo && strlen(options.ContactInfo)) {
  512. result = snprintf(s+written,maxlen-written, "opt contact %s\n",
  513. options.ContactInfo);
  514. if (result<0 || result+written > maxlen)
  515. return -1;
  516. written += result;
  517. }
  518. /* Write the exit policy to the end of 's'. */
  519. for(tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  520. in.s_addr = htonl(tmpe->addr);
  521. /* Write: "accept 1.2.3.4" */
  522. result = snprintf(s+written, maxlen-written, "%s %s",
  523. tmpe->policy_type == EXIT_POLICY_ACCEPT ? "accept" : "reject",
  524. tmpe->msk == 0 ? "*" : inet_ntoa(in));
  525. if(result < 0 || result+written > maxlen) {
  526. /* apparently different glibcs do different things on snprintf error.. so check both */
  527. return -1;
  528. }
  529. written += result;
  530. if (tmpe->msk != 0xFFFFFFFFu && tmpe->msk != 0) {
  531. /* Write "/255.255.0.0" */
  532. in.s_addr = htonl(tmpe->msk);
  533. result = snprintf(s+written, maxlen-written, "/%s", inet_ntoa(in));
  534. if (result<0 || result+written > maxlen)
  535. return -1;
  536. written += result;
  537. }
  538. if (tmpe->prt_min == 0 && tmpe->prt_max == 65535) {
  539. /* There is no port set; write ":*" */
  540. if (written > maxlen-4)
  541. return -1;
  542. strcat(s+written, ":*\n");
  543. written += 3;
  544. } else if (tmpe->prt_min == tmpe->prt_max) {
  545. /* There is only one port; write ":80". */
  546. result = snprintf(s+written, maxlen-written, ":%d\n", tmpe->prt_min);
  547. if (result<0 || result+written > maxlen)
  548. return -1;
  549. written += result;
  550. } else {
  551. /* There is a range of ports; write ":79-80". */
  552. result = snprintf(s+written, maxlen-written, ":%d-%d\n", tmpe->prt_min,
  553. tmpe->prt_max);
  554. if (result<0 || result+written > maxlen)
  555. return -1;
  556. written += result;
  557. }
  558. } /* end for */
  559. if (written > maxlen-256) /* Not enough room for signature. */
  560. return -1;
  561. /* Sign the directory */
  562. strcat(s+written, "router-signature\n");
  563. written += strlen(s+written);
  564. s[written] = '\0';
  565. if (router_get_router_hash(s, digest) < 0)
  566. return -1;
  567. if (crypto_pk_private_sign(ident_key, digest, 20, signature) < 0) {
  568. log_fn(LOG_WARN, "Error signing digest");
  569. return -1;
  570. }
  571. strcat(s+written, "-----BEGIN SIGNATURE-----\n");
  572. written += strlen(s+written);
  573. if (base64_encode(s+written, maxlen-written, signature, 128) < 0) {
  574. log_fn(LOG_WARN, "Couldn't base64-encode signature");
  575. return -1;
  576. }
  577. written += strlen(s+written);
  578. strcat(s+written, "-----END SIGNATURE-----\n");
  579. written += strlen(s+written);
  580. if (written > maxlen-2)
  581. return -1;
  582. /* include a last '\n' */
  583. s[written] = '\n';
  584. s[written+1] = 0;
  585. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  586. cp = s_tmp = s_dup = tor_strdup(s);
  587. ri_tmp = router_parse_entry_from_string(cp, NULL);
  588. if (!ri_tmp) {
  589. log_fn(LOG_ERR, "We just generated a router descriptor we can't parse: <<%s>>",
  590. s);
  591. return -1;
  592. }
  593. free(s_dup);
  594. routerinfo_free(ri_tmp);
  595. #endif
  596. return written+1;
  597. }
  598. /*
  599. Local Variables:
  600. mode:c
  601. indent-tabs-mode:nil
  602. c-basic-offset:2
  603. End:
  604. */