/soc/2012/michael/android: c119ad50f4e2: Implemented TXT DNS que...

Michael Zangl michael at soc.pidgin.im
Wed Aug 15 06:14:16 EDT 2012


Changeset: c119ad50f4e2fd765bea687e416ddf5f09be5608
Author:	 Michael Zangl <michael at soc.pidgin.im>
Date:	 2012-08-15 12:13 +0200
Branch:	 soc.2012.android
URL: http://hg.pidgin.im/soc/2012/michael/android/rev/c119ad50f4e2

Description:

Implemented TXT DNS queries

diffstat:

 android/workspace/im.pidgin.libpurple/native/SrvTxtQueryResolvedCallback.c                                     |  56 ++++++++-
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/DnsTxtResolver.java              |  44 +++++++
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/SrvTxtQuery.java                 |  22 ++-
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/SrvTxtQueryResolvedCallback.java |   7 +-
 android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/TxtResponse.java                 |  14 ++
 5 files changed, 125 insertions(+), 18 deletions(-)

diffs (215 lines):

diff --git a/android/workspace/im.pidgin.libpurple/native/SrvTxtQueryResolvedCallback.c b/android/workspace/im.pidgin.libpurple/native/SrvTxtQueryResolvedCallback.c
--- a/android/workspace/im.pidgin.libpurple/native/SrvTxtQueryResolvedCallback.c
+++ b/android/workspace/im.pidgin.libpurple/native/SrvTxtQueryResolvedCallback.c
@@ -51,6 +51,39 @@ convert_srv_response(JNIEnv *env, jobjec
 	return response;
 }
 
