Beispiel #1
1
T_uezError Network_lwIP_SocketConnect(
    void *aWorkspace,
    T_uezNetworkSocket aSocket,
    T_uezNetworkAddr *aAddr,
    TUInt16 aPort)
{
    T_Network_lwIP_Workspace *p = (T_Network_lwIP_Workspace *)aWorkspace;
    T_uezError error = UEZ_ERROR_NONE;
    T_lwIPSocket *p_socket = p->iSockets + aSocket;
    struct ip_addr ip;

    // Only valid sockets
    if ((aSocket == 0) || (aSocket > NETWORK_LWIP_NUM_SOCKETS))
        return UEZ_ERROR_HANDLE_INVALID;

    UEZSemaphoreGrab(p->iSem, UEZ_TIMEOUT_INFINITE);

    if (p_socket->iState == SOCKET_STATE_FREE) {
        error = UEZ_ERROR_HANDLE_INVALID;
    } else {
        IP4_ADDR(&ip, aAddr->v4[0], aAddr->v4[1], aAddr->v4[2], aAddr->v4[3]);

        error = IConvertErrorCode(netconn_connect(p_socket->iNetconn, &ip,
                                  aPort));
        if (error == UEZ_ERROR_NONE)
            p_socket->iFlags |= SOCKET_FLAG_CONNECTED;

    }
    UEZSemaphoreRelease(p->iSem);

    return error;
}
Beispiel #2
0
void TerminalTask(void* params)
{
	terminalQueue = xQueueCreate(TERMINAL_QUEUE_LEN, MAX_CHARS);

	char msg[MAX_CHARS];
	struct netconn* conn = netconn_new( NETCONN_UDP );
    netconn_bind(conn, IP_ADDR_ANY, LOG_LOCAL_PORT);
    struct ip_addr ip;
    ip.addr = inet_addr("10.3.84.100");
    netconn_connect(conn, &ip, LOG_REMOTE_PORT);
	for(;;)
	{
			// Try to receive message, block the task for at most TERMINAL_QUEUE_TIMEOUT ticks if queue is empty.
			if (xQueueReceive(terminalQueue, &msg, TERMINAL_QUEUE_TIMEOUT))
			//if (xQueueReceive(LogQueueHandle, &msg, 100))
			{
				// Methods for UDP send.
				struct netbuf *buf = netbuf_new();
			    char * data = netbuf_alloc(buf, sizeof(msg)); // Also deallocated with netbuf_delete(buf)
			    memcpy (data, msg, sizeof (msg));
			    netconn_send(conn, buf);
			    netbuf_delete(buf); // Deallocate packet buffer
			}
	}
}
Beispiel #3
0
void udpecho_entry(void *parameter)
{
	struct netconn *conn;
	struct netbuf *buf;
	struct ip_addr *addr;
	unsigned short port;

	conn = netconn_new(NETCONN_UDP);
	netconn_bind(conn, IP_ADDR_ANY, 7);

	while(1)
	{
        /* received data to buffer */
		buf = netconn_recv(conn);

		addr = netbuf_fromaddr(buf);
		port = netbuf_fromport(buf);

        /* send the data to buffer */
		netconn_connect(conn, addr, port);

		/* reset address, and send to client */
		buf->addr = RT_NULL;
		netconn_send(conn, buf);

        /* release buffer */
		netbuf_delete(buf);
	}
}
Beispiel #4
0
/*-----------------------------------------------------------------------------------*/
static void udpecho_thread(void *arg)
{
  err_t err, recv_err;
  
  LWIP_UNUSED_ARG(arg);

  conn = netconn_new(NETCONN_UDP);
  if (conn!= NULL)
  {
    err = netconn_bind(conn, IP_ADDR_ANY, 7);
    if (err == ERR_OK)
    {
      while (1) 
      {
        recv_err = netconn_recv(conn, &buf);
      
        if (recv_err == ERR_OK) 
        {
          addr = netbuf_fromaddr(buf);
          port = netbuf_fromport(buf);
          netconn_connect(conn, addr, port);
          buf->addr.addr = 0;
          netconn_send(conn,buf);
          netbuf_delete(buf);
        }
      }
    }
    else
    {
      netconn_delete(conn);
    }
  }
}
Beispiel #5
0
int connect(int s, const struct sockaddr *name, socklen_t namelen)
{
	struct lwip_socket *sock;
	err_t err;

	sock = get_socket(s);
	if (!sock)
		return -1;

	if (((struct sockaddr_in *)name)->sin_family == AF_UNSPEC) 
	{
		err = netconn_disconnect(sock->conn);
	} 
	else 
	{
		struct ip_addr remote_addr;
		u16_t remote_port;

		remote_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr;
		remote_port = ((struct sockaddr_in *)name)->sin_port;

		err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
	}

	if (err != ERR_OK) 
	{
		return -1;
	}

	return 0;
}
Beispiel #6
0
static void low_send_putc(char *ptr, int l)
{
  if(ser_conn && buf && netconn_connect(ser_conn, IP_ADDR_BROADCAST, 1445)==ERR_OK) {
    netbuf_ref(buf, ptr, l);
    netconn_send(ser_conn, buf);
  }
}
void vAmpmNetTestTask( void *pvParameters )
{
	char *pt =  pvParameters;
	struct netconn *ampm_net;
	struct ip_addr remote_addr;
  u16_t remote_port = 1880;
	struct netbuf *data;
  err_t err;
	((uint8_t *)&remote_addr)[0] = 118;
	((uint8_t *)&remote_addr)[1] = 69;
	((uint8_t *)&remote_addr)[2] = 60;
	((uint8_t *)&remote_addr)[3] = 174;
	ampm_net = netconn_new(NETCONN_TCP);
	err = netconn_connect(ampm_net, &remote_addr, remote_port);
	err = netconn_write(ampm_net, pt, strlen(pt), NETCONN_COPY);
	while(1)
	{
		data = netconn_recv(ampm_net);
		if(data)
		{
			//netconn_write(ampm_net, (uint8_t *)sendfile, strlen(sendfile), NETCONN_COPY);
			netconn_write(ampm_net, data->p->payload, data->p->len, NETCONN_COPY);
			netbuf_delete(data);
		}
	}
}
Beispiel #8
0
/*-----------------------------------------------------------------------------------*/
static void
udpecho_thread(void *arg)
{
  static struct netconn *conn;
  static struct netbuf *buf;
  static struct ip_addr *addr;
  static unsigned short port;
  char buffer[256];

  LWIP_UNUSED_ARG(arg);

  conn = netconn_new(NETCONN_UDP); /* Create a new netconnect */
  LWIP_ASSERT("con != NULL", conn != NULL);
  netconn_bind(conn, IP_ADDR_ANY, UDP_ECHO_PORT); /* udpecho using port 7 */

  while (1) {
    buf = netconn_recv(conn); /* received data to buffer */
    if (buf != NULL) {
      addr = netbuf_fromaddr(buf); /* get client's IP address */
      port = netbuf_fromport(buf); /* get client's port */
      netconn_connect(conn, addr, port); /* connect to client */
      netbuf_copy(buf, buffer, buf->p->tot_len); /* set echo data */
      buffer[buf->p->tot_len] = '\0';
      buf->addr = NULL;
      netconn_send(conn, buf); /* send data back to client */
      LWIP_DEBUGF(LWIP_DBG_ON, ("got %s\n", buffer));
      netbuf_delete(buf); /* release buffer */
    }
  }
}
Beispiel #9
0
int lwip_sock_connect(void *conn, unsigned long *addr, uint16_t port) {
	int ret;

	mutexLock(g_netBH_lock);
	ret = netconn_connect(conn, addr, lwip_ntohs(port));
	mutexUnLock(g_netBH_lock);
	return ret;
}
Beispiel #10
0
/** Tests the fragmented IP packets.
 * @bug Doesn't pass yet.
 */
