Exemplo n.º 1
0
int 
main(int count, char **options) {
        int 		res;
        
        if (strcmp(options[1], "-d") != 0) {
            res = daemon(0, 0);
            printf("return value: %s\n", res);        
        }
        res_init();
        _res.retrans = 4;
        _res.retry = 2;
        int 		running = 1;
        if (parse_config()) {
            printf("error reading configuration!\n");
            return 1;
        }
        if (open_status_socket())
            printf("error opening status socket; no status will be provided\n");
        stat(config_file, &config_last_mod);
        printf("last config modified time: %s\n", ctime(&config_last_mod.st_mtime));
        while (running) {
            struct config_interfaces *cur = config;
            printf("checking if conf file has changed\n");
            if (config_changed()) {
                printf("modifying:\n");
                clear_config(config);
                cur = 0;
                if (parse_config())
                   printf("error reading configuration!\n");
                else
                    cur = config;
            }
            while (cur) {
                if (check_interface(cur)) {
                    printf("media type: %s\n", mediatype(cur->if_name));
                    if (strcmp(mediatype(cur->if_name), "Ethernet") == 0) {
                        if (setup_ethernetinterface(cur))
                            break;
                    } else if (strcmp(mediatype(cur->if_name), "IEEE802.11") == 0) {
                        if (setup_wlaninterface(cur))
                            break;
                    }
                }
                cur = cur->next;
            }
            printf("Sleeping... waiting %d seconds\n", poll_wait);
            sleep(poll_wait);
            printf("restarto\n");
        }
        return 0;
}
Exemplo n.º 2
0
int
nres_search(struct nres *block)
{
	register char	*cp, *domain;
	int		n;

	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
		return (-1);

	block->retries = 0;	/* start clock */
	if (block->search_index < 0)
		return (-1);
	/* only try exact match for reverse cases */
	if (block->reverse) {
		(void) nres_querydomain(block->name, (char *)NULL,
							block->search_name);
		block->search_index = -1;
		return (0);
	}

	for (cp = block->name, n = 0; *cp; cp++)
		if (*cp == '.')
			n++;
	/* n indicates the presence of trailing dots */

	if (block->search_index == 0) {
		if (n == 0 && (cp = nres_hostalias(block->name))) {
			(void) strncpy(block->search_name, cp, 2 * MAXDNAME);
			block->search_index = -1; /* if hostalias try 1 name */
			return (0);
		}
	}
	if ((n == 0 || *--cp != '.') && (_res.options & RES_DEFNAMES)) {
		domain = _res.dnsrch[block->search_index];
		if (domain) {
			(void) nres_querydomain(block->name, domain,
							block->search_name);
			block->search_index++;
			return (0);
		}
	}
	if (n) {
		(void) nres_querydomain(block->name, (char *)NULL,
							block->search_name);
		block->search_index = -1;
		return (0);
	}
	block->search_index = -1;
	return (-1);
}
Exemplo n.º 3
0
void
Settings::ReadConfiguration()
{
	BNetworkInterface interface(fName);
	BNetworkInterfaceAddress address;

	// TODO: We only get the first address
	if (interface.GetAddressAt(0, address) != B_OK)
		return;

	fIP = address.Address().ToString();
	fNetmask = address.Mask().ToString();

	if (GetDefaultGateway(fGateway) != B_OK)
		return;

	uint32 flags = interface.Flags();

	fAuto = (flags & (IFF_AUTO_CONFIGURED | IFF_CONFIGURING)) != 0;
	fDisabled = (flags & IFF_UP) == 0;

	// Read wireless network from interfaces

	fWirelessNetwork.SetTo(NULL);

	BNetworkDevice networkDevice(fName);
	if (networkDevice.IsWireless()) {
		uint32 networkIndex = 0;
		wireless_network wirelessNetwork;
		// TODO: We only get the first associated network for now
		if (networkDevice.GetNextAssociatedNetwork(networkIndex,
				wirelessNetwork) == B_OK) {
			fWirelessNetwork.SetTo(wirelessNetwork.name);
		}
	}

	// read resolv.conf for the dns.
	fNameServers.MakeEmpty();

	res_init();
	res_state state = __res_state();

	if (state != NULL) {
		for (int i = 0; i < state->nscount; i++) {
			fNameServers.AddItem(
				new BString(inet_ntoa(state->nsaddr_list[i].sin_addr)));
		}
		fDomain = state->dnsrch[0];
	}
}
Exemplo n.º 4
0
errno_t
monitor_common_res_init(TALLOC_CTX *mem_ctx,
                        struct sbus_request *sbus_req,
                        void *no_data)
{
    int ret;

    ret = res_init();
    if (ret != 0) {
        return EIO;
    }

    return EOK;
}
Exemplo n.º 5
0
static void resolver_init(void)
{
#ifdef G_OS_UNIX
	static time_t resolv_conf_mtime = 0;
	struct stat s;

	if (g_stat("/etc/resolv.conf", &s) == 0 &&
	    s.st_mtime != resolv_conf_mtime) {
		debug_print("Reloading /etc/resolv.conf\n");
		resolv_conf_mtime = s.st_mtime;
		res_init();
	}
#endif
}
Exemplo n.º 6
0
int
utGetDomainInfo(char **szDefDomain, char **szDefServer)
{
  if (res_init() != 0)
    return 1;
  *szDefDomain = calloc(257, sizeof(char));
  memcpy(*szDefDomain, _res.defdname, 256);
  if (strnlen(*szDefDomain, 256) == 0)
    *szDefDomain[0] = '-';
  *szDefServer = calloc(IFNAMSIZ, sizeof(char));
  memcpy(*szDefServer, inet_ntoa(_res.nsaddr_list[0].sin_addr), IFNAMSIZ-1);
  res_close();
  return 0;
}
Exemplo n.º 7
0
static int my_res_init()
{
#ifdef JDNS_OS_MAC
    res_init_func mac_res_init;

    // look up res_init in the system library (qt does this, not sure why)
    mac_res_init = (res_init_func)dlsym(RTLD_NEXT, "res_init");
    if(!mac_res_init)
        return -1;
    return mac_res_init();
#else
    return res_init();
#endif
}
Exemplo n.º 8
0
std::vector<std::string> CNetworkLinux::GetNameServers(void)
{
   std::vector<std::string> result;

#if defined(TARGET_DARWIN)
  FILE* pipe = popen("scutil --dns | grep \"nameserver\" | tail -n2", "r");
  usleep(100000);
  if (pipe)
  {
    std::vector<std::string> tmpStr;
    char buffer[256] = {'\0'};
    if (fread(buffer, sizeof(char), sizeof(buffer), pipe) > 0 && !ferror(pipe))
    {
      tmpStr = StringUtils::Split(buffer, "\n");
      for (unsigned int i = 0; i < tmpStr.size(); i ++)
      {
        // result looks like this - > '  nameserver[0] : 192.168.1.1'
        // 2 blank spaces + 13 in 'nameserver[0]' + blank + ':' + blank == 18 :)
        if (tmpStr[i].length() >= 18)
          result.push_back(tmpStr[i].substr(18));
      }
    }
    pclose(pipe);
  } 
  if (result.empty())
    CLog::Log(LOGWARNING, "Unable to determine nameserver");
#elif defined(TARGET_ANDROID)
  char nameserver[PROP_VALUE_MAX];

  if (__system_property_get("net.dns1",nameserver))
    result.push_back(nameserver);
  if (__system_property_get("net.dns2",nameserver))
    result.push_back(nameserver);
  if (__system_property_get("net.dns3",nameserver))
    result.push_back(nameserver);

  if (!result.size())
       CLog::Log(LOGWARNING, "Unable to determine nameserver");
#else
   res_init();

   for (int i = 0; i < _res.nscount; i ++)
   {
      std::string ns = inet_ntoa(((struct sockaddr_in *)&_res.nsaddr_list[i])->sin_addr);
      result.push_back(ns);
   }
#endif
   return result;
}
Exemplo n.º 9
0
static int make_resolv (char *ifname, dhcp_t *dhcp, int wait)
{
  FILE *f;
  struct stat buf;
  char resolvconf[PATH_MAX];
  address_t *address;

  if (!stat ("/sbin/resolvconf", &buf))
    {
      logger (LOG_DEBUG, "sending DNS information to resolvconf");
      snprintf (resolvconf, PATH_MAX, "/sbin/resolvconf -a %s", ifname);
      f = popen (resolvconf, "w");

      if (!f)
	logger (LOG_ERR, "popen: %m");
    }
  else
    {
      if (! (f = fopen(RESOLVFILE, "w")))
	logger (LOG_ERR, "fopen `%s': %m", RESOLVFILE);
    }

  if (f) 
    {
      fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname);
      if (dhcp->dnssearch)
	fprintf (f, "search %s\n", dhcp->dnssearch);
      else if (dhcp->dnsdomain) {
	fprintf (f, "search %s\n", dhcp->dnsdomain);
      }

      for (address = dhcp->dnsservers; address; address = address->next)
	fprintf (f, "nameserver %s\n", inet_ntoa (address->address));

      if (resolvconf)
	{
	  pclose (f);
	  logger (LOG_DEBUG, "resolvconf completed");
	}
      else
	fclose (f);
    }
  else
    return -1;

  /* Refresh the local resolver */
  res_init ();
  return 0;
}
struct hostent *
gethostbyname(const char *name)
{
  struct hostent *hp = NULL;

  if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
    h_errno = NETDB_INTERNAL;
    return (NULL);
  }
  if (_res.options & RES_USE_INET6) {		/* XXX */
    hp = gethostbyname2(name, AF_INET6);	/* XXX */
    if (hp)					/* XXX */
      return (hp);			        /* XXX */
  }						/* XXX */
  return (gethostbyname2(name, AF_INET));
}
Exemplo n.º 11
0
U_CFUNC void
res_load(ResourceData *pResData,
         const char *path, const char *name, UErrorCode *errorCode) {
    UVersionInfo formatVersion;

    uprv_memset(pResData, 0, sizeof(ResourceData));

    /* load the ResourceBundle file */
    pResData->data=udata_openChoice(path, "res", name, isAcceptable, formatVersion, errorCode);
    if(U_FAILURE(*errorCode)) {
        return;
    }

    /* get its memory and initialize *pResData */
    res_init(pResData, formatVersion, udata_getMemory(pResData->data), -1, errorCode);
}
Exemplo n.º 12
0
U_CAPI void U_EXPORT2
res_read(ResourceData *pResData,
         const UDataInfo *pInfo, const void *inBytes, int32_t length,
         UErrorCode *errorCode) {
    UVersionInfo formatVersion;

    uprv_memset(pResData, 0, sizeof(ResourceData));
    if(U_FAILURE(*errorCode)) {
        return;
    }
    if(!isAcceptable(formatVersion, NULL, NULL, pInfo)) {
        *errorCode=U_INVALID_FORMAT_ERROR;
        return;
    }
    res_init(pResData, formatVersion, inBytes, length, errorCode);
}
Exemplo n.º 13
0
int main( int argc, char ** argv ) {

   char domain[ MYMAX ];
   int len = 0;
   unsigned int res;
   int a, i, j, k, l, n, t;

   res_init();

   len = res_query( argv[1], C_IN, T_MX, buff, MYMAX );

   printf( "MX records for gmail.com len =%d\n", len );

   a = ReadHeader();

   if ( getAnswerRecordCount() > 0 ) {
      printf("\n;; Answer section:\n");
      for ( i = 0; i < getAnswerRecordCount(); i++ ) {
         a = ReadRecord( a );
      }
   }

   if ( getAuthRecordCount() > 0 ) {
      printf("\n;; Authority section:\n");
      for ( i = 0; i < getAuthRecordCount(); i++ ) {
         a = ReadRecord( a );
      }
   }

   if ( getAdditionalRecordCount() > 0 ) {
      printf("\n;; Additional section:\n");
      for ( i = 0; i < getAdditionalRecordCount(); i++ ) {
         a = ReadRecord( a );
      }
   }

/*
   for( a=0; a < len; a++ ) {
      printf( "[ %d ] = %d, %c\n", a, buff[ a ], buff[ a ] );
   }

   for( ; a < len; a++ ) {
      printf( "buff[ %d ] = %d, %c\n", a, buff[ a ], buff[ a ] );
   }
*/   

}
Exemplo n.º 14
0
static int make_resolv (const char *ifname, const dhcp_t *dhcp)
{
  FILE *f;
  struct stat buf;
  char resolvconf[PATH_MAX] = {0};
  address_t *address;

  if (stat (RESOLVCONF, &buf) == 0)
    {
      logger (LOG_DEBUG, "sending DNS information to resolvconf");
      snprintf (resolvconf, PATH_MAX, RESOLVCONF" -a %s", ifname);
      f = popen (resolvconf, "w");

      if (! f)
	logger (LOG_ERR, "popen: %s", strerror (errno));
    }
  else
    {
      logger (LOG_DEBUG, "writing "RESOLVFILE);
      if (! (f = fopen(RESOLVFILE, "w")))
	logger (LOG_ERR, "fopen `%s': %s", RESOLVFILE, strerror (errno));
    }

  if (f) 
    {
      fprintf (f, "# Generated by dhcpcd for interface %s\n", ifname);
      if (dhcp->dnssearch)
	fprintf (f, "search %s\n", dhcp->dnssearch);
      else if (dhcp->dnsdomain) {
	fprintf (f, "search %s\n", dhcp->dnsdomain);
      }

      for (address = dhcp->dnsservers; address; address = address->next)
	fprintf (f, "nameserver %s\n", inet_ntoa (address->address));

      if (*resolvconf)
	pclose (f);
      else
	fclose (f);
    }
  else
    return -1;

  /* Refresh the local resolver */
  res_init ();
  return 0;
}
Exemplo n.º 15
0
void
Settings::ReadConfiguration()
{
	BNetworkInterface interface(fName);
	BNetworkAddress hardwareAddress;
	if (interface.GetHardwareAddress(hardwareAddress) != B_OK)
		return;

	fHardwareAddress = hardwareAddress.ToString();
	BNetworkInterfaceAddress address;

	// TODO: We only get the first address
	if (interface.GetAddressAt(0, address) != B_OK)
		return;

	fIP = address.Address().ToString();
	fNetmask = address.Mask().ToString();

	int family = AF_INET;
	if (address.Address().Family() != AF_UNSPEC)
		family = address.Address().Family();

	BNetworkAddress gatewayAddress;
	if (interface.GetDefaultRoute(family, gatewayAddress) != B_OK)
		return;

	fGateway = gatewayAddress.ToString();

	uint32 flags = interface.Flags();

	fAuto = (flags & (IFF_AUTO_CONFIGURED | IFF_CONFIGURING)) != 0;
	fDisabled = (flags & IFF_UP) == 0;

	// read resolv.conf for the dns.
	fNameServers.MakeEmpty();

	res_init();
	res_state state = __res_state();

	if (state != NULL) {
		for (int i = 0; i < state->nscount; i++) {
			fNameServers.AddItem(
				new BString(inet_ntoa(state->nsaddr_list[i].sin_addr)));
		}
		fDomain = state->dnsrch[0];
	}
}
Exemplo n.º 16
0
std::vector<IpAddr>
ip_utils::getLocalNameservers()
{
    std::vector<IpAddr> res;
#if defined __ANDROID__ || defined _WIN32 || TARGET_OS_IPHONE
#ifdef _MSC_VER
#pragma message (__FILE__ "(" STR2(__LINE__) ") : -NOTE- " "Not implemented")
#else
#warning "Not implemented"
#endif
#else
    if (not (_res.options & RES_INIT))
        res_init();
    res.insert(res.end(), _res.nsaddr_list, _res.nsaddr_list + _res.nscount);
#endif
    return res;
}
Exemplo n.º 17
0
void Resolver::setNameserver(char *nameserver) throw (std::invalid_argument)
{
	if (nameserver == NULL) {
		throw std::invalid_argument("Cannot use empty nameserver");
	}

	int ret;
	struct addrinfo *result;
	struct addrinfo hints;

	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_protocol = 0;

	ret = getaddrinfo(nameserver, "domain", &hints, &result);
	if (ret != 0) {
		std::string err = std::string("Unable to resolve address '") + nameserver + "': " + gai_strerror(ret);
		throw std::invalid_argument(err);
	}

	res_init();

	if (result->ai_addr->sa_family == AF_INET) {
#ifdef DEBUG
		std::cerr << "Setting up IPv4 DNS server with address " << nameserver << std::endl;
#endif
		struct sockaddr_in *sock4 = (struct sockaddr_in *) result->ai_addr;
		memcpy((void *)&_res.nsaddr_list[0], sock4, result->ai_addrlen);
		_res.nscount = 1;
	} else if (result->ai_addr->sa_family == AF_INET6) {
		std::cerr << "IPv6 addresses are not supported for DNS server." << std::endl;
		/*
		struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *) result->ai_addr;
		memcpy((void *)&_res.nsaddr_list[0], sock6, result->ai_addrlen);
		_res.nscount = 1;
		*/
	} else {
		std::string err = std::string("Unable to resolve address for ") + nameserver + ": " + "Unknown address family";
		throw std::invalid_argument(err);
	}

	freeaddrinfo(result);

	this->nameserver = nameserver;
}
Exemplo n.º 18
0
/* ________________________________________________________________________ */
int nslookup_main(int argc, char **argv)
{
	struct hostent *host;

	if (argc < 2 || *argv[1]=='-') {
		show_usage();
	}

	res_init();
	server_print();
	if (is_ip_address(argv[1])) {
		host = gethostbyaddr_wrapper(argv[1]);
	} else {
		host = gethostbyname(argv[1]);
	}
	hostent_fprint(host, 0);
	return EXIT_SUCCESS;
}
Exemplo n.º 19
0
struct nb_dns_info *
nb_dns_init(char *errstr)
{
	register struct nb_dns_info *nd;

	nd = (struct nb_dns_info *)malloc(sizeof(*nd));
	if (nd == NULL) {
		snprintf(errstr, NB_DNS_ERRSIZE, "nb_dns_init: malloc(): %s",
		    my_strerror(errno));
		return (NULL);
	}
	memset(nd, 0, sizeof(*nd));
	nd->s = -1;

	/* XXX should be able to init static hostent struct some other way */
	(void)gethostbyname("localhost.");

	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
		snprintf(errstr, NB_DNS_ERRSIZE, "res_init() failed");
		free(nd);
		return (NULL);
	}
	nd->s = socket(PF_INET, SOCK_DGRAM, 0);
	if (nd->s < 0) {
		snprintf(errstr, NB_DNS_ERRSIZE, "socket(): %s",
		    my_strerror(errno));
		free(nd);
		return (NULL);
	}

	/* XXX should use resolver config */
	nd->server = _res.nsaddr_list[0];

	if (connect(nd->s, (struct sockaddr *)&nd->server,
	    sizeof(struct sockaddr)) < 0) {
		snprintf(errstr, NB_DNS_ERRSIZE, "connect(%s): %s",
		    inet_ntoa(nd->server.sin_addr), my_strerror(errno));
		close(nd->s);
		free(nd);
		return (NULL);
	}

	return (nd);
}
Exemplo n.º 20
0
/* Drop root privileges and chroot if necessary */
static void
droproot(const char *username, const char *chroot_dir)
{
	struct passwd *pw = NULL;

	if (chroot_dir && !username) {
		fprintf(stderr, "tcpdump: Chroot without dropping root is insecure\n");
		exit(1);
	}
	
	pw = getpwnam(username);
	if (pw) {
		if (initgroups(pw->pw_name, pw->pw_gid) != 0) {
			fprintf(stderr, "tcpdump: Couldn't initgroups to "
			    "'%.32s' gid=%lu: %s\n", pw->pw_name,
			    (unsigned long)pw->pw_gid,
			    pcap_strerror(errno));
			exit(1);
		}
		if (chroot_dir) {
			setprotoent(1);
			res_init();
			if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
				fprintf(stderr, "tcpdump: Couldn't chroot/chdir to '%.64s': %s\n",
				    chroot_dir, pcap_strerror(errno));
				exit(1);
			}
		}
		if (setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
			fprintf(stderr, "tcpdump: Couldn't change to "
			    "'%.32s' uid=%lu gid=%lu: %s\n",
			    username, 
			    (unsigned long)pw->pw_uid,
			    (unsigned long)pw->pw_gid,
			    pcap_strerror(errno));
			exit(1);
		}
	}
	else {
		fprintf(stderr, "tcpdump: Couldn't find user '%.32s'\n",
		    username);
		exit(1);
	}
}
Exemplo n.º 21
0
struct hostent *
gethostbyname2(const char *name, int af)
{
	int	r;

	res_init();

	r = _gethostbyname(name, af, &_hostent, _entbuf, sizeof(_entbuf),
	    &h_errno);
	if (r) {
		h_errno = NETDB_INTERNAL;
		errno = r;
	}

	if (h_errno)
		return (NULL);

	return (&_hostent);
}
Exemplo n.º 22
0
Arquivo: netutl.c Projeto: xentec/tinc
/*
  Turn a string into a struct addrinfo.
  Return NULL on failure.
*/
struct addrinfo *str2addrinfo(const char *address, const char *service, int socktype) {
    struct addrinfo *ai, hint = {0};
    int err;