+PurpleTxtResponse *
+convert_txt_response(JNIEnv *env, jobject object)
+{
+	static JavaMethodIDCache getTxtCache =
+			METHOD_CACHE("getTxt", "()Ljava/lang/String;");
+
+	PurpleTxtResponse *response;
+	jclass java_class;
+	jmethodID getTxtMid;
+	jstring txt;
+	const char* txt_native;
+
+	g_return_val_if_fail(object != NULL, NULL);
+	java_class = (*env)->GetObjectClass(env, object);
+	g_return_val_if_fail(java_class != NULL, NULL);
+
+	getTxtMid = getMethodIDCachedReferenced(env, java_class, &getTxtCache);
+	g_return_val_if_fail(getTxtMid != NULL, NULL);
+
+	txt = (*env)->CallObjectMethod(env, object, getTxtMid);
+	g_return_val_if_fail(txt != NULL, NULL);
+	txt_native = (*env)->GetStringUTFChars(env, txt, NULL);
+	g_return_val_if_fail(txt_native != NULL, NULL);
+
+	response = g_new(PurpleTxtResponse, 1);
+
+	response->content = g_strdup(txt_native);
+
+	(*env)->ReleaseStringUTFChars(env, txt, txt_native);
+
+	return response;
+}
+
 /*
  * Class:     im_pidgin_libpurple_core_dns_srvtxt_SrvTxtQueryResolvedCallback
  * Method:    call_native
@@ -64,7 +97,8 @@ Java_im_pidgin_libpurple_core_dns_srvtxt
 	jsize length;
 	jsize i;
 	jobject entry;
-	PurpleSrvResponse *entry_native;
+	PurpleSrvResponse *entry_native_srv;
+	PurpleTxtResponse *entry_native_txt;
 	GList *result_native;
 
 	resolvedCallback = getNativePointer(env, object);
@@ -72,17 +106,27 @@ Java_im_pidgin_libpurple_core_dns_srvtxt
 	g_return_if_fail(data_native != NULL);
 	result_native = NULL;
 	length = (*env)->GetArrayLength(env, result);
-	g_log("jni", G_LOG_LEVEL_DEBUG, "converting %d srv entries", length);
 
 	if (purple_srv_txt_query_get_type(data_native) == PurpleDnsTypeSrv) {
+		g_log("jni", G_LOG_LEVEL_DEBUG, "converting %d srv entries", length);
 		for (i = 0; i < length; ++i) {
 			entry = (*env)->GetObjectArrayElement(env, result, i);
-			entry_native = convert_srv_response(env, entry);
+			entry_native_srv = convert_srv_response(env, entry);
 
-			g_return_if_fail(entry_native != NULL);
-			g_log("jni", G_LOG_LEVEL_DEBUG, "converted srv: %d %d %d %s", entry_native->pref, entry_native->weight, entry_native->port,entry_native->hostname );
+			g_return_if_fail(entry_native_srv != NULL);
+			g_log("jni", G_LOG_LEVEL_DEBUG, "converted srv: %d %d %d %s", entry_native_srv->pref, entry_native_srv->weight, entry_native_srv->port,entry_native_srv->hostname );
 			result_native = g_list_append(result_native,
-					entry_native);
+					entry_native_srv);
+		}
+	} else if (purple_srv_txt_query_get_type(data_native) == PurpleDnsTypeTxt) {
+		g_log("jni", G_LOG_LEVEL_DEBUG, "converting %d srv txt", length);
+		for (i = 0; i < length; ++i) {
+			entry = (*env)->GetObjectArrayElement(env, result, i);
+			entry_native_txt = convert_txt_response(env, entry);
+
+			g_return_if_fail(entry_native_txt != NULL);
+			result_native = g_list_append(result_native,
+					entry_native_txt);
 		}
 	} else {
 		g_return_if_reached();
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/DnsTxtResolver.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/DnsTxtResolver.java
new file mode 100644
--- /dev/null
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/DnsTxtResolver.java
@@ -0,0 +1,44 @@
+package im.pidgin.libpurple.core.dns.srvtxt;
+
+import im.pidgin.libpurple.core.dns.DnsExeption;
+import im.pidgin.libpurple.core.dns.DnsResolver;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.List;
+
+public class DnsTxtResolver extends DnsResolver<TxtResponse> {
+	private static final int TXT_CLASS = 1;
+	private static final int TXT_TYPE = 0x10;
+	
+	private final String host;
+
+	public DnsTxtResolver(String host) {
+		this.host = host;
+	}
+
+	@Override
+	protected TxtResponse readAnswer(InputStream inputStream) throws DnsExeption, IOException {read16(inputStream);
+		int typeid = read16(inputStream);
+		int classid = read16(inputStream);
+		if (typeid != TXT_TYPE || classid != TXT_CLASS) {
+			throw new DnsExeption("Unexpected type.");
+		}
+		
+		inputStream.skip(4);
+		
+		int length = read16(inputStream);
+		byte[] txtData = new byte[length];
+		inputStream.read(txtData );
+		String txt = new String(txtData);
+		
+		return new TxtResponse(txt);
+	}
+
+	@Override
+	protected List<Query> getQueries() {
+		return Collections.singletonList(new Query(host, TXT_TYPE, TXT_CLASS));
+	}
+
+}
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/SrvTxtQuery.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/SrvTxtQuery.java
--- a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/SrvTxtQuery.java
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/SrvTxtQuery.java
@@ -2,6 +2,7 @@ package im.pidgin.libpurple.core.dns.srv
 
 import im.pidgin.libpurple.constants.DnsSrvConstants;
 import im.pidgin.libpurple.core.dns.DnsExeption;
+import im.pidgin.libpurple.core.dns.DnsResolver;
 
 import java.util.List;
 
@@ -21,17 +22,22 @@ public class SrvTxtQuery implements Runn
 
 	@Override
 	public void run() {
+		DnsResolver<?> resolver;
 		if (data.getType() == DnsSrvConstants.PurpleDnsTypeSrv) {
-			DnsSrvResolver resolver = new DnsSrvResolver(data.getQuery());
-			try {
-				List<SrvResponse> response = resolver.query();
-				resolvedCallback.call(response);
-			} catch (DnsExeption e) {
-				e.printStackTrace();
-				failedCallback.call(e.getLocalizedMessage());
-			}
+			resolver = new DnsSrvResolver(data.getQuery());
+		} else if (data.getType() == DnsSrvConstants.PurpleDnsTypeTxt) {
+			resolver = new DnsTxtResolver(data.getQuery());
 		} else {
 			failedCallback.call("Dns query type unknown.");
+			return;
+		}
+
+		try {
+			List<?> response = resolver.query();
+			resolvedCallback.call(response);
+		} catch (DnsExeption e) {
+			e.printStackTrace();
+			failedCallback.call(e.getLocalizedMessage());
 		}
 	}
 
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/SrvTxtQueryResolvedCallback.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/SrvTxtQueryResolvedCallback.java
--- a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/SrvTxtQueryResolvedCallback.java
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/SrvTxtQueryResolvedCallback.java
@@ -14,12 +14,11 @@ public class SrvTxtQueryResolvedCallback
 		this.data = data;
 	}
 
-	public void call(List<SrvResponse> response) {
-		SrvResponse[] array = response
-				.toArray(new SrvResponse[response.size()]);
+	public void call(List<?> response) {
+		Object[] array = response.toArray();
 		call_native(data, array);
 	}
 
-	private native void call_native(SrvTxtQueryData data2, SrvResponse[] array);
+	private native void call_native(SrvTxtQueryData data2, Object[] array);
 
 }
diff --git a/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/TxtResponse.java b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/TxtResponse.java
new file mode 100644
--- /dev/null
+++ b/android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/core/dns/srvtxt/TxtResponse.java
@@ -0,0 +1,14 @@
+package im.pidgin.libpurple.core.dns.srvtxt;
+
+public class TxtResponse {
+
+	private final String txt;
+
+	public TxtResponse(String txt) {
+		this.txt = txt;
+	}
+	
+	public String getTxt() {
+		return txt;
+	}
+}



More information about the Commits mailing list