Пример #1
0
static int
hosts(int argc, char *argv[])
{
	struct hostent	*he;
	char		addr[IN6ADDRSZ];
	int		i, rv;

	assert(argc > 1);
	assert(argv != NULL);

	sethostent(1);
	rv = RV_OK;
	if (argc == 2) {
		while ((he = gethostent()) != NULL)
			hostsprint(he);
	} else {
		for (i = 2; i < argc; i++) {
			if (inet_pton(AF_INET6, argv[i], (void *)addr) > 0)
				he = gethostbyaddr(addr, IN6ADDRSZ, AF_INET6);
			else if (inet_pton(AF_INET, argv[i], (void *)addr) > 0)
				he = gethostbyaddr(addr, INADDRSZ, AF_INET);
			else
				he = gethostbyname(argv[i]);
			if (he != NULL)
				hostsprint(he);
			else {
				rv = RV_NOTFOUND;
				break;
			}
		}
	}
	endhostent();
	return rv;
}
Пример #2
0
HOST_R_RETURN
gethostent_r(struct hostent *hptr, HOST_R_ARGS) {
    struct hostent *he = gethostent();
#ifdef HOST_R_SETANSWER
    int n = 0;
#endif

#ifdef HOST_R_ERRNO
    HOST_R_ERRNO;
#endif

#ifdef HOST_R_SETANSWER
    if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0)
        *answerp = NULL;
    else
        *answerp = hptr;

    return (n);
#else
    if (he == NULL)
        return (HOST_R_BAD);

    return (copy_hostent(he, hptr, HOST_R_COPY));
#endif
}
Пример #3
0
int
main(void)
{
	struct hostent *ptr;
	char **p;

	sethostent(0);

	while((ptr = gethostent()) != NULL)
	{
		printf("name:%s , addrtype:%d , length:%d ,addr_list: ",
				ptr->h_name,ptr->h_addrtype,ptr->h_length);
		
		p = ptr->h_addr_list;

		while(*p)
		{
			printf("[%s] ",*p);
			p++;
		}
		printf("\n");
	}

	endhostent();
	return 0;
}
Пример #4
0
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  
 * =====================================================================================
 */
