|
@@ -2,10 +2,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
- * util.c: Common functions for strings, IO, network, data structures,
|
|
|
+
|
|
|
+ * \file util.c
|
|
|
+ *
|
|
|
+ * \brief Common functions for strings, IO, network, data structures,
|
|
|
* process control, and cross-platform portability.
|
|
|
- *****/
|
|
|
+ **/
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
|
@@ -99,11 +101,7 @@
|
|
|
#include "strlcat.c"
|
|
|
#endif
|
|
|
|
|
|
-
|
|
|
- * Memory wrappers
|
|
|
- *****/
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
* result. On error, log and terminate the process. (Same as malloc(size),
|
|
|
* but never returns NULL.)
|
|
|
*/
|
|
@@ -124,7 +122,7 @@ void *tor_malloc(size_t size) {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* zero bytes, and return a pointer to the result. Log and terminate
|
|
|
* the process on error. (Same as calloc(size,1), but never returns NULL.)
|
|
|
*/
|
|
@@ -134,7 +132,7 @@ void *tor_malloc_zero(size_t size) {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* bytes long; return the new memory block. On error, log and
|
|
|
* terminate. (Like realloc(ptr,size), but never returns NULL.)
|
|
|
*/
|
|
@@ -149,7 +147,7 @@ void *tor_realloc(void *ptr, size_t size) {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* error, log and terminate. (Like strdup(s), but never returns
|
|
|
* NULL.)
|
|
|
*/
|
|
@@ -165,10 +163,11 @@ char *tor_strdup(const char *s) {
|
|
|
return dup;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * characters of 's'. If 's' is longer than 'n' characters, only the
|
|
|
- * first 'n' are copied. The result is always NUL-terminated. (Like
|
|
|
- * strndup(s,n), but never returns NULL.)
|
|
|
+
|
|
|
+ * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
|
|
|
+ * characters, only the first <b>n</b> are copied. The result is
|
|
|
+ * always NUL-terminated. (Like strndup(s,n), but never returns
|
|
|
+ * NULL.)
|
|
|
*/
|
|
|
char *tor_strndup(const char *s, size_t n) {
|
|
|
char *dup;
|
|
@@ -179,41 +178,50 @@ char *tor_strndup(const char *s, size_t n) {
|
|
|
return dup;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * lowercase. */
|
|
|
-void tor_strlower(char *s)
|
|
|
-{
|
|
|
- while (*s) {
|
|
|
- *s = tolower(*s);
|
|
|
- ++s;
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
#ifndef UNALIGNED_INT_ACCESS_OK
|
|
|
+
|
|
|
+ * Read a 16-bit value beginning at <b>cp</b>. Equaivalent to
|
|
|
+ * *(uint16_t*)(cp), but will not cause segfaults on platforms that forbid
|
|
|
+ * unaligned memory access.
|
|
|
+ */
|
|
|
uint16_t get_uint16(const char *cp)
|
|
|
{
|
|
|
uint16_t v;
|
|
|
memcpy(&v,cp,2);
|
|
|
return v;
|
|
|
}
|
|
|
+
|
|
|
+ * Read a 32-bit value beginning at <b>cp</b>. Equaivalent to
|
|
|
+ * *(uint32_t*)(cp), but will not cause segfaults on platforms that forbid
|
|
|
+ * unaligned memory access.
|
|
|
+ */
|
|
|
uint32_t get_uint32(const char *cp)
|
|
|
{
|
|
|
uint32_t v;
|
|
|
memcpy(&v,cp,4);
|
|
|
return v;
|
|
|
}
|
|
|
+
|
|
|
+ * Set a 16-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
|
|
|
+ * *(uint16_t)(cp) = v, but will not cause segfaults on platforms that forbid
|
|
|
+ * unaligned memory access. */
|
|
|
void set_uint16(char *cp, uint16_t v)
|
|
|
{
|
|
|
memcpy(cp,&v,2);
|
|
|
}
|
|
|
+
|
|
|
+ * Set a 32-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
|
|
|
+ * *(uint32_t)(cp) = v, but will not cause segfaults on platforms that forbid
|
|
|
+ * unaligned memory access. */
|
|
|
void set_uint32(char *cp, uint32_t v)
|
|
|
{
|
|
|
memcpy(cp,&v,4);
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
-
|
|
|
- * write the result as a NUL-terminated string to 'to'. 'to' must
|
|
|
+
|
|
|
+ * write the result as a NUL-terminated string to <b>to</b>. <b>to</b> must
|
|
|
* have at least (2*fromlen)+1 bytes of free space.
|
|
|
*/
|
|
|
void hex_encode(const char *from, int fromlen, char *to)
|
|
@@ -229,8 +237,8 @@ void hex_encode(const char *from, int fromlen, char *to)
|
|
|
*to = '\0';
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * the first 'fromlen' bytes of 'from'. (fromlen must be <= 32.) The
|
|
|
+
|
|
|
+ * the first <b>fromlen</b> bytes of <b>from</b>. (fromlen must be \<= 32.) The
|
|
|
* result does not need to be deallocated, but repeated calls to
|
|
|
* hex_str will trash old results.
|
|
|
*/
|
|
@@ -253,8 +261,8 @@ const char *hex_str(const char *from, int fromlen)
|
|
|
|
|
|
|
|
|
struct smartlist_t {
|
|
|
-
|
|
|
- * before it needs to be resized. Only the first 'num_used' (<=
|
|
|
+
|
|
|
+ * before it needs to be resized. Only the first <b>num_used</b> (\<=
|
|
|
* capacity) elements point to valid data.
|
|
|
*/
|
|
|
void **list;
|
|
@@ -262,7 +270,7 @@ struct smartlist_t {
|
|
|
int capacity;
|
|
|
};
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
smartlist_t *smartlist_create() {
|
|
|
smartlist_t *sl = tor_malloc(sizeof(smartlist_t));
|
|
@@ -272,7 +280,7 @@ smartlist_t *smartlist_create() {
|
|
|
return sl;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* list's elements.
|
|
|
*/
|
|
|
void smartlist_free(smartlist_t *sl) {
|
|
@@ -280,9 +288,9 @@ void smartlist_free(smartlist_t *sl) {
|
|
|
free(sl);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * the list up to 'n' elements with no further reallocation or wasted
|
|
|
- * space. If 'n' is less than or equal to the number of elements
|
|
|
+
|
|
|
+ * the list up to <b>n</b> elements with no further reallocation or wasted
|
|
|
+ * space. If <b>n</b> is less than or equal to the number of elements
|
|
|
* currently in the list, reduce the list's capacity as much as
|
|
|
* possible without losing elements.
|
|
|
*/
|
|
@@ -295,13 +303,13 @@ void smartlist_set_capacity(smartlist_t *sl, int n) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
void smartlist_clear(smartlist_t *sl) {
|
|
|
sl->num_used = 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* current size). Remove the last smartlist_len(sl)-len elements from the
|
|
|
* list.
|
|
|
*/
|
|
@@ -311,7 +319,7 @@ void smartlist_truncate(smartlist_t *sl, int len)
|
|
|
sl->num_used = len;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
void smartlist_add(smartlist_t *sl, void *element) {
|
|
|
if (sl->num_used >= sl->capacity) {
|
|
|
sl->capacity *= 2;
|
|
@@ -320,13 +328,13 @@ void smartlist_add(smartlist_t *sl, void *element) {
|
|
|
sl->list[sl->num_used++] = element;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2)
|
|
|
{
|
|
|
SMARTLIST_FOREACH(s2, void *, element, smartlist_add(sl, element));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* the order of s1.
|
|
|
*/
|
|
|
void smartlist_remove(smartlist_t *sl, void *element) {
|
|
@@ -340,7 +348,7 @@ void smartlist_remove(smartlist_t *sl, void *element) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
int smartlist_isin(const smartlist_t *sl, void *element) {
|
|
|
int i;
|
|
@@ -350,7 +358,7 @@ int smartlist_isin(const smartlist_t *sl, void *element) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2) {
|
|
|
int i;
|
|
@@ -360,7 +368,7 @@ int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* Does not preserve the order of sl1.
|
|
|
*/
|
|
|
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2) {
|
|
@@ -372,7 +380,7 @@ void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* Does not preserve the order of sl1.
|
|
|
*/
|
|
|
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2) {
|
|
@@ -381,7 +389,7 @@ void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2) {
|
|
|
smartlist_remove(sl1, sl2->list[i]);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
void *smartlist_choose(const smartlist_t *sl) {
|
|
|
if(sl->num_used)
|
|
@@ -389,15 +397,15 @@ void *smartlist_choose(const smartlist_t *sl) {
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
void *smartlist_get(const smartlist_t *sl, int idx)
|
|
|
{
|
|
|
tor_assert(sl && idx>=0 && idx < sl->num_used);
|
|
|
return sl->list[idx];
|
|
|
}
|
|
|
-
|
|
|
- * value of the 'idx'th element.
|
|
|
+
|
|
|
+ * value of the <b>idx</b>th element.
|
|
|
*/
|
|
|
void *smartlist_set(smartlist_t *sl, int idx, void *val)
|
|
|
{
|
|
@@ -407,9 +415,9 @@ void *smartlist_set(smartlist_t *sl, int idx, void *val)
|
|
|
sl->list[idx] = val;
|
|
|
return old;
|
|
|
}
|
|
|
-
|
|
|
- * swap the last element of sl into the 'idx'th space. Return the old value
|
|
|
- * of the 'idx'th element.
|
|
|
+
|
|
|
+ * element, swap the last element of sl into the <b>idx</b>th space.
|
|
|
+ * Return the old value of the <b>idx</b>th element.
|
|
|
*/
|
|
|
void *smartlist_del(smartlist_t *sl, int idx)
|
|
|
{
|
|
@@ -419,9 +427,9 @@ void *smartlist_del(smartlist_t *sl, int idx)
|
|
|
sl->list[idx] = sl->list[--sl->num_used];
|
|
|
return old;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
* moving all subsequent elements back one space. Return the old value
|
|
|
- * of the 'idx'th element.
|
|
|
+ * of the <b>idx</b>th element.
|
|
|
*/
|
|
|
void *smartlist_del_keeporder(smartlist_t *sl, int idx)
|
|
|
{
|
|
@@ -433,14 +441,15 @@ void *smartlist_del_keeporder(smartlist_t *sl, int idx)
|
|
|
memmove(sl->list+idx, sl->list+idx+1, sizeof(void*)*(sl->num_used-idx));
|
|
|
return old;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
int smartlist_len(const smartlist_t *sl)
|
|
|
{
|
|
|
return sl->num_used;
|
|
|
}
|
|
|
-
|
|
|
- * items previously at 'idx' or later forward one space.
|
|
|
+
|
|
|
+ * <b>sl</b>, moving all items previously at <b>idx</b> or later
|
|
|
+ * forward one space.
|
|
|
*/
|
|
|
void smartlist_insert(smartlist_t *sl, int idx, void *val)
|
|
|
{
|
|
@@ -462,9 +471,8 @@ void smartlist_insert(smartlist_t *sl, int idx, void *val)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * Splay-tree implementation of string-to-void* map
|
|
|
- *****/
|
|
|
+
|
|
|
+ */
|
|
|
struct strmap_entry_t {
|
|
|
SPLAY_ENTRY(strmap_entry_t) node;
|
|
|
char *key;
|
|
@@ -484,7 +492,7 @@ static int compare_strmap_entries(struct strmap_entry_t *a,
|
|
|
SPLAY_PROTOTYPE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
|
|
|
SPLAY_GENERATE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
strmap_t* strmap_new(void)
|
|
|
{
|
|
@@ -494,10 +502,10 @@ strmap_t* strmap_new(void)
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * value for <key> if one was set, or NULL if one was not.
|
|
|
+
|
|
|
+ * value for <b>key</b> if one was set, or NULL if one was not.
|
|
|
*
|
|
|
- * This function makes a copy of 'key' if necessary, but not of 'val'.
|
|
|
+ * This function makes a copy of <b>key</b> if necessary, but not of <b>val</b>.
|
|
|
*/
|
|
|
void* strmap_set(strmap_t *map, const char *key, void *val)
|
|
|
{
|
|
@@ -520,7 +528,7 @@ void* strmap_set(strmap_t *map, const char *key, void *val)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* value is set.
|
|
|
*/
|
|
|
void* strmap_get(strmap_t *map, const char *key)
|
|
@@ -537,9 +545,9 @@ void* strmap_get(strmap_t *map, const char *key)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* Return the value if one was set, or NULL if there was no entry for
|
|
|
- * <key>.
|
|
|
+ * <b>key</b>.
|
|
|
*
|
|
|
* Note: you must free any storage associated with the returned value.
|
|
|
*/
|
|
@@ -562,7 +570,7 @@ void* strmap_remove(strmap_t *map, const char *key)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
void* strmap_set_lc(strmap_t *map, const char *key, void *val)
|
|
|
{
|
|
|
|
|
@@ -574,7 +582,7 @@ void* strmap_set_lc(strmap_t *map, const char *key, void *val)
|
|
|
tor_free(lc_key);
|
|
|
return v;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
void* strmap_get_lc(strmap_t *map, const char *key)
|
|
|
{
|
|
|
void *v;
|
|
@@ -584,7 +592,7 @@ void* strmap_get_lc(strmap_t *map, const char *key)
|
|
|
tor_free(lc_key);
|
|
|
return v;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
void* strmap_remove_lc(strmap_t *map, const char *key)
|
|
|
{
|
|
|
void *v;
|
|
@@ -595,14 +603,14 @@ void* strmap_remove_lc(strmap_t *map, const char *key)
|
|
|
return v;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
* fn() is invoked with that entry's key, that entry's value, and the
|
|
|
- * value of <data> supplied to strmap_foreach. fn() must return a new
|
|
|
+ * value of <b>data</b> supplied to strmap_foreach. fn() must return a new
|
|
|
* (possibly unmodified) value for each entry: if fn() returns NULL, the
|
|
|
* entry is removed.
|
|
|
*
|
|
|
* Example:
|
|
|
+ * \code
|
|
|
* static void* upcase_and_remove_empty_vals(const char *key, void *val,
|
|
|
* void* data) {
|
|
|
* char *cp = (char*)val;
|
|
@@ -620,6 +628,7 @@ void* strmap_remove_lc(strmap_t *map, const char *key)
|
|
|
* ...
|
|
|
*
|
|
|
* strmap_foreach(map, upcase_and_remove_empty_vals, NULL);
|
|
|
+ * \endcode
|
|
|
*/
|
|
|
void strmap_foreach(strmap_t *map,
|
|
|
void* (*fn)(const char *key, void *val, void *data),
|
|
@@ -639,10 +648,11 @@ void strmap_foreach(strmap_t *map,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*
|
|
|
* Iterator example:
|
|
|
*
|
|
|
+ * \code
|
|
|
*
|
|
|
*
|
|
|
* strmap_iter_t *iter;
|
|
@@ -661,6 +671,7 @@ void strmap_foreach(strmap_t *map,
|
|
|
* iter = strmap_iter_next(iter);
|
|
|
* }
|
|
|
* }
|
|
|
+ * \endcode
|
|
|
*
|
|
|
*/
|
|
|
strmap_iter_t *strmap_iter_init(strmap_t *map)
|
|
@@ -668,14 +679,14 @@ strmap_iter_t *strmap_iter_init(strmap_t *map)
|
|
|
tor_assert(map);
|
|
|
return SPLAY_MIN(strmap_tree, &map->head);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter)
|
|
|
{
|
|
|
tor_assert(map && iter);
|
|
|
return SPLAY_NEXT(strmap_tree, &map->head, iter);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
* the current entry.
|
|
|
*/
|
|
|
strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter)
|
|
@@ -688,7 +699,7 @@ strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter)
|
|
|
tor_free(iter);
|
|
|
return next;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp)
|
|
|
{
|
|
@@ -696,14 +707,14 @@ void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp)
|
|
|
*keyp = iter->key;
|
|
|
*valp = iter->val;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
int strmap_iter_done(strmap_iter_t *iter)
|
|
|
{
|
|
|
return iter == NULL;
|
|
|
}
|
|
|
-
|
|
|
- * If free_val is provided, it is invoked on every value in <map>.
|
|
|
+
|
|
|
+ * If free_val is provided, it is invoked on every value in <b>map</b>.
|
|
|
*/
|
|
|
void strmap_free(strmap_t *map, void (*free_val)(void*))
|
|
|
{
|
|
@@ -723,7 +734,18 @@ void strmap_free(strmap_t *map, void (*free_val)(void*))
|
|
|
* String manipulation
|
|
|
*/
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+ * lowercase. */
|
|
|
+void tor_strlower(char *s)
|
|
|
+{
|
|
|
+ while (*s) {
|
|
|
+ *s = tolower(*s);
|
|
|
+ ++s;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
* not a comment, or to the terminating NUL if no such character exists.
|
|
|
*/
|
|
|
const char *eat_whitespace(const char *s) {
|
|
@@ -742,7 +764,7 @@ const char *eat_whitespace(const char *s) {
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* or to the terminating NUL if no such character exists. */
|
|
|
const char *eat_whitespace_no_nl(const char *s) {
|
|
|
while(*s == ' ' || *s == '\t')
|
|
@@ -750,7 +772,7 @@ const char *eat_whitespace_no_nl(const char *s) {
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* or to the terminating NUL if no such character exists.
|
|
|
*/
|
|
|
const char *find_whitespace(const char *s) {
|
|
@@ -762,11 +784,11 @@ const char *find_whitespace(const char *s) {
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* Time
|
|
|
- *****/
|
|
|
+ */
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* (Same as gettimeofday(timeval,NULL), but never returns -1.)
|
|
|
*/
|
|
|
void tor_gettimeofday(struct timeval *timeval) {
|
|
@@ -785,7 +807,7 @@ void tor_gettimeofday(struct timeval *timeval) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* If start is after end, return 0.
|
|
|
*/
|
|
|
long
|
|
@@ -808,7 +830,7 @@ tv_udiff(struct timeval *start, struct timeval *end)
|
|
|
return udiff;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
int tv_cmp(struct timeval *a, struct timeval *b) {
|
|
|
if (a->tv_sec > b->tv_sec)
|
|
@@ -822,7 +844,7 @@ int tv_cmp(struct timeval *a, struct timeval *b) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
void tv_add(struct timeval *a, struct timeval *b) {
|
|
|
a->tv_usec += b->tv_usec;
|
|
@@ -830,7 +852,7 @@ void tv_add(struct timeval *a, struct timeval *b) {
|
|
|
a->tv_usec %= 1000000;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
void tv_addms(struct timeval *a, long ms) {
|
|
|
a->tv_usec += (ms * 1000) % 1000000;
|
|
@@ -845,10 +867,11 @@ static int n_leapdays(int y1, int y2) {
|
|
|
--y2;
|
|
|
return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
|
|
|
}
|
|
|
+
|
|
|
static const int days_per_month[] =
|
|
|
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* does not account for leap seconds.
|
|
|
*/
|
|
|
time_t tor_timegm (struct tm *tm) {
|
|
@@ -878,10 +901,10 @@ time_t tor_timegm (struct tm *tm) {
|
|
|
* Low-level I/O.
|
|
|
*/
|
|
|
|
|
|
-
|
|
|
- * was returned by socket() or accept(), and 0 if fd was returned by
|
|
|
- * open(). Return the number of bytes written, or -1 on error. Only
|
|
|
- * use if fd is a blocking fd. */
|
|
|
+
|
|
|
+ * must be 1 if fd was returned by socket() or accept(), and 0 if fd
|
|
|
+ * was returned by open(). Return the number of bytes written, or -1
|
|
|
+ * on error. Only use if fd is a blocking fd. */
|
|
|
int write_all(int fd, const char *buf, size_t count, int isSocket) {
|
|
|
size_t written = 0;
|
|
|
int result;
|
|
@@ -898,7 +921,7 @@ int write_all(int fd, const char *buf, size_t count, int isSocket) {
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* was returned by socket() or accept(), and 0 if fd was returned by
|
|
|
* open(). Return the number of bytes read, or -1 on error. Only use
|
|
|
* if fd is a blocking fd. */
|
|
@@ -918,7 +941,7 @@ int read_all(int fd, char *buf, size_t count, int isSocket) {
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
void set_socket_nonblocking(int socket)
|
|
|
{
|
|
@@ -935,7 +958,7 @@ void set_socket_nonblocking(int socket)
|
|
|
* Process control
|
|
|
*/
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* unix calls fork, on win32 calls beginthread. Returns -1 on failure.
|
|
|
* func should not return, but rather should call spawn_exit.
|
|
|
*/
|
|
@@ -964,7 +987,7 @@ int spawn_func(int (*func)(void *), void *data)
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
void spawn_exit()
|
|
|
{
|
|
@@ -1092,7 +1115,8 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+ * On Windows, WSAEWOULDBLOCK is not always correct: when you see it,
|
|
|
* you need to ask the socket for its actual errno. Also, you need to
|
|
|
* get your errors from WSAGetLastError, not errno. (If you supply a
|
|
|
* socket of -1, we check WSAGetLastError, but don't correct
|
|
@@ -1113,9 +1137,6 @@ int tor_socket_errno(int sock)
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
-
|
|
|
- * Naturally, we have to roll our own.
|
|
|
- */
|
|
|
#ifdef MS_WINDOWS
|
|
|
#define E(code, s) { code, (s " [" #code " ]") }
|
|
|
struct { int code; const char *msg; } windows_socket_errors[] = {
|
|
@@ -1168,7 +1189,7 @@ struct { int code; const char *msg; } windows_socket_errors[] = {
|
|
|
E(WSANO_DATA, "Valid name, no data record of requested type)"),
|
|
|
|
|
|
|
|
|
- * 'OS dependent'. They start with WSA_, apparently for the same
|
|
|
+ * <b>OS dependent</b>. They start with WSA_, apparently for the same
|
|
|
* reason that practitioners of some craft traditions deliberately
|
|
|
* introduce imperfections into their baskets and rugs "to allow the
|
|
|
* evil spirits to escape." If we catch them, then our binaries
|
|
@@ -1177,6 +1198,9 @@ struct { int code; const char *msg; } windows_socket_errors[] = {
|
|
|
*/
|
|
|
{ -1, NULL },
|
|
|
};
|
|
|
+
|
|
|
+ * Naturally, we have to roll our own.
|
|
|
+ */
|
|
|
const char *tor_socket_strerror(int e)
|
|
|
{
|
|
|
int i;
|
|
@@ -1192,7 +1216,7 @@ const char *tor_socket_strerror(int e)
|
|
|
* Filesystem operations.
|
|
|
*/
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* exist, FN_FILE if it is a regular file, or FN_DIR if it's a
|
|
|
* directory. */
|
|
|
file_status_t file_status(const char *fname)
|
|
@@ -1212,7 +1236,7 @@ file_status_t file_status(const char *fname)
|
|
|
return FN_ERROR;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* it does not exist, and create is set, try to create it and return 0
|
|
|
* on success. Else return -1. */
|
|
|
int check_private_dir(const char *dirname, int create)
|
|
@@ -1266,8 +1290,8 @@ int check_private_dir(const char *dirname, int create)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * previous 'fname' if possible. Return 0 on success, -1 on failure.
|
|
|
+
|
|
|
+ * previous <b>fname</b> if possible. Return 0 on success, -1 on failure.
|
|
|
*
|
|
|
* This function replaces the old file atomically, if possible.
|
|
|
*/
|
|
@@ -1307,7 +1331,7 @@ write_str_to_file(const char *fname, const char *str)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* string on success or NULL on failure.
|
|
|
*/
|
|
|
char *read_file_to_str(const char *filename) {
|
|
@@ -1348,7 +1372,7 @@ char *read_file_to_str(const char *filename) {
|
|
|
return string;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* get a non-whitespace line. If it isn't of the form "key value"
|
|
|
* (value can have spaces), return -1.
|
|
|
* Point *key to the first word in line, point *value * to the second.
|
|
@@ -1400,7 +1424,7 @@ try_next_line:
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* or reserved for local networks by RFC 1918.
|
|
|
*/
|
|
|
int is_internal_IP(uint32_t ip) {
|
|
@@ -1415,7 +1439,7 @@ int is_internal_IP(uint32_t ip) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
static char uname_result[256];
|
|
|
|
|
|
static int uname_result_is_set = 0;
|
|
@@ -1450,9 +1474,10 @@ get_uname(void)
|
|
|
static int start_daemon_called = 0;
|
|
|
static int finish_daemon_called = 0;
|
|
|
static int daemon_filedes[2];
|
|
|
-
|
|
|
- * quickly; the parent process will wait around until the child process calls
|
|
|
- * finish_daemon.
|
|
|
+
|
|
|
+ * except standard fds. The parent process never returns, but stays around
|
|
|
+ * until finish_daemon is called. (Note: it's safe to call this more
|
|
|
+ * than once: calls after the first are ignored.)
|
|
|
*/
|
|
|
void start_daemon(char *desired_cwd)
|
|
|
{
|
|
@@ -1508,8 +1533,10 @@ void start_daemon(char *desired_cwd)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * and the daemon is now running.
|
|
|
+
|
|
|
+ * the parent process to exit. (Note: it's safe to call this more than once:
|
|
|
+ * calls after the first are ignored. Calls start_daemon first if it hasn't
|
|
|
+ * been called already.)
|
|
|
*/
|
|
|
void finish_daemon(void)
|
|
|
{
|
|
@@ -1546,7 +1573,7 @@ void start_daemon(char *cp) {}
|
|
|
void finish_daemon(void) {}
|
|
|
#endif
|
|
|
|
|
|
-
|
|
|
+
|
|
|
*/
|
|
|
void write_pidfile(char *filename) {
|
|
|
#ifndef MS_WINDOWS
|
|
@@ -1562,7 +1589,7 @@ void write_pidfile(char *filename) {
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* success. On failure, log and return -1.
|
|
|
*/
|
|
|
int switch_id(char *user, char *group) {
|
|
@@ -1615,7 +1642,7 @@ int switch_id(char *user, char *group) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* Return 1 on success, 0 if c is badly formatted. (Like inet_aton(c,addr),
|
|
|
* but works on Windows and Solaris.)
|
|
|
*/
|
|
@@ -1638,18 +1665,18 @@ int tor_inet_aton(const char *c, struct in_addr* addr)
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
* *addr to the proper IP address, in network byte order. Returns 0
|
|
|
* on success, -1 on failure; 1 on transient failure.
|
|
|
*
|
|
|
* (This function exists because standard windows gethostbyname
|
|
|
* doesn't treat raw IP addresses properly.)
|
|
|
*/
|
|
|
-
|
|
|
- * something.
|
|
|
- */
|
|
|
int tor_lookup_hostname(const char *name, uint32_t *addr)
|
|
|
{
|
|
|
+
|
|
|
+ * something.
|
|
|
+ */
|
|
|
struct in_addr iaddr;
|
|
|
struct hostent *ent;
|
|
|
tor_assert(addr);
|