コード例 #1
0
ファイル: senslip.c プロジェクト: EDAyele/wsn430
void write_to_serial(int outfd, void *inbuf, int len) {
	u_int8_t *p = inbuf;
	int i, ecode;
	struct ip *iphdr = inbuf;

	/*
	 * Sanity checks.
	 */
	ecode = check_ip(inbuf, len);
	if (ecode < 0) {
		fprintf(stderr, "tun_to_serial: drop packet %d\n", ecode);
		return;
	}

	if (iphdr->ip_id == 0 && iphdr->ip_off & IP_DF) {
		uint16_t nid = htons(ip_id++);
		iphdr->ip_id = nid;
		nid = ~nid; /* negate */
		iphdr->ip_sum += nid; /* add */
		if (iphdr->ip_sum < nid) { /* 1-complement overflow? */
			iphdr->ip_sum++;
		}
		ecode = check_ip(inbuf, len);
		if (ecode < 0) {
			fprintf(stderr, "tun_to_serial: drop packet %d\n", ecode);
			return;
		}
	}

	/* It would be ``nice'' to send a SLIP_END here but it's not
	 * really necessary.
	 */
	/* slip_send(outfd, SLIP_END); */

	for (i = 0; i < len; i++) {
		switch (p[i]) {
		case SLIP_END:
			slip_send(outfd, SLIP_ESC);
			slip_send(outfd, SLIP_ESC_END);
			break;
		case SLIP_ESC:
			slip_send(outfd, SLIP_ESC);
			slip_send(outfd, SLIP_ESC_ESC);
			break;
		default:
			slip_send(outfd, p[i]);
			break;
		}

	}
	slip_send(outfd, SLIP_END);
	PROGRESS("t");
}
コード例 #2
0
ファイル: fm_arpdect.c プロジェクト: xiaomailong/fms
int main(int argc, char *argv[])
{
	in_addr_t	dst_ip;
	unsigned char	*mac;
	in_addr_t	ip;
	int		chkip;

	if ( argc < 2 ) {
		printf("too few argument\n");
		exit(-1);
	}
	dst_ip = inet_addr(argv[1]);
	mac = (char*)malloc(16);
	get_if_info("eth0", mac, &ip);

	chkip = check_ip("eth0", dst_ip);
	if ( chkip < 0 ){
		printf("check_ip error\n");
		return -1;
	}else if ( chkip == 0 ) 
		printf("IP free\n");
	else
		printf("IP has been used\n");

	return 0;
}
コード例 #3
0
int main(int argc, char *argv[])
{
	char *ip, ip_tmp[32];
	unsigned short int port = 3306; /* mysql default port */
	
	setvbuf(stdout, NULL, _IONBF, 0);
	textcolor(BRIGHT, RED, BLACK);
	fprintf(stderr, "\n\t[   finger_mysql.c  ]");
	fprintf(stderr, "\n\t[      by st0ic     ]");
	fprintf(stderr, "\n\t[      Fsix.Net     ]");
	textcolor(BRIGHT, BLUE, BLACK);
	fprintf(stderr, "\n\n\t---> Don't Be Scer'ed!\n\n");
	textcolor(RESET, DEFAULT, DEFAULT);
	
	if (argc < 2 || argc > 3)
	{
		fprintf(stderr, "usage: %s <ip> [port]\n", argv[0]);
		fprintf(stderr, "\t ip\t Required. C Class IP to scan (ex. 10.219.1.).\n");
		fprintf(stderr, "\t port\t Optional. MySQL port to connect to (ex. 3306).\n");
		fprintf(stderr, "\t Examples: %s 192.168.1. 3306\n", argv[0]);
		fprintf(stderr, "\t           %s 192.168.1. > dump.txt\n\n", argv[0]);
		exit(1);
	}
	ip = argv[1];
	sprintf(ip_tmp, "%s", ip);
	if (argv[2] != NULL)
		port = ((unsigned short int)atoi(argv[2]));
	bzero(&sockfd, sizeof(sockfd));
	
	check_ip (ip_tmp);
	parse_ip (ip);
	start_threads(ip, port);
	
	return 0;
}
コード例 #4
0
ファイル: dhcpd.c プロジェクト: vilpalu/GPL_2.6.31
int check_and_ack(struct dhcpMessage* packet, uint32_t ip)
{
	uint32_t static_ip = 0;
	
	/* There is an Static IP for this guy, whether it is requested or not */
	if ((static_ip = getIpByMac(server_config.static_leases, packet->chaddr)) != 0)
	{
		msglogd(LOG_INFO, LOGTYPE_DHCP, "DHCPS:REQUEST ip %x, static ip %x", ip, static_ip);
		return sendACK(packet, static_ip);
	}

	/* requested ip is reserved by a static lease -- if it is himself, situation above match */
	if (reservedIp(server_config.static_leases, ip))
	{
		msglogd(LOG_INFO, LOGTYPE_DHCP, "DHCPS:REQUEST ip %x is reserved as a static ip", ip);
		return sendNAK(packet);
	}
	
	/* if some one reserve it */
	if ( ip != packet->ciaddr && check_ip(packet, ip) )
	{
		msglogd(LOG_INFO, LOGTYPE_DHCP, "DHCPS:REQUEST ip %x already reserved by someone", ip);
		return sendNAK(packet);
	}

	if (ntohl(ip) < ntohl(server_config.start) || ntohl(ip) > ntohl(server_config.end))
	{
		msglogd(LOG_INFO, LOGTYPE_DHCP, "DHCPS:REQUEST ip %x is not in the address pool", ip);
		return sendNAK(packet);
	}
	
	return sendACK(packet, ip);
}
コード例 #5
0
ファイル: module-radegast.c プロジェクト: joolzg/oscam
static void radegast_auth_client(in_addr_t ip)
{
  int32_t ok;
  struct s_auth *account;
  struct s_client *cl = cur_client();

  ok = check_ip(cfg.rad_allowed, ip);

  if (!ok)
  {
    cs_log ("radegast: IP not allowed");
    cs_auth_client(cl, (struct s_auth *)0, NULL);
    cs_disconnect_client(cl);
  }

  for (ok=0, account=cfg.account; (cfg.rad_usr[0]) && (account) && (!ok); account=account->next)
  {
    ok=(!strcmp(cfg.rad_usr, account->usr));
    if (ok && cs_auth_client(cl, account, NULL))
       cs_disconnect_client(cl);
  }

  if (!ok)
    cs_auth_client(cl, ok ? account : (struct s_auth *)(-1), "radegast");
}
コード例 #6
0
void testcase(int index , int size)
{
	//printf("inside testcase");	
	int checkip=0;
	strcpy(testname,input_arra[index]);
	checkip=check_ip(size);
	if(checkip!=0)
	{
		strcpy(ip," ");
		strcpy(ip,input_arra[checkip+1]);
	}
	//printf("testcase name %s is  checking on ip = %s and ",testname, ip);	
	int checks=-1;
	checks=check_s(index,size);	
	if(checks != -1)
	{
		teststream=atoi(input_arra[checks]);
		printf("stream number is %d  \n",teststream);	
		checks=teststream;	
	}
	
	if( !strcmp(testname,"FR") || !strcmp(testname,"fr"))	
	{
		//printf("inside fr");		
		 tc_fr(checks , checkip);

	}

}
コード例 #7
0
ファイル: module-radegast.c プロジェクト: kamyk11/oscam
static void radegast_auth_client(IN_ADDR_T ip)
{
  int32_t ok;
  struct s_auth *account;
  struct s_client *cl = cur_client();

  ok = check_ip(cfg.rad_allowed, ip);

  if (!ok)
  {
    cs_log ("radegast: IP not allowed");
    cs_auth_client(cl, (struct s_auth *)0, NULL);
    cs_disconnect_client(cl);
  }

  for (ok = 0, account = cfg.account; cfg.rad_usr && account && !ok; account = account->next)
  {
    ok = streq(cfg.rad_usr, account->usr);
    if (ok && cs_auth_client(cl, account, NULL))
       cs_disconnect_client(cl);
  }

  if (!ok)
    cs_auth_client(cl, ok ? account : (struct s_auth *)(-1), "radegast");
}
コード例 #8
0
ファイル: leases.c プロジェクト: emuikernel/WNR2000v4
/* find an assignable address, it check_expired is true, we check all the expired leases as well.
 * Maybe this should try expired leases by age... */
