[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Make clients regenerate their keys when their IP address ch...



Update of /home/or/cvsroot/tor/src/common
In directory moria:/tmp/cvs-serv15996/src/common

Modified Files:
	util.c util.h 
Log Message:
Make clients regenerate their keys when their IP address changes.

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/util.c,v
retrieving revision 1.211
retrieving revision 1.212
diff -u -d -r1.211 -r1.212
--- util.c	30 Jun 2005 06:56:00 -0000	1.211
+++ util.c	3 Aug 2005 20:42:17 -0000	1.212
@@ -695,7 +695,8 @@
  * 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) {
+int
+write_all(int fd, const char *buf, size_t count, int isSocket) {
   size_t written = 0;
   int result;
 
@@ -716,7 +717,8 @@
  * 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. */
-int read_all(int fd, char *buf, size_t count, int isSocket) {
+int
+read_all(int fd, char *buf, size_t count, int isSocket) {
   size_t numread = 0;
   int result;
 
@@ -1319,6 +1321,59 @@
   return 1;
 }
 
+/**
+ * Set *<b>addr</b> to the host-order IPv4 address (if any) of whatever
+ * interface connects to the internet.  This address should only be used in
+ * checking whether our address has changed.  Return 0 on success, -1 on
+ * failure.
+ */
+int
+get_interface_address(uint32_t *addr)
+{
+  int sock=-1, r=-1;
+  struct sockaddr_in target_addr, my_addr;
+  socklen_t my_addr_len = sizeof(my_addr);
+
+  tor_assert(addr);
+  *addr = 0;
+
+  sock = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
+  if (sock < 0) {
+    int e = tor_socket_errno(-1);
+    log_fn(LOG_WARN, "unable to create socket: %s", tor_socket_strerror(e));
+    goto err;
+  }
+
+  memset(&target_addr, 0, sizeof(target_addr));
+  target_addr.sin_family = AF_INET;
+  /* discard port */
+  target_addr.sin_port = 9;
+  /* 18.0.0.1 (Don't worry: no packets are sent. We just need a real address
+   * on the internet.) */
+  target_addr.sin_addr.s_addr = htonl(0x12000001);
+
+  if (connect(sock,(struct sockaddr *)&target_addr,sizeof(target_addr))<0) {
+    int e = tor_socket_errno(sock);
+    log_fn(LOG_WARN, "connnect() failed: %s", tor_socket_strerror(e));
+    goto err;
+  }
+
+  /* XXXX Can this be right on IPv6 clients? */
+  if (getsockname(sock, &my_addr, &my_addr_len)) {
+    int e = tor_socket_errno(sock);
+    log_fn(LOG_WARN, "getsockname() failed: %s", tor_socket_strerror(e));
+    goto err;
+  }
+
+  *addr = ntohl(my_addr.sin_addr.s_addr);
+
+  r=0;
+ err:
+  if (sock >= 0)
+    tor_close_socket(sock);
+  return r;
+}
+
 /* =====
  * Process helpers
  * ===== */

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/util.h,v
retrieving revision 1.134
retrieving revision 1.135
diff -u -d -r1.134 -r1.135
--- util.h	9 Jun 2005 19:03:31 -0000	1.134
+++ util.h	3 Aug 2005 20:42:17 -0000	1.135
@@ -135,6 +135,7 @@
 #define INET_NTOA_BUF_LEN 16
 int tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len);
 int is_plausible_address(const char *name);
+int get_interface_address(uint32_t *addr);
 
 /* Process helpers */
 void start_daemon(void);