1 /* Copyright 2003 Roger Dingledine
2 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
3 /* See LICENSE for licensing information */
5 const char util_c_id
[] = "$Id$";
9 * \brief Common functions for strings, IO, network, data structures,
13 /* This is required on rh7 to make strptime not complain.
22 #include "container.h"
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
42 #ifdef HAVE_ARPA_INET_H
43 #include <arpa/inet.h>
51 #ifdef HAVE_SYS_LIMITS_H
52 #include <sys/limits.h>
54 #ifdef HAVE_MACHINE_LIMITS_H
56 /* FreeBSD has a bug where it complains that this file is obsolete,
57 and I should migrate to using sys/limits. It complains even when
59 #include <machine/limits.h>
62 #ifdef HAVE_SYS_TYPES_H
63 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
65 #ifdef HAVE_SYS_SOCKET_H
66 #include <sys/socket.h>
68 #ifdef HAVE_SYS_TIME_H
74 #ifdef HAVE_SYS_STAT_H
77 #ifdef HAVE_SYS_FCNTL_H
78 #include <sys/fcntl.h>
99 #define DMALLOC_FN_ARGS , file, line
101 #define dmalloc_strdup(file, line, string, xalloc_b) strdup(string)
103 #define dmalloc_malloc(file, line, size, func_id, alignment, xalloc_b) malloc(size)
104 #define DMALLOC_FUNC_MALLOC 0
106 #define dmalloc_realloc(file, line, old_pnt, new_size, func_id, xalloc_b) realloc((old_pnt), (new_size))
107 #define DMALLOC_FUNC_REALLOC 0
108 #define DMALLOC_FN_ARGS
111 /** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
112 * result. On error, log and terminate the process. (Same as malloc(size),
113 * but never returns NULL.)
115 * <b>file</b> and <b>line</b> are used if dmalloc is enabled, and
119 _tor_malloc(size_t size DMALLOC_PARAMS
)
123 /* Some libcs don't do the right thing on size==0. Override them. */
127 result
= dmalloc_malloc(file
, line
, size
, DMALLOC_FUNC_MALLOC
, 0, 0);
130 log_fn(LOG_ERR
, "Out of memory. Dying.");
131 /* XXX if these functions die within a worker process, they won't
135 // memset(result,'X',size); /* deadbeef to encourage bugs */
139 /* Allocate a chunk of <b>size</b> bytes of memory, fill the memory with
140 * zero bytes, and return a pointer to the result. Log and terminate
141 * the process on error. (Same as calloc(size,1), but never returns NULL.)
144 _tor_malloc_zero(size_t size DMALLOC_PARAMS
)
146 void *result
= _tor_malloc(size DMALLOC_FN_ARGS
);
147 memset(result
, 0, size
);
151 /** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b>
152 * bytes long; return the new memory block. On error, log and
153 * terminate. (Like realloc(ptr,size), but never returns NULL.)
156 _tor_realloc(void *ptr
, size_t size DMALLOC_PARAMS
)
160 result
= dmalloc_realloc(file
, line
, ptr
, size
, DMALLOC_FUNC_REALLOC
, 0);
162 log_fn(LOG_ERR
, "Out of memory. Dying.");
168 /** Return a newly allocated copy of the NUL-terminated string s. On
169 * error, log and terminate. (Like strdup(s), but never returns
173 _tor_strdup(const char *s DMALLOC_PARAMS
)
178 dup
= dmalloc_strdup(file
, line
, s
, 0);
180 log_fn(LOG_ERR
,"Out of memory. Dying.");
186 /** Allocate and return a new string containing the first <b>n</b>
187 * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
188 * characters, only the first <b>n</b> are copied. The result is
189 * always NUL-terminated. (Like strndup(s,n), but never returns
193 _tor_strndup(const char *s
, size_t n DMALLOC_PARAMS
)
197 dup
= _tor_malloc((n
+1) DMALLOC_FN_ARGS
);
198 /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
199 * this function gets called a whole lot, and platform strncpy is
200 * much faster than strlcpy when strlen(s) is much longer than n.
208 * String manipulation
211 /** Remove from the string <b>s</b> every character which appears in
212 * <b>strip</b>. Return the number of characters removed. */
214 tor_strstrip(char *s
, const char *strip
)
218 if (strchr(strip
, *read
)) {
228 /** Set the <b>dest_len</b>-byte buffer <b>buf</b> to contain the
229 * string <b>s</b>, with the string <b>insert</b> inserted after every
230 * <b>n</b> characters. Return 0 on success, -1 on failure.
232 * If <b>rule</b> is ALWAYS_TERMINATE, then always end the string with
233 * <b>insert</b>, even if its length is not a multiple of <b>n</b>. If
234 * <b>rule</b> is NEVER_TERMINATE, then never end the string with
235 * <b>insert</b>, even if its length <i>is</i> a multiple of <b>n</b>.
236 * If <b>rule</b> is TERMINATE_IF_EVEN, then end the string with <b>insert</b>
237 * exactly when its length <i>is</i> a multiple of <b>n</b>.
240 tor_strpartition(char *dest
, size_t dest_len
,
241 const char *s
, const char *insert
, size_t n
,
242 part_finish_rule_t rule
)
245 size_t len_in
, len_out
, len_ins
;
246 int is_even
, remaining
;
250 tor_assert(n
< SIZE_T_CEILING
);
251 tor_assert(dest_len
< SIZE_T_CEILING
);
253 len_ins
= strlen(insert
);
254 tor_assert(len_in
< SIZE_T_CEILING
);
255 tor_assert(len_in
/n
< SIZE_T_CEILING
/len_ins
); /* avoid overflow */
256 len_out
= len_in
+ (len_in
/n
)*len_ins
;
257 is_even
= (len_in
%n
) == 0;
260 case ALWAYS_TERMINATE
:
261 if (!is_even
) len_out
+= len_ins
;
263 case NEVER_TERMINATE
:
264 if (is_even
&& len_in
) len_out
-= len_ins
;
266 case TERMINATE_IF_EVEN
:
269 if (dest_len
< len_out
+1)
274 strncpy(destp
, s
, n
);
277 if (rule
== ALWAYS_TERMINATE
)
278 strcpy(destp
+n
+remaining
,insert
);
280 } else if (remaining
== 0 && rule
== NEVER_TERMINATE
) {
284 strcpy(destp
+n
, insert
);
288 tor_assert(len_out
== strlen(dest
));
292 /** Return a pointer to a NUL-terminated hexadecimal string encoding
293 * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
294 * result does not need to be deallocated, but repeated calls to
295 * hex_str will trash old results.
298 hex_str(const char *from
, size_t fromlen
)
301 if (fromlen
>(sizeof(buf
)-1)/2)
302 fromlen
= (sizeof(buf
)-1)/2;
303 base16_encode(buf
,sizeof(buf
),from
,fromlen
);
307 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
310 tor_strlower(char *s
)
318 /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
321 tor_strupper(char *s
)
329 /* Compares the first strlen(s2) characters of s1 with s2. Returns as for
333 strcmpstart(const char *s1
, const char *s2
)
335 size_t n
= strlen(s2
);
336 return strncmp(s1
, s2
, n
);
339 /* Compares the first strlen(s2) characters of s1 with s2. Returns as for
343 strcasecmpstart(const char *s1
, const char *s2
)
345 size_t n
= strlen(s2
);
346 return strncasecmp(s1
, s2
, n
);
349 /* Compares the last strlen(s2) characters of s1 with s2. Returns as for
353 strcmpend(const char *s1
, const char *s2
)
355 size_t n1
= strlen(s1
), n2
= strlen(s2
);
357 return strcmp(s1
,s2
);
359 return strncmp(s1
+(n1
-n2
), s2
, n2
);
362 /* Compares the last strlen(s2) characters of s1 with s2. Returns as for
366 strcasecmpend(const char *s1
, const char *s2
)
368 size_t n1
= strlen(s1
), n2
= strlen(s2
);
369 if (n2
>n1
) /* then they can't be the same; figure out which is bigger */
370 return strcasecmp(s1
,s2
);
372 return strncasecmp(s1
+(n1
-n2
), s2
, n2
);
375 /** Return a pointer to the first char of s that is not whitespace and
376 * not a comment, or to the terminating NUL if no such character exists.
379 eat_whitespace(const char *s
)
383 while (TOR_ISSPACE(*s
) || *s
== '#') {
384 while (TOR_ISSPACE(*s
))
386 if (*s
== '#') { /* read to a \n or \0 */
387 while (*s
&& *s
!= '\n')
396 /** Return a pointer to the first char of s that is not a space or a tab,
397 * or to the terminating NUL if no such character exists. */
399 eat_whitespace_no_nl(const char *s
)
401 while (*s
== ' ' || *s
== '\t')
406 /** Return a pointer to the first char of s that is whitespace or <b>#</b>,
407 * or to the terminating NUL if no such character exists.
410 find_whitespace(const char *s
)
414 while (*s
&& !TOR_ISSPACE(*s
) && *s
!= '#')
420 #define CHECK_STRTOX_RESULT() \
421 /* Was at least one character converted? */ \
424 /* Were there unexpected unconverted characters? */ \
425 if (!next && *endptr) \
427 /* Is r within limits? */ \
428 if (r < min || r > max) \
431 if (next) *next = endptr; \
435 if (next) *next = endptr; \
438 /** Extract a long from the start of s, in the given numeric base. If
439 * there is unconverted data and next is provided, set *next to the
440 * first unconverted character. An error has occurred if no characters
441 * are converted; or if there are unconverted characters and next is NULL; or
442 * if the parsed value is not between min and max. When no error occurs,
443 * return the parsed value and set *ok (if provided) to 1. When an error
444 * occurs, return 0 and set *ok (if provided) to 0.
447 tor_parse_long(const char *s
, int base
, long min
, long max
,
448 int *ok
, char **next
)
453 r
= strtol(s
, &endptr
, base
);
454 CHECK_STRTOX_RESULT();
458 tor_parse_ulong(const char *s
, int base
, unsigned long min
,
459 unsigned long max
, int *ok
, char **next
)
464 r
= strtoul(s
, &endptr
, base
);
465 CHECK_STRTOX_RESULT();
468 /** Only base 10 is guaranteed to work for now. */
470 tor_parse_uint64(const char *s
, int base
, uint64_t min
,
471 uint64_t max
, int *ok
, char **next
)
477 r
= (uint64_t)strtoull(s
, &endptr
, base
);
478 #elif defined(MS_WINDOWS)
480 tor_assert(base
<= 10);
481 r
= (uint64_t)_atoi64(s
);
483 while (TOR_ISSPACE(*endptr
)) endptr
++;
484 while (TOR_ISDIGIT(*endptr
)) endptr
++;
486 r
= (uint64_t)_strtoui64(s
, &endptr
, base
);
488 #elif SIZEOF_LONG == 8
489 r
= (uint64_t)strtoul(s
, &endptr
, base
);
491 #error "I don't know how to parse 64-bit numbers."
494 CHECK_STRTOX_RESULT();
498 base16_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
)
503 tor_assert(destlen
>= srclen
*2+1);
504 tor_assert(destlen
< SIZE_T_CEILING
);
509 sprintf(cp
,"%02X",*(const uint8_t*)src
);
516 static const char HEX_DIGITS
[] = "0123456789ABCDEFabcdef";
519 hex_decode_digit(char c
)
523 cp
= strchr(HEX_DIGITS
, c
);
528 return n
; /* digit or uppercase */
530 return n
-6; /* lowercase */
534 base16_decode(char *dest
, size_t destlen
, const char *src
, size_t srclen
)
538 if ((srclen
% 2) != 0)
540 if (destlen
< srclen
/2 || destlen
> SIZE_T_CEILING
)
544 v1
= hex_decode_digit(*src
);
545 v2
= hex_decode_digit(*(src
+1));
548 *(uint8_t*)dest
= (v1
<<4)|v2
;
559 /** Return the number of microseconds elapsed between *start and *end.
562 tv_udiff(struct timeval
*start
, struct timeval
*end
)
565 long secdiff
= end
->tv_sec
- start
->tv_sec
;
567 if (labs(secdiff
+1) > LONG_MAX
/1000000) {
568 log_fn(LOG_WARN
, "comparing times too far apart.");
572 udiff
= secdiff
*1000000L + (end
->tv_usec
- start
->tv_usec
);
576 /** Return -1 if *a \< *b, 0 if *a==*b, and 1 if *a \> *b.
579 tv_cmp(struct timeval
*a
, struct timeval
*b
)
581 if (a
->tv_sec
> b
->tv_sec
)
583 if (a
->tv_sec
< b
->tv_sec
)
585 if (a
->tv_usec
> b
->tv_usec
)
587 if (a
->tv_usec
< b
->tv_usec
)
592 /** Increment *a by the number of seconds and microseconds in *b.
595 tv_add(struct timeval
*a
, struct timeval
*b
)
597 a
->tv_usec
+= b
->tv_usec
;
598 a
->tv_sec
+= b
->tv_sec
+ (a
->tv_usec
/ 1000000);
599 a
->tv_usec
%= 1000000;
602 /** Increment *a by <b>ms</b> milliseconds.
605 tv_addms(struct timeval
*a
, long ms
)
607 a
->tv_usec
+= (ms
* 1000) % 1000000;
608 a
->tv_sec
+= ((ms
* 1000) / 1000000) + (a
->tv_usec
/ 1000000);
609 a
->tv_usec
%= 1000000;
612 #define IS_LEAPYEAR(y) (!(y % 4) && ((y % 100) || !(y % 400)))
614 n_leapdays(int y1
, int y2
)
618 return (y2
/4 - y1
/4) - (y2
/100 - y1
/100) + (y2
/400 - y1
/400);
620 /** Number of days per month in non-leap year; used by tor_timegm. */
621 static const int days_per_month
[] =
622 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
624 /** Return a time_t given a struct tm. The result is given in GMT, and
625 * does not account for leap seconds.
628 tor_timegm(struct tm
*tm
)
630 /* This is a pretty ironclad timegm implementation, snarfed from Python2.2.
631 * It's way more brute-force than fiddling with tzset().
634 unsigned long year
, days
, hours
, minutes
;
636 year
= tm
->tm_year
+ 1900;
637 tor_assert(year
>= 1970);
638 tor_assert(tm
->tm_mon
>= 0);
639 tor_assert(tm
->tm_mon
<= 11);
640 days
= 365 * (year
-1970) + n_leapdays(1970,year
);
641 for (i
= 0; i
< tm
->tm_mon
; ++i
)
642 days
+= days_per_month
[i
];
643 if (tm
->tm_mon
> 1 && IS_LEAPYEAR(year
))
645 days
+= tm
->tm_mday
- 1;
646 hours
= days
*24 + tm
->tm_hour
;
648 minutes
= hours
*60 + tm
->tm_min
;
649 ret
= minutes
*60 + tm
->tm_sec
;
653 /* strftime is locale-specific, so we need to replace those parts */
654 static const char *WEEKDAY_NAMES
[] =
655 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
656 static const char *MONTH_NAMES
[] =
657 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
658 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
661 format_rfc1123_time(char *buf
, time_t t
)
665 tor_gmtime_r(&t
, &tm
);
667 strftime(buf
, RFC1123_TIME_LEN
+1, "___, %d ___ %Y %H:%M:%S GMT", &tm
);
668 tor_assert(tm
.tm_wday
>= 0);
669 tor_assert(tm
.tm_wday
<= 6);
670 memcpy(buf
, WEEKDAY_NAMES
[tm
.tm_wday
], 3);
671 tor_assert(tm
.tm_wday
>= 0);
672 tor_assert(tm
.tm_mon
<= 11);
673 memcpy(buf
+8, MONTH_NAMES
[tm
.tm_mon
], 3);
677 parse_rfc1123_time(const char *buf
, time_t *t
)
684 if (strlen(buf
) != RFC1123_TIME_LEN
)
686 memset(&tm
, 0, sizeof(tm
));
687 if (sscanf(buf
, "%3s, %d %3s %d %d:%d:%d GMT", weekday
,
688 &tm
.tm_mday
, month
, &tm
.tm_year
, &tm
.tm_hour
,
689 &tm
.tm_min
, &tm
.tm_sec
) < 7) {
690 log_fn(LOG_WARN
, "Got invalid RFC1123 time \"%s\"", buf
);
695 for (i
= 0; i
< 12; ++i
) {
696 if (!strcmp(month
, MONTH_NAMES
[i
])) {
702 log_fn(LOG_WARN
, "Got invalid RFC1123 time \"%s\"", buf
);
708 *t
= tor_timegm(&tm
);
713 format_local_iso_time(char *buf
, time_t t
)
716 strftime(buf
, ISO_TIME_LEN
+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t
, &tm
));
720 format_iso_time(char *buf
, time_t t
)
723 strftime(buf
, ISO_TIME_LEN
+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t
, &tm
));
727 parse_iso_time(const char *cp
, time_t *t
)
731 if (!strptime(cp
, "%Y-%m-%d %H:%M:%S", &st_tm
)) {
732 log_fn(LOG_WARN
, "Published time was unparseable"); return -1;
735 unsigned int year
=0, month
=0, day
=0, hour
=100, minute
=100, second
=100;
736 if (sscanf(cp
, "%u-%u-%u %u:%u:%u", &year
, &month
,
737 &day
, &hour
, &minute
, &second
) < 6) {
738 log_fn(LOG_WARN
, "Published time was unparseable"); return -1;
740 if (year
< 1970 || month
< 1 || month
> 12 || day
< 1 || day
> 31 ||
741 hour
> 23 || minute
> 59 || second
> 61) {
742 log_fn(LOG_WARN
, "Published time was nonsensical"); return -1;
744 st_tm
.tm_year
= year
-1900;
745 st_tm
.tm_mon
= month
-1;
747 st_tm
.tm_hour
= hour
;
748 st_tm
.tm_min
= minute
;
749 st_tm
.tm_sec
= second
;
751 *t
= tor_timegm(&st_tm
);
759 /** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. <b>isSocket</b>
760 * must be 1 if fd was returned by socket() or accept(), and 0 if fd
761 * was returned by open(). Return the number of bytes written, or -1
762 * on error. Only use if fd is a blocking fd. */
764 write_all(int fd
, const char *buf
, size_t count
, int isSocket
)
769 while (written
!= count
) {
771 result
= send(fd
, buf
+written
, count
-written
, 0);
773 result
= write(fd
, buf
+written
, count
-written
);
781 /** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
782 * or reach the end of the file. <b>isSocket</b> must be 1 if fd
783 * was returned by socket() or accept(), and 0 if fd was returned by
784 * open(). Return the number of bytes read, or -1 on error. Only use
785 * if fd is a blocking fd. */
787 read_all(int fd
, char *buf
, size_t count
, int isSocket
)
792 if (count
> SIZE_T_CEILING
)
795 while (numread
!= count
) {
797 result
= recv(fd
, buf
+numread
, count
-numread
, 0);
799 result
= read(fd
, buf
+numread
, count
-numread
);
802 else if (result
== 0)
810 * Filesystem operations.
813 /** Clean up <b>name</b> so that we can use it in a call to "stat". On Unix,
814 * we do nothing. On Windows, we remove a trailing slash, unless the path is
815 * the root of a disk. */
817 clean_name_for_stat(char *name
)
820 size_t len
= strlen(name
);
823 if (name
[len
-1]=='\\' || name
[len
-1]=='/') {
824 if (len
== 1 || (len
==3 && name
[1]==':'))
831 /** Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
832 * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
835 file_status(const char *fname
)
840 f
= tor_strdup(fname
);
841 clean_name_for_stat(f
);
845 if (errno
== ENOENT
) {
850 if (st
.st_mode
& S_IFDIR
)
852 else if (st
.st_mode
& S_IFREG
)
858 /** Check whether dirname exists and is private. If yes return 0. If
859 * it does not exist, and check==CPD_CREATE is set, try to create it
860 * and return 0 on success. If it does not exist, and
861 * check==CPD_CHECK, and we think we can create it, return 0. Else
864 check_private_dir(const char *dirname
, cpd_check_t check
)
870 f
= tor_strdup(dirname
);
871 clean_name_for_stat(f
);
875 if (errno
!= ENOENT
) {
876 log(LOG_WARN
, "Directory %s cannot be read: %s", dirname
,
880 if (check
== CPD_NONE
) {
881 log(LOG_WARN
, "Directory %s does not exist.", dirname
);
883 } else if (check
== CPD_CREATE
) {
884 log(LOG_INFO
, "Creating directory %s", dirname
);
888 r
= mkdir(dirname
, 0700);
891 log(LOG_WARN
, "Error creating directory %s: %s", dirname
,
896 /* XXXX In the case where check==CPD_CHECK, we should look at the
897 * parent directory a little harder. */
900 if (!(st
.st_mode
& S_IFDIR
)) {
901 log(LOG_WARN
, "%s is not a directory", dirname
);
905 if (st
.st_uid
!= getuid()) {
906 log(LOG_WARN
, "%s is not owned by this UID (%d). You must fix this to proceed.", dirname
, (int)getuid());
909 if (st
.st_mode
& 0077) {
910 log(LOG_WARN
, "Fixing permissions on directory %s", dirname
);
911 if (chmod(dirname
, 0700)) {
912 log(LOG_WARN
, "Could not chmod directory %s: %s", dirname
,
923 /** Create a file named <b>fname</b> with the contents <b>str</b>. Overwrite the
924 * previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
926 * This function replaces the old file atomically, if possible.
929 write_str_to_file(const char *fname
, const char *str
, int bin
)
932 if (!bin
&& strchr(str
, '\r')) {
934 "How odd. Writing a string that does contain CR already.");
937 return write_bytes_to_file(fname
, str
, strlen(str
), bin
);
942 write_chunks_to_file_impl(const char *fname
, const smartlist_t
*chunks
,
949 tempname_len
= strlen(fname
)+16;
950 tor_assert(tempname_len
> strlen(fname
)); /*check for overflow*/
951 tempname
= tor_malloc(tempname_len
);
952 if (open_flags
& O_APPEND
) {
953 strlcpy(tempname
, fname
, tempname_len
);
955 if (tor_snprintf(tempname
, tempname_len
, "%s.tmp", fname
)<0) {
956 log(LOG_WARN
, "Failed to generate filename");
960 if ((fd
= open(tempname
, open_flags
, 0600))
962 log(LOG_WARN
, "Couldn't open \"%s\" for writing: %s", tempname
,
966 SMARTLIST_FOREACH(chunks
, sized_chunk_t
*, chunk
,
968 result
= write_all(fd
, chunk
->bytes
, chunk
->len
, 0);
969 if (result
< 0 || (size_t)result
!= chunk
->len
) {
970 log(LOG_WARN
, "Error writing to \"%s\": %s", tempname
, strerror(errno
));
976 log(LOG_WARN
,"Error flushing to \"%s\": %s", tempname
, strerror(errno
));
979 if (!(open_flags
& O_APPEND
)) {
980 if (replace_file(tempname
, fname
)) {
981 log(LOG_WARN
, "Error replacing \"%s\": %s", fname
, strerror(errno
));
994 write_chunks_to_file(const char *fname
, const smartlist_t
*chunks
, int bin
)
996 int flags
= O_WRONLY
|O_CREAT
|O_TRUNC
|(bin
?O_BINARY
:O_TEXT
);
997 return write_chunks_to_file_impl(fname
, chunks
, flags
);
1000 /** As write_str_to_file, but does not assume a NUL-terminated *
1001 * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
1003 write_bytes_to_file(const char *fname
, const char *str
, size_t len
,
1006 int flags
= O_WRONLY
|O_CREAT
|O_TRUNC
|(bin
?O_BINARY
:O_TEXT
);
1008 sized_chunk_t c
= { str
, len
};
1009 smartlist_t
*chunks
= smartlist_create();
1010 smartlist_add(chunks
, &c
);
1011 r
= write_chunks_to_file_impl(fname
, chunks
, flags
);
1012 smartlist_free(chunks
);
1018 append_bytes_to_file(const char *fname
, const char *str
, size_t len
,
1021 int flags
= O_WRONLY
|O_CREAT
|O_APPEND
|(bin
?O_BINARY
:O_TEXT
);
1023 sized_chunk_t c
= { str
, len
};
1024 smartlist_t
*chunks
= smartlist_create();
1025 smartlist_add(chunks
, &c
);
1026 r
= write_chunks_to_file_impl(fname
, chunks
, flags
);
1027 smartlist_free(chunks
);
1031 /** Read the contents of <b>filename</b> into a newly allocated
1032 * string; return the string on success or NULL on failure.
1035 * This function <em>may</em> return an erroneous result if the file
1036 * is modified while it is running, but must not crash or overflow.
1037 * Right now, the error case occurs when the file length grows between
1038 * the call to stat and the call to read_all: the resulting string will
1042 read_file_to_str(const char *filename
, int bin
)
1044 int fd
; /* router file */
1045 struct stat statbuf
;
1049 tor_assert(filename
);
1051 f
= tor_strdup(filename
);
1052 clean_name_for_stat(f
);
1053 r
= stat(f
, &statbuf
);
1056 log_fn(LOG_INFO
,"Could not stat \"%s\".",filename
);
1060 fd
= open(filename
,O_RDONLY
|(bin
?O_BINARY
:O_TEXT
),0);
1062 log_fn(LOG_WARN
,"Could not open \"%s\".",filename
);
1066 string
= tor_malloc(statbuf
.st_size
+1);
1068 r
= read_all(fd
,string
,statbuf
.st_size
,0);
1070 log_fn(LOG_WARN
,"Error reading from file \"%s\": %s", filename
,
1076 string
[r
] = '\0'; /* NUL-terminate the result. */
1078 if (bin
&& r
!= statbuf
.st_size
) {
1079 /* If we're in binary mode, then we'd better have an exact match for
1080 * size. Otherwise, win32 encoding may throw us off, and that's okay. */
1081 log_fn(LOG_WARN
,"Could read only %d of %ld bytes of file \"%s\".",
1082 r
, (long)statbuf
.st_size
,filename
);
1088 if (!bin
&& strchr(string
, '\r')) {
1089 log_fn(LOG_DEBUG
, "We didn't convert CRLF to LF as well as we hoped when reading %s. Coping.",
1091 tor_strstrip(string
, "\r");
1099 /** Given a string containing part of a configuration file or similar format,
1100 * advance past comments and whitespace and try to parse a single line. If we
1101 * parse a line successfully, set *<b>key_out</b> to the key portion and
1102 * *<b>value_out</b> to the value portion of the line, and return a pointer to
1103 * the start of the next line. If we run out of data, return a pointer to the
1104 * end of the string. If we encounter an error, return NULL.
1106 * NOTE: We modify <b>line</b> as we parse it, by inserting NULs to terminate
1107 * the key and value.
1110 parse_line_from_str(char *line
, char **key_out
, char **value_out
)
1112 char *key
, *val
, *cp
;
1114 tor_assert(key_out
);
1115 tor_assert(value_out
);
1117 *key_out
= *value_out
= key
= val
= NULL
;
1118 /* Skip until the first keyword. */
1120 while (TOR_ISSPACE(*line
))
1123 while (*line
&& *line
!= '\n')
1130 if (!*line
) { /* End of string? */
1131 *key_out
= *value_out
= NULL
;
1135 /* Skip until the next space. */
1137 while (*line
&& !TOR_ISSPACE(*line
) && *line
!= '#')
1140 /* Skip until the value */
1141 while (*line
== ' ' || *line
== '\t')
1145 /* Find the end of the line. */
1146 while (*line
&& *line
!= '\n' && *line
!= '#')
1153 while (cp
>=val
&& TOR_ISSPACE(*cp
))
1159 } while (*line
&& *line
!= '\n');
1170 /** Expand any homedir prefix on 'filename'; return a newly allocated
1173 expand_filename(const char *filename
)
1175 tor_assert(filename
);
1176 if (*filename
== '~') {
1178 char *home
, *result
;
1181 if (filename
[1] == '/' || filename
[1] == '\0') {
1182 home
= getenv("HOME");
1184 log_fn(LOG_WARN
, "Couldn't find $HOME environment variable while expanding %s", filename
);
1187 home
= tor_strdup(home
);
1188 rest
= strlen(filename
)>=2?(filename
+2):NULL
;
1191 char *username
, *slash
;
1192 slash
= strchr(filename
, '/');
1194 username
= tor_strndup(filename
+1,slash
-filename
-1);
1196 username
= tor_strdup(filename
+1);
1197 if (!(home
= get_user_homedir(username
))) {
1198 log_fn(LOG_WARN
,"Couldn't get homedir for \"%s\"",username
);
1203 rest
= slash
? (slash
+1) : NULL
;
1205 log_fn(LOG_WARN
, "Couldn't expend homedir on system without pwd.h");
1206 return tor_strdup(filename
);
1210 /* Remove trailing slash. */
1211 if (strlen(home
)>1 && !strcmpend(home
,"/")) {
1212 home
[strlen(home
)-1] = '\0';
1214 /* Plus one for /, plus one for NUL.
1215 * Round up to 16 in case we can't do math. */
1216 len
= strlen(home
)+strlen(rest
)+16;
1217 result
= tor_malloc(len
);
1218 tor_snprintf(result
,len
,"%s/%s",home
,rest
?rest
:"");
1222 return tor_strdup(filename
);
1226 /** Return a new list containing the filenames in the directory <b>dirname</b>.
1227 * Return NULL on error or if <b>dirname</b> is not a directory.
1230 tor_listdir(const char *dirname
)
1232 smartlist_t
*result
;
1236 WIN32_FIND_DATA findData
;
1237 size_t pattern_len
= strlen(dirname
)+16;
1238 pattern
= tor_malloc(pattern_len
);
1239 tor_snprintf(pattern
, pattern_len
, "%s\\*", dirname
);
1240 if (!(handle
= FindFirstFile(pattern
, &findData
))) {
1244 result
= smartlist_create();
1246 smartlist_add(result
, tor_strdup(findData
.cFileName
));
1247 if (!FindNextFile(handle
, &findData
)) {
1248 if (GetLastError() != ERROR_NO_MORE_FILES
) {
1249 log_fn(LOG_WARN
, "Error reading directory.");
1259 if (!(d
= opendir(dirname
)))
1262 result
= smartlist_create();
1263 while ((de
= readdir(d
))) {
1264 if (!strcmp(de
->d_name
, ".") ||
1265 !strcmp(de
->d_name
, ".."))
1267 smartlist_add(result
, tor_strdup(de
->d_name
));
1278 /** Return true iff <b>ip</b> (in host order) is an IP reserved to localhost,
1279 * or reserved for local networks by RFC 1918.
1282 is_internal_IP(uint32_t ip
)
1284 if (((ip
& 0xff000000) == 0x0a000000) || /* 10/8 */
1285 ((ip
& 0xff000000) == 0x00000000) || /* 0/8 */
1286 ((ip
& 0xff000000) == 0x7f000000) || /* 127/8 */
1287 ((ip
& 0xffff0000) == 0xa9fe0000) || /* 169.254/16 */
1288 ((ip
& 0xfff00000) == 0xac100000) || /* 172.16/12 */
1289 ((ip
& 0xffff0000) == 0xc0a80000)) /* 192.168/16 */
1294 /** Return true iff <b>ip</b> (in host order) is judged to be on the
1295 * same network as us. For now, check if it's an internal IP.
1297 * XXX Also check if it's on the same class C network as our public IP.
1300 is_local_IP(uint32_t ip
)
1302 return is_internal_IP(ip
);
1305 /** Parse a string of the form "host[:port]" from <b>addrport</b>. If
1306 * <b>address</b> is provided, set *<b>address</b> to a copy of the
1307 * host portion of the string. If <b>addr</b> is provided, try to
1308 * resolve the host portion of the string and store it into
1309 * *<b>addr</b> (in host byte order). If <b>port_out</b> is provided,
1310 * store the port number into *<b>port_out</b>, or 0 if no port is given.
1311 * If <b>port_out</b> is NULL, then there must be no port number in
1313 * Return 0 on success, -1 on failure.
1316 parse_addr_port(const char *addrport
, char **address
, uint32_t *addr
,
1320 char *_address
= NULL
;
1324 tor_assert(addrport
);
1326 colon
= strchr(addrport
, ':');
1328 _address
= tor_strndup(addrport
, colon
-addrport
);
1329 _port
= (int) tor_parse_long(colon
+1,10,1,65535,NULL
,NULL
);
1331 log_fn(LOG_WARN
, "Port '%s' out of range", colon
+1);
1335 log_fn(LOG_WARN
, "Port '%s' given on '%s' when not required", colon
+1,
1340 _address
= tor_strdup(addrport
);
1345 /* There's an addr pointer, so we need to resolve the hostname. */
1346 if (tor_lookup_hostname(_address
,addr
)) {
1347 log_fn(LOG_WARN
, "Couldn't look up '%s'", _address
);
1351 *addr
= ntohl(*addr
);
1354 if (address
&& ok
) {
1355 *address
= _address
;
1362 *port_out
= ok
? ((uint16_t) _port
) : 0;
1367 /** Parse a string <b>s</b> in the format of
1368 * (IP(/mask|/mask-bits)?|*):(*|port(-maxport)?), setting the various
1369 * *out pointers as appropriate. Return 0 on success, -1 on failure.
1372 parse_addr_and_port_range(const char *s
, uint32_t *addr_out
,
1373 uint32_t *mask_out
, uint16_t *port_min_out
,
1374 uint16_t *port_max_out
)
1377 char *mask
, *port
, *endptr
;
1382 tor_assert(addr_out
);
1383 tor_assert(mask_out
);
1384 tor_assert(port_min_out
);
1385 tor_assert(port_max_out
);
1387 address
= tor_strdup(s
);
1388 /* Break 'address' into separate strings.
1390 mask
= strchr(address
,'/');
1391 port
= strchr(mask
?mask
:address
,':');
1396 /* Now "address" is the IP|'*' part...
1397 * "mask" is the Mask|Maskbits part...
1398 * and "port" is the *|port|min-max part.
1401 if (strcmp(address
,"*")==0) {
1403 } else if (tor_inet_aton(address
, &in
) != 0) {
1404 *addr_out
= ntohl(in
.s_addr
);
1406 log_fn(LOG_WARN
, "Malformed IP \"%s\" in address pattern; rejecting.",address
);
1411 if (strcmp(address
,"*")==0)
1414 *mask_out
= 0xFFFFFFFFu
;
1417 bits
= (int) strtol(mask
, &endptr
, 10);
1419 /* strtol handled the whole mask. */
1420 if (bits
< 0 || bits
> 32) {
1421 log_fn(LOG_WARN
, "Bad number of mask bits on address range; rejecting.");
1424 *mask_out
= ~((1<<(32-bits
))-1);
1425 } else if (tor_inet_aton(mask
, &in
) != 0) {
1426 *mask_out
= ntohl(in
.s_addr
);
1428 log_fn(LOG_WARN
, "Malformed mask \"%s\" on address range; rejecting.",
1434 if (!port
|| strcmp(port
, "*") == 0) {
1436 *port_max_out
= 65535;
1439 *port_min_out
= (uint16_t) tor_parse_long(port
, 10, 1, 65535,
1441 if (*endptr
== '-') {
1444 *port_max_out
= (uint16_t) tor_parse_long(port
, 10, 1, 65535, NULL
,
1446 if (*endptr
|| !*port_max_out
) {
1447 log_fn(LOG_WARN
, "Malformed port \"%s\" on address range rejecting.",
1450 } else if (*endptr
|| !*port_min_out
) {
1451 log_fn(LOG_WARN
, "Malformed port \"%s\" on address range; rejecting.",
1455 *port_max_out
= *port_min_out
;
1457 if (*port_min_out
> *port_max_out
) {
1458 log_fn(LOG_WARN
,"Insane port range on address policy; rejecting.");
1470 /** Given an IPv4 address <b>in</b> (in network order, as usual),
1471 * write it as a string into the <b>buf_len</b>-byte buffer in
1475 tor_inet_ntoa(struct in_addr
*in
, char *buf
, size_t buf_len
)
1477 uint32_t a
= ntohl(in
->s_addr
);
1478 return tor_snprintf(buf
, buf_len
, "%d.%d.%d.%d",
1479 (int)(uint8_t)((a
>>24)&0xff),
1480 (int)(uint8_t)((a
>>16)&0xff),
1481 (int)(uint8_t)((a
>>8 )&0xff),
1482 (int)(uint8_t)((a
)&0xff));
1485 /** Given a host-order <b>addr</b>, call tor_inet_ntoa() on it
1486 * and return a strdup of the resulting address.
1489 tor_dup_addr(uint32_t addr
)
1491 char buf
[INET_NTOA_BUF_LEN
];
1494 in
.s_addr
= htonl(addr
);
1495 tor_inet_ntoa(&in
, buf
, sizeof(buf
));
1496 return tor_strdup(buf
);
1499 /* Return true iff <b>name</b> looks like it might be a hostname or IP
1500 * address of some kind. */
1502 is_plausible_address(const char *name
)
1506 /* We could check better here. */
1507 for (cp
=name
; *cp
; cp
++) {
1508 if (*cp
!= '.' && *cp
!= '-' && !TOR_ISALNUM(*cp
))
1516 * Set *<b>addr</b> to the host-order IPv4 address (if any) of whatever
1517 * interface connects to the internet. This address should only be used in
1518 * checking whether our address has changed. Return 0 on success, -1 on
1522 get_interface_address(uint32_t *addr
)
1525 struct sockaddr_in target_addr
, my_addr
;
1526 socklen_t my_addr_len
= sizeof(my_addr
);
1531 sock
= socket(PF_INET
,SOCK_DGRAM
,IPPROTO_UDP
);
1533 int e
= tor_socket_errno(-1);
1534 log_fn(LOG_WARN
, "unable to create socket: %s", tor_socket_strerror(e
));
1538 memset(&target_addr
, 0, sizeof(target_addr
));
1539 target_addr
.sin_family
= AF_INET
;
1541 target_addr
.sin_port
= 9;
1542 /* 18.0.0.1 (Don't worry: no packets are sent. We just need a real address
1543 * on the internet.) */
1544 target_addr
.sin_addr
.s_addr
= htonl(0x12000001);
1546 if (connect(sock
,(struct sockaddr
*)&target_addr
,sizeof(target_addr
))<0) {
1547 int e
= tor_socket_errno(sock
);
1548 log_fn(LOG_WARN
, "connnect() failed: %s", tor_socket_strerror(e
));
1552 /* XXXX Can this be right on IPv6 clients? */
1553 if (getsockname(sock
, (struct sockaddr
*)&my_addr
, &my_addr_len
)) {
1554 int e
= tor_socket_errno(sock
);
1555 log_fn(LOG_WARN
, "getsockname() failed: %s", tor_socket_strerror(e
));
1559 *addr
= ntohl(my_addr
.sin_addr
.s_addr
);
1564 tor_close_socket(sock
);
1573 /* Based on code contributed by christian grothoff */
1574 static int start_daemon_called
= 0;
1575 static int finish_daemon_called
= 0;
1576 static int daemon_filedes
[2];
1577 /** Start putting the process into daemon mode: fork and drop all resources
1578 * except standard fds. The parent process never returns, but stays around
1579 * until finish_daemon is called. (Note: it's safe to call this more
1580 * than once: calls after the first are ignored.)
1587 if (start_daemon_called
)
1589 start_daemon_called
= 1;
1591 pipe(daemon_filedes
);
1594 log_fn(LOG_ERR
,"fork failed. Exiting.");
1597 if (pid
) { /* Parent */
1601 close(daemon_filedes
[1]); /* we only read */
1603 while (0 < read(daemon_filedes
[0], &c
, sizeof(char))) {
1611 exit(1); /* child reported error */
1612 } else { /* Child */
1613 close(daemon_filedes
[0]); /* we only write */
1615 pid
= setsid(); /* Detach from controlling terminal */
1617 * Fork one more time, so the parent (the session group leader) can exit.
1618 * This means that we, as a non-session group leader, can never regain a
1619 * controlling terminal. This part is recommended by Stevens's
1620 * _Advanced Programming in the Unix Environment_.
1629 /** Finish putting the process into daemon mode: drop standard fds, and tell
1630 * the parent process to exit. (Note: it's safe to call this more than once:
1631 * calls after the first are ignored. Calls start_daemon first if it hasn't
1632 * been called already.)
1635 finish_daemon(const char *desired_cwd
)
1639 if (finish_daemon_called
)
1641 if (!start_daemon_called
)
1643 finish_daemon_called
= 1;
1647 /* Don't hold the wrong FS mounted */
1648 if (chdir(desired_cwd
) < 0) {
1649 log_fn(LOG_ERR
,"chdir to \"%s\" failed. Exiting.",desired_cwd
);
1653 nullfd
= open("/dev/null",
1654 O_CREAT
| O_RDWR
| O_APPEND
);
1656 log_fn(LOG_ERR
,"/dev/null can't be opened. Exiting.");
1659 /* close fds linking to invoking terminal, but
1660 * close usual incoming fds, but redirect them somewhere
1661 * useful so the fds don't get reallocated elsewhere.
1663 if (dup2(nullfd
,0) < 0 ||
1664 dup2(nullfd
,1) < 0 ||
1665 dup2(nullfd
,2) < 0) {
1666 log_fn(LOG_ERR
,"dup2 failed. Exiting.");
1671 write(daemon_filedes
[1], &c
, sizeof(char)); /* signal success */
1672 close(daemon_filedes
[1]);
1675 /* defined(MS_WINDOWS) */
1681 finish_daemon(const char *cp
)
1686 /** Write the current process ID, followed by NL, into <b>filename</b>.
1689 write_pidfile(char *filename
)
1694 if ((pidfile
= fopen(filename
, "w")) == NULL
) {
1695 log_fn(LOG_WARN
, "Unable to open \"%s\" for writing: %s", filename
,
1698 fprintf(pidfile
, "%d\n", (int)getpid());