int main ( int argc, char *argv[] )
{
    struct hostent * host ;
    while((host =  gethostent())!=NULL){
        char ** aliases=NULL;
        char **addr_list = NULL;
        char addr_p[INET_ADDRSTRLEN];   // IPV4
        printf("hostname: %s\n", host->h_name);
        for(aliases = host->h_aliases; *aliases; aliases++)
        {
            printf("alternate name: %s\n", *aliases);
        }
        /*  print address type and length */
        if(host->h_addrtype == AF_INET)  
        {
            printf("address type: AF_INET\n");
        }
        else
       {
            printf("Not an IPv4 address.\n");
       }
       printf("address length: %d\n", host->h_length);
      /*  print address list */
      //printf("%d\n", sizeof(*(host->h_addr_list)));
       printf("%x\n", *(int *)(*(host->h_addr_list)));
      for(addr_list = host->h_addr_list; *addr_list; addr_list++)  
      {
        printf("address: %s\n", inet_ntop(host->h_addrtype, *addr_list, addr_p, INET_ADDRSTRLEN));
      }
    }
    return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
Пример #5
0
/*
 * Lookup the local host table (usually /etc/hosts) for a given hostname.
 *
 * If found, ip_addr is populated and 0 is returned.
 * If NOT found, -1 is return and ip_addr is untouched.
 */
static int hosts_file_resolve(const char *hostname, uint32_t *ip_addr)
{
	int ret;
	struct hostent *host;

	assert(hostname);
	assert(ip_addr);

	DBG("Looking in local host table for %s", hostname);

	/* Query the local host table if the hostname is present. */
	while ((host = gethostent()) != NULL) {
		if (strncasecmp(hostname, host->h_name, strlen(hostname)) == 0) {
			/* IP is found, copying and returning success. */
			memcpy(ip_addr, host->h_addr_list[0], sizeof(uint32_t));
			ret = 0;
			goto end;
		}
	}

	/* Not found. */
	ret = -1;

end:
	endhostent();
	return ret;
}
Пример #6
0
struct hostent *wp_gethostent (void)
{
	struct hostent *p;
	if ((p = gethostent ()) == NULL)
		wp_warning ("gethostent() error");
	return p;
}
Пример #7
0
static int
hosts(int argc, char *argv[])
{
	char		addr[IN6ADDRSZ];
	int		i, rv = RV_OK;
	struct hostent	*he;

	sethostent(1);
	if (argc == 2) {
		while ((he = gethostent()) != NULL)
			hostsprint(he);
	} else {
		for (i = 2; i < argc; i++) {
			he = NULL;
			if (inet_pton(AF_INET6, argv[i], (void *)addr) > 0)
				he = gethostbyaddr(addr, IN6ADDRSZ, AF_INET6);
			else if (inet_pton(AF_INET, argv[i], (void *)addr) > 0)
				he = gethostbyaddr(addr, INADDRSZ, AF_INET);
			if (he != NULL)
				hostsprint(he);
			else if ((rv = hostsaddrinfo(argv[i])) == RV_NOTFOUND)
				break;
		}
	}
	endhostent();
	return rv;
}
Пример #8
0
void testValues() {
    f = 2;
    struct hostent * result;
    
    result = gethostent();
    //@ assert result == \null || \valid(result);
    
    //@ assert f == 2;
    //@ assert vacuous: \false;
}
Пример #9
0
main()
{
	//打开DNS服务器或者其他host文件
	sethostent(1);
	struct hostent*hptr;
	while((hptr=gethostent()))
	{
		printf("IP地址:%hhu.%hhu.%hhu.%hhu\n",hptr->h_addr[0],hptr->h_addr[1],hptr->h_addr[2],hptr->h_addr[3]);
	}
	endhostent();
}
Пример #10
0
char XPCGetHostInfo::cGetNextHost()
{
     // Get the next host from the database
     if (cIteratorFlag == 1)
     {
         if ((hostPtr = gethostent()) == NULL)
              return 0;
         else
              return 1;
     }
     return 0;
}
Пример #11
0
char myHostInfo::getNextHost()
{
	// winLog<<"UNIX getNextHost() is called...\n";
    // Get the next host from the database
    if (searchHostDB == 1)
    {
         if ((hostPtr = gethostent()) == NULL)
              return 0;
         else
              return 1;
    }
    return 0;
}
Пример #12
0
int
noit_check_etc_hosts_cache_refresh(eventer_t e, int mask, void *closure,
                                   struct timeval *now) {
  static struct stat last_stat;
  struct stat sb;
  struct hostent *ent;
  int reload = 0;

  memset(&sb, 0, sizeof(sb));
  stat("/etc/hosts", &sb);
#define CSTAT(f) (sb.f == last_stat.f)
  reload = ! (CSTAT(st_dev) && CSTAT(st_ino) && CSTAT(st_mode) && CSTAT(st_uid) &&
              CSTAT(st_gid) && CSTAT(st_size) && CSTAT(st_mtime));
  memcpy(&last_stat, &sb, sizeof(sb));

  if(reload) {
    mtev_hash_delete_all(&etc_hosts_cache, free, free);
    while(NULL != (ent = gethostent())) {
      int i = 0;
      char *name = ent->h_name;
      while(name) {
        void *vnode;
        static_host_node *node;
        if(!mtev_hash_retrieve(&etc_hosts_cache, name, strlen(name), &vnode)) {
          vnode = node = calloc(1, sizeof(*node));
          node->target = strdup(name);
          mtev_hash_store(&etc_hosts_cache, node->target, strlen(node->target), node);
        }
        node = vnode;
  
        if(ent->h_addrtype == AF_INET) {
          node->has_ip4 = 1;
          memcpy(&node->ip4, ent->h_addr_list[0], ent->h_length);
        }
        if(ent->h_addrtype == AF_INET6) {
          node->has_ip6 = 1;
          memcpy(&node->ip6, ent->h_addr_list[0], ent->h_length);
        }
        
        name = ent->h_aliases[i++];
      }
    }
    endhostent();
    mtevL(noit_debug, "reloaded %d /etc/hosts targets\n", mtev_hash_size(&etc_hosts_cache));
  }

  eventer_add_in_s_us(noit_check_etc_hosts_cache_refresh, NULL, 1, 0);
  return 0;
}
Пример #13
0
struct hostent *
_gethostbyhtaddr(
	const char *addr,
	int len, 
	int af)
{
	register struct hostent *p;

	sethostent(0);
	while ((p = gethostent()) != NULL)
		if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
			break;
	endhostent();
	return (p);
}
Пример #14
0
int main()
{
    struct hostent h;
    size_t size = sizeof(h);
    struct hostent *ph;
    while((ph=gethostent())!=NULL) {
        memcpy(&h,gethostent(),size);
        printf("------------- host names------------\n");
        printf("\t%s\t(name)\n",h.h_name);
        char **list = h.h_aliases;
        for(int i=0;list[i]!=0;++i) {
            printf("\t%s\n",list[i]);
        }
        printf("------------- host addr------------\n");
        list = h.h_addr_list;
        for(int i=0;list[i]!=0;++i) {
            char buf[64];
            inet_ntop(AF_INET, list[i], buf, 64);
            printf("\t%s\n",buf);
        }
        printf("\n\n");
    }
    return 0;
}
Пример #15
0
main()
{

	//参数主要对域名查找设置有效,对本地/etc/hosts没有意义
	sethostent(1);

	struct hostent *ent;

	while(1)
	{
		ent = gethostent();
		if(!ent) break;
		printf(":%s:%hhu.%hhu.%hhu.%hhu\n",
		//printf(":%s:%u.%u.%u.%u\n",
				ent->h_name,
				ent->h_addr[0],
				ent->h_addr[1],
				ent->h_addr[2],
				ent->h_addr[3]);
	}

	endhostent();

	/////////////////////////////////
	struct utsname name;
	uname(&name);
	printf("%s\n",name.sysname);
	printf("%s\n",name.nodename);
	printf("%s\n",name.release);
	printf("%s\n",name.version);
	printf("%s\n",name.machine);
	//printf("%s\n",name.domainname);

	//////////////////////////////
	struct hostent *ipent;
	ipent=gethostbyname("www.baidu.com");	
	printf("%s:%hhu.%hhu.%hhu.%hhu\n",
			ipent->h_name,
			ipent->h_addr[0],
			ipent->h_addr[1],
			ipent->h_addr[2],
			ipent->h_addr[3]);
	
}
Пример #16
0
void test2()
{
	// 打开主机配置数据库文件
	sethostent(1);

	hostent* ent;
	while (1) {
		ent = gethostent();
		if (ent) {
			printf("主机名:%s\n", ent->h_name);
			printf("ip地址:%hhu.%hhu.%hhu.%hhu\n", ent->h_addr[0], ent->h_addr[1], ent->h_addr[2], ent->h_addr[3]);
		} else {
			break;
		}
	}

	// 关闭
	endhostent();
}
Пример #17
0
int main(void)
{
	struct hostent *ent;
	char **aliases, **addrs;
	int domain;
	char addrbuf[MAXBUFLEN];

	while ((ent = gethostent()) != NULL)
	{
		printf("---------------------\n");
		printf("hostname: %s\n", ent->h_name);

		// print aliases
		for (aliases = ent->h_aliases; *aliases != NULL; aliases++)
		{
			printf("  %s\n", *aliases);
		}

		// print domain
		domain = ent->h_addrtype;
		if (domain == AF_INET)
			printf("domain: IPV4\n");
		else if (domain == AF_INET6)
			printf("domain: IPV6\n");
		else
			printf("domain: unknown %d\n", domain);

		// print addresses
		if (domain == AF_INET || 
			domain == AF_INET6)
		{
			for (addrs = ent->h_addr_list; *addrs != NULL; addrs++)
			{
				inet_ntop(domain, *addrs, addrbuf, MAXBUFLEN);
				printf("  address:%s\n", addrbuf);
			}
		}

		printf("\n");
	}
	endhostent();
}
Пример #18
0
int main(){

	struct hostent *ent;
	/* open host config */
	sethostent(1);

	while(1){
		ent = gethostent();
		if(ent == 0){
			break;
		}
		printf("name:%s IP name:%hhu.%hhu.%hhu.%hhu alarm:%s\n",
				ent->h_name,ent->h_addr[0],
				ent->h_addr[1],ent->h_addr[2],
				ent->h_addr[3],ent->h_aliases[0]);
	}

	endhostent();
	return 0;
}
Пример #19
0
struct hostent *
_gethostbyhtname(
	const char *name,
	int af)
{
	register struct hostent *p;
	register char **cp;
	
	sethostent(0);
	while ((p = gethostent()) != NULL) {
		if (p->h_addrtype != af)
			continue;
		if (strcasecmp(p->h_name, name) == 0)
			break;
		for (cp = p->h_aliases; *cp != 0; cp++)
			if (strcasecmp(*cp, name) == 0)
				goto found;
	}
found:
	endhostent();
	return (p);
}
Пример #20
0
main(int argc, char *argv[])
{
   struct hostent *h;

/* sethostent() opens the prefix.ETC.HOSTS file, or when using a  */
/* nameserver, opens a TCP connection to the nameserver.          */

   sethostent(TRUE);

/* gethostent() reads the next sequential entry in the            */
/* prefix.ETC.HOSTS file. It returns a pointer to a "hostent"     */
/* structure.                                                     */

   while (h=gethostent())
      prthost(h);

/* endhostent() closes the prefix.ETC.HOSTS file, or the          */
/* connection to the nameserver.                                  */

   endhostent();
   exit(EXIT_SUCCESS);
}
Пример #21
0
static struct hostent *__check_hostdb( const char *name )
{
    int             i;
    struct hostent  *one;
    int             alias;

    one = NULL;
    if( name != NULL ) {
        alias = 0;
        sethostent( 1 );
        while( alias == 0 && (one = gethostent()) != NULL ) {
            if( one->h_name != NULL && strcmp( one->h_name, name ) == 0 )
                break;
            for( i = 0; one->h_aliases[i] != NULL; i++ ) {
                if( strcmp( one->h_aliases[i], name ) == 0 ) {
                    alias = 1;
                    break;
                }
            }
        }
        endhostent();
    }
    return( one );
}
Пример #22
0
struct hostent *proxy_gethostbyname(const char *name, struct gethostbyname_data* data) {
	char buff[256];
	uint32_t i, hash;
	// yep, new_mem never gets freed. once you passed a fake ip to the client, you can't "retreat" it
	void *new_mem;
	size_t l;
	struct hostent *hp;

	data->resolved_addr_p[0] = (char *) &data->resolved_addr;
	data->resolved_addr_p[1] = NULL;

	data->hostent_space.h_addr_list = data->resolved_addr_p;

	data->resolved_addr = 0;

	gethostname(buff, sizeof(buff));

	if(!strcmp(buff, name)) {
		data->resolved_addr = inet_addr(buff);
		if(data->resolved_addr == (in_addr_t) (-1))
			data->resolved_addr = (in_addr_t) (local_host.as_int);

		snprintf(hostname,sizeof hostname, "%s", name);
	    data->hostent_space.h_name = hostname;
	    data->hostent_space.h_length = sizeof (in_addr_t);
	    data->hostent_space.h_addrtype = AF_INET;

		return &data->hostent_space;
	}

	memset(buff, 0, sizeof(buff));

	while((hp = gethostent()))
		if(!strcmp(hp->h_name, name))
			return hp;

	hash = dalias_hash((char *) name);

	MUTEX_LOCK(&internal_ips_lock);

	// see if we already have this dns entry saved.
	if(internal_ips.counter) {
		for(i = 0; i < internal_ips.counter; i++) {
			if(internal_ips.list[i]->hash == hash && !strcmp(name, internal_ips.list[i]->string)) {
				data->resolved_addr = make_internal_ip(i);
				PDEBUG("got cached ip for %s\n", name);
				goto have_ip;
			}
		}
	}

	// grow list if needed.
	if(internal_ips.capa < internal_ips.counter + 1) {
		PDEBUG("realloc\n");
		new_mem = realloc(internal_ips.list, (internal_ips.capa + 16) * sizeof(void *));
		if(new_mem) {
			internal_ips.capa += 16;
			internal_ips.list = new_mem;
		} else {
	oom:
			proxychains_write_log("out of mem\n");
			goto err_plus_unlock;
		}
	}

	data->resolved_addr = make_internal_ip(internal_ips.counter);
	if(data->resolved_addr == (in_addr_t) - 1)
		goto err_plus_unlock;

	l = strlen(name);
	new_mem = malloc(sizeof(string_hash_tuple) + l + 1);
	if(!new_mem)
		goto oom;

	PDEBUG("creating new entry %d for ip of %s\n", (int) internal_ips.counter, name);

	internal_ips.list[internal_ips.counter] = new_mem;
	internal_ips.list[internal_ips.counter]->hash = hash;

	internal_ips.list[internal_ips.counter]->string = (char *) new_mem + sizeof(string_hash_tuple);

	memcpy(internal_ips.list[internal_ips.counter]->string, name, l + 1);

	internal_ips.counter += 1;

	have_ip:

	MUTEX_UNLOCK(&internal_ips_lock);

	strncpy(data->addr_name, name, sizeof(data->addr_name));

	data->hostent_space.h_name = data->addr_name;
	data->hostent_space.h_length = sizeof(in_addr_t);
	data->hostent_space.h_addrtype = AF_INET;

	return &data->hostent_space;

	err_plus_unlock:
	MUTEX_UNLOCK(&internal_ips_lock);
	return NULL;
}
Пример #23
0
struct hostent* proxy_gethostbyname(const char *name)
{
	int pipe_fd[2];
	char buff[256];
	in_addr_t addr;
	pid_t pid;
	int status;
	struct hostent* hp;

	hostent_space.h_addr_list = &resolved_addr_p;
	*hostent_space.h_addr_list = (char*)&resolved_addr;
	resolved_addr = 0;
	
	gethostname(buff,sizeof(buff));
	if(!strcmp(buff,name))
		goto got_buff;

	bzero(buff,sizeof(buff));
	
	// TODO: this works only once, so cache it  ...
	// 	 later
	while (hp=gethostent())
		if (!strcmp(hp->h_name,name)) 
			return hp; 
	
	if(pipe(pipe_fd))
		goto err;
	pid = fork();
	switch(pid) {
	
		case 0: // child
			proxychains_write_log("|DNS-request| %s \n", name);
			dup2(pipe_fd[1],1);
			//dup2(pipe_fd[1],2);
		//	putenv("LD_PRELOAD=");
			execlp("proxyresolv","proxyresolv",name,NULL);
			perror("can't exec proxyresolv");
			exit(2);

		case -1: //error
			close(pipe_fd[0]);
			close(pipe_fd[1]);
			perror("can't fork");
			goto err;
		
		default:
			close(pipe_fd[1]);
			waitpid(pid, &status, 0);
			read(pipe_fd[0],&buff,sizeof(buff));
			close(pipe_fd[0]);
got_buff:
			addr = inet_addr(buff);
			if (addr == -1)
				goto err_dns;
			memcpy(*(hostent_space.h_addr_list),
						&addr ,sizeof(struct in_addr));
			hostent_space.h_name = addr_name;
			hostent_space.h_length = sizeof (in_addr_t);
	}
	proxychains_write_log("|DNS-response| %s is %s\n",
			name, inet_ntoa(*(struct in_addr*)&addr));
	return &hostent_space;
err_dns:
	proxychains_write_log("|DNS-response|: %s is not exist\n", name);
err:
	return NULL;
}
Пример #24
0
void runSuccess() {
    gethostent();
}
Пример #25
0
static void
test_hosts (void)
{
  struct hostent *hptr1, *hptr2;
  char *name = NULL;
  size_t namelen = 0;
  struct in_addr ip;

  hptr1 = gethostbyname ("localhost");
  hptr2 = gethostbyname ("LocalHost");
  if (hptr1 != NULL || hptr2 != NULL)
    {
      if (hptr1 == NULL)
	{
	  printf ("localhost not found - but LocalHost found:-(\n");
	  ++error_count;
	}
      else if (hptr2 == NULL)
	{
	  printf ("LocalHost not found - but localhost found:-(\n");
	  ++error_count;
	}
      else if (strcmp (hptr1->h_name, hptr2->h_name) != 0)
	{
	  printf ("localhost and LocalHost have different canoncial name\n");
	  printf ("gethostbyname (\"localhost\")->%s\n", hptr1->h_name);
	  printf ("gethostbyname (\"LocalHost\")->%s\n", hptr2->h_name);
	  ++error_count;
	}
      else
	output_hostent ("gethostbyname(\"localhost\")", hptr1);
    }

  hptr1 = gethostbyname ("127.0.0.1");
  output_hostent ("gethostbyname (\"127.0.0.1\")", hptr1);

  hptr1 = gethostbyname ("10.1234");
  output_hostent ("gethostbyname (\"10.1234\")", hptr1);

  hptr1 = gethostbyname2 ("localhost", AF_INET);
  output_hostent ("gethostbyname2 (\"localhost\", AF_INET)", hptr1);

  while (gethostname (name, namelen) < 0 && errno == ENAMETOOLONG)
    {
      namelen += 2;		/* tiny increments to test a lot */
      name = realloc (name, namelen);
    }
  if (gethostname (name, namelen) == 0)
    {
      printf ("Hostname: %s\n", name);
      if (name != NULL)
	{
	  hptr1 = gethostbyname (name);
	  output_hostent ("gethostbyname (gethostname(...))", hptr1);
	}
    }

  ip.s_addr = htonl (INADDR_LOOPBACK);
  hptr1 = gethostbyaddr ((char *) &ip, sizeof(ip), AF_INET);
  if (hptr1 != NULL)
    {
      printf ("official name of 127.0.0.1: %s\n", hptr1->h_name);
    }

  sethostent (0);
  do
    {
      hptr1 = gethostent ();
      output_hostent ("gethostent ()", hptr1);
    }
  while (hptr1 != NULL);
  endhostent ();

}
Пример #26
0
void ReadHostsFile (const char *fname)
{
  static BOOL been_here = FALSE;

  if (!fname || !*fname)
     return;

  if (been_here)  /* loading multiple hosts files */
  {
    free (hostFname);
    fclose (hostFile);
    hostFile = NULL;
  }

  hostFname = strdup (fname);
  if (!hostFname)
     return;

  sethostent (1);
  if (!hostFile)
     return;

  been_here = TRUE;

  while (1)
  {
    struct  hostent *h = gethostent();
    struct _hostent *h2;
    int     i;

    if (!h)
       break;

    h2 = (struct _hostent*) calloc (sizeof(*h2), 1);
    if (!h2)
    {
      outs (hostFname);
      outsnl (_LANG(" too big!"));
      break;
    }

    for (i = 0; h->h_aliases[i]; i++)
        h2->h_aliases[i] = strdup (h->h_aliases[i]);
    h2->h_name       = strdup (h->h_name);
    h2->h_address[0] = *(DWORD*) h->h_addr_list[0];
    h2->h_num_addr   = 1;
    if (!h2->h_name)
       break;
    h2->h_next = host0;
    host0      = h2;
  }

#if 0  /* test !! */
  {
    const struct _hostent *h;
    int   i;

    printf ("\n%s entries:\n", hostFname);
    for (h = host0; h; h = h->h_next)
    {
      printf ("address = %-17.17s name = %-30.30s  Aliases:",
               inet_ntoa(*(struct in_addr*)&h->h_address[0]), h->h_name);
      for (i = 0; h->h_aliases[i]; i++)
          printf (" %s,", h->h_aliases[i]);
      puts ("");
    }
    fflush (stdout);
  }
#endif
  rewind (hostFile);
  RUNDOWN_ADD (endhostent, 254);
}
Пример #27
0
int main(int argc, char *argv[])
{
    int sockfd, portno, n,play_n,hops,i;
    struct sockaddr_in serv_addr;
    struct hostent *server,*my;
int rand_no;
char ip_arr[MAX][255];

my=gethostent();
 
    char buffer[256];
sockf = socket(AF_INET, SOCK_STREAM, 0);
     if (sockf< 0) 
        error("ERROR opening socket");
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,server->h_length);
    serv_addr.sin_port = htons(portno);
    while(connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) ;



portno=make_socket(sockf,fserver);

    n = send(sockfd,&portno,sizeof(int),0);
    if (n < 0) 
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = recv(sockfd,buffer,255,0);
    if (n < 0) 
         error("ERROR reading from socket");
    
    close(sockfd);
 ////////////////////
     int  newsockfd;
     socklen_t clilen;
     struct sockaddr_in  cli_addr;
     listen(sockf,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockf,(struct sockaddr *) &cli_addr,&clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
     bzero(buffer,256);
int mast_port;
     n = recv(newsockfd,&mast_port,sizeof(int),0);
     if (n < 0) error("ERROR reading from socket");
    

     n = send(newsockfd,"I got your message",18,0);
     if (n < 0) error("ERROR writing to socket");
     close(newsockfd);
     close(sockf);
int mast_sock;
int play;
struct sockaddr_in mast_str;
mast_sock = socket(AF_INET, SOCK_STREAM, 0);
     if (mast_sock< 0) 
        error("ERROR opening socket");
 bzero((char *) &mast_str, sizeof(mast_str));
    mast_str.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&mast_str.sin_addr.s_addr,
         server->h_length);
    mast_str.sin_port = htons(mast_port);
    while(connect(mast_sock,(struct sockaddr *) &mast_str,sizeof(mast_str)) < 0);

n = recv(mast_sock,&play,sizeof(int),0);
     if (n < 0) error("ERROR reading from socket");
printf("Connected as player %d\n",play);

int left_port,left_s;
struct sockaddr_in leftsock;
left_s = socket(AF_INET, SOCK_STREAM, 0);
if (left_s< 0) 
        error("ERROR opening socket");

left_port=make_socket(left_s,leftsock);



n = send(mast_sock,&left_port,sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");

n = recv(mast_sock,&play_n,sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");

n = recv(mast_sock,&hops,sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");


int j;
for(j=1;j<=play_n;j++)
n = recv(mast_sock,&left_arr[j],sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");


for(j=1;j<=play_n;j++)
n = recv(mast_sock,&ip_arr[j],256*sizeof(char),0);
     if (n < 0) error("ERROR writing to socket");

if(play_n==1 && hops==1)
{
/*char k='r';
n = send(mast_sock,&k,sizeof(char),0);
     if (n < 0) error("ERROR writing to socket");
printf("I'm it\n");
		char fini='f';
		l=l+1;
		n = send(mast_sock,&fini,sizeof(char),0);
		if (n < 0) error("ERROR writing to socket");
		n = send(mast_sock,&l,sizeof(int),0);
		if (n < 0) error("ERROR writing to socket");
		for(i=0;i<l+1;i++)
		{
		n = send(mast_sock,&potato[i],sizeof(int),0);
     		if (n < 0) error("ERROR writing to socket");
		//if(n>0) printf("data\n");
	
*/
exit(1);
}
/////*starts...........ring 

int right_sock,left_sock;
struct sockaddr_in righ,lef;
struct hostent *right_ent;

if(play==1)
{
if(play!=play_n)
{
      right_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (right_sock< 0) 
        error("ERROR opening socket");
     right_ent= gethostbyname(ip_arr[play+1]);
    if (right_ent == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &righ, sizeof(righ));
    righ.sin_family = AF_INET;
    bcopy((char *)right_ent->h_addr,(char *)&righ.sin_addr.s_addr,right_ent->h_length);
    righ.sin_port = htons(left_arr[play+1]);
    while(connect(right_sock,(struct sockaddr *) &righ,sizeof(righ)) < 0) ;
       
}
else if(play==play_n)
{
      right_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (right_sock< 0) 
        error("ERROR opening socket");
     right_ent= gethostbyname(ip_arr[1]);
    if (right_ent == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &righ, sizeof(righ));
    righ.sin_family = AF_INET;
    bcopy((char *)right_ent->h_addr, 
         (char *)&righ.sin_addr.s_addr,right_ent->h_length);
    righ.sin_port = htons(left_arr[1]);
    while(connect(right_sock,(struct sockaddr *) &righ,sizeof(righ)) < 0); 
        


}

listen(left_s,3);
clilen = sizeof(lef);

     left_sock = accept(left_s,(struct sockaddr *) &lef,&clilen);
     if (left_sock < 0) 
          error("ERROR on accept");
else
{

if(play==1)
{
char k='r';
n = send(mast_sock,&k,sizeof(char),0);
     if (n < 0) error("ERROR writing to socket");
}}

}
else
{
listen(left_s,3);
clilen = sizeof(lef);

     left_sock = accept(left_s,(struct sockaddr *) &lef,&clilen);
     if (left_sock < 0) 
          error("ERROR on accept");


if(play!=play_n)
{
      right_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (right_sock< 0) 
        error("ERROR opening socket");
     right_ent= gethostbyname(ip_arr[play+1]);
    if (right_ent == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &righ, sizeof(righ));
    righ.sin_family = AF_INET;
    bcopy((char *)right_ent->h_addr, 
         (char *)&righ.sin_addr.s_addr,right_ent->h_length);
    righ.sin_port = htons(left_arr[play+1]);
    while(connect(right_sock,(struct sockaddr *) &righ,sizeof(righ)) < 0); 
        

}
else if(play==play_n)
{
      right_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (right_sock< 0) 
        error("ERROR opening socket");
     right_ent= gethostbyname(ip_arr[1]);
    if (right_ent == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &righ, sizeof(righ));
    righ.sin_family = AF_INET;
    bcopy((char *)right_ent->h_addr, 
         (char *)&righ.sin_addr.s_addr,right_ent->h_length);
    righ.sin_port = htons(left_arr[1]);
   while(connect(right_sock,(struct sockaddr *) &righ,sizeof(righ)) < 0);

}
}

//Acknowledgement of ring completed 



//receive potato from left, right players or master


while(1)
{
fd_set monito;
int readsock;
int highfd;
FD_ZERO(&monito);

highfd=mast_sock;
FD_SET(mast_sock,&monito);

if(left_sock>highfd)
highfd=left_sock;
FD_SET(left_sock,&monito);

if(right_sock>highfd)
highfd=right_sock;
FD_SET(right_sock,&monito);


struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
readsock = select(highfd+1, &monito, (fd_set *) 0,(fd_set *) 0, &timeout);
if (readsock < 0) {
			perror("select");
			exit(EXIT_FAILURE);
		}
		 else if(readsock >0)
{
		if (FD_ISSET(mast_sock,&monito))
			{
		char p;
		n = recv(mast_sock,&p,sizeof(char),0);
     		
		if (n < 0) error("ERROR writing to socket");
		if(n>0 && p=='p')
		{
if(hops==1)
		{
int potato[1],l;
		printf("I'm it\n");
		char fini='f';
		l=1;
		potato[0]=play;
		n = send(mast_sock,&fini,sizeof(char),0);
		if (n < 0) error("ERROR writing to socket");
		n = send(mast_sock,&l,sizeof(int),0);
		if (n < 0) error("ERROR writing to socket");
		for(i=0;i<l;i++)
		{
		n = send(mast_sock,&potato[i],sizeof(int),0);
     		if (n < 0) error("ERROR writing to socket");
//		if(n>0) printf("data\n");
		}
}
else
{
		

time_t seco;
time(&seco);

srand(seco);
rand_no=rand();
i=(rand_no%(play*2))+1;
if(i<=play)
{
if(play!=1)
printf("Sending potato to %d\n",play-1);
else
printf("Sending potato to %d\n",play_n);
int k=play,no=1;
n = send(left_sock,&no,sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");
n = send(left_sock,&k,sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");
}
if(i>play)
{
if(play!=play_n)
printf("Sending potato to %d\n",play+1);
else
printf("Sending potato to 1\n");

int k=play,no=1;
n = send(right_sock,&no,sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");
n = send(right_sock,&k,sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");
}
}
		}
if(n>0 && p=='f')
{

close(mast_sock);
close(right_sock);
close(left_sock);
exit(EXIT_SUCCESS);

}			}
		if (FD_ISSET(left_sock,&monito))
			{
		
		int l,val;
		n = recv(left_sock,&l,sizeof(int),0);
		
     		if (n < 0) error("ERROR writing to socket");
		if(n>0)
		{	

		
		int potato[l+1];
		for(i=0;i<l;i++)
		{
		n = recv(left_sock,&val,sizeof(int),0);
     		if (n < 0) error("ERROR writing to socket");
		
		potato[i]=val;
		}
int ran_play;
potato[l]=play;
if(l==hops-1)
		{
printf("I'm it\n");
		char fini='f';
		l=l+1;
		n = send(mast_sock,&fini,sizeof(char),0);
		if (n < 0) error("ERROR writing to socket");
		n = send(mast_sock,&l,sizeof(int),0);
		if (n < 0) error("ERROR writing to socket");
		for(i=0;i<l+1;i++)
		{
		n = send(mast_sock,&potato[i],sizeof(int),0);
     		if (n < 0) error("ERROR writing to socket");
		//if(n>0) printf("data\n");
		}
				}
else
{
time_t seco;
time(&seco);
srand(seco);
rand_no=rand();
ran_play=(rand_no%(play*2))+1;
//printf("Random play: %d\n",ran_play);
if(ran_play<=play)
{
if(play!=1)
printf("Sending potato to %d\n",play-1);
else
printf("Sending potato to %d\n",play_n);

int k=play,no=l+1;
n = send(left_sock,&no,sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");
for(i=0;i<l+1;i++)
		{
		n = send(left_sock,&potato[i],sizeof(int),0);
     		if (n < 0) error("ERROR writing to socket");
		}
}
if(ran_play>play)
{
if(play!=play_n)
printf("Sending potato to %d\n",play+1);
else
printf("Sending potato to 1\n");

int k=play,no=l+1;
n = send(right_sock,&no,sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");
for(i=0;i<l+1;i++)
		{
		n = send(right_sock,&potato[i],sizeof(int),0);
     		if (n < 0) error("ERROR writing to socket");
		}
}		
		}
		
}			}
		if (FD_ISSET(right_sock,&monito))
			{
		
		int l,val;
		n = recv(right_sock,&l,sizeof(int),0);
     		if (n < 0) error("ERROR writing to socket");
		if(n>0)
		{

		
		int potato[l+1];
		for(i=0;i<l;i++)
		{
		n = recv(right_sock,&val,sizeof(int),0);
     		if (n < 0) error("ERROR writing to socket");
		//printf("%d : %d\n",i,val);
		potato[i]=val;
		}
int ran_play;
potato[l]=play;
if(l==hops-1)
		{
		printf("I'm it\n");
		char fini='f';
		l=l+1;
		n = send(mast_sock,&fini,sizeof(char),0);
		if (n < 0) error("ERROR writing to socket");
		n = send(mast_sock,&l,sizeof(int),0);
		if (n < 0) error("ERROR writing to socket");
		for(i=0;i<l+1;i++)
		{
		n = send(mast_sock,&potato[i],sizeof(int),0);
     		if (n < 0) error("ERROR writing to socket");
		//if(n>0) printf("data\n");
		}
		
		}
else
{
time_t seco;
time(&seco);
srand(seco);
rand_no=rand();
ran_play=(rand_no%(play*2))+1;
//printf("Random play: %d\n",ran_play);
if(ran_play<=play)
{
if(play!=1)
printf("Sending potato to %d\n",play-1);
else
printf("Sending potato to %d\n",play_n);

int k=play,no=l+1;
n = send(left_sock,&no,sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");
for(i=0;i<l+1;i++)
		{
		n = send(left_sock,&potato[i],sizeof(int),0);
     		if (n < 0) error("ERROR writing to socket");
		}
}
if(ran_play>play)
{
if(play!=play_n)
printf("Sending potato to %d\n",play+1);
else
printf("Sending potato to 1\n");


int k=play,no=l+1;
n = send(right_sock,&no,sizeof(int),0);
     if (n < 0) error("ERROR writing to socket");
for(i=0;i<l+1;i++)
		{
		n = send(right_sock,&potato[i],sizeof(int),0);
     		if (n < 0) error("ERROR writing to socket");
		}
}		
		}
		
}		
			}

	
}
}


}
Пример #28
0
struct hostent *
nw_gethostent()
{
        return(gethostent());
}
Пример #29
0
static void
find_another_host (ACE_TCHAR other_host[])
{
  static ACE_TCHAR cached_other_host[MAXHOSTNAMELEN] = {'\0'};

  if (cached_other_host[0] == '\0')
    {

      ACE_OS::strcpy (other_host,
                      ACE_DEFAULT_SERVER_HOST); // If all else fails

#if !defined (ACE_LACKS_GETHOSTENT)
      // These gethost-type things don't work everywhere.
      struct hostent *h = 0;
      ACE_utsname un;

      ACE_OS::uname (&un);

      h = ACE_OS::gethostbyname (un.nodename);

      if (h == 0)
        ACE_OS::strcpy (other_host, ACE_LOCALHOST);
      else
        // Use me if can't find another
        ACE_OS::strcpy (other_host, ACE_TEXT_CHAR_TO_TCHAR (h->h_name));

      // @@ We really need to add wrappers for these hostent methods.

      // Optimize for sequential access of DNS or hosts file.
      sethostent (1);

      int candidate_count = 0;

      // Accumulate candidates first.  There is some interaction on
      // Linux systems between <gethostent> and <gethostbyname_r>
      // (called by ACE_INET_Addr in host_is_up) This otherwise causes
      // an infinite loop on Linux --mas 03-08-2001
      while ((h = gethostent ()) != 0)
        {
          if (ACE_OS::strcmp (h->h_name,
                              ACE_TEXT_ALWAYS_CHAR (ACE_DEFAULT_SERVER_HOST)) == 0)
            continue;
          // AIX just _has_ to be different
          if (ACE_OS::strcmp (h->h_name, "loopback") == 0)
            continue;

          // If not me.
          if (ACE_OS::strcmp
                (h->h_name, ACE_TEXT_ALWAYS_CHAR (other_host)) != 0
              && ACE_OS::strcmp (h->h_name, un.nodename) != 0)
            {
               ACE_OS::strcpy (candidate[candidate_count].host_name,
                               ACE_TEXT_CHAR_TO_TCHAR (h->h_name));
               if (++candidate_count >= MAX_CANDIDATES)
                 break;
            }
        }

      // Now try to connect to candidates
      for (int i = 0; i < candidate_count; i++)
        if (host_is_up (candidate[i].host_name))
          {
            ACE_OS::strcpy (other_host, candidate[i].host_name);
            break;
          }

      sethostent (0);
      endhostent ();
#endif /* ! ACE_LACKS_GETHOSTENT */

      ACE_OS::strcpy (cached_other_host, other_host);
    }
  else
    ACE_OS::strcpy (other_host, cached_other_host);
}
Пример #30
0
/* :call-seq:
 *    Sys::Host.info
 *
 * Returns an array of HostInfo structs containing various bits of
 * information about the local machine for each entry in the hosts
 * table.
 *
 * The Struct::HostInfo struct contains 5 fields:
 *
 * * name      (String)
 * * aliases   (Array)
 * * addr_type (Integer) => Typically 2 (AF_INET) or 28 (AF_INET6)
 * * length    (Integer) => Typically 4 (IPv4) or 16 (IPv6)
 * * addr_list (Array)
 */
static VALUE host_info(VALUE klass){
  char ibuf[INET6_ADDRSTRLEN];
  struct hostent* host;
  VALUE v_hostinfo, v_aliases, v_addr;
  VALUE v_array = rb_ary_new();

  sethostent(0);

#ifdef HAVE_GETHOSTENT_R
  struct hostent temp;
  char sbuf[HOSTENT_BUF];
  int err;
#endif

#ifdef HAVE_GETHOSTENT_R
  while(!gethostent_r(&temp, sbuf, HOSTENT_BUF, &host, &err)){
#else
  while((host = gethostent())){
#endif
    char **aliases = host->h_aliases;
    char **addrs = host->h_addr_list;
    v_aliases = rb_ary_new();
    v_addr    = rb_ary_new();

    while(*aliases){
      rb_ary_push(v_aliases, rb_str_new2(*aliases));
      *aliases++;
    }

    while(*addrs){
      if(!inet_ntop(host->h_addrtype, addrs, ibuf, sizeof(ibuf)))
        rb_raise(cHostError, "inet_ntop() failed: %s", strerror(errno));

      rb_ary_push(v_addr, rb_str_new2(ibuf));
      *addrs++;
    }

    v_hostinfo = rb_struct_new(sHostInfo,
      rb_str_new2(host->h_name),
      v_aliases,
      INT2FIX(host->h_addrtype),
      INT2FIX(host->h_length),
      v_addr
    );

    OBJ_FREEZE(v_hostinfo);
    rb_ary_push(v_array, v_hostinfo);
  }

  endhostent();

  return v_array;
}

#ifdef HAVE_GETHOSTID
/*
 * Sys::Host.host_id
 *
 * Returns the host id of the current machine.
 */
static VALUE host_host_id(){
  return ULL2NUM(gethostid());
}
#endif

void Init_host()
{
   VALUE sys_mSys, cHost;

   /* The Sys module serves as a toplevel namespace, nothing more. */
   sys_mSys = rb_define_module("Sys");

   /* The Host class encapsulates information about your machine, such as
    * the host name and IP address.
    */
   cHost = rb_define_class_under(sys_mSys, "Host", rb_cObject);

   /* This error is raised if any of the Host methods fail. */
   cHostError = rb_define_class_under(cHost, "Error", rb_eStandardError);

   /* 0.6.3: The version of this library. This is a string, not a number. */
   rb_define_const(cHost, "VERSION", rb_str_new2(SYS_HOST_VERSION));
   
   /* Structs */
   sHostInfo = rb_struct_define("HostInfo", "name", "aliases", "addr_type",
      "length", "addr_list", NULL
   );

   /* Class Methods */
   rb_define_singleton_method(cHost, "hostname", host_hostname, 0);
   rb_define_singleton_method(cHost, "ip_addr", host_ip_addr, 0);
   rb_define_singleton_method(cHost, "info", host_info, 0);

#ifdef HAVE_GETHOSTID
   rb_define_singleton_method(cHost, "host_id", host_host_id, 0);
#endif
}