From 172579d4d2c0af3986943ef1ca907ebe43b7031d Mon Sep 17 00:00:00 2001 From: Martin Schanzenbach Date: Thu, 15 Mar 2012 16:35:54 +0000 Subject: -fixes --- src/gns/gnunet-service-gns.c | 43 ++++++++++++++++++++++-- src/gns/gnunet-service-gns_resolver.c | 61 +++++++++++++++++++++++++++-------- src/gns/proxy/proxy.py | 39 +++++++++++----------- 3 files changed, 108 insertions(+), 35 deletions(-) (limited to 'src/gns') diff --git a/src/gns/gnunet-service-gns.c b/src/gns/gnunet-service-gns.c index aa3d3b3e8..ac5c74818 100644 --- a/src/gns/gnunet-service-gns.c +++ b/src/gns/gnunet-service-gns.c @@ -414,7 +414,7 @@ send_shorten_response(void* cls, const char* name) GNUNET_SERVER_receive_done (csh->client, GNUNET_OK); GNUNET_free(rmsg); - GNUNET_free(csh->name); + GNUNET_free_non_null(csh->name); GNUNET_free(csh); } @@ -462,6 +462,23 @@ static void handle_shorten(void *cls, csh->unique_id = sh_msg->id; name = (char*)&sh_msg[1]; + + if (strlen (name) < strlen(GNUNET_GNS_TLD)) { + csh->name = NULL; + send_shorten_response(csh, name); + return; + } + + if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD), + GNUNET_GNS_TLD) != 0) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "%s is not our domain. Returning\n", name); + csh->name = NULL; + send_shorten_response(csh, name); + return; + } + csh->name = GNUNET_malloc(strlen(name) - strlen(GNUNET_GNS_TLD) + 1); memset(csh->name, 0, @@ -512,7 +529,7 @@ send_get_auth_response(void *cls, const char* name) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n"); GNUNET_free(rmsg); - GNUNET_free(cah->name); + GNUNET_free_non_null(cah->name); GNUNET_free(cah); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n"); @@ -557,12 +574,32 @@ static void handle_get_authority(void *cls, GNUNET_SERVER_receive_done (client, GNUNET_OK); return; } + + name = (char*)&sh_msg[1]; cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle)); cah->client = client; cah->unique_id = sh_msg->id; + + if (strlen(name) < strlen(GNUNET_GNS_TLD)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "%s is too short. Returning\n", name); + cah->name = NULL; + send_get_auth_response(cah, name); + return; + } + + if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD), + GNUNET_GNS_TLD) != 0) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "%s is not our domain. Returning\n", name); + cah->name = NULL; + send_get_auth_response(cah, name); + return; + } - name = (char*)&sh_msg[1]; cah->name = GNUNET_malloc(strlen(name) - strlen(GNUNET_GNS_TLD) + 1); memset(cah->name, 0, diff --git a/src/gns/gnunet-service-gns_resolver.c b/src/gns/gnunet-service-gns_resolver.c index dee082e99..a2f61af0e 100644 --- a/src/gns/gnunet-service-gns_resolver.c +++ b/src/gns/gnunet-service-gns_resolver.c @@ -1095,17 +1095,36 @@ gns_resolver_lookup_record(GNUNET_HashCode zone, "Starting resolution for %s (type=%d)!\n", name, record_type); + + if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "%s is canonical and gnunet not our TLD!\n", name); + proc(cls, 0, NULL); + return; + } + rlh = GNUNET_malloc(sizeof(struct RecordLookupHandle)); rh = GNUNET_malloc(sizeof (struct ResolverHandle)); rh->authority = zone; rh->proc_cls = rlh; - rh->name = GNUNET_malloc(strlen(name) - - strlen(GNUNET_GNS_TLD)); - memset(rh->name, 0, - strlen(name)-strlen(GNUNET_GNS_TLD)); - memcpy(rh->name, name, - strlen(name)-strlen(GNUNET_GNS_TLD) - 1); + + if (strcmp(GNUNET_GNS_TLD, name) == 0) + { + rh->name = GNUNET_malloc(2); + strcpy(rh->name, ""); + } + else + { + rh->name = GNUNET_malloc(strlen(name) + - strlen(GNUNET_GNS_TLD)); + memset(rh->name, 0, + strlen(name)-strlen(GNUNET_GNS_TLD)); + memcpy(rh->name, name, + strlen(name)-strlen(GNUNET_GNS_TLD) - 1); + } + rh->authority_name = GNUNET_malloc(sizeof(char)*MAX_DNS_LABEL_LENGTH); rh->authority_chain_head = GNUNET_malloc(sizeof(struct AuthorityChain)); rh->authority_chain_head->prev = NULL; @@ -1164,11 +1183,15 @@ process_zone_to_name_shorten(void *cls, answer_len = strlen(rh->name) + strlen(name) + strlen(GNUNET_GNS_TLD) + 3; result = GNUNET_malloc(answer_len); memset(result, 0, answer_len); - strcpy(result, rh->name); - strcpy(result+strlen(rh->name), "."); - strcpy(result+strlen(rh->name)+1, name); - strcpy(result+strlen(rh->name)+strlen(name)+1, "."); - strcpy(result+strlen(rh->name)+strlen(name)+2, GNUNET_GNS_TLD); + if (strlen(rh->name) > 0) + { + strcpy(result, rh->name); + strcpy(result+strlen(rh->name), "."); + } + + strcpy(result+strlen(result), name); + strcpy(result+strlen(result), "."); + strcpy(result+strlen(result), GNUNET_GNS_TLD); GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending shorten result %s\n", result); @@ -1313,7 +1336,20 @@ gns_resolver_shorten_name(GNUNET_HashCode zone, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting shorten for %s!\n", name); + if (is_canonical((char*)name)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "%s is canonical. Returning verbatim\n", name); + proc(cls, name); + return; + } + nsh = GNUNET_malloc(sizeof (struct NameShortenHandle)); + + + nsh->proc = proc; + nsh->proc_cls = cls; + rh = GNUNET_malloc(sizeof (struct ResolverHandle)); rh->authority = zone; rh->name = GNUNET_malloc(strlen(name) @@ -1329,9 +1365,6 @@ gns_resolver_shorten_name(GNUNET_HashCode zone, rh->authority_chain_head->zone = zone; rh->proc = &handle_delegation_ns_shorten; rh->proc_cls = nsh; - - nsh->proc = proc; - nsh->proc_cls = cls; /* Start delegation resolution in our namestore */ resolve_delegation_ns(rh); diff --git a/src/gns/proxy/proxy.py b/src/gns/proxy/proxy.py index 7a73c4797..4438f33fa 100644 --- a/src/gns/proxy/proxy.py +++ b/src/gns/proxy/proxy.py @@ -31,7 +31,6 @@ class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler): server_version = "TinyHTTPProxy/" + __version__ rbufsize = 0 # self.rfile Be unbuffered host_port = () - to_replace = "" def handle(self): (ip, port) = self.client_address @@ -43,19 +42,20 @@ class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler): def _connect_to(self, netloc, soc): i = netloc.find(':') + to_replace = "" if i >= 0: print 'calling gnunet-gns -a '+netloc[:i] auth = os.popen("gnunet-gns -a "+netloc[:i]) lines = auth.readlines() print 'result: '+lines[0].split(" ")[-1].rstrip() - self.to_replace = lines[0].split(" ")[-1].rstrip() + to_replace = lines[0].split(" ")[-1].rstrip() self.host_port = netloc[:i], int(netloc[i+1:]) else: print 'calling gnunet-gns -a '+netloc auth = os.popen("gnunet-gns -a "+netloc) lines = auth.readlines() print 'result: '+lines[0].split(" ")[-1].rstrip() - self.to_replace = lines[0].split(" ")[-1].rstrip() + to_replace = lines[0].split(" ")[-1].rstrip() self.host_port = netloc, 80 print "\t" "connect to %s:%d" % self.host_port try: soc.connect(self.host_port) @@ -63,31 +63,33 @@ class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler): try: msg = arg[1] except: msg = arg self.send_error(404, msg) - return 0 - return 1 + return (0, 0) + return (1, to_replace) def do_CONNECT(self): soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: - if self._connect_to(self.path, soc): + res, to_repl = self._connect_to(self.path, soc) + if res: self.log_request(200) self.wfile.write(self.protocol_version + " 200 Connection established\r\n") self.wfile.write("Proxy-agent: %s\r\n" % self.version_string()) self.wfile.write("\r\n") - self._read_write(soc, 300) + self._read_write(soc, to_repl, 300) finally: print "\t" "bye" soc.close() self.connection.close() - def replace_and_shorten(self, mo): - full = string.replace(mo.group(1)+self.to_replace, 'a href="', "") - print 'calling gnunet-gns -s '+full - s = os.popen("gnunet-gns -s "+full) - lines = s.readlines() - print 'short: '+lines[0].split(" ")[-1].rstrip() - return 'a href="'+lines[0].split(" ")[-1].rstrip() + def replace_and_shorten(self, to_repl): + return lambda mo: 'a href="http://'+os.popen("gnunet-gns -s "+string.replace(mo.group(1)+to_repl, 'a href="http://', "")).readlines()[0].split(" ")[-1].rstrip() + #full = string.replace(mo.group(1)+to_repl, 'a href="http://', "") + #print 'calling gnunet-gns -s '+full + #s = os.popen("gnunet-gns -s "+full) + #lines = s.readlines() + #print 'short: '+lines[0].split(" ")[-1].rstrip() + #return 'a href="'+lines[0].split(" ")[-1].rstrip() def do_GET(self): (scm, netloc, path, params, query, fragment) = urlparse.urlparse( @@ -97,7 +99,8 @@ class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler): return soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: - if self._connect_to(netloc, soc): + res, to_repl = self._connect_to(netloc, soc) + if res: self.log_request() soc.send("%s %s %s\r\n" % ( self.command, @@ -108,13 +111,13 @@ class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler): for key_val in self.headers.items(): soc.send("%s: %s\r\n" % key_val) soc.send("\r\n") - self._read_write(soc) + self._read_write(soc, to_repl) finally: print "\t" "bye" soc.close() self.connection.close() - def _read_write(self, soc, max_idling=20): + def _read_write(self, soc, to_repl="", max_idling=20): iw = [self.connection, soc] ow = [] count = 0 @@ -136,7 +139,7 @@ class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler): arr = self.host_port[0].split('.') arr.pop(0) data = re.sub('(a href="http://(\w+\.)*)(\+)', - self.replace_and_shorten, data) + self.replace_and_shorten(to_repl), data) print data out.send(data) count = 0 -- cgit v1.2.3