| 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 | |
| | 139 | |
| | 140 | int 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 | |