︙ | | | ︙ | |
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#include <string.h>
#include "unistd_wrapper.h"
#import "OFDNSResolver.h"
#import "OFArray.h"
#import "OFCharacterSet.h"
#import "OFData.h"
#import "OFDictionary.h"
#import "OFFile.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFString.h"
#import "OFTimer.h"
#import "OFUDPSocket.h"
|
>
|
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#include <string.h>
#include "unistd_wrapper.h"
#import "OFDNSResolver.h"
#import "OFArray.h"
#import "OFCharacterSet.h"
#import "OFData.h"
#import "OFDate.h"
#import "OFDictionary.h"
#import "OFFile.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFString.h"
#import "OFTimer.h"
#import "OFUDPSocket.h"
|
︙ | | | ︙ | |
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
* also want to limit it to avoid DoS. Limiting it to 16 levels of pointers and
* immediately rejecting pointers to itself seems like a fair balance.
*/
#define MAX_ALLOWED_POINTERS 16
#define TIMEOUT 2
#define ATTEMPTS 3
/*
* TODO:
*
* - Resolve with each search domain
* - Fallback to TCP
*/
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
* also want to limit it to avoid DoS. Limiting it to 16 levels of pointers and
* immediately rejecting pointers to itself seems like a fair balance.
*/
#define MAX_ALLOWED_POINTERS 16
#define TIMEOUT 2
#define ATTEMPTS 3
#if defined(OF_HAIKU)
# define HOSTS_PATH @"/system/settings/network/hosts"
# define RESOLV_CONF_PATH @"/system/settings/network/resolv.conf"
#elif defined(OF_MORPHOS)
# define HOSTS_PATH @"ENV:sys/net/hosts"
# define RESOLV_CONF_PATH @"ENV:sys/net/resolv.conf"
#elif defined(OF_AMIGAOS4)
# define HOSTS_PATH @"DEVS:Internet/hosts"
# define RESOLV_CONF_PATH @"DEVS:Internet/resolv.conf"
#elif defined(OF_AMIGAOS)
# define HOSTS_PATH @"AmiTCP:db/hosts"
# define RESOLV_CONF_PATH @"AmiTCP:db/resolv.conf"
#else
# define HOSTS_PATH @"/etc/hosts"
# define RESOLV_CONF_PATH @"/etc/resolv.conf"
#endif
/*
* TODO:
*
* - Resolve with each search domain
* - Fallback to TCP
*/
|
︙ | | | ︙ | |
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains
target: (id)target
selector: (SEL)selector
context: (id)context;
@end
@interface OFDNSResolver ()
#ifdef OF_HAVE_FILES
- (void)of_parseHosts: (OFString *)path;
# ifndef OF_WINDOWS
- (void)of_parseResolvConf: (OFString *)path;
- (void)of_parseResolvConfOption: (OFString *)option;
# endif
#endif
#ifdef OF_WINDOWS
- (void)of_parseNetworkParams;
#endif
- (void)of_sendQuery: (OFDNSResolverQuery *)query;
- (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query;
- (size_t)of_socket: (OFUDPSocket *)sock
didSendBuffer: (void **)buffer
bytesSent: (size_t)bytesSent
receiver: (of_socket_address_t *)receiver
context: (OFDNSResolverQuery *)query
|
>
>
|
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
searchDomains: (OFArray OF_GENERIC(OFString *) *)searchDomains
target: (id)target
selector: (SEL)selector
context: (id)context;
@end
@interface OFDNSResolver ()
- (void)of_parseConfig;
#ifdef OF_HAVE_FILES
- (void)of_parseHosts: (OFString *)path;
# ifndef OF_WINDOWS
- (void)of_parseResolvConf: (OFString *)path;
- (void)of_parseResolvConfOption: (OFString *)option;
# endif
#endif
#ifdef OF_WINDOWS
- (void)of_parseNetworkParams;
#endif
- (void)of_reloadConfig;
- (void)of_sendQuery: (OFDNSResolverQuery *)query;
- (void)of_queryWithIDTimedOut: (OFDNSResolverQuery *)query;
- (size_t)of_socket: (OFUDPSocket *)sock
didSendBuffer: (void **)buffer
bytesSent: (size_t)bytesSent
receiver: (of_socket_address_t *)receiver
context: (OFDNSResolverQuery *)query
|
︙ | | | ︙ | |
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
|
}
@end
@implementation OFDNSResolver
@synthesize staticHosts = _staticHosts, nameServers = _nameServers;
@synthesize localDomain = _localDomain, searchDomains = _searchDomains;
@synthesize minNumberOfDotsInAbsoluteName = _minNumberOfDotsInAbsoluteName;
@synthesize usesTCP = _usesTCP;
+ (instancetype)resolver
{
return [[[self alloc] init] autorelease];
}
- (instancetype)init
{
self = [super init];
@try {
void *pool = objc_autoreleasePoolPush();
#ifdef OF_WINDOWS
OFString *path;
#endif
_minNumberOfDotsInAbsoluteName = 1;
#ifdef OF_HAVE_FILES
# if defined(OF_WINDOWS)
path = [[OFWindowsRegistryKey localMachineKey]
stringForValue: @"DataBasePath"
subKeyPath: @"SYSTEM\\CurrentControlSet\\Services\\"
@"Tcpip\\Parameters"];
path = [path stringByAppendingPathComponent: @"hosts"];
if (path != nil)
[self of_parseHosts: path];
# elif defined(OF_HAIKU)
[self of_parseHosts: @"/boot/common/settings/network/hosts"];
# elif defined(OF_MORPHOS)
[self of_parseHosts: @"ENVARC:sys/net/hosts"];
# elif defined(OF_AMIGAOS4)
[self of_parseHosts: @"DEVS:Internet/hosts"];
# elif defined(OF_AMIGAOS)
[self of_parseHosts: @"AmiTCP:db/hosts"];
# else
[self of_parseHosts: @"/etc/hosts"];
# endif
# if defined(OF_MORPHOS)
[self of_parseResolvConf: @"ENV:sys/net/resolv.conf"];
# elif !defined(OF_WINDOWS)
[self of_parseResolvConf: @"/etc/resolv.conf"];
[self of_parseResolvConf: @"/etc/resolv.conf.tail"];
# endif
#endif
#ifdef OF_WINDOWS
[self of_parseNetworkParams];
#endif
if (_staticHosts == nil) {
OFArray *localhost =
#ifdef OF_HAVE_IPV6
localhost = [OFArray arrayWithObjects:
@"::1", @"127.0.0.1", nil];
#else
localhost = [OFArray arrayWithObject: @"127.0.0.1"];
#endif
_staticHosts = [[OFDictionary alloc]
initWithObject: localhost
forKey: @"localhost"];
}
if (_nameServers == nil)
#ifdef OF_HAVE_IPV6
_nameServers = [[OFArray alloc]
initWithObjects: @"127.0.0.1", @"::1", nil];
#else
_nameServers = [[OFArray alloc]
initWithObject: @"127.0.0.1"];
#endif
if (_localDomain == nil)
_localDomain = [domainFromHostname() copy];
if (_searchDomains == nil) {
if (_localDomain != nil)
_searchDomains = [[OFArray alloc]
initWithObject: _localDomain];
else
_searchDomains = [[OFArray alloc] init];
}
_queries = [[OFMutableDictionary alloc] init];
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[self close];
[_staticHosts release];
[_nameServers release];
[_localDomain release];
[_searchDomains release];
[_queries release];
[_IPv4Socket release];
#ifdef OF_HAVE_IPV6
[_IPv6Socket release];
#endif
[super dealloc];
}
#ifdef OF_HAVE_FILES
- (void)of_parseHosts: (OFString *)path
{
|
|
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
|
}
@end
@implementation OFDNSResolver
@synthesize staticHosts = _staticHosts, nameServers = _nameServers;
@synthesize localDomain = _localDomain, searchDomains = _searchDomains;
@synthesize minNumberOfDotsInAbsoluteName = _minNumberOfDotsInAbsoluteName;
@synthesize usesTCP = _usesTCP, configReloadInterval = _configReloadInterval;
+ (instancetype)resolver
{
return [[[self alloc] init] autorelease];
}
- (instancetype)init
{
self = [super init];
@try {
_queries = [[OFMutableDictionary alloc] init];
[self of_parseConfig];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)of_parseConfig
{
void *pool = objc_autoreleasePoolPush();
#ifdef OF_WINDOWS
OFString *path;
#endif
_minNumberOfDotsInAbsoluteName = 1;
_usesTCP = false;
_configReloadInterval = 2;
#if defined(OF_WINDOWS)
# ifdef OF_HAVE_FILES
path = [[OFWindowsRegistryKey localMachineKey]
stringForValue: @"DataBasePath"
subKeyPath: @"SYSTEM\\CurrentControlSet\\Services\\"
@"Tcpip\\Parameters"];
path = [path stringByAppendingPathComponent: @"hosts"];
if (path != nil)
[self of_parseHosts: path];
# endif
[self of_parseNetworkParams];
#elif defined(OF_HAVE_FILES)
[self of_parseHosts: HOSTS_PATH];
# ifdef OF_OPENBSD
[self of_parseHosts: @"/etc/resolv.conf.tail"];
# endif
[self of_parseResolvConf: RESOLV_CONF_PATH];
#endif
if (_staticHosts == nil) {
OFArray *localhost =
#ifdef OF_HAVE_IPV6
[OFArray arrayWithObjects: @"::1", @"127.0.0.1", nil];
#else
[OFArray arrayWithObject: @"127.0.0.1"];
#endif
_staticHosts = [[OFDictionary alloc]
initWithObject: localhost
forKey: @"localhost"];
}
if (_nameServers == nil)
#ifdef OF_HAVE_IPV6
_nameServers = [[OFArray alloc]
initWithObjects: @"127.0.0.1", @"::1", nil];
#else
_nameServers = [[OFArray alloc] initWithObject: @"127.0.0.1"];
#endif
if (_localDomain == nil)
_localDomain = [domainFromHostname() copy];
if (_searchDomains == nil) {
if (_localDomain != nil)
_searchDomains = [[OFArray alloc]
initWithObject: _localDomain];
else
_searchDomains = [[OFArray alloc] init];
}
_lastConfigReload = [[OFDate alloc] init];
objc_autoreleasePoolPop(pool);
}
- (void)dealloc
{
[self close];
[_staticHosts release];
[_nameServers release];
[_localDomain release];
[_searchDomains release];
[_lastConfigReload release];
[_IPv4Socket release];
#ifdef OF_HAVE_IPV6
[_IPv6Socket release];
#endif
[_queries release];
[super dealloc];
}
#ifdef OF_HAVE_FILES
- (void)of_parseHosts: (OFString *)path
{
|
︙ | | | ︙ | |
954
955
956
957
958
959
960
961
962
963
964
965
966
967
|
if ([localDomain length] > 0)
_localDomain = [localDomain copy];
objc_autoreleasePoolPop(pool);
}
#endif
- (void)asyncResolveHost: (OFString *)host
target: (id)target
selector: (SEL)selector
context: (id)context
{
[self asyncResolveHost: host
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
|
if ([localDomain length] > 0)
_localDomain = [localDomain copy];
objc_autoreleasePoolPop(pool);
}
#endif
- (void)of_reloadConfig
{
/*
* TODO: Rather than reparsing every, check what actually changed
* (mtime) and only reset those.
*/
if (_lastConfigReload != nil && _configReloadInterval > 0 &&
[_lastConfigReload timeIntervalSinceNow] < _configReloadInterval)
return;
[_staticHosts release];
_staticHosts = nil;
[_nameServers release];
_nameServers = nil;
[_localDomain release];
_localDomain = nil;
[_searchDomains release];
_searchDomains = nil;
_minNumberOfDotsInAbsoluteName = 1;
_usesTCP = false;
_configReloadInterval = 2;
[_lastConfigReload release];
_lastConfigReload = nil;
[self of_parseConfig];
}
- (void)asyncResolveHost: (OFString *)host
target: (id)target
selector: (SEL)selector
context: (id)context
{
[self asyncResolveHost: host
|
︙ | | | ︙ | |
979
980
981
982
983
984
985
986
987
988
989
990
991
992
|
selector: (SEL)selector
context: (id)context
{
void *pool = objc_autoreleasePoolPush();
OFNumber *ID;
OFString *domainName;
OFDNSResolverQuery *query;
/* Random, unused ID */
do {
ID = [OFNumber numberWithUInt16: (uint16_t)of_random()];
} while ([_queries objectForKey: ID] != nil);
if ([host hasSuffix: @"."])
|
>
>
|
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
|
selector: (SEL)selector
context: (id)context
{
void *pool = objc_autoreleasePoolPush();
OFNumber *ID;
OFString *domainName;
OFDNSResolverQuery *query;
[self of_reloadConfig];
/* Random, unused ID */
do {
ID = [OFNumber numberWithUInt16: (uint16_t)of_random()];
} while ([_queries objectForKey: ID] != nil);
if ([host hasSuffix: @"."])
|
︙ | | | ︙ | |