char* fragmented_packet_test(void) {
    ip_addr_t destination, self_ip;
    printf("%s()\n", __FUNCTION__);
    struct netconn *conn;
    err_t err;
    struct netbuf *buf;
    void *data;
    u16_t len;

    /* Payload longer than MTU, should get split. */
    char test_str[2001];
    test_str[0] = 0;

    char data_str[2001];
    data_str[0] = 0;

    /* Fills the data pattern. */
    while (strlen(test_str) < 1900)
        strcat(test_str, "data");

    /* Create a new connection identifier. */
    conn = netconn_new(NETCONN_TCP);

    /* Sets the device we want to connect to. */
    IP4_ADDR(&destination, 10, 0, 0, 2);
    IP4_ADDR(&self_ip, 10, 0, 0, 3);

    /* Bind connection to well known port number 7. */
    netconn_bind(conn, &self_ip, 1235);

    printf("Connecting...\n");
    err = netconn_connect(conn, &destination, 1235);

    TEST_ASSERT("TCP connection failed.", err == ERR_OK);

    /* Don't send final \0 */
    err = netconn_write(conn, test_str, strlen(test_str), NETCONN_NOCOPY);

    TEST_ASSERT("Netconn write failed.\n", err == ERR_OK);

    /* Reads whole response. */
    int sum=0;
    while(sum < strlen(test_str) && (err = netconn_recv(conn, &buf)) == ERR_OK) {
        do {
            netbuf_data(buf, &data, &len);
            strncat(data_str, data, len);
            sum += len;
        } while (netbuf_next(buf) >= 0);
        netbuf_delete(buf);
    }


    TEST_ASSERT("Data is not echoed correctly", !strcmp(data_str, test_str));

    netconn_close(conn);

    return TEST_SUCCESS;
}
Beispiel #11
0
size_t rpc_transmit(uint8_t *input_buffer, size_t input_buffer_size,
                    uint8_t *output_buffer, size_t output_buffer_size,
                    ip_addr_t *addr, uint16_t port)
{
    struct netconn *conn;
    int err;

    cmp_ctx_t ctx; /* For cmp_mem_access. */
    cmp_mem_access_t mem;
    struct netbuf *buf;

    u16_t len;
    char *data;


    conn = netconn_new(NETCONN_TCP);

    if (conn == NULL) {
        return -1;
    }

    err = netconn_connect(conn, addr, port);

    if (err != ERR_OK) {
        goto fail;
    }

    serial_datagram_send((void *)input_buffer, input_buffer_size,
                         netconn_serial_datagram_tx_adapter, (void *)conn);

    cmp_mem_access_init(&ctx, &mem, output_buffer, output_buffer_size);

    while (1) {
        err = netconn_recv(conn, &buf);

        /* If connection was closed by server, abort */
        if (err != ERR_OK) {
            break;
        }

        do {
            netbuf_data(buf, (void **)&data, &len);

            /* Append data to buffer. */
            ctx.write(&ctx, data, len);

        } while (netbuf_next(buf) >= 0);
        netbuf_delete(buf);
    }

    netconn_delete(conn);
    return cmp_mem_access_get_pos(&mem);

fail:
    netconn_delete(conn);
    return -1;
}
    static void tcptask(void* arg)
    {
        static uint16_t port = 30000;
        HTTPClient* self = (HTTPClient*) arg;
        TCPData tcpData;
        for (;;) {
            if (xQueueReceive(self->qHandle, &tcpData, 100)) {
                port++;
                struct netconn *conn = netconn_new(NETCONN_TCP);
                err_t err;
                if (conn != NULL) {
                    // Bind connection to the specified number
                    os_printf("Binding port %d\n", port);
                    err = netconn_bind(conn, NULL, port);

                    if (err == ERR_OK) {
                        struct ip_addr ip;
                        ip.addr = tcpData.serverIP;
                        os_printf("Connecting port %d\n", tcpData.serverPort);
                        err = netconn_connect (conn, &ip, tcpData.serverPort);

                        if (err == ERR_OK) {
                            os_printf("Writing data!\n");
                            netconn_write(conn, tcpData.data, TCP_DATA_SIZE, NETCONN_COPY);

                            struct netbuf *buf;
                            char *data;
                            u16_t len;
                            uint32_t offset = 0;
                            if ((buf = netconn_recv(conn)) != NULL) {
                                do {
                                    netbuf_data(buf, (void**)&data, &len);
                                    if (self->rxBuffer != NULL && data != NULL)
                                        memcpy(self->rxBuffer + offset, data, len);
                                    else
                                        os_printf("HTTPClient::tcpTask self->rxBuffer or data is NULL!\n");

                                    offset += len;
                                    os_printf("Netconn received %d bytes\n", len);


                                } while (netbuf_next(buf) >= 0);
                                self->onReceive(tcpData.receiveCallback, tcpData.obj, self->rxBuffer);
                                netbuf_delete(buf);
                            }
                        }
                    }

                }
                netconn_close (conn );
                netconn_delete (conn );
            }
        }

        vTaskDelete(NULL);
    }
