void database_internal_adduserblk(database_t* db, database_blk_user_t& user) { database_user_t* nuser = (database_user_t*) malloc(sizeof(database_user_t)); nuser->status = user.status; nuser->m_name = netbuf_copy(user.name); nuser->m_key = netbuf_copy(user.key); nuser->m_iv = netbuf_copy(user.iv); db->data.push_back(nuser); __current_user = nuser; }
static void lwip_perf_thread(void *arg) { static struct netconn *conn; static struct netbuf *buf; //static ip_addr_t *addr; static unsigned short port; char *buffer; lwip_perf_cmd_t cmd; err_t err = ERR_OK; uint16_t len, i, j; LWIP_UNUSED_ARG(arg); conn = netconn_new(NETCONN_UDP); LWIP_ASSERT("con != NULL", conn != NULL); netconn_bind(conn, NULL, 7); // echo port buffer = malloc(PERF_MAX_PAYLOAD_SIZE); assert(buffer); while (1) { err = netconn_recv(conn, &buf); if (err == ERR_OK) { //addr = netbuf_fromaddr(buf); port = netbuf_fromport(buf); uprintf(UPRINT_INFO, UPRINT_BLK_NET, "LWIP perf Rx: port=%d\n", port); len = netbuf_copy(buf, (char *)&cmd, sizeof(lwip_perf_cmd_t)); /* no need netconn_connect here, since the netbuf contains the address */ if(len != sizeof(lwip_perf_cmd_t)) { LWIP_DEBUGF(LWIP_DBG_ON, ("netbuf_copy failed\n")); } else { /* check packet size */ if(cmd.nSize>PERF_MAX_PAYLOAD_SIZE || cmd.nSize==0) cmd.nSize = PERF_MAX_PAYLOAD_SIZE; /* link buffer to netbuf */ err = netbuf_ref(buf, buffer, cmd.nSize); LWIP_DEBUGF(LWIP_DBG_ON, ("lwip perf: nPacket=%d nSize=%d nDelay=%d\n", cmd.nPacket, cmd.nSize, cmd.nDelay)); for(i=0; i<cmd.nPacket && err==ERR_OK; i++) { /* simulate buffer construction */ for(j=0; j<cmd.nSize; j++) buffer[j] = (j&0xFF); /* send packet now */ err = netconn_send(conn, buf); if(err != ERR_OK) { LWIP_DEBUGF(LWIP_DBG_ON, ("netconn_send failed: %d\n", (int)err)); } if(cmd.nDelay) task_delay(cmd.nDelay); } LWIP_DEBUGF(LWIP_DBG_ON, ("lwip perf: send %d packets\n", i)); } netbuf_delete(buf); } } free(buffer); }
void message_server_thread(void *arg) { struct netconn *conn; struct netbuf *buf; static uint8_t buffer[4096]; err_t err; LWIP_UNUSED_ARG(arg); chRegSetThreadName("rpc_message"); conn = netconn_new(NETCONN_UDP); if (conn == NULL) { chSysHalt("Cannot create SimpleRPC message server connection (out of memory)."); } netconn_bind(conn, NULL, MSG_SERVER_PORT); while (1) { err = netconn_recv(conn, &buf); if (err == ERR_OK) { netbuf_copy(buf, buffer, buf->p->tot_len); message_process(buffer, buf->p->tot_len, message_callbacks, message_callbacks_len); } netbuf_delete(buf); } }
/*-----------------------------------------------------------------------------------*/ 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 */ } } }
static nsapi_size_or_error_t mbed_lwip_socket_recvfrom(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t *addr, uint16_t *port, void *data, nsapi_size_t size) { struct lwip_socket *s = (struct lwip_socket *)handle; struct netbuf *buf; err_t err = netconn_recv(s->conn, &buf); if (err != ERR_OK) { return mbed_lwip_err_remap(err); } convert_lwip_addr_to_mbed(addr, netbuf_fromaddr(buf)); *port = netbuf_fromport(buf); u16_t recv = netbuf_copy(buf, data, (u16_t)size); netbuf_delete(buf); return recv; }
static int lwip_socket_recvfrom(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t *addr, uint16_t *port, void *data, unsigned size) { struct lwip_socket *s = (struct lwip_socket *)handle; struct netbuf *buf; err_t err = netconn_recv(s->conn, &buf); if (err != ERR_OK) { return lwip_err_remap(err); } addr->version = NSAPI_IPv4; memcpy(addr->bytes, netbuf_fromaddr(buf), sizeof addr->bytes); *port = netbuf_fromport(buf); u16_t recv = netbuf_copy(buf, data, (u16_t)size); netbuf_delete(buf); return recv; }
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); } }
nsapi_size_or_error_t LWIP::socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, void *data, nsapi_size_t size) { struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; struct netbuf *buf; err_t err = netconn_recv(s->conn, &buf); if (err != ERR_OK) { return err_remap(err); } if (address) { nsapi_addr_t addr; convert_lwip_addr_to_mbed(&addr, netbuf_fromaddr(buf)); address->set_addr(addr); address->set_port(netbuf_fromport(buf)); } u16_t recv = netbuf_copy(buf, data, (u16_t)size); netbuf_delete(buf); return recv; }
static void udpecho_thread(void *arg) { static struct netconn *conn; static struct netbuf *buf; char buffer[100]; err_t err; LWIP_UNUSED_ARG(arg); netif_set_up(&fsl_netif0); conn = netconn_new(NETCONN_UDP); LWIP_ASSERT("con != NULL", conn != NULL); netconn_bind(conn, NULL, 7); while (1) { err = netconn_recv(conn, &buf); if (err == ERR_OK) { if (netbuf_copy(buf, buffer, buf->p->tot_len) != buf->p->tot_len) { LWIP_DEBUGF(UDPECHO_DBG, ("netbuf_copy failed\r\n")); } else { buffer[buf->p->tot_len] = '\0'; err = netconn_send(conn, buf); if (err != ERR_OK) { LWIP_DEBUGF(UDPECHO_DBG, ("netconn_send failed: %d\r\n", (int)err)); } else { LWIP_DEBUGF(UDPECHO_DBG, ("got %s\r\n", buffer)); } } netbuf_delete(buf); } } }
/*-----------------------------------------------------------------------------------*/ static void udpecho_thread(void *arg) { static struct netconn *conn; static struct netbuf *buf; static ip_addr_t *addr; static unsigned short port; char buffer[4096]; err_t err; LWIP_UNUSED_ARG(arg); conn = netconn_new(NETCONN_UDP); LWIP_ASSERT("con != NULL", conn != NULL); netconn_bind(conn, NULL, 7); while (1) { err = netconn_recv(conn, &buf); if (err == ERR_OK) { addr = netbuf_fromaddr(buf); port = netbuf_fromport(buf); /* no need netconn_connect here, since the netbuf contains the address */ if(netbuf_copy(buf, buffer, buf->p->tot_len) != buf->p->tot_len) { LWIP_DEBUGF(LWIP_DBG_ON, ("netbuf_copy failed\n")); } else { buffer[buf->p->tot_len] = '\0'; err = netconn_send(conn, buf); if(err != ERR_OK) { LWIP_DEBUGF(LWIP_DBG_ON, ("netconn_send failed: %d\n", (int)err)); } else { LWIP_DEBUGF(LWIP_DBG_ON, ("got %s\n", buffer)); } } netbuf_delete(buf); } } }
/*-----------------------------------------------------------------------------------*/ 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); } }
void XMLRPCServer::UDPreceive(void* params) { struct netconn *conn; struct netbuf *buf; err_t err; // Initialize memory (in stack) for message. char message[60]; // TODO: Set its size according to UDPMessage. os_printf("Test!\n"); conn = netconn_new(NETCONN_UDP); for (;;) { // Check if connection was created successfully. if (conn != NULL) { err = netconn_bind(conn, IP_ADDR_ANY, UDP_RECEIVE_PORT); // Check if we were able to bind to port. if (err == ERR_OK) { portTickType xLastWakeTime; // Initialize the xLastWakeTime variable with the current time. xLastWakeTime = xTaskGetTickCount(); // Start periodic loop. while (1) { buf = netconn_recv(conn); if (buf != NULL) { struct ip_addr* ip; uint16_t port; ip = buf->addr; port = buf->port; //if(ip != NULL) //os_printf("Received from %d:%d!\n", ip->addr, port); // Copy received data into message. uint16_t len = netbuf_len(buf); if (len > 15) { netbuf_copy (buf, &message, len); uint32_t connectionID = *((uint32_t*)&message[0]); TopicReader* tr = getTopicReader(connectionID); if (tr != NULL) { tr->enqueueMessage(&message[8]); //os_printf("ConnectionID: %d, topic:%s\n", connectionID, tr->getTopic()); } } // Deallocate previously created memory. netbuf_delete(buf); } // Use delay until to guarantee periodic execution of each loop iteration. else { os_printf("buf = NULL!\n"); vTaskDelayUntil(&xLastWakeTime, 30); } } } else { os_printf("cannot bind netconn\n"); } } else { os_printf("cannot create new UDP netconn\n"); conn = netconn_new(NETCONN_UDP); } // If connection failed, wait for 50 ms before retrying. vTaskDelay(50); } }
static void dhcpserver_task(void *pxParameter) { /* netif_list isn't assigned until after user_init completes, which is why we do it inside the task */ state->server_if = netif_list; /* TODO: Make this configurable */ state->nc = netconn_new (NETCONN_UDP); if(!state->nc) { printf("DHCP Server Error: Failed to allocate socket.\r\n"); return; } netconn_bind(state->nc, IP_ADDR_ANY, DHCP_SERVER_PORT); while(1) { struct netbuf *netbuf; struct dhcp_msg received = { 0 }; /* Receive a DHCP packet */ err_t err = netconn_recv(state->nc, &netbuf); if(err != ERR_OK) { printf("DHCP Server Error: Failed to receive DHCP packet. err=%d\r\n", err); continue; } /* expire any leases that have passed */ uint32_t now = xTaskGetTickCount(); for(int i = 0; i < state->max_leases; i++) { uint32_t expires = state->leases[i].expires; if(expires && expires < now) state->leases[i].expires = 0; } ip_addr_t received_ip; u16_t port; netconn_addr(state->nc, &received_ip, &port); if(netbuf_len(netbuf) < offsetof(struct dhcp_msg, options)) { /* too short to be a valid DHCP client message */ netbuf_delete(netbuf); continue; } if(netbuf_len(netbuf) >= sizeof(struct dhcp_msg)) { printf("DHCP Server Warning: Client sent more options than we know how to parse. len=%d\r\n", netbuf_len(netbuf)); } netbuf_copy(netbuf, &received, sizeof(struct dhcp_msg)); netbuf_delete(netbuf); uint8_t *message_type = find_dhcp_option(&received, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN, NULL); if(!message_type) { printf("DHCP Server Error: No message type field found"); continue; } printf("State dump. Message type %d\n", *message_type); for(int i = 0; i < state->max_leases; i++) { dhcp_lease_t *lease = &state->leases[i]; printf("lease slot %d expiry %d hwaddr %02x:%02x:%02x:%02x:%02x:%02x\r\n", i, lease->expires, lease->hwaddr[0], lease->hwaddr[1], lease->hwaddr[2], lease->hwaddr[3], lease->hwaddr[4], lease->hwaddr[5]); } switch(*message_type) { case DHCP_DISCOVER: handle_dhcp_discover(&received); break; case DHCP_REQUEST: handle_dhcp_request(&received); break; case DHCP_RELEASE: handle_dhcp_release(&received); default: printf("DHCP Server Error: Unsupported message type %d\r\n", *message_type); break; } } }