Changeset 1733

Show
Ignore:
Timestamp:
11/14/10 16:37:20 (3 years ago)
Author:
ahu
Message:

implement CNAME handling for NAT64 & full dns record passthrough to Lua for phun and profit

Location:
branches/pdns-nat64/pdns
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/pdns-nat64/pdns/lua-pdns-recursor.cc

    r1732 r1733  
    11#include "lua-pdns-recursor.hh" 
    22#include "syncres.hh" 
     3#include <boost/foreach.hpp> 
    34 
    45#if !defined(PDNS_ENABLE_LUA) && !defined(LIBDIR) 
     
    8990   
    9091  ComboAddress prefixAddress(prefix); 
    91   ComboAddress ipv4(ret.rbegin()->content); 
    92   uint32_t tmp; 
    93   memcpy((void*)&tmp, &ipv4.sin4.sin_addr.s_addr, 4); 
    94   tmp=htonl(tmp); 
    95   memcpy(((char*)&prefixAddress.sin6.sin6_addr.s6_addr)+12, &tmp, 4); 
    96   cerr<<"Going to return: "<<prefixAddress.toString()<<endl; 
    97   lua_pushstring(lua, prefixAddress.toString().c_str()); 
     92 
     93  lua_newtable(lua); 
     94  int pos=1; 
     95   
     96  BOOST_FOREACH(DNSResourceRecord& rr, ret) 
     97  { 
     98    // row number 
     99    lua_pushnumber(lua, pos++); 
     100    // "row" table 
     101    lua_newtable(lua); 
     102     
     103    lua_pushstring(lua, rr.qname.c_str()); 
     104    lua_setfield(lua, -2, "qname");  // pushes value at the top of the stack to the table immediately below that (-1 = top, -2 is below) 
     105     
     106    lua_pushnumber(lua, rr.ttl); 
     107    lua_setfield(lua, -2, "ttl"); 
     108     
     109    if(rr.qtype.getCode() == QType::A && rr.d_place==DNSResourceRecord::ANSWER) { 
     110      lua_pushnumber(lua, QType::AAAA); 
     111      lua_setfield(lua, -2, "qtype"); 
     112     
     113      ComboAddress ipv4(rr.content); 
     114      uint32_t tmp; 
     115      memcpy((void*)&tmp, &ipv4.sin4.sin_addr.s_addr, 4); 
     116      // tmp=htonl(tmp); 
     117      memcpy(((char*)&prefixAddress.sin6.sin6_addr.s6_addr)+12, &tmp, 4); 
     118   
     119     
     120      lua_pushstring(lua, prefixAddress.toString().c_str()); 
     121      lua_setfield(lua, -2, "content"); 
     122    } 
     123    else { 
     124      lua_pushnumber(lua, rr.qtype.getCode()); 
     125      lua_setfield(lua, -2, "qtype"); 
     126     
     127     
     128      lua_pushstring(lua, rr.content.c_str()); 
     129      lua_setfield(lua, -2, "content"); 
     130    } 
     131     
     132     
     133    lua_settable(lua, -3); 
     134    cerr<<"pushed row at right number"<<pos-1<<endl; 
     135  } 
     136   
    98137  return 1; 
    99138} 
     139 
     140int resolveRecords(lua_State *lua) 
     141{ 
     142  string qname = lua_tostring(lua, 1); 
     143  uint16_t qtype = lua_tonumber(lua, 2); 
     144  cerr<<"Request from Lua to resolveRecords  '"<<qname<<"', "<<qtype<<"\n"; 
     145  vector<DNSResourceRecord> ret; 
     146  directResolve(qname, QType(qtype), 1, ret); 
     147  cerr<<"Have "<<ret.size()<<" answers for Lua"<<endl; 
     148   
     149  // make a table of tables 
     150  lua_newtable(lua); 
     151   
     152   
     153  int pos=0; 
     154  BOOST_FOREACH(const DNSResourceRecord& rr, ret) 
     155  { 
     156    // row number 
     157    lua_pushnumber(lua, ++pos); 
     158    // "row" table 
     159    lua_newtable(lua); 
     160     
     161    lua_pushstring(lua, rr.qname.c_str()); 
     162    lua_setfield(lua, -2, "qname");  // pushes value at the top of the stack to the table immediately below that (-1 = top, -2 is below) 
     163     
     164    lua_pushstring(lua, rr.content.c_str()); 
     165    lua_setfield(lua, -2, "content"); 
     166     
     167    lua_pushnumber(lua, rr.qtype.getCode()); 
     168    lua_setfield(lua, -2, "qtype"); 
     169     
     170    lua_pushnumber(lua, rr.ttl); 
     171    lua_setfield(lua, -2, "ttl"); 
     172     
     173    lua_settable(lua, -3); 
     174    cerr<<"pushed row at right number"<<pos<<endl; 
     175  } 
     176   
     177   
     178  return 1; 
     179} 
     180 
    100181 
    101182int netmaskMatchLua(lua_State *lua) 
     
    175256  lua_setglobal(d_lua, "getFakeAAAARecords"); 
    176257 
     258  lua_pushcfunction(d_lua, resolveRecords); 
     259  lua_setglobal(d_lua, "resolveRecords"); 
     260 
     261 
    177262  lua_pushcfunction(d_lua, logLua); 
    178263  lua_setglobal(d_lua, "pdnslog"); 
     
    213298bool PowerDNSLua::nodata(const ComboAddress& remote, const ComboAddress& local,const string& query, const QType& qtype, vector<DNSResourceRecord>& ret, int& res, bool* variable) 
    214299{ 
    215   vector<DNSResourceRecord> better; 
    216   directResolve(query, QType(QType::A), 1, better); 
    217   cerr<<"Had "<<better.size()<<" hits"<<endl; 
    218    
    219300  return passthrough("nodata", remote, local, query, qtype, ret, res, variable); 
    220301} 
     
    302383  int tableLen = lua_objlen(d_lua, 2); 
    303384#endif 
     385  cerr<<"Got back "<<tableLen<< " answers from Lua"<<endl; 
    304386 
    305387  for(int n=1; n < tableLen + 1; ++n) { 
  • branches/pdns-nat64/pdns/powerdns-example-script.lua

    r1732 r1733  
    6262        print ("nodata called for: ", remoteip, getlocaladdress(), domain, qtype, pdns.AAAA) 
    6363        if qtype ~= pdns.AAAA then return -1, {} end  --  only AAAA records 
    64          
    65         ipv6=getFakeAAAARecords(domain, "fe80::21b:77ff:0:0") 
    66         ret={} 
    67         ret[1]={qtype=pdns.AAAA, content=ipv6, ttl=3602} 
     64 
     65        ret=getFakeAAAARecords(domain, "fe80::21b:77ff:0:0") 
     66        print("ret[1]: ",ret[1]) 
    6867        return 0, ret 
    6968end