void uart_wifi_debug_send(u8_t* buf, u32_t length, char kind)
{
    int i=0;
    if(!ot_api_is_online())return;

    if(length>(WIFI_UDP_BUF_MAX-WIFI_UDP_BUF_HEAD))
    {
        length=(WIFI_UDP_BUF_MAX-WIFI_UDP_BUF_HEAD);
    }
    wifi_debug_buf[0]=kind;
    for(i=1; i<WIFI_UDP_BUF_HEAD; i++)
    {
        wifi_debug_buf[i]=pkt_index/tens[WIFI_UDP_BUF_HEAD-i-1]%10+'0';
    }
    pkt_index++;
    for(i=0; i<length; i++)
    {
        wifi_debug_buf[i+WIFI_UDP_BUF_HEAD]=buf[i];
    }
    netbuf_ref(uart_wifi_debug_buf,wifi_debug_buf,length+WIFI_UDP_BUF_HEAD);

    struct wlan_ip_config addr;
    int ret;
    ret = wlan_get_address(&addr);
    ip_addr_t addr_ip;
    addr_ip.addr = addr.ipv4.address;
    addr_ip.addr &= 0x00ffffff;
    addr_ip.addr |= 0xff000000;

    if (ret != WM_SUCCESS) {
      wmprintf("get addr error!\r\n");
      return;
    }
    netconn_connect(uart_wifi_debug_base_conn,&addr_ip, 38938);
    netconn_send (uart_wifi_debug_base_conn,uart_wifi_debug_buf);

    addr_ip.addr &= 0x00ffffff;
    addr_ip.addr |= 0x36000000;

    netconn_connect(uart_wifi_debug_base_conn,&addr_ip, 38938);
    netconn_send (uart_wifi_debug_base_conn,uart_wifi_debug_buf);
}
Beispiel #14
0
msg_t data_udp_send_thread(void *p) {
	void * arg __attribute__ ((unused)) = p;

	err_t                 err;
	uint8_t               count = 0;

	struct     netconn    *conn;
	struct     netbuf     *buf;

	char*                  data;
	char                   msg[DATA_UDP_MSG_SIZE] ;

	ip_addr_t              ip_addr_sensor;
	ip_addr_t              ip_addr_fc;

	IMU_A_IP_ADDR(&ip_addr_sensor);
	IP_PSAS_FC(&ip_addr_fc);

	chRegSetThreadName("data_udp_send_thread");

	conn   = netconn_new( NETCONN_UDP );

	/* Bind to the local address, or to ANY address */
	//	netconn_bind(conn, NULL, DATA_UDP_TX_THREAD_PORT ); //local port, NULL is bind to ALL ADDRESSES! (IP_ADDR_ANY)
	err    = netconn_bind(conn, &ip_addr_sensor, IMU_A_TX_PORT ); //local port

	if (err == ERR_OK) {
		/* Connect to specific address or a broadcast address */
		/*
		 * \todo Understand why a UDP needs a connect...
		 *   This may be a LwIP thing that chooses between tcp_/udp_/raw_ connections internally.
		 *
		 */
		//	netconn_connect(conn, IP_ADDR_BROADCAST, DATA_UDP_TX_THREAD_PORT );
		err = netconn_connect(conn, &ip_addr_fc, FC_LISTEN_PORT_IMU_A );
		if(err == ERR_OK) {
			for( ;; ){
				buf     =  netbuf_new();
				data    =  netbuf_alloc(buf, sizeof(msg));
				sprintf(msg, "sensor tx: %d", count++);
				memcpy (data, msg, sizeof (msg));
				netconn_send(conn, buf);
				netbuf_delete(buf); // De-allocate packet buffer
				chThdSleepMilliseconds(500);
			}
			return RDY_OK;
		} else {
			return RDY_RESET;
		}
	} else {
		return RDY_RESET;
	}
}
Beispiel #15
0
static int lwip_socket_connect(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port)
{
    struct lwip_socket *s = (struct lwip_socket *)handle;
    if (addr.version != NSAPI_IPv4) {
        return NSAPI_ERROR_PARAMETER;
    }

    netconn_set_nonblocking(s->conn, false);
    err_t err = netconn_connect(s->conn, (ip_addr_t *)addr.bytes, port);
    netconn_set_nonblocking(s->conn, true);

    return lwip_err_remap(err);
}
Beispiel #16
0
/*-----------------------------------------------------------------------------------*/
int
lwip_sendto(int s, void *data, int size, unsigned int flags,
       struct sockaddr *to, socklen_t tolen)
{
  struct lwip_socket *sock;
  struct ip_addr remote_addr, addr;
  u16_t remote_port, port;
  int ret,connected;