uint32_t find_address(int check_expired)
{
	uint32_t addr, ret;
	struct dhcpOfferedAddr *lease = NULL;

	addr = ntohl(server_config.start); /* addr is in host order here */
	for (;addr <= ntohl(server_config.end); addr++) {

		/* ie, 192.168.55.0 */
		if (!(addr & 0xFF)) continue;

		/* ie, 192.168.55.255 */
		if ((addr & 0xFF) == 0xFF) continue;

		/* Only do if it isn't an assigned as a static lease */
		if (!reservedIp(server_config.static_leases, htonl(addr))) {

			/* lease is not taken */
			ret = htonl(addr);
			lease = find_lease_by_yiaddr(ret);

			/* no lease or it expired and we are checking for expired leases */
			if ( (!lease || (check_expired && lease_expired(lease)))
			 && /* and it isn't on the network */ !check_ip(ret)
			) {
				return ret;
				break;
			}
		}
	}
	return 0;
}
コード例 #9
0
ファイル: tls.c プロジェクト: hutchinson/freetds
static int
tds_check_ip(gnutls_x509_crt_t cert, const char *hostname)
{
	int ret;
	union {
		struct in_addr v4;
		struct in6_addr v6;
	} ip;
	unsigned ip_size;

	/* check whether @hostname is an ip address */
	if (strchr(hostname, ':') != NULL) {
		ip_size = 16;
		ret = inet_pton(AF_INET6, hostname, &ip.v6);
	} else {
		ip_size = 4;
		ret = inet_pton(AF_INET, hostname, &ip.v4);
	}

	if (ret != 0)
		ret = check_ip(cert, &ip, ip_size);

	/* There are several misconfigured servers, that place their IP
	 * in the DNS field of subjectAlternativeName. Don't break these
	 * configurations and verify the IP as it would have been a DNS name. */

	return ret;
}
コード例 #10
0
ファイル: module-monitor.c プロジェクト: kamyk11/oscam
static int8_t monitor_check_ip(void)
{
	int32_t ok=0;
	struct s_client *cur_cl = cur_client();

	if (cur_cl->auth) return 0;
	ok = check_ip(cfg.mon_allowed, cur_cl->ip);
	if (!ok)
	{
		cs_auth_client(cur_cl, (struct s_auth *)0, "invalid ip");
		return -1;
	}
	return 0;
}
コード例 #11
0
ファイル: module-radegast.c プロジェクト: timoRo/oscam-stable
static void radegast_auth_client(in_addr_t ip)
{
  int32_t ok;
  struct s_auth *account;

  ok = check_ip(cfg.rad_allowed, ip);

  if (!ok)
  {
    cs_auth_client(cur_client(), (struct s_auth *)0, NULL);
    cs_exit(0);
  }

  for (ok=0, account=cfg.account; (cfg.rad_usr[0]) && (account) && (!ok); account=account->next)
  {
    ok=(!strcmp(cfg.rad_usr, account->usr));
    if (ok && cs_auth_client(cur_client(), account, NULL))
      cs_exit(0);
  }

  if (!ok)
    cs_auth_client(cur_client(), (struct s_auth *)(-1), NULL);
}
コード例 #12
0
u_int32_t find_address(int check_expired) 
#endif
{
	u_int32_t addr, ret = 0;
	struct dhcpOfferedAddr *lease = NULL;		

#ifdef NK_CONFIG_SUPPORT_MULTI_DHCP_SUBNET
	addr = ntohl(selected_dhcp_pool->start_ip.s_addr);
	for (;ntohl(addr) <= ntohl(selected_dhcp_pool->end_ip.s_addr) ;addr = htonl(ntohl(addr) + 1)) {

		if (addr == ntohl(selected_dhcp_pool->server_ip.s_addr)) continue;

		if (ret == selected_dhcp_pool->server_ip.s_addr) continue;
#else
	addr = ntohl(server_config.start);
	for (;ntohl(addr) <= ntohl(server_config.end) ;addr = htonl(ntohl(addr) + 1)) {

		if (addr == ntohl(server_config.server)) continue;

		if (ret == server_config.server) continue;
#endif
		/* ie, 192.168.55.0 */
		if (!(ntohl(addr) & 0xFF)) continue;

		/* ie, 192.168.55.255 */
		if ((ntohl(addr) & 0xFF) == 0xFF) continue;

		/* lease is not taken */
		if ((!(lease = find_lease_by_yiaddr(addr)) ||

		     /* or it expired and we are checking for expired leases */
		     (check_expired  && lease_expired(lease))) &&

		     /* and it isn't on the network */
	    	     !check_ip(addr)) {
			ret = addr;
			break;
		}
	}
	return ret;
}


/* check is an IP is taken, if it is, add it to the lease table */
int check_ip(u_int32_t addr)
{
	char blank_chaddr[] = {[0 ... 15] = 0};
	struct in_addr temp;
	int i;//fix if lease is static IP,don't check arp
	for (i = 0; i < server_config.max_leases; i++) {
		if (leases[i].yiaddr == addr && leases[i].is_static == 1) {
			return 0;
		}
	}	
	if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
		temp.s_addr = addr;
	 	LOG(LOG_INFO, "%s belongs to someone, reserving it for %ld seconds", 
	 		inet_ntoa(temp), server_config.conflict_time);
/* 2007/07/30 jane: bug fix dhcp status do not show client's host name */
#if 1
		add_lease(blank_chaddr, addr, server_config.conflict_time, NULL, (u_int32_t)0);
#else
		add_lease(blank_chaddr, addr, server_config.conflict_time);
#endif
		return 1;
	} else return 0;
}
コード例 #13
0
ファイル: leases.c プロジェクト: wingedboar/rtl819x-toolchain
/* find an assignable address, it check_expired is true, we check all the expired leases as well.
 * Maybe this should try expired leases by age... */
