Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit d8804fb5b60dd762581de5f00741f2b1a077fe1f @pyromaniac committed Apr 21, 2011
Showing with 107 additions and 0 deletions.
  1. +16 −0 README
  2. BIN libnss_pow.so.2
  3. +91 −0 nss_pow.c
  4. BIN nss_pow.o
@@ -0,0 +1,16 @@
+Linux version of https://github.com/37signals/pow/
+
+Now we have a simple resolver.
+To install just symlink libnss_pow.so.2 to /lib/libnss_pow.so.2
+
+Then change your /etc/nsswitch.conf like this:
+
+hosts: pow files mdns4_minimal [NOTFOUND=return] dns mdns4
+
+Add pow option anywhere an hosts section.
+
+Next step - create ~/.pow
+
+Then - at ~/pow create symlink anywhere.
+
+ping my_symlink_name.dev - Wuola!
Binary file not shown.
@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <netdb.h>
+#include <nss.h>
+#include <resolv.h>
+#include <arpa/inet.h>
+#include <dirent.h>
+
+int _fill_hostent (const char *name, int af, struct hostent *result)
+{
+ result->h_name = malloc((strlen(name) + 1) * sizeof(char));
+ strcpy(result->h_name, name);
+
+ result->h_aliases = malloc(sizeof(char *));
+ *result->h_aliases = NULL;
+
+ switch (af)
+ {
+ case AF_INET:
+ result->h_addrtype = AF_INET;
+ result->h_length = INADDRSZ;
+ break;
+ case AF_INET6:
+ result->h_addrtype = AF_INET6;
+ result->h_length = IN6ADDRSZ;
+ }
+
+ result->h_addr_list = malloc(sizeof(char *) * 2);
+ *result->h_addr_list = malloc(sizeof(in_addr_t));
+ in_addr_t addr = inet_addr("127.0.0.1");
+ memcpy(*result->h_addr_list, &addr, sizeof(in_addr_t));
+ *(result->h_addr_list + 1) = NULL;
+
+ return 0;
+}
+
+enum nss_status
+_nss_pow_gethostbyname2_r (const char *name, int af, struct hostent *result,
+ char *buffer, size_t buflen, int *errnop,
+ int *h_errnop)
+{
+ enum nss_status status = NSS_STATUS_NOTFOUND;
+
+ char *home = getenv("HOME");
+ char *home_cpy = malloc((strlen(home) + 6) * sizeof(char));
+ strcpy(home_cpy, home);
+ DIR *hosts_dir = opendir(strcat(home_cpy, "/.pow"));
+ if (hosts_dir != NULL) {
+ char *name_cpy = malloc((strlen(name) + 1) * sizeof(char));
+ strcpy(name_cpy, name);
+
+ char *pointer = strtok(name_cpy, ".");
+ char *domain;
+ char *zone;
+ if (pointer != NULL) {
+ domain = pointer;
+ zone = pointer;
+ }
+ while (pointer != NULL)
+ {
+ pointer = strtok(NULL, ".");
+ if (pointer != NULL) {
+ domain = zone;
+ zone = pointer;
+ }
+ }
+
+ if (strcmp(zone, "dev") == 0) {
+ struct dirent *entry;
+ while ((entry = readdir(hosts_dir)) != NULL) {
+ if (entry->d_type == DT_LNK && strcmp(domain, entry->d_name) == 0) {
+ _fill_hostent(name, af, result);
+ status = NSS_STATUS_SUCCESS;
+ }
+ }
+ }
+
+ closedir(hosts_dir);
+ }
+
+ return status;
+}
+
+enum nss_status
+_nss_pow_gethostbyname_r (const char *name, struct hostent *result,
+ char *buffer, size_t buflen, int *errnop,
+ int *h_errnop)
+{
+ return _nss_pow_gethostbyname2_r(name, AF_INET, result, buffer, buflen, errnop, h_errnop);
+}
Binary file not shown.

0 comments on commit d8804fb

Please sign in to comment.