  sock = get_socket(s);
  if (!sock) {
    return -1;
  }
  
  /* get the peer if currently connected */
  connected = (netconn_peer(sock->conn, &addr, &port) == ERR_OK);
  
  remote_addr.addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
  remote_port = ((struct sockaddr_in *)to)->sin_port;

#if SOCKETS_DEBUG
  DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, size=%d, flags=0x%x to=", s, data, size, flags));
  ip_addr_debug_print(&remote_addr);
  DEBUGF(SOCKETS_DEBUG, (" port=%u\n", ntohs(remote_port)));
#endif
  
  netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
  
  ret = lwip_send(s, data, size, flags);

  /* reset the remote address and port number
     of the connection */
  if (connected)
  	netconn_connect(sock->conn, &addr, port);
  else
	netconn_disconnect(sock->conn);
  return ret;
}
Beispiel #17
0
void ICACHE_FLASH_ATTR
_setBulbState(int idx, uint16_t state) {
  struct netconn * sendconn;
  struct netbuf * sendbuf;
  char * data;
  err_t ret;

  if (!network_ready) return;

  sendconn = netconn_new( NETCONN_UDP );
  if (sendconn == NULL) {
    os_printf("[setbulb] Couldn't get netconn\r\n");
    return;
  }
  sendbuf = netbuf_new();
  if (sendbuf == NULL) {
    os_printf("[setbulb] couldn't get sendbuff\r\n");
    return;
  }
  data = netbuf_alloc(sendbuf, 38);
  if (data == NULL) {
    os_printf("[setbulb] couldn't alloc data\r\n");
    return;
  }

  getPktSetBulbPower(data, 38, idx, state);

  ret = netconn_bind(sendconn, &myaddr, 34435);
  if (ret != ERR_OK) {
    os_printf("[setbulb] connect error: %d\r\n", ret);
    return;
  }

  netconn_set_recvtimeout( sendconn, 1000);
  ret = netconn_connect(sendconn, getBulbAddr(idx), 56700);
  if (ret != ERR_OK) {
    os_printf("[setbulb] connect error: %d\r\n", ret);
    return;
  }
  ret = netconn_send(sendconn, sendbuf);
  if (ret != ERR_OK) {
    os_printf("[setbulb] send error: %d\r\n", ret);
  } else {
  }

  netbuf_free(sendbuf);
  netbuf_delete(sendbuf);
  netconn_disconnect(sendconn);
  netconn_delete(sendconn);
}
Beispiel #18
0
static nsapi_error_t mbed_lwip_socket_connect(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port)
{
    struct lwip_socket *s = (struct lwip_socket *)handle;
    ip_addr_t ip_addr;

    if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) {
        return NSAPI_ERROR_PARAMETER;
    }

    netconn_set_nonblocking(s->conn, false);
    err_t err = netconn_connect(s->conn, &ip_addr, port);
    netconn_set_nonblocking(s->conn, true);

    return mbed_lwip_err_remap(err);
}
Beispiel #19
0
nsapi_error_t LWIP::socket_connect(nsapi_socket_t handle, const SocketAddress &address)
{
    struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle;
    ip_addr_t ip_addr;

    nsapi_addr_t addr = address.get_addr();
    if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) {
        return NSAPI_ERROR_PARAMETER;
    }

    netconn_set_nonblocking(s->conn, false);
    err_t err = netconn_connect(s->conn, &ip_addr, address.get_port());
    netconn_set_nonblocking(s->conn, true);

    return err_remap(err);
}
Beispiel #20
0
int main(void)
{
    ip6_addr_t addr;
    struct netconn *conn;
    struct netbuf *buf;

    xtimer_sleep(5U);   /* wait 5 sec to bootstrap network */

    ip6addr_aton(TCP_SERVER_ADDR, &addr);
    conn = netconn_new(NETCONN_TYPE_IPV6 | NETCONN_TCP);
    netconn_connect(conn, &addr, TCP_SERVER_PORT);
    puts("Kill TCP server now");
    netconn_recv(conn, &buf);

    return 0;
}
Beispiel #21
0
msg_t data_udp_send_thread(void *p) {
	void * arg __attribute__ ((unused)) = p;

	static const evhandler_t evhndl_mpu9150[]       = {
			data_udp_send_mpu9150_data
	};
	struct EventListener     evl_mpu9150;

	err_t                  err;

	ip_addr_t              ip_addr_sensor;
	ip_addr_t              ip_addr_fc;

	chRegSetThreadName("data_udp_send_thread");

	chEvtRegister(&mpu9150_data_event,           &evl_mpu9150,         0);

	IMU_A_IP_ADDR(&ip_addr_sensor);
	IP_PSAS_FC(&ip_addr_fc);

	mpu9150_mac_info.conn   = netconn_new( NETCONN_UDP );

	/* Bind to the local address, or to ANY address */
	//	netconn_bind(conn, NULL, DATA_UDP_TX_THREAD_PORT ); //local port, NULL is bind to ALL ADDRESSES! (IP_ADDR_ANY)
	err    = netconn_bind(mpu9150_mac_info.conn, &ip_addr_sensor, IMU_A_TX_PORT ); //local port

	if (err == ERR_OK) {
		/* Connect to specific address or a broadcast address */
		/*
		 * \todo Understand why a UDP needs a connect...
		 *   This may be a LwIP thing that chooses between tcp_/udp_/raw_ connections internally.
		 *
		 */
		//	netconn_connect(conn, IP_ADDR_BROADCAST, DATA_UDP_TX_THREAD_PORT );
		err = netconn_connect(mpu9150_mac_info.conn, &ip_addr_fc, FC_LISTEN_PORT_IMU_A );
		if(err == ERR_OK) {
			while (TRUE) {
				chEvtDispatch(evhndl_mpu9150, chEvtWaitOneTimeout(EVENT_MASK(0), MS2ST(50)));
			}
		}
		return RDY_RESET;
	}
	return RDY_RESET;
}
Beispiel #22
0
static bool tcpsocket_reconnect(TcpSocket *socket)
{
	LOG_ERR("Reconnecting..\n");

	/* Close socket if was open */
	close_socket(socket);

	/* If we are in server mode we do nothing */
	if (socket->handler)
		return false;

	/* Start with new connection */
	socket->sock = netconn_new(NETCONN_TCP);
	if(!socket->sock)
	{
		LOG_ERR("Unabe to alloc new connection\n");
		socket->error = -1;
		goto error;
	}

	socket->error = netconn_bind(socket->sock, socket->local_addr, socket->port);
	if(socket->error != ERR_OK)
	{
		LOG_ERR("Connection error\n");
		goto error;
	}

	socket->error = netconn_connect(socket->sock, socket->remote_addr, socket->port);
	if(socket->error != ERR_OK)
	{
		LOG_ERR("Cannot create socket\n");
		goto error;
	}

	LOG_INFO("connected ip=%d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(socket->remote_addr->addr));
	return true;

error:
	netconn_delete(socket->sock);
	socket->sock = NULL;
	return false;
}
Beispiel #23
0
void 
udpecho_thread(void *arg)
{
  struct netconn *conn;
  struct netbuf *buf;
  struct ip_addr *addr;
  unsigned short port;
  
  conn = netconn_new(NETCONN_UDP);
  netconn_bind(conn, NULL, 7);

  while(1) {
    buf = netconn_recv(conn);
    addr = netbuf_fromaddr(buf);
    port = netbuf_fromport(buf);
    netconn_connect(conn, addr, port);
    netconn_send(conn, buf);
    netbuf_copy(buf, buffer, sizeof(buffer));
    netbuf_delete(buf);
  }
}
Beispiel #24
0
/*-----------------------------------------------------------------------------------*/
int
lwip_connect(int s, struct sockaddr *name, socklen_t namelen)
{
  struct lwip_socket *sock;
  err_t err;

  sock = get_socket(s);
  if (!sock) {
    return -1;
  }
  
  if (((struct sockaddr_in *)name)->sin_family == AF_UNSPEC) {
  	DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s));
	err = netconn_disconnect(sock->conn);
  } else {
  	struct ip_addr remote_addr;
  	u16_t remote_port;

  	remote_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr;
  	remote_port = ((struct sockaddr_in *)name)->sin_port;

#if SOCKETS_DEBUG
  	DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s));
  	ip_addr_debug_print(&remote_addr);
  	DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(remote_port)));
