Exemple #1
0
void tcpecho6(void)
{
	struct netconn *conn, *newconn;
	err_t err;

	conn = netconn_new(NETCONN_TCP_IPV6);
	netconn_bind_ip6(conn, IP6_ADDR_ANY, TCP_ECHO_PORT);
	netconn_listen(conn);

	while(1)
	{
		err = netconn_accept(conn, &newconn);
		if(err == ERR_OK)
		{
			struct netbuf *buf;
			void *data;
			u16_t len;

			while(netconn_recv(newconn, &buf) == ERR_OK)
			{
				do
				{
					netbuf_data(buf, &data, &len);
					err = netconn_write(newconn, data, len, NETCONN_COPY);
					if(err != ERR_OK)
						printf("netconn_write() error\n");
				}while(netbuf_next(buf) >= 0);
				netbuf_delete(buf);
			}
			netconn_delete(newconn);
		}
	}
}
Exemple #2
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;
}
/* Private functions ---------------------------------------------------------*/
static void tcpecho_thread(void *arg)
{
  struct netconn *conn, *newconn;
  err_t err, accept_err;
  struct netbuf *buf;
  void *data;
  u16_t len;
      
  LWIP_UNUSED_ARG(arg);

  /* Create a new connection identifier. */
  conn = netconn_new(NETCONN_TCP);
  
  if (conn!=NULL)
  {  
    /* Bind connection to well known port number 7. */
    err = netconn_bind(conn, NULL, 7);
    
    if (err == ERR_OK)
    {
      /* Tell connection to go into listening mode. */
      netconn_listen(conn);
    
      while (1) 
      {
        /* Grab new connection. */
         accept_err = netconn_accept(conn, &newconn);
    
        /* Process the new connection. */
        if (accept_err == ERR_OK) 
        {
		  HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET);
          while (netconn_recv(newconn, &buf) == ERR_OK) 
          {
            do 
            {
              netbuf_data(buf, &data, &len);
              netconn_write(newconn, data, len, NETCONN_COPY);
          
            } 
            while (netbuf_next(buf) >= 0);
          
            netbuf_delete(buf);
          }
        
          /* Close connection and discard connection identifier. */
          netconn_close(newconn);
          netconn_delete(newconn);
          HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_RESET);
        }
      }
    }
    else
    {
      netconn_delete(newconn);
    }
  }
}
Exemple #4
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 tcp_task(void)
{
    struct netconn *conn, *newconn;
    err_t err;

    conn = netconn_new(NETCONN_TCP);

    if(conn != NULL) 
    {  
        err = netconn_bind(conn, NULL, 7);

        if (err == ERR_OK) 
        {
            /* Tell connection to go into listening mode. */
            netconn_listen(conn);

            while (1) 
            {
                /* Grab new connection. */
                newconn = netconn_accept(conn);

                /* Process the new connection. */
                if (newconn) {
                    struct netbuf *buf;
                    void *data;
                    u16_t len;

                    while ((buf = netconn_recv(newconn)) != NULL) 
                    {
                        do 
                        {
                            netbuf_data(buf, &data, &len);              
                            netconn_write(newconn, data, len, NETCONN_COPY);

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

                    /* Close connection and discard connection identifier. */
                    netconn_close(newconn);
                    netconn_delete(newconn);
                }
            }
        }
        else
        {
            printf(" can not bind TCP netconn");
        }
    } 
    else 
    {
        printf("can not create TCP netconn");
    }
}
Exemple #7
0
/*-----------------------------------------------------------------------------------*/
static void 
tcpecho_thread(void *arg)
{
  struct netconn *conn, *newconn;
  err_t err;
  LWIP_UNUSED_ARG(arg);

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

  /* Bind connection to well known port number TCP_ECHO_PORT. */
  netconn_bind(conn, IP_ADDR_ANY, TCP_ECHO_PORT);

  /* Tell connection to go into listening mode. */
  netconn_listen(conn);

  while (1) {

    /* Grab new connection. */
    newconn = netconn_accept(conn);
    printf("accepted new connection %p\n", newconn);
    /* Process the new connection. */
    if (newconn != NULL) {
      struct netbuf *buf;
      void *data;
      u16_t len;
      
      while ((buf = netconn_recv(newconn)) != NULL) {
        printf("Recved\n");
        do {
             netbuf_data(buf, &data, &len);
             err = netconn_write(newconn, data, len, NETCONN_COPY);
#if 0
            if (err != ERR_OK) {
              printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(err));
            }
#endif
        } while (netbuf_next(buf) >= 0);
        netbuf_delete(buf);
      }
      printf("Got EOF, looping\n"); 
      /* Close connection and discard connection identifier. */
      netconn_close(newconn);
      netconn_delete(newconn);
    }
  }
}
Exemple #8
0
void tcpecho_entry(void *parameter)
{
	struct netconn *conn, *newconn;
	err_t err;

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

	/* Bind connection to well known port number 7. */
	netconn_bind(conn, NULL, TCP_ECHO_PORT);

	/* Tell connection to go into listening mode. */
	netconn_listen(conn);

	while(1)
	{
		/* Grab new connection. */
		newconn = netconn_accept(conn);
		/* Process the new connection. */
		if(newconn != NULL)
		{
			struct netbuf *buf;
			void *data;
			u16_t len;

			while((buf = netconn_recv(newconn)) != NULL)
			{
				do
				{
					netbuf_data(buf, &data, &len);
					err = netconn_write(newconn, data, len, NETCONN_COPY);
					if(err != ERR_OK){}
				}
				while(netbuf_next(buf) >= 0);
				netbuf_delete(buf);
			}
			/* Close connection and discard connection identifier. */
			netconn_delete(newconn);
		}
	}
}
Exemple #9
0
int netstack_socket_nextbuf(struct netstack_socket_buf *buf)
{
	s8_t err;
	struct netbuf *nb;

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

	nb = buf->priv;

	err = netbuf_next(nb);
	if (err != 0 && err != 1) {
		return VMM_ENOENT;
	}

	netbuf_data(nb, &buf->data, &buf->len);
	buf->priv = nb;

	return VMM_OK;
}
Exemple #10
0
void rpc_server_thread(void *p)
{
    struct netconn *conn, *client_conn;
    int error;

    (void)p;

    struct netbuf *buf;
    char *data;
    u16_t len;
    err_t err;

    serial_datagram_rcv_handler_t handler;

    chRegSetThreadName("rpc_service_call");

    /* Creates a TCP server */
    conn = netconn_new(NETCONN_TCP);
    if (conn == NULL) {
        chSysHalt("Cannot create SimpleRPC service call server connection (out of memory).");
    }

    netconn_bind(conn, IP_ADDR_ANY, RPC_SERVER_PORT);
    netconn_listen(conn);

    while (1) {
        serial_datagram_rcv_handler_init(&handler,
                                         input_buffer, sizeof input_buffer,
                                         serial_datagram_recv_cb,
                                         NULL);

        error = netconn_accept(conn, &client_conn);

        if (error != ERR_OK) {
            continue;
        }

        /* method_called will be set to true once a callback is fired. */
        method_called = false;
        output_bytes_written = 0;

        while (!method_called) {
            /* Tries to receive something from the connection. */
            err = netconn_recv(client_conn, &buf);

            /* If connection closed early */
            if (err != ERR_OK) {
                break;
            }

            do {
                netbuf_data(buf, (void **)&data, &len);
                err = serial_datagram_receive(&handler, data, len);
            } while (netbuf_next(buf) >= 0);
            netbuf_delete(buf);
        }

        if (output_bytes_written > 0) {
            netconn_write(client_conn, output_buffer, output_bytes_written, NETCONN_COPY);
        }

        netconn_close(client_conn);
        netconn_delete(client_conn);
    }
}
T_uezError Network_lwIP_SocketRead(
    void *aWorkspace,
    T_uezNetworkSocket aSocket,
    void *aData,
    TUInt32 aNumBytes,
    TUInt32 *aNumBytesRead,
    TUInt32 aTimeout)
{
    T_Network_lwIP_Workspace *p = (T_Network_lwIP_Workspace *)aWorkspace;
    T_uezError error = UEZ_ERROR_NONE;
    T_lwIPSocket *p_socket = p->iSockets + aSocket;
    TUInt16 numCopy;
    //    TUInt32 timeStart = UEZTickCounterGet();

    // No bytes read yet
    *aNumBytesRead = 0;

    // 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 if ((p_socket->iFlags & SOCKET_FLAG_CONNECTED) == 0) {
        error = UEZ_ERROR_NOT_OPEN;
    } else {
        // Read segments of the incoming data while we need data
        while (aNumBytes) {
            // Do we have any data waiting to be read?
            if (p_socket->iReceiveRemaining) {
                // Copy over the remaining bytes waiting in the buffer
                if (aNumBytes < p_socket->iReceiveRemaining)
                    numCopy = (TUInt16)aNumBytes;
                else
                    numCopy = p_socket->iReceiveRemaining;
                memcpy(aData, p_socket->iReceiveData + p_socket->iReceiveLength
                       - p_socket->iReceiveRemaining, numCopy);

                // Update counts and pointers
                *aNumBytesRead += numCopy;
                p_socket->iReceiveRemaining -= numCopy;
                aData = (void *)(((TUInt8 *)aData) + numCopy);

                // Decrement the amount we are waiting on
                aNumBytes -= numCopy;

                // If count goes to zero, then we need to release this pbuf
                // and go to the next.
                if (p_socket->iReceiveRemaining == 0) {
                    if (netbuf_next(p_socket->iReceiveNetBuf) >= 0) {
                        // Got more data!  Setup the info for it
                        netbuf_data(p_socket->iReceiveNetBuf,
                                    (void **)&p_socket->iReceiveData, &p_socket->iReceiveLength);
                        p_socket->iReceiveRemaining = p_socket->iReceiveLength;
                    } else {
                        // Done with this netbuf, delete it
                        netbuf_delete(p_socket->iReceiveNetBuf);
                        p_socket->iReceiveNetBuf = 0;
                        p_socket->iReceiveData = 0;
                    }
                }
            }

            // Do we need another packet of dat?  If so, we need to fetch more data
            if ((aNumBytes) && (p_socket->iReceiveRemaining == 0)) {
                // Change the timeout for this connection
                p_socket->iNetconn->recv_timeout = aTimeout;
                if (p_socket->iNetconn->err == ERR_TIMEOUT)
                    p_socket->iNetconn->err = ERR_OK;
                p_socket->iReceiveNetBuf = netconn_recv(p_socket->iNetconn);
                if (p_socket->iReceiveNetBuf == 0) {
                    // Received no data, possibly timed out?
                    // Stop with the given returned data
                    error = IConvertErrorCode(p_socket->iNetconn->err);
                    break;
                }
                // We did get data, pull out the pertinent info
                netbuf_data(p_socket->iReceiveNetBuf, (void **)&p_socket->iReceiveData,
                            &p_socket->iReceiveLength);
                p_socket->iReceiveRemaining = p_socket->iReceiveLength;
            }
        }
    }

    UEZSemaphoreRelease(p->iSem);

    return error;
}
Exemple #12
0
/**
 * TCP echo server thread.
 */
