util.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright 2003 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #ifndef __UTIL_H
  5. #define __UTIL_H
  6. #include "../or/or.h"
  7. #if _MSC_VER > 1300
  8. #include <winsock2.h>
  9. #include <ws2tcpip.h>
  10. #elif defined(_MSC_VER)
  11. #include <winsock.h>
  12. #endif
  13. #ifndef HAVE_GETTIMEOFDAY
  14. #ifdef HAVE_FTIME
  15. #define USING_FAKE_TIMEVAL
  16. #include <sys/timeb.h>
  17. #define timeval timeb
  18. #define tv_sec time
  19. #define tv_usec millitm
  20. #endif
  21. #endif
  22. #ifdef MS_WINDOWS
  23. /* Windows names string functions funnily. */
  24. #define strncasecmp strnicmp
  25. #define strcasecmp stricmp
  26. #define INLINE __inline
  27. #else
  28. #define INLINE inline
  29. #endif
  30. void *tor_malloc(size_t size);
  31. void *tor_malloc_zero(size_t size);
  32. void *tor_realloc(void *ptr, size_t size);
  33. char *tor_strdup(const char *s);
  34. char *tor_strndup(const char *s, size_t n);
  35. #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
  36. typedef struct {
  37. void **list;
  38. int num_used;
  39. int max;
  40. } smartlist_t;
  41. smartlist_t *smartlist_create(int max_elements);
  42. void smartlist_free(smartlist_t *sl);
  43. void smartlist_add(smartlist_t *sl, void *element);
  44. void smartlist_remove(smartlist_t *sl, void *element);
  45. int smartlist_isin(smartlist_t *sl, void *element);
  46. int smartlist_overlap(smartlist_t *sl1, smartlist_t *sl2);
  47. void smartlist_intersect(smartlist_t *sl1, smartlist_t *sl2);
  48. void smartlist_subtract(smartlist_t *sl1, smartlist_t *sl2);
  49. void *smartlist_choose(smartlist_t *sl);
  50. const char *eat_whitespace(const char *s);
  51. const char *eat_whitespace_no_nl(const char *s);
  52. const char *find_whitespace(const char *s);
  53. void tor_gettimeofday(struct timeval *timeval);
  54. long tv_udiff(struct timeval *start, struct timeval *end);
  55. void tv_addms(struct timeval *a, long ms);
  56. void tv_add(struct timeval *a, struct timeval *b);
  57. int tv_cmp(struct timeval *a, struct timeval *b);
  58. time_t tor_timegm (struct tm *tm);
  59. int write_all(int fd, const char *buf, size_t count);
  60. int read_all(int fd, char *buf, size_t count);
  61. void set_socket_nonblocking(int socket);
  62. typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
  63. file_status_t file_status(const char *filename);
  64. int check_private_dir(const char *dirname, int create);
  65. int write_str_to_file(const char *fname, const char *str);
  66. char *read_file_to_str(const char *filename);
  67. int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
  68. int spawn_func(int (*func)(void *), void *data);
  69. void spawn_exit();
  70. int tor_socketpair(int family, int type, int protocol, int fd[2]);
  71. const char *get_uname(void);
  72. void daemonize(void);
  73. void write_pidfile(char *filename);
  74. int switch_id(char *user, char *group);
  75. /* For stupid historical reasons, windows sockets have an independent set of
  76. * errnos which they use as the fancy strikes them.
  77. */
  78. #ifdef MS_WINDOWS
  79. #define ERRNO_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  80. #define ERRNO_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  81. #define ERRNO_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e) == WSAEINVAL)
  82. int correct_socket_errno(int s);
  83. #else
  84. #define ERRNO_EAGAIN(e) ((e) == EAGAIN)
  85. #define ERRNO_EINPROGRESS(e) ((e) == EINPROGRESS)
  86. #define ERRNO_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
  87. #define correct_socket_errno(s) (errno)
  88. #endif
  89. #endif