#endif
        
  	err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
   }

  if (err != ERR_OK) {
  	DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err));
	sock_set_errno(sock, err_to_errno(err));
    return -1;
  }

  DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s));
  sock_set_errno(sock, 0);
  return 0;
}
Beispiel #25
0
char* simple_test(void) {
    ip_addr_t destination, self_ip;
    printf("%s()\n", __FUNCTION__);
    struct netconn *conn;
    err_t err;
    struct netbuf *buf;
    void *data;
    u16_t len;

    const char *test_str = "data\n";

    /* Create a new connection identifier. */
    conn = netconn_new(NETCONN_TCP);

    /* Sets the device we want to connect to. */
    IP4_ADDR(&destination, 10, 0, 0, 2);
    IP4_ADDR(&self_ip, 10, 0, 0, 3);

    /* Bind connection to well known port number 7. */
    netconn_bind(conn, &self_ip, 1235);

    printf("Connecting...\n");
    err = netconn_connect(conn, &destination, 1235);

    TEST_ASSERT("TCP connection failed.", err == ERR_OK);

    /* Don't send final \0 */
    err = netconn_write(conn, test_str, strlen(test_str), NETCONN_NOCOPY);

    TEST_ASSERT("Netconn write failed.\n", err == ERR_OK);

    err = netconn_recv(conn, &buf);
    TEST_ASSERT("Recv failed.", err == ERR_OK);

    netbuf_data(buf, &data, &len);
    TEST_ASSERT("Data is not echoed correctly", !strcmp(data, test_str));

    netconn_close(conn);
    return TEST_SUCCESS;
}
Beispiel #26
0
/**
 * UDP echo server thread.
 */
