1 /* Copyright 2003 Roger Dingledine */
2 /* See LICENSE for licensing information */
9 void *tor_malloc(size_t size
) {
12 result
= malloc(size
);
15 log_fn(LOG_ERR
, "Out of memory. Dying.");
23 my_gettimeofday(struct timeval
*timeval
)
25 #ifdef HAVE_GETTIMEOFDAY
26 if (gettimeofday(timeval
, NULL
)) {
27 log_fn(LOG_ERR
, "gettimeofday failed.");
28 /* If gettimeofday dies, we have either given a bad timezone (we didn't),
32 #elif defined(HAVE_FTIME)
35 #error "No way to get time."
41 tv_udiff(struct timeval
*start
, struct timeval
*end
)
44 long end_usec
= end
->tv_usec
;
45 long secdiff
= end
->tv_sec
- start
->tv_sec
;
47 if (secdiff
+1 > LONG_MAX
/1000000) {
48 log_fn(LOG_NOTICE
, "comparing times too far apart.");
52 udiff
= secdiff
*1000000L + (end_usec
- start
->tv_usec
);
54 log_fn(LOG_NOTICE
, "start is after end. Returning 0.");
60 int tv_cmp(struct timeval
*a
, struct timeval
*b
) {
61 if (a
->tv_sec
> b
->tv_sec
)
63 if (a
->tv_sec
< b
->tv_sec
)
65 if (a
->tv_usec
> b
->tv_usec
)
67 if (a
->tv_usec
< b
->tv_usec
)
72 void tv_add(struct timeval
*a
, struct timeval
*b
) {
73 a
->tv_usec
+= b
->tv_usec
;
74 a
->tv_sec
+= b
->tv_sec
+ (a
->tv_usec
/ 1000000);
75 a
->tv_usec
%= 1000000;
78 void tv_addms(struct timeval
*a
, long ms
) {
79 a
->tv_usec
+= (ms
* 1000) % 1000000;
80 a
->tv_sec
+= ((ms
* 1000) / 1000000) + (a
->tv_usec
/ 1000000);
81 a
->tv_usec
%= 1000000;
84 void set_socket_nonblocking(int socket
)
87 /* Yes means no and no means yes. Do you not want to be nonblocking? */
89 ioctlsocket(socket
, FIONBIO
, (unsigned long*) &nonblocking
);
91 fcntl(socket
, F_SETFL
, O_NONBLOCK
);
95 int spawn_func(int (*func
)(void *), void *data
)
99 rv
= _beginthread(func
, 0, data
);
100 if (rv
== (unsigned long) -1)
111 assert(0); /* Should never reach here. */
129 /* Fake socket pair over TCP. Code adapted from perl 5.8.0's util.c */
131 tor_socketpair(int family
, int type
, int protocol
, int fd
[2])
133 #ifdef HAVE_SOCKETPAIR_XXX
134 /* For testing purposes, we never fall back to real socketpairs. */
135 return socketpair(family
, type
, protocol
, fd
);
140 struct sockaddr_in listen_addr
;
141 struct sockaddr_in connect_addr
;
149 errno
= EAFNOSUPPORT
;
157 listener
= socket(AF_INET
, type
, 0);
160 memset (&listen_addr
, 0, sizeof (listen_addr
));
161 listen_addr
.sin_family
= AF_INET
;
162 listen_addr
.sin_addr
.s_addr
= htonl (INADDR_LOOPBACK
);
163 listen_addr
.sin_port
= 0; /* kernel choses port. */
164 if (bind(listener
, (struct sockaddr
*) &listen_addr
, sizeof (listen_addr
))
166 goto tidy_up_and_fail
;
167 if (listen(listener
, 1) == -1)
168 goto tidy_up_and_fail
;
170 connector
= socket(AF_INET
, type
, 0);
172 goto tidy_up_and_fail
;
173 /* We want to find out the port number to connect to. */
174 size
= sizeof (connect_addr
);
175 if (getsockname(listener
, (struct sockaddr
*) &connect_addr
, &size
) == -1)
176 goto tidy_up_and_fail
;
177 if (size
!= sizeof (connect_addr
))
178 goto abort_tidy_up_and_fail
;
179 if (connect(connector
, (struct sockaddr
*) &connect_addr
,
180 sizeof (connect_addr
)) == -1)
181 goto tidy_up_and_fail
;
183 size
= sizeof (listen_addr
);
184 acceptor
= accept(listener
, (struct sockaddr
*) &listen_addr
, &size
);
186 goto tidy_up_and_fail
;
187 if (size
!= sizeof(listen_addr
))
188 goto abort_tidy_up_and_fail
;
190 /* Now check we are talking to ourself by matching port and host on the
192 if (getsockname(connector
, (struct sockaddr
*) &connect_addr
, &size
) == -1)
193 goto tidy_up_and_fail
;
194 if (size
!= sizeof (connect_addr
)
195 || listen_addr
.sin_family
!= connect_addr
.sin_family
196 || listen_addr
.sin_addr
.s_addr
!= connect_addr
.sin_addr
.s_addr
197 || listen_addr
.sin_port
!= connect_addr
.sin_port
) {
198 goto abort_tidy_up_and_fail
;
204 abort_tidy_up_and_fail
:
205 errno
= ECONNABORTED
; /* I hope this is portable and appropriate. */
208 int save_errno
= errno
;