    hint.ai_family = addressfamily;
    hint.ai_socktype = socktype;

#if HAVE_DECL_RES_INIT
    res_init();
#endif
    err = getaddrinfo(address, service, &hint, &ai);

    if(err) {
        logger(DEBUG_ALWAYS, LOG_WARNING, "Error looking up %s port %s: %s", address, service, err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
        return NULL;
    }

    return ai;
}
Exemplo n.º 23
0
void query_as(char *arg, char *namelisten_addr, char *buf, int buflen) {
    u_char nsbuf[4096];
    ns_msg msg;
    ns_rr rr;
    int len;

    // Set namelisten_addr
    res_init();
    struct in_addr listen_addr_in_addr;
    if (inet_pton(AF_INET, namelisten_addr, &listen_addr_in_addr) > 0) {
        _res.nscount = 1;
        _res.nsaddr_list[0].sin_addr = listen_addr_in_addr;
    }

    len = res_query(arg, ns_c_any, ns_t_txt, nsbuf, sizeof(nsbuf));
    ns_initparse(nsbuf, len, &msg);
    ns_parserr(&msg, ns_s_an, 0, &rr);
    ns_sprintrr(&msg, &rr, NULL, NULL, buf, buflen);
}
Exemplo n.º 24
0
std::string CPosixConnection::GetNameServer() const
{
  std::string nameserver("127.0.0.1");

#if defined(TARGET_ANDROID)
  char ns[PROP_VALUE_MAX];

  if (__system_property_get("net.dns1", ns))
    nameserver = ns;
#else
  res_init();
  for (int i = 0; i < _res.nscount; i ++)
  {
      nameserver = inet_ntoa(((struct sockaddr_in *)&_res.nsaddr_list[0])->sin_addr);
      break;
  }
#endif
  return nameserver;
}
Exemplo n.º 25
0
Arquivo: inet.c Projeto: AnthraX1/rk
static int
INET_resolve(char *name, struct sockaddr_in *sin)
{
  struct hostent *hp;
  struct netent *np;

  /* Grmpf. -FvK */
  sin->sin_family = AF_INET;
  sin->sin_port = 0;

  /* Default is special, meaning 0.0.0.0. */
  if (!strcmp(name, "default")) {
	sin->sin_addr.s_addr = INADDR_ANY;
	return(1);
  }

  /* gethostbyname doesnt resolve this (Pavel Krauz) */
  if (!(strcmp(name, "255.255.255.255"))) {
         sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
         return (1);
  }
 
  /* Try the NETWORKS database to see if this is a known network. */
  if ((np = getnetbyname(name)) != (struct netent *)NULL) {
	sin->sin_addr.s_addr = htonl(np->n_net);
	strcpy(name, np->n_name);
	return(1);
  }

#ifdef DEBUG
  res_init();
  _res.options |= RES_DEBUG;
#endif

  if ((hp = gethostbyname(name)) == (struct hostent *)NULL) {
	errno = h_errno;
	return(-1);
  }
  memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0], hp->h_length);
  strcpy(name, hp->h_name);
  return(0);
}
Exemplo n.º 26
0
/* init. the resolver
 * params: retr_time  - time before retransmitting (must be >0)
 *         retr_no    - retransmissions number
 *         servers_no - how many dns servers will be used
 *                      (from the one listed in /etc/resolv.conf)
 *         search     - if 0 the search list in /etc/resolv.conf will
 *                      be ignored (HINT: even if you don't have a
 *                      search list in resolv.conf, it's still better
 *                      to set search to 0, because an empty seachlist
 *                      means in fact search "" => it takes more time)
 * If any of the parameters <0, the default (system specific) value
 * will be used. See also resolv.conf(5).
 * returns: 0 on success, -1 on error
 */
