void create_raw_socket_server_demo(void)
{	
	struct tls_ethif * ethif;

	ethif = tls_netif_get_ethif();
	printf("\nip=%d.%d.%d.%d\n",ip4_addr1(&ethif->ip_addr.addr),ip4_addr2(&ethif->ip_addr.addr),
		ip4_addr3(&ethif->ip_addr.addr),ip4_addr4(&ethif->ip_addr.addr));

	DemoRawSockOneshotSendMac();
	
	memset(&socket_desc, 0, sizeof(struct tls_socket_desc));
	socket_desc.recvf = raw_sk_server_recv;
	socket_desc.errf = raw_sk_server_err;
	socket_desc.pollf = raw_sk_server_poll;

	socket_desc.cs_mode = SOCKET_CS_MODE_SERVER;
	socket_desc.acceptf = raw_sk_server_accept;

	socket_desc.protocol = SOCKET_PROTO_TCP;
	socket_desc.port = LocalPort;
	printf("\nlisten port=%d\n",socket_desc.port);
	if(gDemoSys.socket_ok != true)
	{
		tls_socket_create(&socket_desc);
	}
}
void DemoRawSockOneshotSendMac(void)
{
	int idx;
	int socket_num = 0;
	u8 mac_addr[8];
	struct tls_socket_desc socket_desc;

	memset(&socket_desc, 0, sizeof(struct tls_socket_desc));
	socket_desc.cs_mode = SOCKET_CS_MODE_CLIENT;
	socket_desc.protocol = SOCKET_PROTO_UDP;
	for(idx = 0; idx < 4; idx++){
		socket_desc.ip_addr[idx] = 255;
	}
	socket_desc.port = 65534;
	socket_num = tls_socket_create(&socket_desc);
	//printf("\nraw sk one shot sock num=%d\n",socket_num);
	
	memset(mac_addr,0,sizeof(mac_addr));
	tls_get_mac_addr(mac_addr);
	tls_os_time_delay(200);				
	for(idx = 0;idx < 3;idx ++)
	{
		tls_socket_send(socket_num,mac_addr, 6);
		tls_os_time_delay(50);
	}
	tls_socket_close(socket_num);
	socket_num = 0;	
	//printf("\none shot success!\n");
}
void create_raw_socket_client_demo(void)
{	
	int idx;
	struct tls_ethif * ethif;
	
	ethif = tls_netif_get_ethif();
	printf("\nip=%d.%d.%d.%d\n",ip4_addr1(&ethif->ip_addr.addr),ip4_addr2(&ethif->ip_addr.addr),
		ip4_addr3(&ethif->ip_addr.addr),ip4_addr4(&ethif->ip_addr.addr));

	DemoRawSockOneshotSendMac();
	
	memset(&raw_socket_c_desc, 0, sizeof(struct tls_socket_desc));
	raw_socket_c_desc.recvf = raw_sk_client_recv;
	raw_socket_c_desc.errf = raw_sk_client_err;
	raw_socket_c_desc.pollf = raw_sk_client_poll;

	raw_socket_c_desc.cs_mode = SOCKET_CS_MODE_CLIENT;
	raw_socket_c_desc.connf = raw_sk_client_connected;

	raw_socket_c_desc.protocol = SOCKET_PROTO_TCP;
	for(idx = 0; idx < 4; idx++){
		raw_socket_c_desc.ip_addr[idx] = RemoteIp[idx];
	}
	raw_socket_c_desc.port = RemotePort;

	printf("\nserver ip=%d.%d.%d.%d,port=%d\n",RemoteIp[0],RemoteIp[1],RemoteIp[2],RemoteIp[3],raw_socket_c_desc.port);
	if(gDemoSys.socket_ok != true)
	{
		tls_socket_create(&raw_socket_c_desc);
	}
}
/**
 * Run client to perform echo test
 */
