Example #1
0
/**
 * Convert IP address or domain name and port into struct sockaddr_in
 */
struct sockaddr_in addr_parse(char* ip, int port) {
	struct sockaddr_in taddr;
	int ret;

	memset(&taddr, 0x00, sizeof(taddr));

	taddr.sin_family = AF_INET;
	taddr.sin_port = htons(otcfg_target_port);
	ret = inet_aton(ip, &taddr.sin_addr);
	if (ret == 0) {
		char* ipstr = NULL;

		OT_LOGI("Resolving domain name `%s'\n", otcfg_target_host);

		ipstr = dns_query(ip, NULL);
		if (ipstr == NULL) {
			OT_LOGE("Could not resolve domain `%s'\n", otcfg_target_host);
			return taddr;
		}
		else {
			OT_LOGI("Resolved domain: %s --> %s\n", otcfg_target_host, ipstr);
		}

		inet_aton(ipstr, &taddr.sin_addr);
	}

	return taddr;
}
Example #2
0
//----------------------------------------------------------------------------
//resolve domain name
unsigned char dns_resolve (char *name)
{
    dns_state = DNS_STATE_IDLE;
  
    dns_query(name);

    if (dns_state != DNS_STATE_REQ_SENT)
    {
        return(1);
    }
    gp_timer = 30; //is decremented in timer.c

    do
    {
        eth_get_data();
    }
    while ( (dns_state == DNS_STATE_REQ_SENT) &&
          (gp_timer > 0                   )     );
          
    if ( ( dns_state != DNS_STATE_FINISHED) ||
         ( gp_timer == 0 )                       )
    {
        return(1);
    }
    return (0);
}
Example #3
0
static int8 mn_dns(menu_ctrl mctrl, int8 *mbuf)
{
	uint8 domain_ip[4];

	if(mctrl == MC_START) {
		printf("Enter the Domain name you want to check (ex: www.abc.com)\r\n");
	} else if(mctrl == MC_END) {

	} else if(mctrl == MC_DATA) {	//printf("start dns\r\n");
		if(mbuf[0] == 0 || strchr((char*)mbuf, '.') == NULL) {
			printf("wrong input(%s)\r\n", mbuf);
			return RET_NOK;
		}

		if(dns_query(SOCK_DNS, (uint8*)mbuf, domain_ip) == RET_OK) {
			printf("IP Address of (%s) is (%d.%d.%d.%d)\r\n", mbuf,
				domain_ip[0], domain_ip[1], domain_ip[2], domain_ip[3]);
		}else {
			printf("DNS fail\r\n");
		}
		return RET_OK;
	}

	return RET_NOK;
}
Example #4
0
/**
 * dns_resolve_server_name_to_ip - Resolve UNC server name to ip address.
 * @unc: UNC path specifying the server (with '/' as delimiter)
 * @ip_addr: Where to return the IP address.
 *
 * The IP address will be returned in string form, and the caller is
 * responsible for freeing it.
 *
 * Returns length of result on success, -ve on error.
 */
int
dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
{
	struct sockaddr_storage ss;
	const char *hostname, *sep;
	char *name;
	int len, rc;

	if (!ip_addr || !unc)
		return -EINVAL;

	len = strlen(unc);
	if (len < 3) {
		cifs_dbg(FYI, "%s: unc is too short: %s\n", __func__, unc);
		return -EINVAL;
	}

	/* Discount leading slashes for cifs */
	len -= 2;
	hostname = unc + 2;

	/* Search for server name delimiter */
	sep = memchr(hostname, '/', len);
	if (sep)
		len = sep - hostname;
	else
		cifs_dbg(FYI, "%s: probably server name is whole unc: %s\n",
			 __func__, unc);

	/* Try to interpret hostname as an IPv4 or IPv6 address */
	rc = cifs_convert_address((struct sockaddr *)&ss, hostname, len);
	if (rc > 0)
		goto name_is_IP_address;

	/* Perform the upcall */
	rc = dns_query(NULL, hostname, len, NULL, ip_addr, NULL);
	if (rc < 0)
		cifs_dbg(FYI, "%s: unable to resolve: %*.*s\n",
			 __func__, len, len, hostname);
	else
		cifs_dbg(FYI, "%s: resolved: %*.*s to %s\n",
			 __func__, len, len, hostname, *ip_addr);
	return rc;

name_is_IP_address:
	name = kmalloc(len + 1, GFP_KERNEL);
	if (!name)
		return -ENOMEM;
	memcpy(name, hostname, len);
	name[len] = 0;
	cifs_dbg(FYI, "%s: unc is IP, skipping dns upcall: %s\n",
		 __func__, name);
	*ip_addr = name;
	return 0;
}
Example #5
0
struct hostent * gethostbyaddr(const void *addr, socklen_t len,
		int type) {
	int ret;
	struct hostent *he;
	struct dns_result result;
	struct dns_rr *rr;
	struct in_addr in;
	char hostaddr[] = "xxx.xxx.xxx.xxx.in-addr.arpa";
	size_t i;

	if ((type != AF_INET) || (len != sizeof in)) {
		return NULL; /* error: IPv4 supported only */
	}

	memcpy(&in, addr, sizeof in);
	in.s_addr = swab32(in.s_addr);

	if (0 > snprintf(&hostaddr[0], ARRAY_SIZE(hostaddr),
			"%s.in-addr.arpa", inet_ntoa(in))) {
		return NULL;
	}

	ret = dns_query(&hostaddr[0], DNS_RR_TYPE_PTR, DNS_RR_CLASS_IN, &result);
	if (ret != 0) {
		h_errno = HOST_NOT_FOUND;
		return NULL;
	}

	if (((he = hostent_create()) == NULL)
			|| (hostent_set_addr_info(he, AF_INET, len) != 0)
			|| (hostent_add_addr(he, addr) != 0)) {
		dns_result_free(&result);
		return NULL;
	}

	for (i = 0, rr = result.an; i < result.ancount; ++i, ++rr) {
		switch (rr->rtype) {
		default:
			ret = 0;
			break;
		case DNS_RR_TYPE_PTR:
			ret = hostent_set_name(he, &rr->rdata.ptr.ptrdname[0]);
			break;
		}
		if (ret != 0) {
			dns_result_free(&result);
			return NULL;
		}
	}

	dns_result_free(&result);

	return he;
}
Example #6
0
ssize_t nfs_dns_resolve_name(char *name, size_t namelen,
		struct sockaddr *sa, size_t salen)
{
	ssize_t ret;
	char *ip_addr = NULL;
	int ip_len;

