- /* ethertype is ipv6 */
- if (ntohs(pkt_tun->tun.type) == 0x86dd)
- {
- struct ip6_pkt *pkt6 = (struct ip6_pkt*) message;
- GNUNET_assert(pkt6->ip6_hdr.version == 6);
- struct ip6_tcp *pkt6_tcp;
- struct ip6_udp *pkt6_udp;
- struct ip6_icmp *pkt6_icmp;
-
- pkt_printf(pkt6);
- switch(pkt6->ip6_hdr.nxthdr)
- {
- case 0x06:
- pkt6_tcp = (struct ip6_tcp*)pkt6;
- pkt_printf_ip6tcp(pkt6_tcp);
- break;
- case 0x11:
- pkt6_udp = (struct ip6_udp*)pkt6;
- pkt_printf_ip6udp(pkt6_udp);
- if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
- pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
- }
- break;
- case 0x3a:
- /* ICMPv6 */
- pkt6_icmp = (struct ip6_icmp*)pkt6;
- /* If this packet is an icmp-echo-request and a mapping exists, answer */
- if (pkt6_icmp->icmp_hdr.type == 0x80 && address_mapping_exists(pkt6->ip6_hdr.dadr))
- {
- pkt6_icmp = GNUNET_malloc(ntohs(pkt6->shdr.size));
- memcpy(pkt6_icmp, pkt6, ntohs(pkt6->shdr.size));
- GNUNET_SCHEDULER_add_now(&send_icmp_response, pkt6_icmp);
- }
- break;
- }
- }
- /* ethertype is ipv4 */
- else if (ntohs(pkt_tun->tun.type) == 0x0800)
- {
- struct ip_pkt *pkt = (struct ip_pkt*) message;
- struct ip_udp *udp = (struct ip_udp*) message;
- GNUNET_assert(pkt->ip_hdr.version == 4);
-
- /* Send dns-packets to the service-dns */
- if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 )
- {
- /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
- size_t len = sizeof(struct query_packet) + ntohs(udp->udp_hdr.len) - 9;
-
- struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*));
- query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
- query->pkt.hdr.size = htons(len);
- query->pkt.orig_to = pkt->ip_hdr.dadr;
- query->pkt.orig_from = pkt->ip_hdr.sadr;
- query->pkt.src_port = udp->udp_hdr.spt;
- memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8);
-
- GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, query);
-
- if (dns_connection != NULL)
- GNUNET_CLIENT_notify_transmit_ready(dns_connection,
- len,
- GNUNET_TIME_UNIT_FOREVER_REL,
- GNUNET_YES,
- &send_query,
- NULL);
- }
- }
+ uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
+
+ struct map_entry *value =
+ GNUNET_malloc (sizeof (struct map_entry) + namelen);
+ char *name = (char *) (value + 1);
+
+ value->namelen = namelen;
+ memcpy (name, pkt->data + 12, namelen);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
+ pkt->addrsize);
+ value->addrlen = pkt->addrsize;
+ memcpy (&value->addr, &pkt->addr, pkt->addrsize);
+ memset (value->additional_ports, 0, 8192);
+
+ memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
+
+ if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
+ {
+ GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+ value->heap_node =
+ GNUNET_CONTAINER_heap_insert (heap, value,
+ GNUNET_TIME_absolute_get ().abs_value);
+ if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
+ GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
+ }
+ else
+ GNUNET_free (value);
+
+ list =
+ GNUNET_malloc (htons (pkt->hdr.size) +
+ sizeof (struct answer_packet_list) -
+ sizeof (struct answer_packet));
+
+ memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
+ }
+ else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A)
+ {
+ pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
+
+ GNUNET_HashCode key;
+
+ memset (&key, 0, sizeof (GNUNET_HashCode));
+
+ unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
+
+ new_ip4addr_remote (c, pkt->addr, pkt->addrsize);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n", c[0],
+ c[1], c[2], c[3]);
+ unsigned char *k = (unsigned char *) &key;
+
+ /*
+ * Copy the newly generated ip-address to the key backwards (as only the first part is used in the hash-table)
+ */
+ unsigned int i;
+
+ for (i = 0; i < 4; i++)
+ k[3 - i] = c[i];
+
+ uint16_t namelen = strlen ((char *) pkt->data + 12) + 1;
+
+ struct map_entry *value =
+ GNUNET_malloc (sizeof (struct map_entry) + namelen);
+ char *name = (char *) (value + 1);
+
+ value->namelen = namelen;
+ memcpy (name, pkt->data + 12, namelen);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting addrlen to %d\n",
+ pkt->addrsize);
+ value->addrlen = pkt->addrsize;
+ memcpy (&value->addr, &pkt->addr, pkt->addrsize);
+ memset (value->additional_ports, 0, 8192);
+
+ memcpy (&value->hash, &key, sizeof (GNUNET_HashCode));
+
+ if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (hashmap, &key))
+ {
+ GNUNET_CONTAINER_multihashmap_put (hashmap, &key, value,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+ value->heap_node =
+ GNUNET_CONTAINER_heap_insert (heap, value,
+ GNUNET_TIME_absolute_get ().abs_value);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Mapping is saved in the hashmap with key %08x.\n",
+ *((uint32_t *) (&key)));
+ if (GNUNET_CONTAINER_heap_get_size (heap) > max_mappings)
+ GNUNET_SCHEDULER_add_now (collect_mappings, NULL);
+ }
+ else
+ GNUNET_free (value);
+
+ list =
+ GNUNET_malloc (htons (pkt->hdr.size) +
+ sizeof (struct answer_packet_list) -
+ sizeof (struct answer_packet));
+
+ memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
+ }
+ else
+ {
+ GNUNET_break (0);
+ GNUNET_free (pkt);
+ return;
+ }
+
+ GNUNET_free (pkt);
+
+ GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail,
+ answer_proc_tail, list);
+
+ schedule_helper_write (GNUNET_TIME_UNIT_FOREVER_REL, NULL);
+
+ return;