static void run_echo_client(echo_server_config_t *config)
{
	tls_socket_t *tls;
	ssize_t len, rd, wr;
	int fd;
	host_t *host;
	identification_t *server, *client = NULL;
	char buf[128];

	host = host_create_from_string(config->addr, config->port);
	server = identification_create_from_string(config->addr);
	if (config->cauth)
	{
		client = server;
	}

	fd = socket(AF_INET, SOCK_STREAM, 0);
	ck_assert(fd != -1);
	ck_assert(connect(fd, host->get_sockaddr(host),
					  *host->get_sockaddr_len(host)) != -1);
	tls = tls_socket_create(FALSE, server, client, fd, NULL,
							config->version, TRUE);
	ck_assert(tls != NULL);

	wr = rd = 0;
	while (rd < config->data.len)
	{
		len = tls->write(tls, config->data.ptr + wr, config->data.len - wr);
		ck_assert(len >= 0);
		wr += len;

		len = tls->read(tls, buf, sizeof(buf), FALSE);
		if (len == -1 && errno == EWOULDBLOCK)
		{
			continue;
		}
		if (len == 0)
		{
			ck_assert_int_eq(rd, config->data.len);
			break;
		}
		ck_assert(len > 0);
		ck_assert(rd + len <= config->data.len);
		ck_assert(memeq(buf, config->data.ptr + rd, len));
		rd += len;
	}

	tls->destroy(tls);
	close(fd);
	host->destroy(host);
	server->destroy(server);
}
Beispiel #5
0
/**
 * Server routine
 */
static int serve(host_t *host, identification_t *server,
                 int times, tls_cache_t *cache)
{
    tls_socket_t *tls;
    int fd, cfd;

    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd == -1)
    {
        DBG1(DBG_TLS, "opening socket failed: %s", strerror(errno));
        return 1;
    }
    if (bind(fd, host->get_sockaddr(host),
             *host->get_sockaddr_len(host)) == -1)
    {
        DBG1(DBG_TLS, "binding to %#H failed: %s", host, strerror(errno));
        close(fd);
        return 1;
    }
    if (listen(fd, 1) == -1)
    {
        DBG1(DBG_TLS, "listen to %#H failed: %m", host, strerror(errno));
        close(fd);
        return 1;
    }

    while (times == -1 || times-- > 0)
    {
        cfd = accept(fd, host->get_sockaddr(host), host->get_sockaddr_len(host));
        if (cfd == -1)
        {
            DBG1(DBG_TLS, "accept failed: %s", strerror(errno));
            close(fd);
            return 1;
        }
        DBG1(DBG_TLS, "%#H connected", host);

        tls = tls_socket_create(TRUE, server, NULL, cfd, cache, TLS_1_2, TRUE);
        if (!tls)
        {
            close(fd);
            return 1;
        }
        tls->splice(tls, 0, 1);
        DBG1(DBG_TLS, "%#H disconnected", host);
        tls->destroy(tls);
    }
    close(fd);

    return 0;
}
/**
 * Run an echo server
 */
static job_requeue_t serve_echo(echo_server_config_t *config)
{
	tls_socket_t *tls;
	int sfd, cfd;
	identification_t *server, *client = NULL;
	ssize_t len, total, done;
	char buf[128];

	server = identification_create_from_string(config->addr);
	if (config->cauth)
	{
		client = server;
	}
	sfd = config->fd;
	while (TRUE)
	{
		cfd = accept(sfd, NULL, NULL);
		if (cfd < 0)
		{
			break;
		}

		tls = tls_socket_create(TRUE, server, client, cfd, NULL,
								config->version, TRUE);
		ck_assert(tls != NULL);

		while (TRUE)
		{
			len = tls->read(tls, buf, sizeof(buf), TRUE);
			if (len <= 0)
			{
				break;
			}
			total = 0;
			while (total < len)
			{
				done = tls->write(tls, buf + total, len - total);
				ck_assert_msg(done > 0, "%s", strerror(errno));
				total += done;
			}
		}

		tls->destroy(tls);
		close(cfd);
	}
	server->destroy(server);

	return JOB_REQUEUE_NONE;
}
Beispiel #7
0
// Lua: net.createConnection(type, secure)
int net_createConnection( lua_State *L ) {
  int type, secure;

  type = luaL_optlong(L, 1, TYPE_TCP);
  secure = luaL_optlong(L, 2, 0);

  if (type == TYPE_UDP) {
    platform_print_deprecation_note("net.createConnection with net.UDP type", "in next version");
    return net_createUDPSocket( L );
  }
  if (type != TYPE_TCP) return luaL_error(L, "invalid type");
  if (secure) {
    platform_print_deprecation_note("net.createConnection with secure flag", "in next version");
#ifdef TLS_MODULE_PRESENT
    return tls_socket_create( L );
#else
    return luaL_error(L, "secure connections not enabled");
#endif
  }
  net_create(L, TYPE_TCP_CLIENT);
  return 1;
}
void create_socket_fwup_demo(void)
{
	struct tls_ethif * ethif;
	ethif = tls_netif_get_ethif();
	printf("\nip=%d.%d.%d.%d\n",ip4_addr1(&ethif->ip_addr.addr),ip4_addr2(&ethif->ip_addr.addr),
		ip4_addr3(&ethif->ip_addr.addr),ip4_addr4(&ethif->ip_addr.addr));

	/*oneshot config broadcast mac addr*/
	DemoRawSockOneshotSendMac();
	
	if(fwup_skt_num<0)
	{
		memset(&sock_desc, 0, sizeof(struct tls_socket_desc));
		sock_desc.cs_mode = SOCKET_CS_MODE_SERVER;
		sock_desc.acceptf = socket_fwup_accept;
		sock_desc.recvf = socket_fwup_recv;
		sock_desc.errf = socket_fwup_err;
		sock_desc.pollf = socket_fwup_poll;
		sock_desc.protocol = SOCKET_PROTO_TCP;
		sock_desc.port = SOCKET_FWUP_PORT;
		fwup_skt_num = tls_socket_create(&sock_desc);
	}
}
Beispiel #9
0
/**
 * Client routine
 */
