aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2014-02-11 11:16:15 +0000
committerFlorian Dold <florian.dold@gmail.com>2014-02-11 11:16:15 +0000
commitf3279afd44bea2c4264dbec468a186ff11878071 (patch)
tree52a911d18e62b0e41a8b68c128e0e7714bd9e8f1
parent039e930a3b14781520843cf7d178aa0118953e2c (diff)
downloadgnunet-java-f3279afd44bea2c4264dbec468a186ff11878071.tar.gz
gnunet-java-f3279afd44bea2c4264dbec468a186ff11878071.zip
- missing GNS classes
-rw-r--r--src/main/java/org/gnunet/gns/Gns.java137
-rw-r--r--src/main/java/org/gnunet/gns/GnsTool.java81
-rw-r--r--src/main/java/org/gnunet/gns/RecordFlags.java48
-rw-r--r--src/main/java/org/gnunet/gns/callbacks/LookupResultProcessor.java35
-rw-r--r--src/main/java/org/gnunet/gns/records/ARecordData.java68
-rw-r--r--src/main/java/org/gnunet/gns/records/PkeyRecordData.java33
-rw-r--r--src/main/java/org/gnunet/gns/records/RecordData.java30
-rw-r--r--src/main/java/org/gnunet/gns/records/UnknownRecordData.java39
8 files changed, 471 insertions, 0 deletions
diff --git a/src/main/java/org/gnunet/gns/Gns.java b/src/main/java/org/gnunet/gns/Gns.java
new file mode 100644
index 0000000..3175be0
--- /dev/null
+++ b/src/main/java/org/gnunet/gns/Gns.java
@@ -0,0 +1,137 @@
1/*
2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21package org.gnunet.gns;
22
23import org.gnunet.gns.callbacks.LookupResultProcessor;
24import org.gnunet.gns.messages.ClientLookupMessage;
25import org.gnunet.gns.messages.ClientLookupResultMessage;
26import org.gnunet.requests.MatchingRequestContainer;
27import org.gnunet.requests.SimpleRequest;
28import org.gnunet.util.*;
29import org.gnunet.util.crypto.EcdsaPrivateKey;
30import org.gnunet.util.crypto.EcdsaPublicKey;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34/**
35 * API to the GNUnet name system.
36 */
37public class Gns {
38 private static final Logger logger = LoggerFactory
39 .getLogger(Gns.class);
40 /**
41 * All pending and active lookup requests.
42 */
43 private MatchingRequestContainer<Long, SimpleRequest<LookupResultProcessor>> lookupRequests;
44
45 /**
46 * Request ID for lookup requests.
47 */
48 private long nextUID = 1;
49
50 /**
51 * Client connected to the GNS service.
52 */
53 private Client client;
54
55 private RelativeTime reconnectBackoff = RelativeTime.STD_BACKOFF;
56
57
58 public class GNSMessageReceiver extends RunaboutMessageReceiver {
59 public void visit(ClientLookupResultMessage m) {
60 SimpleRequest<LookupResultProcessor> r = lookupRequests.pollRequest(m.id);
61 if (null == r) {
62 logger.warn("no matching request for lookup result");
63 return;
64 }
65 r.getContext().process(m.records);
66 }
67
68 @Override
69 public void handleError() {
70 logger.warn("Error receiving from GNS service, reconnecting.");
71 Scheduler.addDelayed(reconnectBackoff, new Scheduler.Task() {
72 @Override
73 public void run(Scheduler.RunContext ctx) {
74 client.reconnect();
75 // re-send active requests
76 lookupRequests.restart();
77 }
78 });
79 reconnectBackoff = reconnectBackoff.backoff();
80 }
81 }
82
83 /**
84 * Connect to the GNS service
85 *
86 * @param cfg configuration to use
87 */
88 public Gns(Configuration cfg) {
89 client = new Client("gns", cfg);
90 lookupRequests = new MatchingRequestContainer<Long, SimpleRequest<LookupResultProcessor>>(client);
91 client.installReceiver(new GNSMessageReceiver());
92 }
93
94 /**
95 * Perform an asynchronous lookup operation on the GNS.
96 *
97 * @param name the name to look up
98 * @param zone zone to look in
99 * @param type the GNS record type to look for
100 * @param onlyCached true to only check locally (not in the DHT)
101 * @param shortenZoneKey the private key of the shorten zone (can be NULL);
102 * specify to enable automatic shortening (given a PSEU
103 * record, if a given pseudonym is not yet used in the
104 * shorten zone, we automatically add the respective zone
105 * under that name)
106 * @param proc function to call on result
107 * @return handle to the queued request
108 */
109 public Cancelable lookup(String name,
110 EcdsaPublicKey zone,
111 long type, boolean onlyCached,
112 EcdsaPrivateKey shortenZoneKey,
113 LookupResultProcessor proc) {
114 ClientLookupMessage m = new ClientLookupMessage();
115
116 if (null != shortenZoneKey) {
117 m.haveKey = 1;
118 m.shortenKey = shortenZoneKey;
119 } else {
120 m.haveKey = 0;
121 m.shortenKey = EcdsaPrivateKey.zeroKey();
122 }
123 m.id = nextUID++;
124 m.name = name;
125 m.onlyCached = onlyCached ? 1 : 0;
126 m.type = type;
127 m.zone = zone;
128
129 return lookupRequests.addRequest(m.id, new SimpleRequest<LookupResultProcessor>(m, proc));
130 }
131
132
133 public void disconnect() {
134 client.disconnect();
135 client = null;
136 }
137}
diff --git a/src/main/java/org/gnunet/gns/GnsTool.java b/src/main/java/org/gnunet/gns/GnsTool.java
new file mode 100644
index 0000000..81cd6d3
--- /dev/null
+++ b/src/main/java/org/gnunet/gns/GnsTool.java
@@ -0,0 +1,81 @@
1package org.gnunet.gns;
2
3
4import org.gnunet.gns.callbacks.LookupResultProcessor;
5import org.gnunet.identity.Identity;
6import org.gnunet.identity.IdentityCallback;
7import org.gnunet.util.Program;
8import org.gnunet.util.getopt.Argument;
9import org.gnunet.util.getopt.ArgumentAction;
10
11public class GnsTool {
12 public static void main(String[] args) {
13 int ret = new Program(args) {
14 @Argument(
15 shortname = "u",
16 longname = "lookup",
17 action = ArgumentAction.STORE_STRING,
18 description = "Lookup a record for the given name")
19 String name;
20
21 @Argument(
22 shortname = "z",
23 longname = "zone",
24 action = ArgumentAction.STORE_STRING,
25 description = "Lookup a record in the given zone")
26 String zone = "master-zone";
27
28 @Argument(
29 shortname = "t",
30 longname = "type",
31 action = ArgumentAction.STORE_STRING,
32 description = "Lookup a record of the given type (defaut: A)")
33 String type = "A";
34
35 @Override
36 protected void run() {
37 if (null == name) {
38 System.err.println("no name given");
39 setReturnValue(1);
40 return;
41 }
42
43 final long typeId = GnsRecord.getIdFromString(type);
44 if (typeId < 0) {
45 System.err.println(String.format("type '%s' not known", type));
46 setReturnValue(1);
47 return;
48 }
49
50 System.out.println("looking for records of type id " + typeId);
51
52 Identity.lookup(getConfiguration(), zone, new IdentityCallback() {
53 @Override
54 public void onEgo(Identity.Ego ego) {
55 System.out.println("looking in zone " + ego.getPublicKey());
56 final Gns gns = new Gns(getConfiguration());
57 gns.lookup(name, ego.getPublicKey(), typeId, false, null, new LookupResultProcessor() {
58 @Override
59 public void process(GnsRecord[] records) {
60 System.out.println("got " + records.length + " records");
61 gns.disconnect();
62 for (GnsRecord record : records) {
63 String s = record.getRecordData().asRecordString();
64 System.out.println(
65 String.format("Type: %s, Value: %s", record.recordType, s));
66 }
67 }
68 });
69 }
70
71 @Override
72 public void onError(String errorMessage) {
73 System.err.println("could not look up zone ego '" + name + "': " + errorMessage);
74 }
75 });
76
77 }
78 }.start();
79 System.exit(ret);
80 }
81}
diff --git a/src/main/java/org/gnunet/gns/RecordFlags.java b/src/main/java/org/gnunet/gns/RecordFlags.java
new file mode 100644
index 0000000..58a9141
--- /dev/null
+++ b/src/main/java/org/gnunet/gns/RecordFlags.java
@@ -0,0 +1,48 @@
1/*
2 This file is part of GNUnet.
3 (C) 2014 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21package org.gnunet.gns;
22
23
24public interface RecordFlags {
25 /**
26 * No special options.
27 */
28 public static final int NONE = 0;
29 /**
30 * No special options.
31 */
32 public static final int PRIVATE = 2;
33 /**
34 * This record was added automatically by the system
35 * and is pending user confimation.
36 */
37 public static final int PENDING = 4;
38 /**
39 * This expiration time of the record is a relative
40 * time (not an absolute time).
41 */
42 public static final int EXPIRATION = 8;
43 /**
44 * This record should not be used unless all (other) records with an absolute
45 * expiration time have expired.
46 */
47 public static final int SHADOW_RECORD = 16;
48}
diff --git a/src/main/java/org/gnunet/gns/callbacks/LookupResultProcessor.java b/src/main/java/org/gnunet/gns/callbacks/LookupResultProcessor.java
new file mode 100644
index 0000000..daa6d3e
--- /dev/null
+++ b/src/main/java/org/gnunet/gns/callbacks/LookupResultProcessor.java
@@ -0,0 +1,35 @@
1/*
2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21package org.gnunet.gns.callbacks;
22
23import org.gnunet.gns.GnsRecord;
24
25/**
26 * Processor for GNS request results.
27 */
28public interface LookupResultProcessor {
29 /**
30 * Process an array of records.
31 *
32 * @param records Records to process.
33 */
34 void process(GnsRecord[] records);
35}
diff --git a/src/main/java/org/gnunet/gns/records/ARecordData.java b/src/main/java/org/gnunet/gns/records/ARecordData.java
new file mode 100644
index 0000000..1154a44
--- /dev/null
+++ b/src/main/java/org/gnunet/gns/records/ARecordData.java
@@ -0,0 +1,68 @@
1/*
2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21package org.gnunet.gns.records;
22
23
24import com.google.common.net.InetAddresses;
25import org.gnunet.construct.FixedSizeIntegerArray;
26import org.gnunet.construct.UnionCase;
27
28import java.net.Inet4Address;
29import java.net.InetAddress;
30import java.net.UnknownHostException;
31
32/**
33 * A DNS A Record.
34 */
35@UnionCase(1)
36public class ARecordData implements RecordData {
37
38 @FixedSizeIntegerArray(length = 4, bitSize = 8, signed = false)
39 public byte[] addr;
40
41 @SuppressWarnings("UnusedDeclaration")
42 public static String recordTypeString = "A";
43
44 public static ARecordData fromString(String s) {
45 InetAddress addr = InetAddresses.forString(s);
46 byte[] addrBytes = addr.getAddress();
47 if (addrBytes.length != 4) {
48 return null;
49 }
50 ARecordData recordData = new ARecordData();
51 recordData.addr = addrBytes;
52 return recordData;
53 }
54
55 @Override
56 public String asRecordString() {
57 if (addr.length != 4) {
58 return null;
59 }
60 InetAddress inetAddress;
61 try {
62 inetAddress = Inet4Address.getByAddress(addr);
63 } catch (UnknownHostException e) {
64 return null;
65 }
66 return inetAddress.getHostAddress();
67 }
68}
diff --git a/src/main/java/org/gnunet/gns/records/PkeyRecordData.java b/src/main/java/org/gnunet/gns/records/PkeyRecordData.java
new file mode 100644
index 0000000..bdbe06e
--- /dev/null
+++ b/src/main/java/org/gnunet/gns/records/PkeyRecordData.java
@@ -0,0 +1,33 @@
1package org.gnunet.gns.records;
2
3import org.gnunet.construct.NestedMessage;
4import org.gnunet.construct.UnionCase;
5import org.gnunet.util.crypto.EcdsaPublicKey;
6
7/**
8 * A GNS PKEY record.
9 */
10@UnionCase(65536)
11public class PkeyRecordData implements RecordData {
12
13 @NestedMessage
14 public EcdsaPublicKey publicKey;
15
16 @SuppressWarnings("UnusedDeclaration")
17 public static String recordTypeString = "PKEY";
18
19 public static PkeyRecordData fromString(String s) {
20 EcdsaPublicKey publicKey = EcdsaPublicKey.fromString(s);
21 if (null == publicKey) {
22 return null;
23 }
24 PkeyRecordData recordData = new PkeyRecordData();
25 recordData.publicKey = publicKey;
26 return recordData;
27 }
28
29 @Override
30 public String asRecordString() {
31 return publicKey.toString();
32 }
33}
diff --git a/src/main/java/org/gnunet/gns/records/RecordData.java b/src/main/java/org/gnunet/gns/records/RecordData.java
new file mode 100644
index 0000000..f8f317b
--- /dev/null
+++ b/src/main/java/org/gnunet/gns/records/RecordData.java
@@ -0,0 +1,30 @@
1/*
2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21package org.gnunet.gns.records;
22
23import org.gnunet.construct.MessageUnion;
24
25/**
26 * Tag interface for message for GNS record data union members.
27 */
28public interface RecordData extends MessageUnion {
29 public String asRecordString();
30}
diff --git a/src/main/java/org/gnunet/gns/records/UnknownRecordData.java b/src/main/java/org/gnunet/gns/records/UnknownRecordData.java
new file mode 100644
index 0000000..c885458
--- /dev/null
+++ b/src/main/java/org/gnunet/gns/records/UnknownRecordData.java
@@ -0,0 +1,39 @@
1/*
2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21package org.gnunet.gns.records;
22
23
24import org.gnunet.construct.IntegerFill;
25
26public class UnknownRecordData implements RecordData {
27
28 @IntegerFill(signed = true, bitSize = 8)
29 public byte[] data;
30
31 public String toString() {
32 return "(unknown record)";
33 }
34
35 @Override
36 public String asRecordString() {
37 return "(unknown)";
38 }
39}