	ip_len = dns_query(NULL, name, namelen, NULL, &ip_addr, NULL);
	if (ip_len > 0)
		ret = rpc_pton(ip_addr, ip_len, sa, salen);
	else
		ret = -ESRCH;
	kfree(ip_addr);
	return ret;
}
Example #7
0
int main(int argc, char **argv) {
	int ret;
	struct dns_result result;
	struct dns_rr *rr;
	size_t i;

	if (argc != 2) {
		printf("Usage: %s name\n", argv[0]);
		return -EINVAL;
	}

	ret = dns_query(argv[1], DNS_RR_TYPE_A, DNS_RR_CLASS_IN, &result);
	if (ret != 0) {
		return ret;
	}

	/* lookup info */
	printf("Server: %s\n", dns_get_nameserver());
	printf("Address: %s#%d\n", dns_get_nameserver(), DNS_PORT_NUMBER);

	/* answer */
	printf("\n%s:\n", result.nscount != 0 ? "Authoritative answer"
			: "Non-authoritative answer");
	for (i = 0, rr = result.an; i < result.ancount; ++i, ++rr) {
		if (rr->rtype == DNS_RR_TYPE_A) {
			printf("Name: %s\tAddress: %s\n", &rr->rname[0],
					inet_ntoa(*(struct in_addr *)&rr->rdata.a.address[0]));
		}
	}

	/* authoritive */
	if (result.nscount != 0) {
		printf("\nAuthoritative nameservers:\n");
		for (i = 0, rr = result.ns; i < result.nscount; ++i, ++rr) {
			printf("Name: %s\tNameserver: %s\n", &rr->rname[0],
					&rr->rdata.ns.nsdname[0]);
		}
	}

	dns_result_free(&result);

	return 0;
}
void resolver_lookup_impl(Callback<Error, std::string> callback,
                          Settings settings,
                          SharedPtr<Reactor> reactor,
                          SharedPtr<Logger> logger) {
  dns_query("IN", "A", "whoami.akamai.net",
      [=](Error error, SharedPtr<dns::Message> message) {
        if (!error) {
          for (auto answer : message->answers) {
            if (answer.ipv4 != "") {
              logger->debug("ip address of resolver is %s",
                            answer.ipv4.c_str());
              callback(NoError(), answer.ipv4);
              return;
            }
          }
        } else {
          logger->debug("failed to lookup resolver ip address");
          callback(error, "");
          return;
        }
      }, settings, reactor, logger);
}
static int lib_dns_query(FAR const char *hostname,
                         FAR struct sockaddr *addr, socklen_t *addrlen)
{
  int sd;
  int ret;

  /* Create and bind a socket to the DNS server */

  sd = dns_bind();
  if (sd < 0)
    {
      return sd;
    }

  /* Perform the query to get the IP address */

  ret = dns_query(sd, hostname, addr, addrlen);

  /* Release the socket */

  close(sd);
  return ret;
}
Example #10
0
static int8 mn_email(menu_ctrl mctrl, int8 *mbuf)
{
#define SET_STAGE(next_name_v, cur_var_v) \
{ \
	if(strlen((char*)mbuf) > 31) printf("buf overflow - try again\r\n"); \
	else { \
		strcpy((char*)cur_var_v, (char*)mbuf); \
		printf("Type a "next_name_v"\r\n"); \
		stage++; \
	} \
}
	uint8 ret;
	static uint8 stage = 0, ip[4];
	static int8 sender[32], passwd[32], recipient[32], subject[32];

	if(mctrl == MC_START) {
		printf("Enter Mail Server Address [Domain Name/IP Address]\r\n");
	} else if(mctrl == MC_END) {
		stage = 0;
	} else if(mctrl == MC_DATA) {
		switch(stage) {
		case 0:	// Server IP
			if(ip_check(mbuf, ip) == RET_NOK) {
				if(dns_query(SOCK_DNS, (void *)mbuf, ip) == RET_OK) {
					printf("DNS success - IP Address is (%d.%d.%d.%d)\r\n\r\n",	
						ip[0], ip[1], ip[2], ip[3]);
					printf("Type a Sender\r\n");
					stage = 1;
				} else {
					printf("DNS failed\r\n\r\n");
					printf("Enter Mail Server Address [Domain Name/IP Address]\r\n");
				}
			} else {
				printf("Type a Sender\r\n");
				stage = 1;
			}
			break;
		case 1:	// Sender
			SET_STAGE("Password", sender);
			break;
		case 2:	// Password
			SET_STAGE("Recipient", passwd);
			break;
		case 3:	// Recipient
			SET_STAGE("Subject", recipient);
			break;
		case 4:	// Subject
			SET_STAGE("message" , subject); 
			break;
		case 5:	// Message
			ret = send_mail(SOCK_SMTP, (uint8*)sender, (uint8*)passwd, 
				(uint8*)recipient, (uint8*)subject, (uint8*)mbuf, ip);
			if(ret == RET_OK) printf("mail send success\r\n");
			else printf("mail send fail\r\n");
			return RET_OK;
		default: printf("wrong stage(%d)\r\n", stage);
		}
	}

	return RET_NOK;

#undef SET_STAGE
}
Example #11
0
int ot_accept_client(int fd) {
	int tfd;
	int ret;
	int flags;
	struct sockaddr_in taddr;

	taddr.sin_family = AF_INET;
	taddr.sin_port = htons(otcfg_target_port);
	ret = inet_aton(otcfg_target_host, &taddr.sin_addr);
	if (ret == 0) {
		char* ipstr = NULL;

		OT_LOGI("Resolving domain name `%s'\n", otcfg_target_host);

		ipstr = dns_query(otcfg_target_host, NULL);
		if (ipstr == NULL) {
			OT_LOGW("Could not resolve domain `%s'\n", otcfg_target_host);
			shutdown(fd, SHUT_RDWR);
			close(fd);
			exit(-3);
		}
		else {
			OT_LOGI("Resolved domain: %s --> %s\n", otcfg_target_host, ipstr);
		}

		inet_aton(ipstr, &taddr.sin_addr);
	}


	// 连接到目标服务器
	tfd = socket(AF_INET, SOCK_STREAM, 0);
	if (tfd <= 0) {
		perror("sub socket");
		shutdown(fd, SHUT_RDWR);
		close(fd);
		exit(-1);
	}

	OT_LOGI("Connecting to %s:%d\n", inet_ntoa(taddr.sin_addr), ntohs(taddr.sin_port));
	ret = connect(tfd, (struct sockaddr*)&taddr, sizeof(taddr));
	if (ret < 0) {
		perror("sub connect");
		shutdown(fd, SHUT_RDWR);
		close(fd);
		close(tfd);
		exit(-2);
	}

	flags = fcntl(tfd, F_GETFL, 0);
	/*
	ret = fcntl(tfd, F_SETFL, flags | O_NONBLOCK);
	if (ret == -1) {
		perror("sub fcntl");
		shutdown(fd, SHUT_RDWR);
		shutdown(tfd, SHUT_RDWR);
		close(tfd);
		close(fd);
		exit(-1);
	}
	*/

	printf("Connected to target host %s:%d\n", otcfg_target_host, otcfg_target_port);

	/// 转发
	ot_tunneling_tcp(fd, tfd);

	shutdown(fd, SHUT_RDWR);
	shutdown(tfd, SHUT_RDWR);

	close(tfd);

	return 0;
}
Example #12
0
/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{  
	uint16 i; 
        RCC_Configuration(); // Configure the system clocks
	NVIC_Configuration(); // NVIC Configuration
	GPIO_Configuration();
	USART1_Init(115200);
	Timer_Configuration();
        
        printf("============================================\r\n");
	printf("   HTTP client for W5200 \r\n");
	printf("============================================\r\n");
       
	WIZ_SPI_Init();	
        Reset_W5200();
        wizInit();
        InitNetInfo();
        DisplayNetInfo();
	              
        // LED3 and LED4 On!
	LED3_onoff(ON);
	LED4_onoff(ON);
     
       
        uint8  url[MAX_URL_SIZE];			//full url
	uint8  url_dn[MAX_URL_SIZE];			//domain name url
	uint8  url_path[MAX_URL_SIZE];		//local path in html server
	char * split;				//string split pointer 
	
	
	uint16 done_http = 0;
	uint16 start_idx = 0 ;
	uint16 end_idx = 0;
	uint16 tmp_start;
	uint16 tmp_end;
	uint8 BR [4]= "<BR>";
	uint8 rBR [4]= "\r\n";
	uint8 TITLE [7]= "<TITLE>";
	uint8 bTITLE [8]= "</TITLE>";
	

	uint16 tmp_idx;
	uint8 no_pr = 0;
	uint8 str[17];
       
	
            while (1)
            {
                          
              sprintf((char*)str,"%.3d.%.3d.%.3d.%.3d ",
				IINCHIP_READ (SIPR0+0), IINCHIP_READ (SIPR0+1), 
				IINCHIP_READ (SIPR0+2), IINCHIP_READ (SIPR0+3));
                            
                           
                            /* Get Http Address  */
                            printf("\r\n Please enter a HTTP Address without 'http://' \n\r"); 
                            printf("http://");
                            memset(url,0,sizeof(url));	
                            //Modified by Gang 2011-10-04
                            zScanf_s(1, url);
                            printf("\r\n Your HTTP address is: %s \r\n",url);		
                            
                            /* Parse URL Path  */
                            split = strchr((char const*)url,'/');			
                            strcpy((char*)url_path,split); 			
                            printf("Domain path: %s \r\n",url_path);
                    
                            /* Parse URL Domain  */
                            split = strtok((char*)url,"/");			
                            strcpy((char*)url_dn,split);				
                            printf("Domain name: %s \r\n",url_dn);
                            
               
                            /* Do DNS Client */
                            memset(httpclient_NetInfo.HTTPs_IP,0,sizeof(httpclient_NetInfo.HTTPs_IP));
                            //Delay_ms(10);
                            done_dns = dns_query(DNS_SOCK, url_dn, httpclient_NetInfo.HTTPs_IP);
                            printf("\r\n HTTPs_IP= %d.%d.%d.%d",httpclient_NetInfo.HTTPs_IP[0],httpclient_NetInfo.HTTPs_IP[1],httpclient_NetInfo.HTTPs_IP[2],httpclient_NetInfo.HTTPs_IP[3]);
                          
                            while(done_dns) {
                            
                            /* Do HTTP Client */
                            done_http = http_client(HTTPC_SOCK, httpclient_NetInfo.HTTPs_IP, url_path, url_dn,data_buf);
                           
                            if(done_http) { // on success, done_dns is not  '0'
                                            
    #define Recieved_DATA
    #ifdef Recieved_DATA
                                            printf("\r\n<< Recieved Data -- START>> \r\n");
                                           
                                            for(i=0; i<done_http; i++){
                                                  
                                                  printf("%c",(uint8)data_buf[i]);
                                              }   
                                            printf("\r\n");
                                            printf("<< Recieved Data -- END>> \r\n");
                                           
    #endif
                                        /* parsed index */
                                            //All other HTML elements are nested between the opening <html> and </html> tags.
                                            start_idx = pased_idx((uint8 *)str_start , sizeof(str_start), done_http );
                                            end_idx   = pased_idx((uint8 *)str_end , sizeof(str_end), done_http );			
    
                                            /* printf get <html> ...</html> */							
                                            for(i=start_idx; i<(end_idx+7); i++){						
                                                    /* remove header */
                                                    data_buf[i-start_idx] = data_buf[i];
                                             }   
                                            printf("\r\n");
                                            
    
                                            /* replace <br> tag to \r\n */
                                            //The br tag is used for specifying a line break.
                                            do{
                                                    tmp_idx = pased_idx((uint8*)BR, sizeof(BR) , end_idx-start_idx) ;
                                                    if(tmp_idx == 0 ) 
                                                            break;
                                                 
                                                    memcpy((uint8 *)data_buf+tmp_idx, (uint8*)rBR, sizeof(rBR)) ;
                                            }while(tmp_idx!=end_idx-start_idx);
    
    #define Parsed_DATA
    #ifdef Parsed_DATA
                                            /* parsed DATA */
                                            printf("\r\n<< Parsed Data -- START >>");
                                            printf("\r\nTITLE : \r\n");
                                            /* parse <TITLE> and </TITLE> tags */
                                            tmp_start = pased_idx((uint8 *)TITLE , sizeof(TITLE),(end_idx-start_idx) ) + sizeof(TITLE);
                                            tmp_end   = pased_idx((uint8 *)bTITLE , sizeof(bTITLE),(end_idx-start_idx) );												
                                            for(i=tmp_start; i<tmp_end; i++){
                                                   
                                                    printf("%c",(uint8)data_buf[i]); // printf title
                                            }   
    
                                            printf("\r\n BODY : \r\n");
                                            /*DO NOT PRINT TAG COMMAND: between '<' with '>' */
                                            for(i=tmp_end; i<(end_idx-start_idx); i++){
                                                    //Tag command - ex.)<PRE>
                                                    //'<' is a start point of tag command.
                                                    if((uint8)data_buf[i]=='<'){ 
                                                            no_pr = 0;
                                                    }
                                                    //'>' is a end point of Tag command.
                                                    //To avoid in row tags -> ex.) <PRE><H1>						
                                                    if((uint8)data_buf[i]=='>' && (uint8)data_buf[i+1] !='<' ) {
                                                            no_pr = 1;
                                                            i++;
                                                    }					
                                                    if(no_pr){
                                                        Delay_ms(1);    
                                                        printf("%c",(uint8)data_buf[i]);
                                                    }
                                            }   
                                            printf("\r\n<< Parsed Data -- END >>\r\n");
    #endif
                                            /* Init. parameter */
                                            start_idx= 0;
                                            end_idx= 0;				
                                    done_dns = 0;  
                                    break;
    
                            }
                           //done_http
                            //done_dns=0;
                    }//while : done_dns == 1		
            }
}
Example #13
0
int
in_reshost(char * host, /* IN - textual IP address or host name */
   ip_addr *   address, /* OUT - address if successful */
   int   flags)         /* IN - RH_VERBOSE, RH_BLOCK */
{
   char *   cp;      /* error holder */
   unsigned snbits;  /* for pass to parse_ipad() */
#ifdef DNS_CLIENT
   int   e;          /* Net error code */
   u_long   tmo;     /* timeout for blocking calls */
   int   blocking =  flags &  RH_BLOCK;
#endif   /* DNS_CLIENT */
   int   verbose  =  flags &  RH_VERBOSE;

   cp = parse_ipad(address, &snbits, host);

   if (!cp) /* worked, string was parsable dot notation */
      return 0;

#ifndef DNS_CLIENT
   if (verbose)
      dprintf("Unable to parse IP host %s.\nerror: %s\n", host, (char*)cp);
   return ENP_PARAM;
#else /* DNS_CLIENT enabled in build */

   if (verbose)
      dprintf("can't parse %s (%s), trying DNS\n", host, cp);

   tmo = cticks + (5 * TPS);  /* set timeout value */

   if (dns_servers[0])  /* dont bother if no servers are set */
   {
      if (verbose)
         dprintf("trying DNS lookup...\n");
      e = dns_query(host, address);
      if (e == ENP_SEND_PENDING)
      {
         if (blocking)
         {
            while (tmo > cticks)
            {
               tk_yield();
               e = dns_query(host, address);
               if (e == 0)
                  goto rh_got_dns;
            }
         }
         if (verbose)
            dprintf("DNS inquiry sent\n");
         return 0;
      }
      else if(e == 0)
      {
         rh_got_dns:
         /* DNS resolution worked */
         if (verbose)
            dprintf("active host found via DNS (%u.%u.%u.%u)\n", 
          PUSH_IPADDR(*address));
         return 0;
      }
      else if(e == ENP_TIMEOUT)  /* timeout? */
      {
         if (verbose)
            dprintf("DNS timeout");
      }
      else
      {
         if (verbose)
            dprintf("DNS error %d", e);
      }
      if (verbose)
         dprintf(", host not set\n");
      return e;
   }

   if (verbose)
      dprintf("DNS/host-parse failed.\n");
   return ENP_PARAM;
#endif   /* DNS_CLIENT */
}
Example #14
0
File: net.c Project: zabbix/zabbix
int	NET_DNS_RECORD(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	return dns_query(request, result, 0);
}
Example #15
0
File: net.c Project: zabbix/zabbix
int	NET_DNS(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	return dns_query(request, result, 1);
}
Example #16
0
/// Get an IP address from a name. Sets out_ip_addr to 0 on failure
void net_wiznet_gethostbyname(JsNetwork *net, char * hostName, unsigned long* out_ip_addr) {
  NOT_USED(net);
  if (dns_query(0, net_wiznet_getFreeSocket(), (uint8_t*)hostName) == 1) {
    *out_ip_addr = *(unsigned long*)&Server_IP_Addr[0];
  }
}
Example #17
0
/*******************************************************************************
* Function Name  : Main_Menu
* Description    : Display/Manage a Menu on HyperTerminal Window
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void Main_Menu(void)
{
	static char choice[3];
	static char subject[32], msg[256], sender[32], passwd[32], recipient[32], encodedText[256];
	static bool bTreat;
	static u8 Sip[4];
	static char key = 0;
	static wiz_NetInfo netinfo;
	
	
	while (1)
	{
		/* Display Menu on HyperTerminal Window */
		bTreat = (bool)RESET ;
   	        SerialPutString("\r\n====================== STM32-Discovery ===================\r\n");
		SerialPutString("This Application is basic example of UART interface with\r\n");
		SerialPutString("Windows Hyper Terminal. \r\n");
		SerialPutString("\r\n==========================================================\r\n");
		SerialPutString("                          APPLICATION MENU :\r\n");
		SerialPutString("\r\n==========================================================\r\n\n");
		SerialPutString(" 1 - Set LD1 on \r\n");
		SerialPutString(" 2 - Set LD1 off \r\n");
		SerialPutString(" 3 - Show network setting\r\n");
		SerialPutString(" 4 - Set  network setting\r\n");
		SerialPutString(" 5 - Run TCP Loopback\r\n");
		SerialPutString(" 6 - Run UDP Loopback\r\n");
		SerialPutString(" 7 - DNS test\r\n");
		SerialPutString(" 8 - BASE64 test\r\n");
		SerialPutString(" 9 - Send Mail\r\n");
		
		SerialPutString("Enter your choice : ");
		GetInputString(choice);
		/* Set LD1 on */
		if (strcmp(choice,"1")== 0)
		{
			bTreat = (bool)SET;
			LED3_onoff(ON);
			LED4_onoff(ON);
		}
		/* Set LD1 off */
		if ((strcmp(choice,"2") == 0))
		{
			bTreat = (bool)SET;
			LED3_onoff(OFF);
			LED4_onoff(OFF);
		}
		if (strcmp(choice,"3") == 0)
		{
			bTreat = (bool)SET;
			GetNetInfo(&netinfo);
			printf("\r\nIP : %d.%d.%d.%d", netinfo.IP[0],netinfo.IP[1],netinfo.IP[2],netinfo.IP[3]);
			printf("\r\nSN : %d.%d.%d.%d", netinfo.Subnet[0],netinfo.Subnet[1],netinfo.Subnet[2],netinfo.Subnet[3]);
			printf("\r\nGW : %d.%d.%d.%d", netinfo.Gateway[0],netinfo.Gateway[1],netinfo.Gateway[2],netinfo.Gateway[3]);
			printf("\r\nDNS server : %d.%d.%d.%d", netinfo.DNSServerIP[0],netinfo.DNSServerIP[1],netinfo.DNSServerIP[2],netinfo.DNSServerIP[3]);

		}

		if (strcmp(choice,"4") == 0)
		{
			bTreat = (bool)SET;
			// IP address
			SerialPutString("\r\nIP address : ");
			GetInputString(msg);
			if(!VerifyIPAddress(msg, netinfo.IP))
			{
				SerialPutString("\aInvalid.");
			}

			// Subnet mask
			SerialPutString("\r\nSubnet mask : ");
			GetInputString(msg);
			if(!VerifyIPAddress(msg, netinfo.Subnet))
			{
				SerialPutString("\aInvalid.");
			}
			
			// gateway address
			SerialPutString("\r\nGateway address : ");
			GetInputString(msg);
			if(!VerifyIPAddress(msg, netinfo.Gateway))
			{
				SerialPutString("\aInvalid.");
			}

			// DNS address
			SerialPutString("\r\nDNS address : ");
			GetInputString(msg);
			if(!VerifyIPAddress(msg, netinfo.DNSServerIP))
			{
				SerialPutString("\aInvalid.");
			}

			printf("\r\nIP : %d.%d.%d.%d", netinfo.IP[0],netinfo.IP[1],netinfo.IP[2],netinfo.IP[3]);
			printf("\r\nSN : %d.%d.%d.%d", netinfo.Subnet[0],netinfo.Subnet[1],netinfo.Subnet[2],netinfo.Subnet[3]);
			printf("\r\nGW : %d.%d.%d.%d", netinfo.Gateway[0],netinfo.Gateway[1],netinfo.Gateway[2],netinfo.Gateway[3]);
			printf("\r\nDNS server : %d.%d.%d.%d", netinfo.DNSServerIP[0],netinfo.DNSServerIP[1],netinfo.DNSServerIP[2],netinfo.DNSServerIP[3]);

			SetNetInfo(&netinfo);
		}
		

		if (strcmp(choice,"5") == 0)
		{
			bTreat = (bool)SET;
		  
			SerialPutString("\r\nRun TCP loopback");
			printf("\r\nRun TCP loopback, port number [%d] is listened", (u16)TCP_LISTEN_PORT);
			SerialPutString("\r\nTo Exit, press [Q]");
			
		  while(1) {

			if ((SerialKeyPressed((char*)&key) == 1) && (key == 'Q')) {
				SerialPutString("\r\n Stop ");
				
				break;
			}

			loopback_tcps(7, (u16)TCP_LISTEN_PORT);
		  }
		  

		}


		if (strcmp(choice,"6") == 0)
		{
			bTreat = (bool)SET;
		  
			SerialPutString("\r\nRun UDP loopback");
			printf("\r\nRun UDP loopback, port number [%d] is listened", (u16)UDP_LISTEN_PORT);
			SerialPutString("\r\nTo Exit, press [Q]");
			
			while(1) {

				if ((SerialKeyPressed((char*)&key) == 1) && (key == 'Q')) {
					SerialPutString("\r\n Stop ");
				
					break;

				}

				loopback_udp(7, (u16)UDP_LISTEN_PORT);
			}

		}


		if (strcmp(choice,"7")== 0)
		{
		  	bTreat = (bool)SET;
			
			SerialPutString("\r\nServer address : ");
			GetInputString(msg);

			SerialPutString("URL = ");
			SerialPutString(msg);
			  	
			if (dns_query(SOCK_DNS, (void *)msg, Sip) == 1) {

				printf("\r\nSIP : %d.%d.%d.%d", (u16)Sip[0],(u16)Sip[1],(u16)Sip[2],(u16)Sip[3]);

			}else {
				SerialPutString("\n\r DNS fail");

			}

			
		}


		if (strcmp(choice,"8")== 0)
		{
			bTreat = (bool)SET;
			
			memset(encodedText, '\0', 256);

			SerialPutString("\r\n");
			SerialPutString(" 1 - BASE64 Encode \r\n");
			SerialPutString(" 2 - BASE64 Decode \r\n");
			SerialPutString("Enter your choice : ");
			GetInputString(choice);

			if (strcmp(choice,"1")== 0) {

				SerialPutString("Type Plain Text\r\n");
				GetInputString(msg);
				base64_encode(msg, strlen(msg)+1, encodedText);
				SerialPutString("Encoded Text\r\n");
				printf("%s\r\n", encodedText);

			}else if(strcmp(choice,"2")== 0){

				SerialPutString("Type Encoded Text\r\n");
				GetInputString(msg);
				base64_decode(msg, (void *)encodedText, strlen(msg));
				SerialPutString("Decoded Text\r\n");
				printf("%s\r\n", encodedText);

			}

		}


		if (strcmp(choice,"9")== 0) {
		  	bTreat = (bool)SET;

			SerialPutString("\r\nServer address : ");
			GetInputString(msg);

			SerialPutString("URL = ");
			SerialPutString(msg);

			// DNS
			if (dns_query(SOCK_DNS, (void *)msg, Sip) == 1) {
				printf("\r\nSIP : %d.%d.%d.%d", (u16)Sip[0],(u16)Sip[1],(u16)Sip[2],(u16)Sip[3]);

				while(1) {

					SerialPutString("\r\nType a Sender: ");
					GetInputString(sender);

					SerialPutString("Type a Password: "******"Type a Recipient: ");
					GetInputString(recipient);

					SerialPutString("Type a Subject: ");
					GetInputString(subject);

					SerialPutString("Type a message: ");
					GetInputString(msg);

					send_mail(SOCK_SMTP, (void *)sender, (void *) passwd, (void *)recipient, (void *)subject, (void *)msg, Sip);

					SerialPutString("\r\nIf you want send another message? [YES]: any key, [NO]: Q");
					key = GetKey();

					if (key == 'Q') {
						SerialPutString("\r\n Stop ");

						break;

					}

				}

			}else {
				SerialPutString("\r\nDNS error");

			}
		}


		/* OTHERS CHOICE*/
		if (bTreat == (bool)RESET)
		{
			SerialPutString(" wrong choice  \r\n");
		}			
	} /* While(1)*/
}/* Main_Menu */
Example #18
0
int	NET_DNS_RECORD(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
	return dns_query(cmd, param, flags, result, 0);
}
Example #19
0
/*******************************************************************************
* Function Name  : Main_Menu
* Description    : Display/Manage a Menu on HyperTerminal Window
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void Main_Menu(void)
{
	static char choice[3];
	static char msg[256];
	static bool bTreat;
	static u8 Sip[4];
	static char key = 0;
	static wiz_NetInfo netinfo;


	while (1)
	{
		/* Display Menu on HyperTerminal Window */
		bTreat = (bool)RESET ;
   	        SerialPutString("\r\n====================== STM32-Discovery ===================\r\n");
		SerialPutString("This Application is basic example of UART interface with\r\n");
		SerialPutString("Windows Hyper Terminal. \r\n");
		SerialPutString("\r\n==========================================================\r\n");
		SerialPutString("                          APPLICATION MENU :\r\n");
		SerialPutString("\r\n==========================================================\r\n\n");
		SerialPutString(" 1 - Set LD1 on \r\n");
		SerialPutString(" 2 - Set LD1 off \r\n");
		SerialPutString(" 3 - Show network setting\r\n");
		SerialPutString(" 4 - Set  network setting\r\n");
		SerialPutString(" 5 - Run TCP Loopback\r\n");
		SerialPutString(" 6 - Run UDP Loopback\r\n");
		SerialPutString(" 7 - DNS test\r\n");
		SerialPutString(" 8 - Twitter\r\n");
		
		SerialPutString("Enter your choice : ");
		GetInputString(choice);
		/* Set LD1 on */
		if (strcmp(choice,"1")== 0)
		{
			bTreat = (bool)SET;
			LED3_onoff(ON);
			LED4_onoff(ON);
		}
		/* Set LD1 off */
		if ((strcmp(choice,"2") == 0))
		{
			bTreat = (bool)SET;
			LED3_onoff(OFF);
			LED4_onoff(OFF);
		}
		if (strcmp(choice,"3") == 0)
		{
			bTreat = (bool)SET;
			GetNetInfo(&netinfo);
			printf("\r\nIP : %d.%d.%d.%d", netinfo.IP[0],netinfo.IP[1],netinfo.IP[2],netinfo.IP[3]);
			printf("\r\nSN : %d.%d.%d.%d", netinfo.Subnet[0],netinfo.Subnet[1],netinfo.Subnet[2],netinfo.Subnet[3]);
			printf("\r\nGW : %d.%d.%d.%d", netinfo.Gateway[0],netinfo.Gateway[1],netinfo.Gateway[2],netinfo.Gateway[3]);
			printf("\r\nDNS server : %d.%d.%d.%d", netinfo.DNSServerIP[0],netinfo.DNSServerIP[1],netinfo.DNSServerIP[2],netinfo.DNSServerIP[3]);

		}

		if (strcmp(choice,"4") == 0)
		{
			bTreat = (bool)SET;
			// IP address
			SerialPutString("\r\nIP address : ");
			GetInputString(msg);
			if(!VerifyIPAddress(msg, netinfo.IP))
			{
				SerialPutString("\aInvalid.");
			}

			// Subnet mask
			SerialPutString("\r\nSubnet mask : ");
			GetInputString(msg);
			if(!VerifyIPAddress(msg, netinfo.Subnet))
			{
				SerialPutString("\aInvalid.");
			}
			
			// gateway address
			SerialPutString("\r\nGateway address : ");
			GetInputString(msg);
			if(!VerifyIPAddress(msg, netinfo.Gateway))
			{
				SerialPutString("\aInvalid.");
			}

			// DNS address
			SerialPutString("\r\nDNS address : ");
			GetInputString(msg);
			if(!VerifyIPAddress(msg, netinfo.DNSServerIP))
			{
				SerialPutString("\aInvalid.");
			}

			printf("\r\nIP : %d.%d.%d.%d", netinfo.IP[0],netinfo.IP[1],netinfo.IP[2],netinfo.IP[3]);
			printf("\r\nSN : %d.%d.%d.%d", netinfo.Subnet[0],netinfo.Subnet[1],netinfo.Subnet[2],netinfo.Subnet[3]);
			printf("\r\nGW : %d.%d.%d.%d", netinfo.Gateway[0],netinfo.Gateway[1],netinfo.Gateway[2],netinfo.Gateway[3]);
			printf("\r\nDNS server : %d.%d.%d.%d", netinfo.DNSServerIP[0],netinfo.DNSServerIP[1],netinfo.DNSServerIP[2],netinfo.DNSServerIP[3]);

			SetNetInfo(&netinfo);
		}
		

		if (strcmp(choice,"5") == 0)
		{
			bTreat = (bool)SET;
		  
			SerialPutString("\r\nRun TCP loopback");
			printf("\r\nRun TCP loopback, port number [%d] is listened", (u16)TCP_LISTEN_PORT);
			SerialPutString("\r\nTo Exit, press [Q]");
			
			while(1) {

				if ((SerialKeyPressed((char*)&key) == 1) && (key == 'Q')) {
					SerialPutString("\r\n Stop ");
				
					break;
				}

				loopback_tcps(7, (u16)TCP_LISTEN_PORT);
			}

		}


		if (strcmp(choice,"6") == 0)
		{
			bTreat = (bool)SET;
		  
			SerialPutString("\r\nRun UDP loopback");
			printf("\r\nRun UDP loopback, port number [%d] is listened", (u16)UDP_LISTEN_PORT);
			SerialPutString("\r\nTo Exit, press [Q]");
			
			while(1) {

				if ((SerialKeyPressed((char*)&key) == 1) && (key == 'Q')) {
					SerialPutString("\r\n Stop ");
				
					break;

				}

				loopback_udp(7, (u16)UDP_LISTEN_PORT);
			}

		}


		if (strcmp(choice,"7")== 0)
		{
			bTreat = (bool)SET;

			SerialPutString("\r\nServer address : ");
			GetInputString(msg);

			SerialPutString("URL = ");
			SerialPutString(msg);
			  	
			if (dns_query(SOCK_DNS, (void *)msg, Sip) == 1) {

				printf("\r\nSIP : %d.%d.%d.%d", (u16)Sip[0],(u16)Sip[1],(u16)Sip[2],(u16)Sip[3]);

			}else {
				SerialPutString("\n\r DNS fail");

			}

		}


		if (strcmp(choice,"8")== 0) {
			bTreat = (bool)SET;

			// DNS
			if (dns_query(SOCK_DNS, LIB_DOMAIN, Sip) == 1) {
				printf("\r\nSIP : %d.%d.%d.%d", (u16)Sip[0],(u16)Sip[1],(u16)Sip[2],(u16)Sip[3]);

				while(1) {
					/*
					sprintf(msg, "Hello, World [%d] from wiz820io", twit_cnt++);

					SerialPutString("\r\nSend Twitter Message: ");
					SerialPutString(msg);
					*/

					SerialPutString("\r\nType a message: ");
					GetInputString(msg);

					Twitter_Post(SOCK_TWITTER, (void *)msg, Sip);

					SerialPutString("\r\nIf you want send another message? [YES]: any key, [NO]: Q");
					key = GetKey();

					if (key == 'Q') {
						SerialPutString("\r\n Stop ");

						break;

					}

				}

			}else {
				SerialPutString("\r\nDNS error");

			}
		}


		if (strcmp(choice,"9") == 0)
		{
                  uint8 l_ip[4] = {222, 98, 173, 237};
			bTreat = (bool)SET;
		  
			SerialPutString("\r\nRun TCP loopback [Client]");
			SerialPutString("\r\nTo Exit, press [Q]");
			
			while(1) {

				if ((SerialKeyPressed((char*)&key) == 1) && (key == 'Q')) {
					SerialPutString("\r\n Stop ");
				
					break;
				}

				TCPClientOpen(7, 5000, l_ip, 5000);
                                Delay_ms(3000);
                                TCPClose(7);
                                Delay_ms(3000);
			}

		}


		/* OTHERS CHOICE*/
		if (bTreat == (bool)RESET)
		{
			SerialPutString(" wrong choice  \r\n");
		}			
	} /* While(1)*/
}/* Main_Menu */