static int run_client(host_t *host, identification_t *server,
                      identification_t *client, int times, tls_cache_t *cache)
{
    tls_socket_t *tls;
    int fd, res;

    while (times == -1 || times-- > 0)
    {
        fd = socket(AF_INET, SOCK_STREAM, 0);
        if (fd == -1)
        {
            DBG1(DBG_TLS, "opening socket failed: %s", strerror(errno));
            return 1;
        }
        if (connect(fd, host->get_sockaddr(host),
                    *host->get_sockaddr_len(host)) == -1)
        {
            DBG1(DBG_TLS, "connecting to %#H failed: %s", host, strerror(errno));
            close(fd);
            return 1;
        }
        tls = tls_socket_create(FALSE, server, client, fd, cache, TLS_1_2, TRUE);
        if (!tls)
        {
            close(fd);
            return 1;
        }
        res = tls->splice(tls, 0, 1) ? 0 : 1;
        tls->destroy(tls);
        close(fd);
        if (res)
        {
            break;
        }
    }
    return res;
}
static void demo_raw_sock_s_task(void *sdata)
{
	ST_Demo_Sys *sys = (ST_Demo_Sys *)sdata;
	void *msg;
	struct tls_ethif * ethif = tls_netif_get_ethif();
	
	printf("\nraw sock s task\n");
//用于socket接收数据使用
	sys->sock_rx = tls_mem_alloc(DEMO_BUF_SIZE);
	if(NULL == sys->sock_rx)
	{
		printf("\nmalloc socket rx fail\n");
		return;
	}
	memset(sys->sock_rx, 0, DEMO_BUF_SIZE);	
//////	
	if(ethif->status)	//已经在网
	{
		tls_os_queue_send(demo_raw_sock_s_q, (void *)DEMO_MSG_SOCKET_CREATE, 0);
	}
	else
	{
		struct tls_param_ip ip_param;
		
		tls_param_get(TLS_PARAM_ID_IP, &ip_param, TRUE);
		ip_param.dhcp_enable = true;
		tls_param_set(TLS_PARAM_ID_IP, &ip_param, TRUE);

		tls_wifi_set_oneshot_flag(1);		/*一键配置使能*/
		printf("\nwait one shot......\n");
	}
	tls_netif_add_status_event(raw_sock_s_net_status_changed_event);
	for(;;) 
	{
		tls_os_queue_receive(demo_raw_sock_s_q, (void **)&msg, 0, 0);
		//printf("\n msg =%d\n",msg);
		switch((u32)msg)
		{
			case DEMO_MSG_WJOIN_SUCCESS:
				break;
				
			case DEMO_MSG_SOCKET_CREATE:
				create_raw_socket_server_demo();
				break;
				
			case DEMO_MSG_WJOIN_FAILD:
				if(sys->socket_num > 0)
				{
					sys->socket_num = 0;
					sys->socket_ok = FALSE;
				}
				break;

			case DEMO_MSG_SOCKET_RECEIVE_DATA:
				/*收到数据,自行处理*/
#if	(TLS_CONFIG_UART)
				tls_uart_tx(sys->sock_rx,sys->sock_data_len);	/*发到串口上显示*/
#endif
				break;

			case DEMO_MSG_SOCKET_ERR:
				tls_os_time_delay(200);
				printf("\nsocket err\n");
				tls_socket_create(&socket_desc);
				break;

			default:
				break;
		}
	}

}