msg_t tcp_echo_server(void *p)
{
    struct netconn *conn, *newconn;
  err_t err, accept_err;
  struct netbuf *buf;
  void *data;
  u16_t len;
  err_t recv_err;
      
  LWIP_UNUSED_ARG(p);

  /* Create a new connection identifier. */
  conn = netconn_new(NETCONN_TCP);
  
  if (conn!=NULL)
  {  
    /* Bind connection to well known port number 7. */
    err = netconn_bind(conn, NULL, TCP_THREAD_PORT);
    
    if (err == ERR_OK)
    {
      /* Tell connection to go into listening mode. */
      netconn_listen(conn);
    
      while (1) 
      {
        /* Grab new connection. */
         accept_err = netconn_accept(conn, &newconn);
    
        /* Process the new connection. */
        if (accept_err == ERR_OK) 
        {
          recv_err = netconn_recv(newconn, &buf);
          while ( recv_err == ERR_OK) 
          {
            do 
            {
              netbuf_data(buf, &data, &len);
              netconn_write(newconn, data, len, NETCONN_COPY);
          
            } 
            while (netbuf_next(buf) >= 0);
          
            netbuf_delete(buf);
            recv_err = netconn_recv(newconn, &buf);
          }
        
          /* Close connection and discard connection identifier. */
          netconn_close(newconn);
          netconn_delete(newconn);
        }
      }
    }
    else
    {
      netconn_delete(newconn);
      // printf(" can not bind TCP netconn");
    }
  }
  else
  {
    // printf("can not create TCP netconn");
  }
  return RDY_OK;
}