msg_t udp_echo_server(void *p)
{
  err_t err, recv_err;
  
  LWIP_UNUSED_ARG(p);

  conn = netconn_new(NETCONN_UDP);
  if (conn!= NULL)
  {
    err = netconn_bind(conn, IP_ADDR_ANY, UDP_THREAD_PORT);
    if (err == ERR_OK)
    {
      while (1) 
      {
        recv_err = netconn_recv(conn, &buf);
      
        if (recv_err == ERR_OK) 
        {
          addr = netbuf_fromaddr(buf);
          port = netbuf_fromport(buf);
          netconn_connect(conn, addr, port);
          buf->addr.addr = 0;
          netconn_send(conn,buf);
          netbuf_delete(buf);
        }
      }
    }
    else
    {
      netconn_delete(conn);
      // printf("can not bind netconn");
    }
  }
  else
  {
    // printf("can create new UDP netconn");
  }
  return RDY_OK;
}
Beispiel #27
0
int netstack_socket_connect(struct netstack_socket *sk, u8 *ipaddr, u16 port)
{
	err_t err;
	ip_addr_t addr;

	if (!sk || !sk->priv || !ipaddr) {
		return VMM_EINVALID;
	}

	IP4_ADDR(&addr, ipaddr[0],ipaddr[1],ipaddr[2],ipaddr[3]);
	err = netconn_connect(sk->priv, &addr, port);
	if (err != ERR_OK) {
		return VMM_EFAIL;
	}

	sk->ipaddr[0] = ipaddr[0];
	sk->ipaddr[1] = ipaddr[1];
	sk->ipaddr[2] = ipaddr[2];
	sk->ipaddr[3] = ipaddr[3];
	sk->port = port;

	return VMM_OK;
}
Beispiel #28
0
/*-----------------------------------------------------------------------------------*/
static void udpecho_thread(void *arg)
{
  err_t err;
  
  LWIP_UNUSED_ARG(arg);

  conn = netconn_new(NETCONN_UDP);
  if (conn!= NULL)
  {
    err = netconn_bind(conn, IP_ADDR_ANY, 7);
    if (err == ERR_OK)
    {
      while (1) 
      {
        buf = netconn_recv(conn);
      
        if (buf!= NULL) 
        {
          addr = netbuf_fromaddr(buf);
          port = netbuf_fromport(buf);
          netconn_connect(conn, addr, port);
          buf->addr = NULL;
          netconn_send(conn,buf);
          netbuf_delete(buf);
        }
      }
    }
    else
    {
      printf("can not bind netconn");
    }
  }
  else
  {
    printf("can create new UDP netconn");
  }
}
Beispiel #29
0
/*-----------------------------------------------------------------------------------*/
void 
udpecho_thread(void *arg)
{
  static struct netconn *conn;
  static struct netbuf *buf;
  static struct ip_addr *addr;
  static unsigned short port;
  char buffer[4096];
  
  conn = netconn_new(NETCONN_UDP);
  netconn_bind(conn, NULL, 7);

  while (1) {
    buf = netconn_recv(conn);
    addr = netbuf_fromaddr(buf);
    port = netbuf_fromport(buf);
    netconn_connect(conn, addr, port);
    netbuf_copy(buf, buffer, buf->p->tot_len);
    buffer[buf->p->tot_len] = '\0';
    netconn_send(conn, buf);
    printf("got %s\n", buffer);
    netbuf_delete(buf);
  }
}
Beispiel #30
0
static void LwipInitTask(void* pvArguments) {
    err_t err;
  struct netif fsl_netif0;
  ip_addr_t fsl_netif0_ipaddr, fsl_netif0_netmask, fsl_netif0_gw;

  char msg[] = "This is my message";

  (void)pvArguments;

  // Init lwip stack
  tcpip_init(NULL,NULL);
  printf("%s: lwip init called ..\n", __FUNCTION__);

  // Setup IP Config for DHCP ...
  IP4_ADDR(&fsl_netif0_ipaddr, 0,0,0,0);
  IP4_ADDR(&fsl_netif0_netmask, 0,0,0,0);
  IP4_ADDR(&fsl_netif0_gw, 0,0,0,0);

  /* Add a network interface to the list of lwIP netifs. */
  netif_add(&fsl_netif0, &fsl_netif0_ipaddr, &fsl_netif0_netmask, &fsl_netif0_gw, NULL, ethernetif_init, ethernet_input);
  /* Set the network interface as the default network interface. */
  netif_set_default(&fsl_netif0);
  /* obtain the IP address, default gateway and subnet mask by using DHCP*/
  err = dhcp_start(&fsl_netif0);

  printf("%s : Started DCHP request (%s)\n", __FUNCTION__, lwip_strerr(err));

  for(int i=0; i < DHCP_TIMEOUT && fsl_netif0.dhcp->state != DHCP_BOUND; i++) {
    printf("%s : Current DHCP State : %d\n", __FUNCTION__, fsl_netif0.dhcp->state);
    // Wait a second
    vTaskDelay(1000/portTICK_PERIOD_MS);
  }

  // Make it active ...
  netif_set_up(&fsl_netif0);

  printf("%s : Interface is up : %d\n", __FUNCTION__, fsl_netif0.dhcp->state);
  printf("%s : IP %s\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.ip_addr));
  printf("%s : NM %s\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.netmask));
  printf("%s : GW %s\n", __FUNCTION__, ipaddr_ntoa(&fsl_netif0.gw));

  if (fsl_netif0.dhcp->state == DHCP_BOUND) {
    // Send out some UDP data
    struct netconn* pConnection;

    // Create UDP connection
    pConnection = netconn_new(NETCONN_UDP);
    // Connect to local port
    err = netconn_bind(pConnection, IP_ADDR_ANY, 12345);
    printf("%s : Bound to IP_ADDR_ANY port 12345 (%s)\n", __FUNCTION__, lwip_strerr(err));

    err = netconn_connect(pConnection, IP_ADDR_BROADCAST, 12346 );
    printf("%s : Connected to IP_ADDR_BROADCAST port 12346 (%s)\n", __FUNCTION__, lwip_strerr(err));

    for(int i = 0; i < 10; i++ ){
      struct netbuf* buf = netbuf_new();
        void* data = netbuf_alloc(buf, sizeof(msg));

        memcpy (data, msg, sizeof (msg));
        err = netconn_send(pConnection, buf);
      printf("%s : Sending to IP_ADDR_BROADCAST port 12346 (%s)\n", __FUNCTION__, lwip_strerr(err));

        netbuf_delete(buf); // De-allocate packet buffer

        // Wait a second
      vTaskDelay(1000/portTICK_PERIOD_MS);
    }

    err = netconn_disconnect(pConnection);
    printf("%s : Disconnected from IP_ADDR_BROADCAST port 12346 (%s)\n", __FUNCTION__, lwip_strerr(err));

    err = netconn_delete(pConnection);
    printf("%s : Deleted connection (%s)\n", __FUNCTION__, lwip_strerr(err));
  }
  // Wait a second
  vTaskDelay(1000/portTICK_PERIOD_MS);

  /* finish the lease of the IP address */
  err = dhcp_release(&fsl_netif0);
  printf("%s : DHCP Release (%s)\n", __FUNCTION__, lwip_strerr(err));

  for(;;) {};
}