• R/O
  • SSH
  • HTTPS

util: Commit


Commit MetaInfo

Revisión150 (tree)
Tiempo2018-02-27 20:04:33
Autorhirukawa_ryo

Log Message

* trick-dns 0.2
QTYPE=28(AAAA) IPv6アドレスの解決にも対応しました。

Cambiar Resumen

Diferencia incremental

--- trick-dns/trunk/trick-dns/trick-dns.c (revision 149)
+++ trick-dns/trunk/trick-dns/trick-dns.c (revision 150)
@@ -5,10 +5,12 @@
55 #include <WinSock2.h>
66 #include <WS2tcpip.h>
77 #include <MSWSock.h>
8+#include <iphlpapi.h>
89 #include <stdio.h>
910 #include <stdlib.h>
1011
1112 #pragma comment(lib, "Ws2_32.lib")
13+#pragma comment(lib, "IPHLPAPI.lib")
1214
1315 #define PORT_DNS 53
1416 #define TTL 86400
@@ -18,8 +20,9 @@
1820 int Initialize();
1921 SOCKET Bind(USHORT port);
2022 int RecvMsg(SOCKET s, BYTE* buf, int len, SOCKADDR_IN* src, IN_ADDR* dst);
21-int Process(BYTE* buf, int len, int size, IN_ADDR dst);
23+int Process(BYTE* buf, int len, int size, IN_ADDR* dst);
2224 LPFN_WSARECVMSG GetWSARecvMsg();
25+int GetIPv6Address(const IN_ADDR* dst, IN6_ADDR* inet6_addr);
2326
2427 DWORD InstallService();
2528 void StartServiceMain();
@@ -33,7 +36,6 @@
3336 SERVICE_STATUS service_status = { 0 };
3437 SERVICE_STATUS_HANDLE hStatus;
3538
36-
3739 int main(int argc, char* argv[])
3840 {
3941 SOCKET s = (SOCKET)NULL;
@@ -44,11 +46,6 @@
4446 IN_ADDR dst;
4547 int ret;
4648
47- /*
48- StopServiceMain();
49- if (0 == 0)return;
50- */
51-
5249 if (argc >= 2)
5350 {
5451 if (_strcmpi(argv[1], "-install") == 0)
@@ -86,10 +83,10 @@
8683 for(;;)
8784 {
8885 size = RecvMsg(s, buf, len, &src, &dst);
89- printf("size=%d\n", size);
86+ //printf("size=%d\n", size);
9087 if (size >= 12) //DNSヘッダーのサイズである12バイト以上のデータがあるときは処理する (12バイト未満のときは何も応答しない)
9188 {
92- size = Process(buf, len, size, dst);
89+ size = Process(buf, len, size, &dst);
9390 if (size > 0)
9491 {
9592 sendto(s, buf, size, 0, (SOCKADDR*)&src, sizeof(src));
@@ -229,7 +226,7 @@
229226 return size;
230227 }
231228
232-int Process(BYTE* buf, int len, int size, IN_ADDR dst)
229+int Process(BYTE* buf, int len, int size, IN_ADDR* dst)
233230 {
234231 int QUESTION_POS = 12;
235232 int QUESTION_SIZE;
@@ -253,7 +250,7 @@
253250 int pos;
254251 int QTYPE;
255252 int QCLASS;
256- u_short RDLENGTH = 4;
253+ u_short RDLENGTH;
257254 u_long RDATA;
258255
259256 buf[2] |= 0x80; //QR = 1 (レスポンス)
@@ -316,7 +313,7 @@
316313 buf[3] |= 1; //Rcode=1 フォーマットエラー
317314 return size;
318315 }
319- printf("%s\n", QNAME);
316+ //printf("%s\n", QNAME);
320317
321318 if (pos + 4 > size) //QNAMEの後にQTYPE(2バイト)、QCLASS(2バイト)が付いているので、その分のデータ長があるか確認する
322319 {
@@ -327,7 +324,7 @@
327324 QTYPE = buf[pos + 0] << 8 | buf[pos + 1];
328325 QCLASS = buf[pos + 2] << 8 | buf[pos + 3];
329326
330- if (QTYPE != 1) //Aレコード以外
327+ if (QTYPE != 1 && QTYPE != 28) //Aレコード(1)、AAAAレコード(28)以外
331328 {
332329 buf[3] |= 4; //Rcode=4 未実装
333330 return size;
@@ -346,18 +343,39 @@
346343 u_long tmp = htonl(TTL);
347344 memcpy(buf + pos + 0, &tmp, 4);
348345 }
346+
347+ if(QTYPE == 1) //Aレコード(IPv4)
349348 {
350- u_short tmp = htons(RDLENGTH);
349+ u_short tmp;
350+
351+ RDLENGTH = sizeof(IN_ADDR); // RDLENGTH = 4
352+ tmp = htons(RDLENGTH);
351353 memcpy(buf + pos + 4, &tmp, 2);
354+ //RDATA = htonl(*(u_long*)dst);
355+ memcpy(buf + pos + 6, dst, RDLENGTH);
352356 }
357+ else if(QTYPE == 28) //AAAAレコード(IPv6)
358+ {
359+ u_short tmp;
360+ IN6_ADDR inet6_addr;
361+ int ret;
353362
354- RDATA = htonl(*(u_long*)&dst);
355- memcpy(buf + pos + 6, &dst, 4);
363+ RDLENGTH = sizeof(IN6_ADDR); // RDLENGTH = 16
364+ tmp = htons(RDLENGTH);
365+ memcpy(buf + pos + 4, &tmp, 2);
366+ ret = GetIPv6Address(dst, &inet6_addr);
367+ if (ret != 0)
368+ {
369+ buf[3] |= 4; //Rcode=4 未実装
370+ return size;
371+ }
372+ memcpy(buf + pos + 6, &inet6_addr, RDLENGTH);
373+ }
356374
357375 buf[2] |= 0x04; //AA(Authoritative Answer)ビットを立てます。
358376 buf[7] = 1; //ANCount(Answer count) = 1
359377
360- return (pos + 10);
378+ return (pos + 6 + RDLENGTH);
361379 }
362380
363381
@@ -385,6 +403,92 @@
385403 }
386404
387405
406+int GetIPv6Address(const IN_ADDR* dst, IN6_ADDR* inet6_addr)
407+{
408+ DWORD ret = 0;
409+ PIP_ADAPTER_ADDRESSES pAddresses = NULL;
410+ ULONG bufSize = 16384;
411+
412+ pAddresses = (IP_ADAPTER_ADDRESSES*)malloc(bufSize);
413+ if (pAddresses == NULL)
414+ {
415+ return 1;
416+ }
417+ ret = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &bufSize);
418+ if (ret == ERROR_BUFFER_OVERFLOW)
419+ {
420+ free(pAddresses);
421+ pAddresses = (IP_ADAPTER_ADDRESSES*)malloc(bufSize);
422+ if (pAddresses == NULL)
423+ {
424+ return 1;
425+ }
426+ ret = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &bufSize);
427+ }
428+
429+ if (ret == NO_ERROR)
430+ {
431+ PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses;
432+
433+ while (pCurrAddresses) {
434+ IN_ADDR* _inet4_addr = NULL;
435+ IN6_ADDR* _inet6_addr = NULL;
436+
437+ if (pCurrAddresses->Ipv4Enabled && pCurrAddresses->Ipv6Enabled)
438+ {
439+ PIP_ADAPTER_UNICAST_ADDRESS pUnicast = pCurrAddresses->FirstUnicastAddress;
440+
441+ if (pUnicast != NULL) {
442+ int i;
443+
444+ for (i = 0; pUnicast != NULL; i++)
445+ {
446+ SOCKADDR* sa = pUnicast->Address.lpSockaddr;
447+
448+ if (sa->sa_family == AF_INET)
449+ {
450+ SOCKADDR_IN* sa_inet4 = (SOCKADDR_IN*)sa;
451+
452+ if (memcmp(&sa_inet4->sin_addr, dst, sizeof(IN_ADDR)) == 0)
453+ {
454+ _inet4_addr = &sa_inet4->sin_addr;
455+ }
456+ }
457+ else if (sa->sa_family == AF_INET6)
458+ {
459+ SOCKADDR_IN6* sa_inet6 = (SOCKADDR_IN6*)sa;
460+
461+ if (_inet6_addr == NULL)
462+ {
463+ _inet6_addr = &sa_inet6->sin6_addr;
464+ }
465+ }
466+ pUnicast = pUnicast->Next;
467+ }
468+ }
469+ }
470+
471+ if (_inet4_addr != NULL)
472+ {
473+ if (_inet6_addr != NULL)
474+ {
475+ memcpy(inet6_addr, _inet6_addr, sizeof(IN6_ADDR));
476+ break;
477+ }
478+ else
479+ {
480+ ret = 2;
481+ }
482+ }
483+
484+ pCurrAddresses = pCurrAddresses->Next;
485+ }
486+ }
487+ free(pAddresses);
488+ return ret;
489+}
490+
491+
388492 DWORD InstallService()
389493 {
390494 int BUF_SIZE = 2048;
@@ -539,7 +643,7 @@
539643
540644 if (size >= 12) //DNSヘッダーのサイズである12バイト以上のデータがあるときは処理する (12バイト未満のときは何も応答しない)
541645 {
542- size = Process(buf, len, size, dst);
646+ size = Process(buf, len, size, &dst);
543647 if (size > 0)
544648 {
545649 sendto(s, buf, size, 0, (SOCKADDR*)&src, sizeof(src));
Show on old repository browser