static void rawd_new(struct netconn *nc) { int i; int free = -1; chMtxLock(&rawd_mutex); /* check for existing connections */ for (i = 0; i < NC_COUNT; i++) { if (ncs[i] == NULL) { free = i; } else if (ncs[i]->pcb.tcp->state != ESTABLISHED) { /* kill stale connections */ netconn_close(ncs[i]); netconn_delete(ncs[i]); ncs[i] = NULL; free = i; } } if (free < 0) { netconn_write(nc, RAWD_FULL, sizeof(RAWD_FULL) - 1, NETCONN_COPY); netconn_close(nc); netconn_delete(nc); chMtxUnlock(); return; } ncs[free] = nc; chMtxUnlock(); netconn_write(nc, RAWD_READY, sizeof(RAWD_READY) - 1, NETCONN_COPY); }
/* 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); } } }
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; }
void vBasicWEBServer( void *pvParameters ) { struct netconn *pxNewConnection; //struct ip_addr xIpAddr, xNetMast, xGateway; extern err_t ethernetif_init( struct netif *netif ); int ret = ERR_OK; /* Parameters are not used - suppress compiler error. */ ( void )pvParameters; /* Create a new tcp connection handle */ pxHTTPListener = netconn_new( NETCONN_TCP ); ip_set_option(pxHTTPListener->pcb.ip, SOF_REUSEADDR); netconn_bind( pxHTTPListener, NULL, webHTTP_PORT ); netconn_listen( pxHTTPListener ); #if CONFIG_READ_FLASH /* Load wifi_config */ LoadWifiConfig(); RestartSoftAP(); #endif //printf("\r\n-0\n"); /* Loop forever */ for( ;; ) { if(webs_terminate) break; //printf("\r\n%d:-1\n", xTaskGetTickCount()); /* Wait for connection. */ // Evan mopdified for adapt two version lwip api diff port_netconn_accept( pxHTTPListener , pxNewConnection, ret); //printf("\r\n%d:-2\n", xTaskGetTickCount()); if( pxNewConnection != NULL && ret == ERR_OK) { /* Service connection. */ vProcessConnection( pxNewConnection ); while( netconn_delete( pxNewConnection ) != ERR_OK ) { vTaskDelay( webSHORT_DELAY ); } } //printf("\r\n%d:-3\n", xTaskGetTickCount()); } //printf("\r\n-4\n"); if(pxHTTPListener) { netconn_close(pxHTTPListener); netconn_delete(pxHTTPListener); pxHTTPListener = NULL; } //printf("\r\nExit Web Server Thread!\n"); xSemaphoreGive(webs_sema); }
/** * \brief HTTP task core function. * * \param pvParameters Junk parameter. */ static void http_task(void *pvParameters) { struct netconn *conn, *newconn; err_t err; /* Just to avoid compiler warnings. */ UNUSED(pvParameters); /* Wait for user to read instructions. */ WAIT_FOR_TOUCH_EVENT; #if LWIP_STATS configure_timer_for_bandwidth_stats(); #endif /* Create a new TCP connection handle */ conn = netconn_new(NETCONN_TCP); if (conn == NULL) { printf("http_task: invalid conn\n"); /* Delete the calling task. */ vTaskDelete(NULL); } /* Bind to port 80 (HTTP) with default IP address */ netconn_bind(conn, NULL, 80); /* Put the connection into LISTEN state */ netconn_listen(conn); do { err = netconn_accept(conn, &newconn); if (err == ERR_OK) { /* Try to instanciate a new HTTP-req task to handle the HTTP request. */ if (NULL == sys_thread_new("HTTP-req", http_request, newconn, mainHTTP_TASK_STACK_SIZE, mainHTTP_TASK_PRIORITY)) { /* Failed to instanciate task, free netconn socket. */ netconn_close(newconn); netconn_delete(newconn); } } } while (err == ERR_OK); printf("http_task: netconn_accept received error %d, shutting down\n", err); /* Free Netconn resource. */ netconn_close(conn); netconn_delete(conn); /* Delete the calling task. */ vTaskDelete(NULL); }
T_uezError Network_lwIP_SocketDelete( void *aWorkspace, T_uezNetworkSocket aSocket) { T_Network_lwIP_Workspace *p = (T_Network_lwIP_Workspace *)aWorkspace; T_uezError error = UEZ_ERROR_NONE; T_lwIPSocket *p_socket = p->iSockets + aSocket; // 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_MUST_CLOSE_FIRST; } else { netconn_delete(p_socket->iNetconn); ISocketFree(p, aSocket); } UEZSemaphoreRelease(p->iSem); return error; }
/* The main() function */ int main() { struct netconn *conn, *newconn; /* create a new TCP connection handle. */ conn = netconn_new(NETCONN_TCP); /* bind the conneciton to port 80 on any local IP address */ netconn_bind(conn, NULL, 80); /* Put the connection into LISTEN state */ netconn_listen(conn); /* Loop forever*/ while(1){ /* Accept a new connection */ newconn = netconn_accpet(conn); /* Process the incoming connection */ processes_connection(newconn); /* Deallocate connection handle.*/ netconn_delete(newconn); } return 0; }
static void uctsk_LWIP(void *pdata) { struct netconn *__pstConn, *__pstNewConn; //add by rezaee OSTimeDlyHMSM(0, 0, 0, 50); /* 500 MS */ Init_lwIP(); __pstConn = netconn_new(NETCONN_TCP); netconn_bind(__pstConn, NULL,80); netconn_listen(__pstConn); for(;;) { __pstNewConn = netconn_accept(__pstConn); if(__pstNewConn != NULL) { vHandler_HTTP(__pstNewConn); while(netconn_delete(__pstNewConn) != ERR_OK) { OSTimeDlyHMSM(0, 0, 0, 10); } } } }
/*-----------------------------------------------------------------------------------*/ 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 7. */ netconn_bind(conn, NULL, 7); /* Tell connection to go into listening mode. */ netconn_listen(conn); memset(data, 'F', DATA_SIZE); while (1) { /* Grab new connection. */ err = netconn_accept(conn, &newconn); /*printf("accepted new connection %p\n", newconn);*/ /* Process the new connection. */ if (err == ERR_OK) { err = netconn_write(newconn, data, DATA_SIZE, NETCONN_COPY); if (err != ERR_OK) { printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(err)); } netconn_close(newconn); netconn_delete(newconn); } } }
void message_transmit(uint8_t *input_buffer, size_t input_buffer_size, ip_addr_t *addr, uint16_t port) { struct netconn *conn; struct netbuf *buf; conn = netconn_new(NETCONN_UDP); if (conn == NULL) { // TODO: Do something useful return; } buf = netbuf_new(); if (buf == NULL) { // TODO: Do something useful return; } netbuf_ref(buf, input_buffer, input_buffer_size); netconn_sendto(conn, buf, addr, port); netbuf_delete(buf); netconn_delete(conn); }
static void closeSrqIo(user_data_t * user_data) { // control connection closed netconn_close(user_data->control_io); netconn_delete(user_data->control_io); user_data->control_io = NULL; iprintf("***Control Connection closed\r\n"); }
/** * @brief http server thread * @param arg: pointer on argument(not used here) * @retval None */ static void http_server_netconn_thread(void *arg) { struct netconn *conn, *newconn; err_t err, accept_err; /* Create a new TCP connection handle */ conn = netconn_new(NETCONN_TCP); if (conn!= NULL) { /* Bind to port 80 (HTTP) with default IP address */ err = netconn_bind(conn, NULL, 80); if (err == ERR_OK) { /* Put the connection into LISTEN state */ netconn_listen(conn); while(1) { /* accept any icoming connection */ accept_err = netconn_accept(conn, &newconn); if(accept_err == ERR_OK) { /* serve connection */ http_server_serve(newconn); /* delete connection */ netconn_delete(newconn); } } } } }
int lwip_close(int s) { struct lwip_socket *sock; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_close(%d)\n", s)); /* We cannot allow multiple closes of the same socket. */ sys_sem_wait(socksem); sock = get_socket(s); if (!sock) { sys_sem_signal(socksem); set_errno(EBADF); return -1; } netconn_delete(sock->conn); if (sock->lastdata) { netbuf_delete(sock->lastdata); } sock->lastdata = NULL; sock->lastoffset = 0; sock->conn = NULL; lwip_sockmap[sock->fdfake]=0; sys_sem_signal(socksem); sock_set_errno(sock, 0); return 0; }
/*! \brief process an incoming connection * parse the request and close/delete the connection * * \note Input. The netconn to use to send and receive data. * */ portTASK_FUNCTION( prvweb_ProcessSingleConnection, pvParameters ) { struct netconn * pxNewConnection; portSHORT i; portLONG err_count; /* while( xSemaphoreTake(xMutexNbHTTPConn , portMAX_DELAY ) != pdTRUE ); sCurrentNbHTTPConn++; // Release the mutex. xSemaphoreGive( xMutexNbHTTPConn ); */ pxNewConnection = ( struct netconn *)pvParameters; prvweb_ParseHTMLRequest( pxNewConnection ); // Since we cannot know when the client has closed the connection(lwIP bug), // we set a 1000ms delay (the time for the file transfer to end), then we close // the connection ourselves. // NOTE FOR IMPROVMENTS: we could set this delay depending on the amount of // data that was sent to the client(the more the longer). vTaskDelay( 50 ); err_count = 4; while( netconn_close( pxNewConnection ) != ERR_OK ) { if (--err_count == 0) break; vTaskDelay( webSHORT_DELAY ); } //vTaskDelay( 100 ); err_count = 4; while( netconn_delete( pxNewConnection ) != ERR_OK ) { if (--err_count == 0) break; vTaskDelay( webSHORT_DELAY ); } // netconn_close( pxNewConnection ); // netconn_delete( pxNewConnection ); while( xSemaphoreTake(xMutexNbHTTPConn , portMAX_DELAY ) != pdTRUE ); for (i = 0 ; i < webHTTP_NB_CONN ; i++) { if (tTaskHandle[i] == xTaskGetCurrentTaskHandle()) { tTaskHandle[i] = NULL; break; } } sCurrentNbHTTPConn--; // Release the xWEBMutex if there are no other active connections. if( 0 == sCurrentNbHTTPConn ) { x_supervisor_SemaphoreGive( xWEBMutex ); } // Release the mutex. xSemaphoreGive( xMutexNbHTTPConn ); vTaskDelete(NULL); /* nothing after delete task : this will not be reached */ }
void stop_web_server() { //printf("\r\nWEB:Enter stop web server!\n"); webs_terminate = 1; if(pxHTTPListener) netconn_abort(pxHTTPListener); if(webs_sema) { if(xSemaphoreTake(webs_sema, 15 * configTICK_RATE_HZ) != pdTRUE) { if(pxHTTPListener) { netconn_close(pxHTTPListener); netconn_delete(pxHTTPListener); pxHTTPListener = NULL; } printf("\r\nWEB: Take webs sema(%p) failed!!!!!!!!!!!\n", webs_sema); } vSemaphoreDelete(webs_sema); webs_sema = NULL; } if(webs_task) { vTaskDelete(webs_task); webs_task = NULL; } printf("\r\nWEB:Exit stop web server!\n"); }
void http_server(void *pdata) { struct netconn *HTTPNetConn = NULL, *xHTTPNetConn; err_t rc1; if (xSemaphoreTake(xDhcpCmplSemaphore_2, portMAX_DELAY) == pdTRUE) { /* Create a new TCP connection handle */ HTTPNetConn = netconn_new(NETCONN_TCP); // netconn_set_recvtimeout(HTTPNetConn, 100); if (HTTPNetConn == NULL) { /*No memory for new connection? */ UART_PrintStr("No mem for new HTTP con\r\n"); } /* Bind to port 80 (HTTP) with default IP address */ netconn_bind(HTTPNetConn, NULL, 80); /* Put the connection into LISTEN state */ netconn_listen(HTTPNetConn); while (1) { rc1 = netconn_accept(HTTPNetConn, &xHTTPNetConn); if(rc1 == ERR_OK){ // http_server_serve(xHTTPNetConn); UART_PrintStr("netconn accept\r\n"); netconn_close(xHTTPNetConn); netconn_delete(xHTTPNetConn); }else{ PrintERR(rc1); } } } while (1) vTaskDelay(1000/portTICK_RATE_MS); }
/*-----------------------------------------------------------------------------------*/ 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); } } }
/* int socket(int domain, int type, int protocol);*/ int socket(int domain, int type, int protocol) { struct sock *conn; int i; /* create a sock */ switch (type) { case SOCK_DGRAM: conn = netconn_new( NETCONN_UDP); break; case SOCK_STREAM: conn = netconn_new(NETCONN_TCP); break; default: return -1; } if (!conn) { return -1; } i = alloc_socket(conn); if (i == -1) { netconn_delete(conn); return -1; } conn->socket = i; return i; }
/** * Delete a netconn and all its resources. * The pcb is NOT freed (since we might not be in the right thread context do this). * * @param conn the netconn to free */ void netconn_free(struct netconn *conn) { void *mem; LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL); /* Drain the recvmbox. */ if (conn->recvmbox != SYS_MBOX_NULL) { while (sys_mbox_tryfetch(conn->recvmbox, &mem) != SYS_MBOX_EMPTY) { if (conn->type == NETCONN_TCP) { if(mem != NULL) { pbuf_free((struct pbuf *)mem); } } else { netbuf_delete((struct netbuf *)mem); } } sys_mbox_free(conn->recvmbox); conn->recvmbox = SYS_MBOX_NULL; } /* Drain the acceptmbox. */ if (conn->acceptmbox != SYS_MBOX_NULL) { while (sys_mbox_tryfetch(conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) { netconn_delete((struct netconn *)mem); } sys_mbox_free(conn->acceptmbox); conn->acceptmbox = SYS_MBOX_NULL; } sys_sem_free(conn->op_completed); conn->op_completed = SYS_SEM_NULL; memp_free(MEMP_NETCONN, conn); }
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); } } }
static msg_t httpd_loop(void *data) { struct netconn *nc, *ncnew; int ret; chRegSetThreadName("net/httpd"); nc = netconn_new(NETCONN_TCP); if (!nc) return 1; netconn_bind(nc, NULL, HTTPD_PORT); netconn_listen(nc); for (;;) { ret = netconn_accept(nc, &ncnew); if (ret) continue; httpd_process(ncnew); netconn_delete(ncnew); } return 0; }
static int lwip_socket_close(nsapi_stack_t *stack, nsapi_socket_t handle) { struct lwip_socket *s = (struct lwip_socket *)handle; err_t err = netconn_delete(s->conn); lwip_arena_dealloc(s); return lwip_err_remap(err); }
void sock_ip_close(sock_ip_t *sock) { assert(sock != NULL); if (sock->conn != NULL) { netconn_delete(sock->conn); sock->conn = NULL; } }
static nsapi_error_t mbed_lwip_socket_close(nsapi_stack_t *stack, nsapi_socket_t handle) { struct lwip_socket *s = (struct lwip_socket *)handle; netbuf_delete(s->buf); err_t err = netconn_delete(s->conn); mbed_lwip_arena_dealloc(s); return mbed_lwip_err_remap(err); }
nsapi_error_t LWIP::socket_close(nsapi_socket_t handle) { struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; netbuf_delete(s->buf); err_t err = netconn_delete(s->conn); arena_dealloc(s); return err_remap(err); }
void netstack_socket_free(struct netstack_socket *sk) { if (!sk || !sk->priv) { return; } netconn_delete(sk->priv); vmm_free(sk); }
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); }
/*! \brief parse the incoming request * parse the HTML request and send file * * \param pxNetCon Input. The netconn to use to send and receive data. * */ static void prvweb_ParseHTMLRequest( struct netconn *pxNetCon ) { struct netbuf *pxRxBuffer; portCHAR *pcRxString; unsigned portSHORT usLength; static unsigned portLONG ulPageHits = 0; #if ( (LWIP_VERSION) == ((1U << 24) | (3U << 16) | (2U << 8) | (LWIP_VERSION_RC)) ) /* We expect to immediately get data. */ pxRxBuffer = netconn_recv( pxNetCon ); #else while(netconn_recv( pxNetCon, &pxRxBuffer) != ERR_OK) { vTaskDelay( webSHORT_DELAY ); } #endif if( pxRxBuffer != NULL ) { /* Where is the data? */ netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength ); /* Is this a GET? We don't handle anything else. */ if(( NULL != pcRxString ) && ( !strncmp( pcRxString, "GET", 3 ) )) { /* Update the hit count. */ ulPageHits++; sprintf( cPageHits, "%d", (int)ulPageHits ); /* Write out the HTTP OK header. */ netconn_write( pxNetCon, webHTTP_OK, (u16_t) strlen( webHTTP_OK ), NETCONN_COPY ); /* Generate the dynamic page... First the page header. */ strcpy( cDynamicPage, webHTML_START ); /* ... Then the hit count... */ strcat( cDynamicPage, cPageHits ); strcat( cDynamicPage, "<p><pre>Task State Priority Stack #<br>************************************************<br>" ); /* ... Then the list of tasks and their status... */ vTaskList( ( signed portCHAR * ) cDynamicPage + strlen( cDynamicPage ) ); /* ... Finally the page footer. */ strcat( cDynamicPage, webHTML_END ); /* Write out the dynamically generated page. */ netconn_write( pxNetCon, cDynamicPage, (u16_t) strlen( cDynamicPage ), NETCONN_COPY ); } netbuf_delete( pxRxBuffer ); } netconn_close( pxNetCon ); netconn_delete( pxNetCon ); }
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"); } }
/*-----------------------------------------------------------------------------------*/ int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) { struct lwip_socket *sock; struct netconn *newconn; struct ip_addr naddr; u16_t port; int newsock; struct sockaddr_in sin; DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s)); sock = get_socket(s); if (!sock) { return -1; } newconn = netconn_accept(sock->conn); /* get the IP address and port of the remote host */ netconn_peer(newconn, &naddr, &port); memset(&sin, 0, sizeof(sin)); sin.sin_len = sizeof(sin); sin.sin_family = AF_INET; sin.sin_port = htons(port); sin.sin_addr.s_addr = naddr.addr; if (*addrlen > sizeof(sin)) *addrlen = sizeof(sin); memcpy(addr, &sin, *addrlen); newsock = alloc_socket(newconn); if (newsock == -1) { netconn_delete(newconn); sock_set_errno(sock, ENOBUFS); return -1; } newconn->callback = event_callback; sock = get_socket(newsock); sys_sem_wait(socksem); sock->rcvevent += -1 - newconn->socket; newconn->socket = newsock; sys_sem_signal(socksem); #if SOCKETS_DEBUG DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock)); ip_addr_debug_print(&naddr); DEBUGF(SOCKETS_DEBUG, (" port=%u\n", port)); #endif sock_set_errno(sock, 0); return newsock; }