u_int32_t find_address(int check_expired) 
{
	u_int32_t addr, ret;
	struct dhcpOfferedAddr *lease = NULL;		

	addr = ntohl(server_config.start); /* addr is in host order here */
	for (;addr <= ntohl(server_config.end); addr++) {
#if 1
		/* Network IP and BroadcastIP 
		ie. netmask 255.255.255.0, server ip 192.168.55.1
		can't lease 192.168.55.0 or 192.168.55.255
		*/
		if(addr==(server_config.netmask & server_config.server))
			continue;
		if(addr==((~(server_config.netmask))|(server_config.server)))
			continue;
#else
		/* ie, 192.168.55.0 */
		if (!(addr & 0xFF)) continue;

		/* ie, 192.168.55.255 */
		if ((addr & 0xFF) == 0xFF) continue;
#endif
		/* not lease server ip */
        if(addr == server_config.server) continue;
        
#ifdef STATIC_LEASE
		/* Only do if it isn't an assigned as a static lease */		
		if(!reservedIp(server_config.static_leases, htonl(addr)))
#endif		
		{
			/* lease is not taken */
			ret = htonl(addr);
			if ((!(lease = find_lease_by_yiaddr(ret)) ||

			     /* or it expired and we are checking for expired leases */
			     ( (check_expired == 1)  && lease_expired(lease))) &&

			     /* and it isn't on the network */
		    	     !check_ip(ret)) {
				return ret;
			}
			else if(check_expired == 2) //some one leases this ip and this ip is not expired.
			{
				int arpping_time = 0;
				int ret_arpping = 0;
				
//printf("\r\n Any one use this ip[%x]?",ret);
				for(arpping_time = 0; arpping_time<3; arpping_time++)
				{
					char ret_hwaddr[6];
					if (arpping(ret, server_config.server, server_config.arp, server_config.interface, ret_hwaddr) == 0)
					{
						ret_arpping = 1;
						break;
					}					
				}

				// But this ip is not alive in network. we release this ip
				if ( ret_arpping == 1)
				{
//printf(" YES.__[%s-%u]\r\n",__FILE__,__LINE__);					
				}
				else
				{
//printf(" NO.__[%s-%u]\r\n",__FILE__,__LINE__);
					return ret;
				}				
			}
		}
	}
	return 0;
}
コード例 #14
0
ファイル: senslip.c プロジェクト: EDAyele/wsn430
/*
 * Read from serial, when we have a packet write it to tun. No output
 * buffering, input buffered by stdio.
 */
