]> git.saurik.com Git - apt.git/commitdiff
add GetSrvRecord helper to apt-pkg/contrib/srvrec.{cc,h}
authorMichael Vogt <mvo@ubuntu.com>
Tue, 20 May 2014 11:15:51 +0000 (13:15 +0200)
committerMichael Vogt <mvo@ubuntu.com>
Tue, 20 May 2014 11:35:43 +0000 (13:35 +0200)
apt-pkg/contrib/srvrec.cc [new file with mode: 0644]
apt-pkg/contrib/srvrec.h [new file with mode: 0644]
cmdline/apt-helper.cc
cmdline/makefile

diff --git a/apt-pkg/contrib/srvrec.cc b/apt-pkg/contrib/srvrec.cc
new file mode 100644 (file)
index 0000000..d5d89fc
--- /dev/null
@@ -0,0 +1,93 @@
+// -*- mode: cpp; mode: fold -*-
+// Description                                                         /*{{{*/
+/* ######################################################################
+
+   SRV record support
+
+   ##################################################################### */
+                                                                       /*}}}*/
+#include <config.h>
+
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <resolv.h>
+
+#include <apt-pkg/error.h>
+#include "srvrec.h"
+
+bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result)
+{
+   unsigned char answer[PACKETSZ];
+   int answer_len, compressed_name_len;
+   int answer_count;
+
+   if (res_init() != 0)
+      return _error->Errno("res_init", "Failed to init resolver");
+
+   answer_len = res_query(name.c_str(), C_IN, T_SRV, answer, sizeof(answer));
+   if (answer_len < (int)sizeof(HEADER))
+      return _error->Warning("Not enough data from res_query (%i)", answer_len);
+
+   // check the header
+   HEADER *header = (HEADER*)answer;
+   if (header->rcode != NOERROR)
+      return _error->Warning("res_query returned rcode %i", header->rcode);
+   answer_count = ntohs(header->ancount);
+   if (answer_count <= 0)
+      return _error->Warning("res_query returned no answers (%i) ", answer_count);
+
+   // skip the header
+   compressed_name_len = dn_skipname(answer+sizeof(HEADER), answer+answer_len);
+   if(compressed_name_len < 0)
+      return _error->Warning("dn_skipname failed %i", compressed_name_len);
+
+   // pt points to the first answer record, go over all of them now
+   unsigned char *pt = answer+sizeof(HEADER)+compressed_name_len+QFIXEDSZ;
+   while ((int)Result.size() < answer_count && pt < answer+answer_len)
+   {
+      SrvRec rec;
+      u_int16_t type, klass, priority, weight, port, dlen;
+      char buf[MAXDNAME];
+      
+      compressed_name_len = dn_skipname(pt, answer+answer_len);
+      if (compressed_name_len < 0)
+         return _error->Warning("dn_skipname failed (2): %i",
+                                compressed_name_len);
+      pt += compressed_name_len;
+      if (((answer+answer_len) - pt) < 16)
+         return _error->Warning("packet too short");
+
+      // extract the data out of the result buffer
+      #define extract_u16(target, p) target = *p++ << 8; target |= *p++; 
+
+      extract_u16(type, pt);
+      if(type != T_SRV)
+         return _error->Warning("Unexpected type excepted %x != %x", 
+                                T_SRV, type);
+      extract_u16(klass, pt);
+      if(klass != C_IN)
+         return _error->Warning("Unexpected class excepted %x != %x", 
+                                C_IN, klass);
+      pt += 4;  // ttl
+      extract_u16(dlen, pt);
+      extract_u16(priority, pt);
+      extract_u16(weight, pt);
+      extract_u16(port, pt);
+
+      #undef extract_u16
+
+      compressed_name_len = dn_expand(answer, answer+answer_len, pt, buf, sizeof(buf));
+      if(compressed_name_len < 0)
+         return _error->Warning("dn_expand failed %i", compressed_name_len);
+      pt += compressed_name_len;
+
+      // add it to our class
+      rec.priority = priority;
+      rec.weight = weight;
+      rec.port = port;
+      rec.target = buf;
+      Result.push_back(rec);
+   }
+
+   return true;
+}
diff --git a/apt-pkg/contrib/srvrec.h b/apt-pkg/contrib/srvrec.h
new file mode 100644 (file)
index 0000000..5839077
--- /dev/null
@@ -0,0 +1,30 @@
+// -*- mode: cpp; mode: fold -*-
+// Description                                                         /*{{{*/
+/* ######################################################################
+
+   SRV record support
+  
+   ##################################################################### */
+                                                                       /*}}}*/
+#ifndef SRVREC_H
+#define SRVREC_H
+
+#include <arpa/nameser.h>
+#include <vector>
+#include <string>
+
+class SrvRec
+{
+ public:
+   std::string target;
+   u_int16_t priority;
+   u_int16_t weight;
+   u_int16_t port;
+
+   // see rfc-2782
+   //int random;
+};
+
+bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result);
+
+#endif
index 2c1107d9053e7845acaee798ac00541c87b2312b..aeeccf06d7a140563026c5ec34f2939cc7d5c2d6 100644 (file)
@@ -21,6 +21,7 @@
 #include <apt-private/private-output.h>
 #include <apt-private/private-download.h>
 #include <apt-private/private-cmndline.h>
+#include <apt-pkg/srvrec.h>
 
 #include <iostream>
 #include <string>
@@ -53,6 +54,29 @@ static bool DoDownloadFile(CommandLine &CmdL)
    return true;
 }
 
+static bool DoSrvLookup(CommandLine &CmdL)
+{
+   if (CmdL.FileSize() < 1)
+      return _error->Error(_("Must specifc at least one srv record"));
+   
+   std::vector<SrvRec> srv_records;
+   for(int i=1; CmdL.FileList[i] != NULL; i++)
+   {
+      if(GetSrvRecords(CmdL.FileList[i], srv_records) == false)
+         _error->Warning(_("GetSrvRec failed for %s"), CmdL.FileList[i]);
+      for (std::vector<SrvRec>::const_iterator I = srv_records.begin();
+           I != srv_records.end(); ++I)
+      {
+         c1out << (*I).target.c_str() << " " 
+               << (*I).priority << " " 
+               << (*I).weight << " "
+               << (*I).port << " "
+               << std::endl;
+      }
+   }
+   return true;
+}
+
 static bool ShowHelp(CommandLine &)
 {
    ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
@@ -79,6 +103,7 @@ int main(int argc,const char *argv[])                                        /*{{{*/
 {
    CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
                                   {"download-file", &DoDownloadFile},
+                                  {"srv-lookup", &DoSrvLookup},
                                    {0,0}};
 
    std::vector<CommandLine::Args> Args = getCommandArgs(
index c4a249cd6c4bbc2bc889056b1eda906677a9e8ff..a24738e63115e92362dba8817fb0460ad1f535cc 100644 (file)
@@ -49,7 +49,7 @@ include $(PROGRAM_H)
 
 # The apt-helper
 PROGRAM=apt-helper
-SLIBS = -lapt-pkg -lapt-private $(INTLLIBS)
+SLIBS = -lapt-pkg -lapt-private $(INTLLIBS) -lresolv
 LIB_MAKES = apt-pkg/makefile
 SOURCE = apt-helper.cc
 include $(PROGRAM_H)