├── .gitignore ├── LICENSE ├── README.md └── src ├── Makefile └── nss_dev_tld.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Libraries 8 | *.lib 9 | *.a 10 | 11 | # Shared objects (inc. Windows DLLs) 12 | *.dll 13 | *.so 14 | *.so.* 15 | *.dylib 16 | 17 | # Executables 18 | *.exe 19 | *.out 20 | *.app 21 | *.i*86 22 | *.x86_64 23 | *.hex 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 M N Islam Shihan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dev-tld-resolver 2 | 3 | Simple top level domain (NSSwitch hosts) resolver for linux based development environment. 4 | 5 | # Supported Linux Distributions 6 | 7 | This tool is developed in a Ubuntu 12.04 system & tested in later versions of Ubuntu & Ubuntu derivatives. Even though not tested, it should also work in other Linux distributions that supports/uses [nsswitch.conf](http://man7.org/linux/man-pages/man5/nsswitch.conf.5.html) file based configuration. In general, if your Linux installation has a file named **nsswitch.conf** in **/etc/** folder, then you should be ready to go with using this tool. 8 | 9 | # Installation 10 | 11 | ## Ubuntu 12 | 13 | - Make sure you have "build-essential" & "git" packages installed. You can install these using following command 14 | ```bash 15 | sudo apt-get install build-essentials git 16 | ``` 17 | - Clone this repository somewhere in your hard drive 18 | ```bash 19 | git clone https://github.com/mnishihan/dev-tld-resolver.git 20 | ``` 21 | - Run following command to build & install the tool 22 | ``` 23 | cd dev-tld-resolver/src && make 24 | sudo make install 25 | ``` 26 | 27 | > It's very important that you run the ```make install``` command as **root** or using **sudo**, otherwise installation will fail. 28 | 29 | - As root (or in super user mode with sudo) also edit **/etc/environment** using a text editor of your choice and export a global environment variable named **DEV_TLD_DOMAINS** with comma separated list of Top Level Domains (**tld**) that you want to resolve to ```127.0.0.1``` automatically. For example, if you want ```.dev```, ```.wp```, ```.dpl``` top level domains to be resolved by **dev-tld-resolver**, your **/etc/environment** should have following line within it somewhere. 30 | ``` 31 | DEV_TLD_DOMAINS=dev,wp,dpl 32 | ``` 33 | 34 | > Above step is optional if you don't need **dev-tld-resolver** to resolve top level domains other than ```.dev```, which is the default 35 | 36 | - Lastly as root (or in super user mode with sudo) edit ```/etc/nsswitch.conf``` file and append ```dev_tld``` to the line starting with ```hosts:```. 37 | 38 | > If you have following line starting with ```hosts:``` in ```/etc/nsswitch.conf``` file 39 | > ``` 40 | > hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4 41 | > ``` 42 | > then you should change it to look like 43 | > ``` 44 | > hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4 dev_tld 45 | > ``` 46 | > If you experience some sort of delay while resolving a host name, you should try to move ```dev_tld``` before dns, as follows 47 | > ``` 48 | > hosts: files mdns4_minimal [NOTFOUND=return] dev_tld dns mdns4 49 | > ``` 50 | 51 | - Now logout or reboot your system and login again. After logging in into the system, open a command line and type following command 52 | ``` 53 | ping test.dev 54 | ``` 55 | If ping is successful, then **dev-tld-resolver** is installed & configured correctly. 56 | 57 | # Credits 58 | 59 | Code is borrowed & modified from prax by ysbaddaden at https://github.com/ysbaddaden/prax 60 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-O2 3 | 4 | OBJ=nss_dev_tld.o 5 | 6 | %.o: %.c 7 | $(CC) -Wall -fPIC -c -o $@ $< $(CFLAGS) 8 | 9 | libnss_dev_tld.so.2: $(OBJ) 10 | $(CC) -shared -o $@ $^ -Wl,-soname,libnss_dev_tld.so.2 $(CFLAGS) 11 | 12 | clean: 13 | rm -f *.o *~ libnss_dev_tld.so.2 14 | 15 | install: libnss_dev_tld.so.2 16 | cp --remove-destination $(CURDIR)/libnss_dev_tld.so.2 /lib 17 | 18 | uninstall: 19 | rm /lib/libnss_dev_tld.so.2 20 | -------------------------------------------------------------------------------- /src/nss_dev_tld.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | // declarations 10 | 11 | void dev_tld_fill_hostent(const char *name, 12 | int af, 13 | struct hostent *result); 14 | 15 | enum nss_status 16 | _nss_dev_tld_gethostbyname2_r(const char *name, 17 | int af, 18 | struct hostent *result, 19 | char *buffer, 20 | size_t buflen, 21 | int *errnop, 22 | int *h_errnop); 23 | 24 | 25 | enum nss_status 26 | _nss_dev_tld_gethostbyname_r(const char *name, 27 | struct hostent *result, 28 | char *buffer, 29 | size_t buflen, 30 | int *errnop, 31 | int *h_errnop); 32 | 33 | 34 | enum nss_status 35 | _nss_dev_tld_gethostbyaddr_r(const void *addr, 36 | int len, 37 | int af, 38 | struct hostent *result, 39 | char *buffer, 40 | size_t buflen, 41 | int *errnop, 42 | int *h_errnop); 43 | 44 | 45 | // definitions 46 | 47 | void 48 | dev_tld_fill_hostent(const char *name, 49 | int af, 50 | struct hostent *result) 51 | { 52 | result->h_name = malloc(sizeof(char) * strlen(name) + 1); 53 | strcpy(result->h_name, name); 54 | 55 | result->h_aliases = malloc(sizeof(char *)); 56 | *result->h_aliases = NULL; 57 | 58 | result->h_addr_list = malloc(sizeof(char *) * 2); 59 | 60 | switch (af) { 61 | case AF_INET: 62 | result->h_addrtype = AF_INET; 63 | result->h_length = INADDRSZ; 64 | in_addr_t addr = inet_addr("127.0.0.1"); 65 | *result->h_addr_list = malloc(sizeof(addr)); 66 | memcpy(*result->h_addr_list, &addr, sizeof(addr)); 67 | break; 68 | case AF_INET6: 69 | result->h_addrtype = AF_INET6; 70 | result->h_length = IN6ADDRSZ; 71 | struct in6_addr addr6 = {}; 72 | inet_pton(AF_INET6, "::1", &addr6); 73 | *result->h_addr_list = malloc(sizeof(addr6)); 74 | memcpy(*result->h_addr_list, &addr6, sizeof(addr6)); 75 | break; 76 | } 77 | 78 | *(result->h_addr_list + 1) = NULL; 79 | } 80 | 81 | enum nss_status 82 | _nss_dev_tld_gethostbyname2_r(const char *name, 83 | int af, 84 | struct hostent *result, 85 | char *buffer, 86 | size_t buflen, 87 | int *errnop, 88 | int *h_errnop) 89 | { 90 | enum nss_status status = NSS_STATUS_NOTFOUND; 91 | const char dot[1] = "."; 92 | char *ext; 93 | int len = 0; 94 | 95 | //FILE *log = fopen("/tmp/nss_prax.log","a+"); 96 | //fprintf(log, "%s (%d)...", name, af); 97 | 98 | char *env = getenv("DEV_TLD_DOMAINS"); 99 | char *domains; 100 | if (env == NULL) { 101 | domains = strdup("dev"); 102 | } else { 103 | domains = strdup(env); 104 | } 105 | 106 | char *domain = strtok(domains, ","); 107 | while (domain != NULL) { 108 | char *name_ext = strrchr(name, *dot); 109 | 110 | if (name_ext != NULL) { 111 | ext = malloc(sizeof(char) * (strlen(domain) + 2)); 112 | sprintf(ext, ".%s", domain); 113 | 114 | if (strcasecmp(name_ext, ext) == 0) { 115 | status = NSS_STATUS_SUCCESS; 116 | dev_tld_fill_hostent(name, af, result); 117 | free(ext); 118 | break; 119 | } 120 | free(ext); 121 | } 122 | domain = strtok(NULL, ","); 123 | len++; 124 | } 125 | 126 | //fprintf(log, " done.\n"); 127 | //fclose(log); 128 | 129 | free(domains); 130 | return status; 131 | } 132 | 133 | enum nss_status 134 | _nss_dev_tld_gethostbyname_r(const char *name, 135 | struct hostent *result, 136 | char *buffer, 137 | size_t buflen, 138 | int *errnop, 139 | int *h_errnop) 140 | { 141 | return _nss_dev_tld_gethostbyname2_r(name, AF_INET, result, buffer, buflen, errnop, h_errnop); 142 | } 143 | 144 | enum nss_status 145 | _nss_dev_tld_gethostbyaddr_r(const void *addr, 146 | int len, 147 | int af, 148 | struct hostent *result, 149 | char *buffer, 150 | size_t buflen, 151 | int *errnop, 152 | int *h_errnop) 153 | { 154 | return NSS_STATUS_UNAVAIL; 155 | } 156 | 157 | --------------------------------------------------------------------------------