void serial_to_tun(int sockfd, int outfd) {
	static union {
		unsigned char inbuf[2000];
		struct ip iphdr;
	} uip;
	static unsigned int inbufptr = 0;

	int ret;
	unsigned char c;
	static unsigned char old_c = 0;

#ifdef linux
	if (old_c)
	{
		c = old_c;
		old_c = 0;
		ret = 1;
	}
	else
	{
		ret = recv(sockfd, &c, 1, 0);
		if(ret == -1 || ret == 0)
		{
			return;
			err(1, "serial_to_tun: read");
		}
	}
	goto after_fread;
#endif

	read_more: if (inbufptr >= sizeof(uip.inbuf)) {
		inbufptr = 0;
	}
	ret = recv(sockfd, &c, 1, 0);
#ifdef linux
	after_fread:
#endif
	if (ret == -1) {
		return;
		err(1, "serial_to_tun: read");
	}
	if (ret == 0) {
		//~ clearerr(inslip);
		return;
		fprintf(stderr, "serial_to_tun: EOF\n");
		exit(1);
	}
	//~ fprintf(stderr, "|%2x",c);
	switch (c) {
	case SLIP_END:
		if (inbufptr > 0) {
			/*
			 * Sanity checks.
			 */
#define DEBUG_LINE_MARKER '\r'
			int ecode;
			ecode = check_ip(&uip.iphdr, inbufptr);
			if (ecode < 0 && inbufptr == 8 && strncmp((char *)uip.inbuf, "=IPA", 4)
					== 0) {
				static struct in_addr ipa;

				inbufptr = 0;
				if (memcmp(&ipa, &uip.inbuf[4], sizeof(ipa)) == 0) {
					break;
				}

				/* New address. */
				if (ipa.s_addr != 0) {
#ifdef linux
					ssystem("route delete -net %s netmask %s dev %s",
							inet_ntoa(ipa), "255.255.255.255", tundev);
#else
					ssystem("route delete -net %s -netmask %s -interface %s",
							inet_ntoa(ipa), "255.255.255.255", tundev);
#endif
				}

				memcpy(&ipa, &uip.inbuf[4], sizeof(ipa));
				if (ipa.s_addr != 0) {
#ifdef linux
					ssystem("route add -net %s netmask %s dev %s",
							inet_ntoa(ipa), "255.255.255.255", tundev);
#else
					ssystem("route add -net %s -netmask %s -interface %s",
							inet_ntoa(ipa), "255.255.255.255", tundev);
#endif
				}
				break;
			} else if (ecode < 0) {
				/*
				 * If sensible ASCII string, print it as debug info!
				 */
				if (uip.inbuf[0] == DEBUG_LINE_MARKER) {
					fwrite(uip.inbuf + 1, inbufptr - 1, 1, stderr);
				} else if (is_sensible_string(uip.inbuf, inbufptr)) {
					fwrite(uip.inbuf, inbufptr, 1, stderr);
				} else {
					fprintf(stderr,
							"serial_to_tun: drop packet len=%d ecode=%d\n",
							inbufptr, ecode);
				}
				inbufptr = 0;
				break;
			}
			PROGRESS("s");

			if (dhsock != -1) {
				struct ip *ip = (void *) uip.inbuf;
				if (ip->ip_p == 17 && ip->ip_dst == 0xffffffff /* UDP and broadcast */
				&& ip->uh_sport == ntohs(BOOTPC) && ip->uh_dport == ntohs(
						BOOTPS)) {
					relay_dhcp_to_server(ip, inbufptr);
					inbufptr = 0;
				}
			}
			if (write(outfd, uip.inbuf, inbufptr) != inbufptr) {
				err(1, "serial_to_tun: write");
			}
			inbufptr = 0;
		}
		break;

	case SLIP_ESC:
		ret = recv(sockfd, &c, 1, 0);
		//~ if(fread(&c, 1, 1, inslip) != 1) {
		if (ret != 1) {
			old_c = SLIP_ESC;
			//~ clearerr(inslip);
			/* Put ESC back and give up! */
			//~ ungetc(SLIP_ESC, inslip);
			return;
		}

		switch (c) {
		case SLIP_ESC_END:
			c = SLIP_END;
			break;
		case SLIP_ESC_ESC:
			c = SLIP_ESC;
			break;
		}
		/* FALLTHROUGH */
	default:
		uip.inbuf[inbufptr++] = c;
		break;
	}

	goto read_more;
}
コード例 #15
0
ファイル: functions.c プロジェクト: kmkangutkar/mini-proj
int solve(int **puz, int *x, int *y, int display){
/*solves the given sudoku stored in puz, returns 1 if solve successful, returns WRONG_IP if i/p was invalid*/
	int *p, s = 0, count = 0, r, q;
	srand(time(NULL));
	int i, j, val;
	istack w;
	char c, chk;
	pstack POS;
	init(&POS);
	chk = check_ip(puz);
	if(chk == WRONG_IP){
		return WRONG_IP;
	}
	if(x == NULL){
		i = j = 0;
	}
	else{
		i = *x;
		j = *y;
	}
	while(i != ROW){
		empty_cell(puz, &i, &j);
		if(!f.found){
			if(display){
				display_puz(puz);
				refresh();
			}
			return 1;	
		}
		p = scan(puz, i, j);
		if(p == NULL){
			return WRONG_IP;	
		}
		w = pos_val(p);
		check:
		if(i_empty(&w)){
			/*no possible values*/
			if(!empty(&POS)){
				w = pop(&POS);
				puz[i][j] = EMPTY;
				if(display){
					display_puz(puz);
					refresh();
				}	
				count--;
				i = cell[count].x;
				j = cell[count].y;
				goto check;
			}	
		}
		else if(!i_empty(&w)){
			val = i_pop(&w);
			push(&POS, w);
			cell[count].x = i;
			cell[count].y = j;
			count++;
		}
		puz[i][j] = val;
		if(display){
			display_puz(puz);
			refresh();
		}
		traverse_line(&i, &j);
	}
	f.flag = 0;
	return 1;
}
コード例 #16
0
ファイル: bonds-groups.c プロジェクト: kichiki/libstokes
struct BONDS_GROUPS *
BONDS_GROUPS_make (struct BONDS *b, int np)
{
  // sort bonds by ia[]
  BONDS_sort_by_ia (b);
  /*
  // check BONDS after sorting
  int i;
  fprintf (stdout, "# b->n = %d =? %d\n", b->n, np);
  for (i = 0; i < b->n; i ++)
    {
      fprintf (stdout, "# b->(ia,ib)[%d] = %d, %d\n",
	       i, b->ia[i], b->ib[i]);
    }
  */

  // make nb[np] : number of bonds for each particle
  int *nb = (int *)calloc (np, sizeof (int));
  CHECK_MALLOC (nb, "BONDS_make_groups");

  int i;
  for (i = 0; i < b->n; i ++)
    {
      if (b->ia[i] < 0 || b->ia[i] >= np)
	{
	  fprintf (stderr, "# BONDS_make_groups()"
		   " : ia=%d is out of range for np=%d\n",
		   b->ia[i], np);
	  exit (1);
	}
      if (b->ib[i] < 0 || b->ib[i] >= np)
	{
	  fprintf (stderr, "# BONDS_make_groups()"
		   " : ib=%d is out of range for np=%d\n",
		   b->ib[i], np);
	  exit (1);
	}

      nb[b->ia[i]] ++;
      nb[b->ib[i]] ++;
    }
  /*
  // check
  for (i = 0; i < np; i ++)
    {
      fprintf (stdout, "# nb[%d] = %d\n", i, nb[i]);
    }
  */

  // make bond[np][nb] : bond list for each particle
  int **bond = (int **)malloc (np * sizeof (int *));
  CHECK_MALLOC (bond, "BONDS_make_groups");
  for (i = 0; i < np; i ++)
    {
      bond[i] = (int *)malloc (nb[i] * sizeof (int));
      CHECK_MALLOC (bond[i], "BONDS_make_groups");
      int j;
      for (j = 0; j < nb[i]; j ++)
	{
	  bond[i][j] = -1;
	}
    }
  for (i = 0; i < b->n; i ++)
    {
      int ia = b->ia[i];
      int j;
      for (j = 0; j < nb[ia]; j ++)
	{
	  if (bond[ia][j] == -1)
	    {
	      bond[ia][j] = i;
	      //fprintf (stdout, "# set (ia) bond[%d][%d] = %d\n", ia, j, i);
	      break;
	    }
	}
      if (bond[ia][j] == -1)
	{
	  fprintf (stderr, "# BONDS_make_groups()"
		   " : something is wrong on bond[ia]...\n");
	  exit (1);
	}

      int ib = b->ib[i];
      for (j = 0; j < nb[ib]; j ++)
	{
	  if (bond[ib][j] == -1)
	    {
	      bond[ib][j] = i;
	      //fprintf (stdout, "# set (ib) bond[%d][%d] = %d\n", ib, j, i);
	      break;
	    }
	}
      if (bond[ib][j] == -1)
	{
	  fprintf (stderr, "# BONDS_make_groups()"
		   " : something is wrong on bond[ib]...\n");
	  exit (1);
	}
    }
  // check
  for (i = 0; i < np; i ++)
    {
      //fprintf (stdout, "# bond[%d] :", i);
      int j;
      for (j = 0; j < nb[i]; j ++)
	{
	  //fprintf (stdout, " %d", bond[i][j]);
	  if (bond[i][j] == -1)
	    {
	      fprintf (stderr, "# BONDS_make_groups()"
		       " : bond[%d][%d] is not assigned.\n",
		       i, j);
	      exit (1);
	    }
	}
      //fprintf (stdout, "\n");
    }


  struct loop_bonds *lb = loop_bonds_init ();

  int *gid = (int *)malloc (np * sizeof (int));
  CHECK_MALLOC (gid, "BONDS_make_groups");
  for (i = 0; i < np; i ++)
    {
      gid[i] = -1;
    }

  int ig = 1;
  for (i = 0; i < np; i ++)
    {
      if (gid[i] != -1) continue;

      if (nb[i] == 0)
	{
	  // single particle
	  gid[i] = 0;
	  continue;
	}

      gid[i] = ig;
      BONDS_GROUPS_gid_check (b,
			      np, gid,
			      nb, bond,
			      i, ig,
			      lb);
      ig ++;
    }
  // check
  for (i = 0; i < np; i ++)
    {
      //fprintf (stdout, "# gid[%d] = %d\n", i, gid[i]);
      if (gid[i] == -1)
	{
	  fprintf (stderr, "# undefined particle %d\n", i);
	}
    }
  /*
  // check
  fprintf (stdout, "# lb->n = %d\n", lb->n);
  for (i = 0; i < lb->n; i ++)
    {
      fprintf (stdout, "# lb->b[%d] = %d\n", i, lb->b[i]);
    }
  */

  // make BONDS_GROUPS
  struct BONDS_GROUPS *gs = BONDS_GROUPS_init ();
  for (i = 0; i < np; i ++)
    {
      if (gid[i] == -1)
	{
	  continue;
	}
      else if (gid[i] == 0)
	{
	  // single particle
	  BONDS_GROUPS_add (gs, 1, &i, NULL);
	}
      else
	{
	  int nn;
	  int *ip = BONDS_GROUPS_gid_pickup (np, gid, gid[i], &nn);
	  /*
	  fprintf (stdout, "# np = %d\n", nn);
	  int ii;
	  for (ii = 0; ii < nn; ii ++)
	    {
	      fprintf (stdout, "# ip[%d] = %d\n", ii, ip[ii]);
	    }
	  exit (1);
	  */

	  // make bonds[]
	  int *bonds = (int *)malloc (sizeof (int) * (nn - 1));
	  CHECK_MALLOC (bonds, "BONDS_make_groups");

	  int k = 0;
	  int j;
	  for (j = 0; j < b->n; j ++)
	    {
	      if (check_ip (lb->n, lb->b, j) == 0 &&
		  (check_ip (nn, ip, b->ia[j]) == 1 ||
		   check_ip (nn, ip, b->ib[j]) == 1))
		{
		  if (k == (nn - 1))
		    {
		      fprintf (stderr, "too many bonds\n");
		      exit (1);
		    }
		  /*
		  fprintf (stdout, "# bond %d is the %d-th member\n",
			   j, k);
		  */
		  bonds[k] = j;
		  k ++;
		}
	    }
	  if (k != (nn - 1))
	    {
	      fprintf (stderr, "too few bonds, isn't it?"
		       " %d for %d particles\n", k, nn);
	      exit (1);
	    }

	  BONDS_GROUPS_add (gs, nn, ip, bonds);

	  free (ip);
	  free (bonds);
	}
    }


  // house keeping
  loop_bonds_free (lb);
  free (gid);
  for (i = 0; i < np; i ++)
    {
      free (bond[i]);
    }
  free (bond);
  free (nb);

  return (gs);
}
コード例 #17
0
ファイル: main.cpp プロジェクト: vipnormalhy/learn
int main(int argc, const char *const *argv) {
	bool is_server = false;
	bool is_client = false;
	std::string ip_str;
	int port;
	// Parse command options
	po::options_description options_desc("Allowed Options");
	options_desc.add_options()
		("help", "produce help message")
		("listen_ip", po::value<std::string>(), "if starts like a server,need a listen ip and port")
		("listen_port", po::value<int>(), "if starts like a server,need a listen port and ip")
		("conn_ip", po::value<std::string>(), "connect server ip")
		("conn_port", po::value<int>(), "connect server port");

	po::variables_map vm;
	po::store(po::parse_command_line(argc, argv, options_desc), vm);
	po::notify(vm);

	if (argc <= 1 || vm.count("help")) {
		usage();
		std::cout << options_desc << std::endl;
		return OK;
	}

	if (vm.count("listen_ip")) {
		ip_str = vm["listen_ip"].as<std::string>();
		if (!check_ip(ip_str)) {
			std::cout << "IP ERROR" << std::endl;
			return -1;
		}
		is_server = true;
	}

	if (vm.count("listen_port")) {
		port = vm["listen_port"].as<int>();
		if (!check_port(port)) {
			std::cout << "PORT ERROR" << std::endl;
			return -1;
		}

		if (vm.count("conn_ip")) {
			is_client = true;
		}

		if (!is_server) {
			std::cout << "HAS NO LISTEN IP!" << std::endl;
		}
	}

	// starts a server
	boost::shared_ptr<CTcpServer> server_ptr = NULL;
	boost::shared_ptr<CUdpServer> udp_server_ptr = NULL;
	if (is_server) {
		boost::asio::ip::address ip_addr = boost::asio::ip::make_address(ip_str);
		server_ptr = boost::make_shared<CTcpServer>(ip_addr, static_cast<unsigned short>(port));
		server_ptr->run();

		udp_server_ptr = boost::make_shared<CUdpServer>(ip_addr, static_cast<unsigned short>(port));
		udp_server_ptr->run();
	}

	// starts a client
	boost::shared_ptr<CTcpClient> client_ptr = NULL;
	if (is_client) {
		std::string client_ip_str(vm["conn_ip"].as<std::string>());
		client_ptr = boost::make_shared<CTcpClient>(client_ip_str, vm["conn_port"].as<int>());
		client_ptr->async_connect();
	}

	common_io_context.run();

	return OK;
}
コード例 #18
0
void testcase(int index , int size)
{
	//printf("inside testcase");	
	int checkip=0 , checkdel =0 ,checkd ;
	strcpy(testname,input_arra[index]);
	checkip=check_ip(size);
	checkd=check_d(size);
	checkdel=check_delta(size);
	if(checkdel != -1)
	{
		//strcpy(ip," ");
		delta=0;		
		delta=atof(input_arra[checkdel]);
		//printf("delta is %f  \n",delta);	
	}
	else
	{
		delta=0.10;
		//printf("delta is %f we\n",delta);	
	}	
	if(checkd != -1)
	{
		//strcpy(ip," ");
		d_value=0;		
		d_value=atof(input_arra[checkd]);
		//printf("delta is %f  \n",delta);	
	}
	else
	{
		d_value=2;
		//printf("delta is %f we\n",delta);	
	}	

	if(checkip != 0)
	{
				
		strcpy(ip," ");
		strcpy(ip,input_arra[checkip]);
	}
	//printf("testcase name %s is  checking on ip = %s and ",testname, ip);	
	int checks=-1;
	checks=check_s(index,size);	
	if(checks != -1)
	{
		teststream=atoi(input_arra[checks]);
		//printf("stream number is %d  \n",teststream);	
		checks=teststream;	
		if(checks>node_count)
		{
			printf(" \t stream number entered is out of range ! \n ");
			return;
		}
	}
	
	if( !strcmp(testname,"FR") || !strcmp(testname,"fr"))	
	{
		
		 tc_fr(checks , checkip );

	}
	else if( !strcmp(testname,"RT") || !strcmp(testname,"rt"))
	{
			
				 tc_rt(checks , checkip);
	}
	else
	{

		printf("wrong test case name \n");
	}		

}
コード例 #19
0
ファイル: serverpacket.c プロジェクト: jeremyd2019/r6300v2
/* send a DHCP OFFER to a DHCP DISCOVER */
int sendOffer(struct dhcpMessage *oldpacket)
{
	struct dhcpMessage packet;
	struct dhcpOfferedAddr *lease = NULL;
	u_int32_t req_align, lease_time_align = server_config.lease;
	unsigned char *req, *lease_time;
	struct option_set *curr;
	struct in_addr addr;
    unsigned char mac[6];
	u_int32_t reserved_ip;

    memcpy(mac, oldpacket->chaddr, 6);
    
	init_packet(&packet, oldpacket, DHCPOFFER);
	
	/* ADDME: if static, short circuit */
	/* the client is in our lease/offered table */
	if ((lease = find_lease_by_chaddr(oldpacket->chaddr)) &&
        
        /* Make sure the IP is not already used on network */
        !check_ip(lease->yiaddr)) {

		if (!lease_expired(lease)) 
			lease_time_align = lease->expires - time(0);
		packet.yiaddr = lease->yiaddr;
		
    /* Find a reserved ip for this MAC */
	} else if ( (reserved_ip = find_reserved_ip(mac)) != 0) {
		packet.yiaddr = htonl(reserved_ip);
        
	/* Or the client has a requested ip */
	} else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) &&

		/* Don't look here (ugly hackish thing to do) */
		memcpy(&req_align, req, 4) &&

        /* check if the requested ip has been reserved */
        check_reserved_ip(req_align, mac) &&
           
		/* and the ip is in the lease range */
		ntohl(req_align) >= ntohl(server_config.start) &&
		ntohl(req_align) <= ntohl(server_config.end) &&

         /* Check that this request ip is not on network */
         //!check_ip(ntohl(req_align)) &&
	 /* the input parameter of check_ip() should be network order */
	 !check_ip(req_align) && /*  modified by Max Ding, 07/07/2011 @TD #42 of WNR3500Lv2 */
           
		/* and its not already taken/offered */ /* ADDME: check that its not a static lease */
		((!(lease = find_lease_by_yiaddr(req_align)) ||
		   
		/* or its taken, but expired */ /* ADDME: or maybe in here */
		lease_expired(lease)))) {
		    packet.yiaddr = req_align; 

	/* otherwise, find a free IP */ /*ADDME: is it a static lease? */
	} else {
		packet.yiaddr = find_address2(0, mac);
		
		/* try for an expired lease */
		if (!packet.yiaddr) packet.yiaddr = find_address2(1, mac);
	}
	
	if(!packet.yiaddr) {
		LOG(LOG_WARNING, "no IP addresses to give -- OFFER abandoned");
		return -1;
	}
	
	if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {
		LOG(LOG_WARNING, "lease pool is full -- OFFER abandoned");
		return -1;
	}		

	if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
		memcpy(&lease_time_align, lease_time, 4);
		lease_time_align = ntohl(lease_time_align);
		if (lease_time_align > server_config.lease) 
			lease_time_align = server_config.lease;
	}

	/* Make sure we aren't just using the lease time from the previous offer */
	if (lease_time_align < server_config.min_lease) 
		lease_time_align = server_config.lease;

	/* For guest network clients, set lease time to 30 minutes */
	if (is_guest_network(mac)) {
		lease_time_align = GUEST_LEASE_TIME;
		DEBUG(LOG_INFO, "send OFFER to guest network client with lease time %d sec", GUEST_LEASE_TIME);
	}

	/* ADDME: end of short circuit */		
	add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));

	curr = server_config.options;
	while (curr) {
		if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
			add_option_string(packet.options, curr->data);
		curr = curr->next;
	}

	add_bootp_options(&packet);
	
	addr.s_addr = packet.yiaddr;
	LOG(LOG_INFO, "sending OFFER of %s", inet_ntoa(addr));
	return send_packet(&packet, 0);
}
コード例 #20
0
// Check the integrity of tablefile
bool tablecheck(FILE *table) {

	// Check IP vals
	if(!check_ip(table)) {
		// IP vals are invalid
		fprintf(stderr, "ERROR: tablecheck failed for IP (Initial Permutation) values.\n");
		return false;
	}

	// Check E vals
	if(!check_e(table)) {
		// E vals are invalid
		fprintf(stderr, "ERROR: tablecheck failed for E (Expansion) values.\n");
		return false;
	}

	// Check P vals
	if(!check_p(table)) {
		// P vals are invalid
		fprintf(stderr, "ERROR: tablecheck failed for P (Permutation) values.\n");
		return false;
	}
	
	// Check S# vals
	char header[3];
	for(int i=1; i<=8; i++) {
		sprintf(header, "S%d=", i);
		if(!check_s(table, header)) {
			// S# vals are invalid
			fprintf(stderr, "ERROR: tablecheck failed for S%d (Substitution) values.\n", i);
			return false;
		}
	}
	
	// Check V vals
	if(!check_v(table)) {
		// V vals are invalid
		fprintf(stderr, "ERROR: tablecheck failed for V (Circular Rotation) values.\n");
		return false;
	}
	
	// Check PC1 vals
	if(!check_pc1(table)) {
		// PC1 vals are invalid
		fprintf(stderr, "ERROR: tablecheck failed for PC1 (Permutated Choice 1) values.\n");
		return false;
	}
	
	// Check PC2 vals
	if(!check_pc2(table)) {
		// PC2 vals are invalid
		fprintf(stderr, "ERROR: tablecheck failed for PC2 (Permutated Choice 2) values.\n");
		return false;
	}
	
	return true;
	
	// PSEUDO-CODE
	// - Use read_line_vals method to search through file until finds a line starting with header
	// - Read each item in the line into an array of ints, using comma delimiters to separate
	// - Run whatever test is needed on the array to ensure that all the values are valid
	//   - Usually something like counting the instances of each number to ensure correct permutation
	// - Repeat for all headers
}
コード例 #21
0
ファイル: main.c プロジェクト: hikinggrass/eiwomisarc_server
/* mainloop */
int mymain(int port, char *serialport, int baud, char *validip)
{
	/* check if port, serialport and baudrate are set, otherwise use defaults */
	if (port == -1) {
		msg_Info("No Port set - using 1337");
		port = 1337;
	}

	if (serialport == NULL) {
		msg_Info("No Serialport set - using /dev/ttyS0");
		serialport = "/dev/ttyS0";
	}

	if (baud == -1) {
		msg_Info("No Baudrate set - using 9600");
		baud = 9600;
	}

	int sock;
	struct sockaddr_in server;
	struct sockaddr_in client;

	unsigned char buffer[BUFFSIZE];

	unsigned int clientlen, serverlen;
	int received = 0;

	/* create the UDP socket */
	if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
		die("Failed to create socket\n");
	}

	/* construct the server sockaddr_in structure */
	memset(&server, 0, sizeof(server));			/* Clear struct */
	server.sin_family = AF_INET;				/* Internet/IP */
	server.sin_addr.s_addr = htonl(INADDR_ANY);	/* Any IP address */
	server.sin_port = htons(port);				/* server port */

	/* bind the socket */
	serverlen = sizeof(server);
	if (bind(sock, (struct sockaddr *) &server, serverlen) < 0) {
		die("Failed to bind server socket\n");
	}
	
	/* signal handler */
	signal(SIGTERM,sigfunc);
	signal(SIGINT,sigfunc);

	/* open serial port */
	global_serialport = open_port(serialport, baud);
	
	/* wait for UDP-packets */
	while (42) {
		/* Receive a message from the client */
		clientlen = sizeof(client);

		if ((received = recvfrom(sock, buffer, BUFFSIZE, 0,
								 (struct sockaddr *) &client,
								 &clientlen)) < 0) {
			die("Failed to receive message\n");
		}
		
		if(validip != NULL && client.sin_addr.s_addr != check_ip(validip)) {
			msg_Info("Wrong client tried to connect to server: %s", inet_ntoa(client.sin_addr));
		} else {
			msg_Info("Client connected: %s", inet_ntoa(client.sin_addr));
			
			if (checkbuffer(buffer) == 0) {
				msg_Dbg("buffer0-5: '%s'", buffer);
				
				/* RS-232 Code start */
				int n = write(global_serialport, buffer, BUFFSIZE);
				
				if (n < 0) {
					msg_Err("write() failed!");
				} else {
					msg_Dbg("Value(s) written to serial port");
				}
				/* RS-232 Code end */
			}
		}
	}
	
	/* close serial port */
	close(global_serialport);
    return 0;
}
コード例 #22
0
ファイル: client.c プロジェクト: LiTianjue/work-proj
int client_handle_ss5(client_t *client,list_t *list,char *err_code)
{
	//0		成功
	//> 0   错误码长度
	//< 0	出现错误但是不需要返回给客户端
	
	int ret = 0;
	char retcode[32] = {0};
	uint8_t host[256] = {0};
	if(client->status == CLIENT_STATUS_DONE)
		return 0;

	socks5_method_req_t *req;			
	struct ss_requests_frame *frame;	

	char *buf_ptr;
	int buf_len;
	buf_ptr = client->tcp_data.buf;
	buf_len = client->tcp_data.len;
	if(buf_len < 2)
	{
		return -1;	// tcp data
	}

	switch(client->status)
	{
		case CLIENT_STATUS_DONE:
			return 0;
		case CLIENT_STATUS_NEW:
			//检查版本号,认证方法
			req = (socks5_method_req_t* )buf_ptr;
			if(req->ver != SOCKS5_VERSION)
			{
				//err on version
				//ret = 2;
				//retcode[0] = SOCKS5_VERSION;
				//retcode[1] = 0XFF;
				ret = -1;
				break;
			}
			if((req->nmethods == 0x01)&&(req->methods[0]==SOCKS5_METHOD_USER_PSW ))
			{
				//用户名密码认证子协议
				client->status = CLIENT_STATUS_USER;
			}
			else
			{
				// fixme:
				client->status = CLIENT_STATUS_DONE;
			}
			break;
		case CLIENT_STATUS_USER:
			//密码认证子协议
			if(0)
			{
				;
			}
			client->status = CLIENT_STATUS_METHOD;
			break;
		case CLIENT_STATUS_METHOD:
			frame  = (struct ss_requests_frame *)buf_ptr;
			if(frame->ver != SOCKS5_VERSION)
			{
			 	//error version
				ret  = -1;
				break;
			}
			if(frame->cmd == CMD_CONNECT)
			{
				if(frame->rsv != SOCKS5_REV)
				{
					//error rev;
					ret = -1;
					break;
				}
				if(frame->atyp == ATYPE_IPV4)
				{
					inet_ntop(AF_INET,frame->dst_addr,host,INET_ADDRSTRLEN);
					if((ip_verify == 0) || check_ip(list,host)==0)/*ip allowd*/
					{
						if(g_debug)
							printf("[Allow]Client Try to Connect [%s] allowd\n",host);
						//printf("IP[%s] is allowd\n",host);
					
					}else
					{
						if(g_debug)
							printf("[Refuse]Client Try to Connect [%s] refuse\n",host);
						//printf("IP[%s] is deny\n",host);
						retcode[0] = SOCKS5_VERSION;
						retcode[1] = REP_CONNECT_NOT_ALLOWED;
						retcode[2] = SOCKS5_REV;
						retcode[3] = ATYPE_IPV4;
						ret = 4;
						break;
					}

				}else if(frame->atyp == ATYPE_DOMAINNAME)
				{
					uint8_t wlen = frame->dst_addr[0];
					memcpy(host,frame->dst_addr+1,wlen);
					host[wlen] ='\0';
				 
					if((ip_verify == 0) || check_ip(list,host)==0)/*ip allowd*/
					{
						if(g_debug)
							printf("[Allow]Client Try to Connect [%s] allowd\n",host);
					}else
					{
						if(g_debug)
							printf("[Refuse]Client Try to Connect [%s] refuse\n",host);
						retcode[0] = SOCKS5_VERSION;
						retcode[1] = REP_CONNECT_NOT_ALLOWED;
						retcode[2] = SOCKS5_REV;
						retcode[3] = ATYPE_DOMAINNAME;
						ret = 4;
						break; 
					}
					
				}else if(frame->atyp == ATYPE_IPV6)
				{ 
					// fixme:
				 	client->status = CLIENT_STATUS_DONE;
				}else
				{
					ret = -1;
				 	break;
				}
			
			} else if (frame->cmd == CMD_BIND)
			{
				;
			} else if (frame->cmd == CMD_UDP)
			{
				;
			}
			else
			{
				ret = -1;
				break;
			}
			// fixme:
			client->status = CLIENT_STATUS_DONE;
			break;

		default:
			break;
	}

	if(ret >  0)
	{
		if(retcode != NULL)
			memcpy(err_code,retcode,ret);
	
	}
	
	return ret;

}
コード例 #23
0
/*
 * Check whether a rule matches the provided info.
 */