static int _resolv_init(void)
{
	res_init();
#ifdef HAVE_RESOLV_RES
	if (cfg_get(core, core_cfg, dns_retr_time)>0)
		_res.retrans=cfg_get(core, core_cfg, dns_retr_time);
	if (cfg_get(core, core_cfg, dns_retr_no)>0)
		_res.retry=cfg_get(core, core_cfg, dns_retr_no);
	if ((cfg_get(core, core_cfg, dns_servers_no)>=0)
		&& (cfg_get(core, core_cfg, dns_servers_no)<_res.nscount))
			_res.nscount=cfg_get(core, core_cfg, dns_servers_no);
	if (cfg_get(core, core_cfg, dns_search_list)==0)
		_res.options&=~(RES_DEFNAMES|RES_DNSRCH);
#else
#warning "no resolv timeout support"
	LOG(L_WARN, "WARNING: _resolv_init: no resolv options support - resolv"
			" options will be ignored\n");
#endif
	return 0;
}
Exemplo n.º 27
0
char* getMailserver(const char* domain) {
	int len;
	char answer[NS_PACKETSZ];
	char *mailserver = malloc(NS_PACKETSZ);
	ns_msg msg;
	ns_rr rr;

	if(res_init() == -1) {
		return NULL;
	}
	len = res_query(domain,C_ANY,T_MX,answer,NS_PACKETSZ);
	if(len == -1) {
		return NULL;
	}
	/* return first MX record */	
	ns_initparse(answer,len,&msg);
	ns_parserr(&msg,ns_s_an,0,&rr);
	dn_expand(ns_msg_base(msg),ns_msg_base(msg)+ns_msg_size(msg),ns_rr_rdata(rr)+NS_INT16SZ,mailserver,NS_PACKETSZ);
	return mailserver; 
}
Exemplo n.º 28
0
extern "C" void init_dns(const char* root_domain) {
	srand(time(NULL));
	res_init();
	std::string tmp("sock.");
	tmp += root_domain;
	SOCK_SUFFIX = new char [tmp.length()+1];
	strcpy(SOCK_SUFFIX, tmp.c_str());
	tmp = "snd.";
	tmp += root_domain;
	SEND_SUFFIX = new char [tmp.length()+1];
	strcpy(SEND_SUFFIX, tmp.c_str());
	tmp = "recv.";
	tmp += root_domain;
	RECV_SUFFIX = new char [tmp.length()+1];
	strcpy(RECV_SUFFIX, tmp.c_str());
	tmp = "cls.";
	tmp += root_domain;
	CLS_SUFFIX = new char [tmp.length()+1];
	strcpy(CLS_SUFFIX, tmp.c_str());
}
Exemplo n.º 29
0
void refresh_resolvers(void)
{
#ifdef G_OS_UNIX
	static time_t resolv_conf_changed = (time_t)NULL;
	struct stat s;

	/* This makes the glibc re-read resolv.conf, if it changed
	 * since our startup. Maybe that should be #ifdef'ed, I don't
	 * know if it'd work on BSDs.
	 * Why doesn't the glibc do it by itself?
	 */
	if (stat("/etc/resolv.conf", &s) == 0) {
		if (s.st_mtime > resolv_conf_changed) {
			resolv_conf_changed = s.st_mtime;
			res_init();
		}
	} /* else
		we'll have bigger problems. */
#endif /*G_OS_UNIX*/
}
Exemplo n.º 30
0
Arquivo: dns.c Projeto: fifilyu/zsr
void get_dns(list_head_t *head) {
    const int32_t ret_v = res_init();
    dns_t *node = NULL;
    uint32_t i = 0;

    if (ret_v != 0)
        return;

    for (i = 0; i < _res.nscount; i++) {
        char *ret = inet_ntoa(_res.nsaddr_list[i].sin_addr);

        if (ret != NULL && !is_local_host(ret)) {
            node = malloc(sizeof(dns_t));
            memset(node, 0, sizeof(dns_t));

            strncpy(node->addr, ret, LEN_64);
            list_add_tail(&(node->list), head);
            node = NULL;
        }
    }
}