static bool
rule_matches(rule_t rule, const char * dbname, const char * user, SockAddr ip, char * hostname) {

	/* only one of the CHECK_IP / CHECK_HOST flags can be set */
	Assert(!((rule.fields & CHECK_IP) && (rule.fields & CHECK_HOST)));

	/* dbname does not match */
	if ((rule.fields & CHECK_DBNAME) && (strcmp(rule.database, dbname) != 0))
		return false;

	/* username does not match */
	if ((rule.fields & CHECK_USER) && (strcmp(rule.user, user) != 0))
		return false;

	/* check the IP address (mask etc.) */
	if (rule.fields & CHECK_IP)
		if (! check_ip(&ip, (struct sockaddr *)&rule.ip, (struct sockaddr *)&rule.mask))
			return false;

	if ((rule.fields & CHECK_HOST) && (strcmp(rule.hostname, hostname) != 0))
	{

		int ret;
		bool found = false;
		struct addrinfo *gai_result, *gai;

		/* was the reverse lookup successfull? */
		if (hostname && (! hostname_match(rule.hostname, hostname)))
			return false;

		ret = getaddrinfo(rule.hostname, NULL, NULL, &gai_result);

		if (ret != 0)
			ereport(WARNING,
					(errmsg("could not translate host name \"%s\" to address: %s",
							rule.hostname, gai_strerror(ret))));

		for (gai = gai_result; gai; gai = gai->ai_next)
		{

			if (gai->ai_addr->sa_family == ip.addr.ss_family)
			{

				if (gai->ai_addr->sa_family == AF_INET)
				{
					if (ipv4eq((struct sockaddr_in *) gai->ai_addr,
							(struct sockaddr_in *) & ip.addr))
					{
						found = true;
						break;
					}
				}
#ifdef HAVE_IPV6
				else if (gai->ai_addr->sa_family == AF_INET6)
				{
					if (ipv6eq((struct sockaddr_in6 *) gai->ai_addr,
							(struct sockaddr_in6 *) & ip.addr))
					{
						found = true;
						break;
					}
				}
#endif
			}
		}

		if (gai_result)
			freeaddrinfo(gai_result);

		if (! found) {
			elog(WARNING, "pg_hba.conf host name \"%s\" rejected because address "
						  "resolution did not return a match with IP address of client",
						  rule.hostname);
			return false;
		}
	}

	return true;

}
コード例 #24
0
/* send a DHCP OFFER to a DHCP DISCOVER */
int sendOffer(struct dhcpMessage *oldpacket, struct dhcpOfferedAddr *lease)
{
	struct dhcpMessage packet;
	u_int32_t req_align, lease_time_align = server_config.lease;
	unsigned char *req, *lease_time;
	struct option_set *curr;
	struct in_addr addr;
	unsigned long offer_time = server_config.offer_time;

	init_packet(&packet, oldpacket, DHCPOFFER);
	
	/* ADDME: if static, short circuit */
	/* the client is in our lease/offered table */
	if (lease) {
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"(1)the client is in our lease/offered table");
#endif
		if(lease->expires == EXPIRES_NEVER) {
			/* static lease */
			packet.yiaddr = lease->yiaddr;
			offer_time = EXPIRES_NEVER; /* offer infinite time */
		} else if(!check_ip(lease->yiaddr)){	// Confirm the ip isn't used by someone, by honor
			if (!lease_expired(lease)) 
				lease_time_align = lease->expires - get_time(0);
			else
				goto find_ip;
			packet.yiaddr = lease->yiaddr;
		}
		else
			goto find_ip;
		
	/* Or the client has a requested ip */
	} else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) &&

		   /* Don't look here (ugly hackish thing to do) */
		   memcpy(&req_align, req, 4) &&

		   /* and the ip is in the lease range */
		   ntohl(req_align) >= ntohl(server_config.start) &&
		   ntohl(req_align) <= ntohl(server_config.end) &&
		   
		   /* and its not already taken/offered */ /* ADDME: check that its not a static lease */
		   ((!(lease = find_lease_by_yiaddr(req_align)) ||
		   
		   /* or its taken, but expired */ /* ADDME: or maybe in here */
		   lease_expired(lease)))) {
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"(2)the client has a requested ip");
#endif
		if(req_align == server_config.server)   // The request ip cann't be router ip, by honor
			goto find_ip;
		else if(!check_ip(req_align))		// Confirm the ip isn't used by someone, by honor
			packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */
		else
			goto find_ip;

	/* otherwise, find a free IP */ /*ADDME: is it a static lease? */
	} else {
find_ip:	// by honor
#ifdef MY_DEBUG
	LOG(LOG_DEBUG,"(3)find a free IP");
#endif
		packet.yiaddr = find_address(0);
		
		/* try for an expired lease */
		if (!packet.yiaddr) packet.yiaddr = find_address(1);
	}
	
	if(!packet.yiaddr) {
		LOG(LOG_WARNING, "no IP addresses to give -- OFFER abandoned");
		return -1;
	}
	
	if (!add_lease(packet.chaddr, packet.yiaddr, offer_time)) {
		LOG(LOG_WARNING, "lease pool is full -- OFFER abandoned");
		return -1;
	}

	if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
		memcpy(&lease_time_align, lease_time, 4);
		lease_time_align = ntohl(lease_time_align);
		if (lease_time_align > server_config.lease) 
			lease_time_align = server_config.lease;
	}

	/* Make sure we aren't just using the lease time from the previous offer */
	if (lease_time_align < server_config.min_lease) 
		lease_time_align = server_config.lease;
	/* ADDME: end of short circuit */	
	add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));

	curr = server_config.options;
	while (curr) {
		if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
			add_option_string(packet.options, curr->data);
		curr = curr->next;
	}

	add_bootp_options(&packet);
	
	addr.s_addr = packet.yiaddr;
	LOG(LOG_INFO, "broadcasting OFFER of %s to %02x:%02x:%02x:%02x:%02x:%02x", inet_ntoa(addr),
	    packet.chaddr[0], packet.chaddr[1], packet.chaddr[2], 
	    packet.chaddr[3], packet.chaddr[4], packet.chaddr[5]);
	return send_packet(&packet, 1);		// Send broadcast rather than unicast, by honor
}