ssize_t zts_recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen) { if (!buf) { return ZTS_ERR_INVALID_ARG; } return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_recvfrom(fd, buf, len, flags, addr, addrlen); }
// ret 1 = error int32_t MTD_FLASHMEM Socket::read(void *buffer, uint32_t maxLength, IPAddress *sourceAddress, uint16_t *sourcePort) { sockaddr_in from; int fromlen; int32_t bytesRecv = lwip_recvfrom(m_socket, buffer, maxLength, 0, (sockaddr *)&from, (socklen_t *)&fromlen); if (bytesRecv > 0) { *sourceAddress = IPAddress(from.sin_addr.s_addr); *sourcePort = ntohs(from.sin_port); } return bytesRecv; }
static void ping_recv(int s, struct ip_addr *addr) { char buf[200]; int fromlen,len; struct sockaddr_in from; len = lwip_recvfrom(s, buf,sizeof(buf),0,(struct sockaddr*)&from,&fromlen); printf("Received %d bytes from %lx\n",len,ntohl(from.sin_addr.s_addr)); }
static void SC_RecvFrom(__SYSCALL_PARAM_BLOCK* pspb) { pspb->lpRetValue = (LPVOID)lwip_recvfrom( (INT)PARAM(0), (void*)PARAM(1), (size_t)PARAM(2), (INT)PARAM(3), (struct sockaddr*)PARAM(4), (socklen_t *)PARAM(5) ); }
static void ping_recv(int s) { char* buf = NULL; int fromlen, len; struct sockaddr_in from; struct ip_hdr *iphdr; struct icmp_echo_hdr *iecho; int ms; BOOL bResult = FALSE; //Allocate a buffer to contain the received data. buf = (char*)KMemAlloc(1500,KMEM_SIZE_TYPE_ANY); if(NULL == buf) { return; } while((len = lwip_recvfrom(s, buf, 1500, 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) { if(len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) { ip_addr_t fromaddr; inet_addr_to_ipaddr(&fromaddr, &from.sin_addr); //Get times between sent and receive. ms = sys_now() - ping_time; ms *= SYSTEM_TIME_SLICE; iphdr = (struct ip_hdr *)buf; iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4)); if (((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)) && iecho->type == ICMP_ER)) { len = len - sizeof(struct ip_hdr) - sizeof(struct icmp_echo_hdr); //Adjust received data's length,since it //includes IP and ICMP headers. _hx_printf(" [%d] Reply from %s,size = %d,time = %d(ms)\r\n",ping_pkt_seq,inet_ntoa(fromaddr),len,ms); ping_succ ++; bResult = TRUE; } else { //printf(" ping : Received invalid replay,drop it.\r\n"); } } } if (!bResult) { _hx_printf(" [%d] Request time out.\r\n",ping_pkt_seq); } if(buf) //Release it. { KMemFree(buf,KMEM_SIZE_TYPE_ANY,0); } }
/** Read data. Be sure to bind to a port before trying to read. If you want to know which address the message came from, see udpReadFrom(). @param socket The UDP socket, as obtained from udpOpen() @param data Where to store the incoming data. @param length How many bytes of data to read. @param from_address (optional) The address the data came from. @param from_port (optional) The port the data arrived on. @return The number of bytes read. @see udpBind() \b Example \code char mydata[128]; int sock = udpOpen(); // create a new socket if (udpBind(sock, 10000) == true) { // listen on port 10000 int read = udpRead(sock, mydata, sizeof(mydata), 0, 0); } \endcode */ int udpRead(int socket, char* data, int length, int* from_address, int* from_port) { struct sockaddr_in from; socklen_t fromlen = sizeof(from); int recvd = lwip_recvfrom(socket, data, length, 0, (struct sockaddr*)&from, &fromlen); if (from_address) *from_address = from.sin_addr.s_addr; if (from_port) *from_port = from.sin_port; return recvd; }
static void ping_recv(int s, ip_addr_t *addr) { char buf[200]; socklen_t fromlen; int len; struct sockaddr_in from; LWIP_UNUSED_ARG(addr); len = lwip_recvfrom(s, buf,sizeof(buf),0,(struct sockaddr*)&from,&fromlen); printf("Received %d bytes from %x\n",len,ntohl(from.sin_addr.s_addr)); }
static void ping_recv(int s) { char buf[64]; int fromlen, len; struct sockaddr_in from; struct ip_hdr *iphdr; struct icmp_echo_hdr *iecho; while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) { if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) { ip_addr_t fromaddr; inet_addr_to_ipaddr(&fromaddr, &from.sin_addr); //LWIP_DEBUGF( PING_DEBUG, ("ping: recv ")); //ip_addr_debug_print(PING_DEBUG, &fromaddr); //LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time))); rt_kprintf("ping: recv "); ip_addr_debug_print1(&fromaddr); rt_kprintf(" - %d ms\n", (sys_now() - ping_time)); iphdr = (struct ip_hdr *)buf; iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4)); if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) { /* do some ping result processing */ PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER)); //rt_kprintf("ping: drop1\n"); return; } else { //LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n")); rt_kprintf("ping: drop\n"); } }else{ //rt_kprintf("ping: drop3\n"); } } //rt_kprintf("ping: len=%d\n",len); if (len <= 0) { //LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", (sys_now()-ping_time))); rt_kprintf("ping: recv - %d ms - timeout\n", (sys_now()-ping_time)); }else{ } /* do some ping result processing */ PING_RESULT(0); }
int LWIP_SOCKETS_Driver::RecvFrom( SOCK_SOCKET socket, char* buf, int len, int flags, SOCK_sockaddr* from, int* fromlen ) { NATIVE_PROFILE_PAL_NETWORK(); sockaddr_in addr; sockaddr *pFrom = NULL; int ret; if(from) { SOCK_SOCKADDR_TO_SOCKADDR(from, addr, fromlen); pFrom = (sockaddr*)&addr; } ret = lwip_recvfrom(socket, buf, len, flags, pFrom, (u32_t*)fromlen); if(from && ret != SOCK_SOCKET_ERROR) { SOCKADDR_TO_SOCK_SOCKADDR(from, addr, fromlen); } return ret; }
int netstack_send_echo(u8 *ripaddr, u16 size, u16 seqno, struct netstack_echo_reply *reply) { u64 ts; int s, i, err; char buf[64]; size_t fromlen, off, len = sizeof(struct icmp_echo_hdr) + size; ip_addr_t to_addr, from_addr; struct sockaddr_in sock; struct ip_hdr *iphdr; struct icmp_echo_hdr *iecho; LWIP_ASSERT("ping_size is too big\n", len <= 0xffff); /* Prepare target address */ IP4_ADDR(&to_addr, ripaddr[0],ripaddr[1],ripaddr[2],ripaddr[3]); /* Open RAW socket */ if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) { vmm_printf("%s: failed to open ICMP socket\n", __func__); return VMM_EFAIL; } /* Set socket option */ i = PING_RCV_TIMEO; lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &i, sizeof(i)); /* Prepare socket address */ sock.sin_len = sizeof(sock); sock.sin_family = AF_INET; inet_addr_from_ipaddr(&sock.sin_addr, &to_addr); /* Prepare ECHO request */ iecho = (struct icmp_echo_hdr *)vmm_zalloc(len); if (!iecho) { return VMM_ENOMEM; } ICMPH_TYPE_SET(iecho, ICMP_ECHO); ICMPH_CODE_SET(iecho, 0); iecho->chksum = 0; iecho->id = PING_ID; iecho->seqno = htons(seqno); for (i = 0; i < size; i++) { ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; } iecho->chksum = inet_chksum(iecho, len); /* Send ECHO request */ err = lwip_sendto(s, iecho, len, 0, (struct sockaddr*)&sock, sizeof(sock)); vmm_free(iecho); if (!err) { return VMM_EFAIL; } /* Get reference timestamp */ ts = vmm_timer_timestamp(); /* Wait for ECHO reply */ err = VMM_EFAIL; off = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&sock, (socklen_t*)&fromlen); if (off >= (sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr))) { inet_addr_to_ipaddr(&from_addr, &sock.sin_addr); iphdr = (struct ip_hdr *)buf; iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4)); if ((iecho->id == PING_ID) && (iecho->seqno == htons(seqno))) { reply->ripaddr[0] = ip4_addr1(&from_addr); reply->ripaddr[1] = ip4_addr2(&from_addr); reply->ripaddr[2] = ip4_addr3(&from_addr); reply->ripaddr[3] = ip4_addr4(&from_addr); reply->ttl = IPH_TTL(iphdr); reply->len = len; reply->seqno = seqno; reply->rtt = udiv64(vmm_timer_timestamp() - ts, 1000); err = VMM_OK; } } while (off < len) { off = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&sock, (socklen_t*)&fromlen); } /* Close RAW socket */ lwip_close(s); return err; }
int lwip_recv(int s, void *mem, int len, unsigned int flags) { return lwip_recvfrom(s, mem, len, flags, NULL, NULL); }
int lwip_read(int s, void *mem, int len) { return lwip_recvfrom(s, mem, len, 0, NULL, NULL); }
/** Read data. Note - this is free to return the number of bytes available, which is not necessarily as much as you asked for. Use tcpAvailable() to see how many are ready to be read, or tcpSetReadTimeout() to change the amount of time to wait for data to become available. @param socket The socket to read on, as returned by tcpOpen(). @param data Where to store the incoming data. @param length How many bytes of data to read. @return The number of bytes successfully read. \b Example \code char mydata[512]; int sock = tcpOpen(IP_ADDRESS(192, 168, 0, 210), 10101); if (sock > -1) { int available = tcpAvailable(sock); if(available > 512) // make sure we don't read more than we have room for available = 512; int read = tcpRead(mydata, sizeof(mydata)); // handle our new data here tcpClose(sock); // and finally close down } \endcode */ int tcpRead(int socket, char* data, int length) { return lwip_recvfrom(socket, data, length, 0, NULL, NULL); }
void udp_setup_thread_entry(void *p) { int sock; int bytes_read; struct sockaddr_in server_addr , client_addr; int optval = 1; fd_set readset; /* create socket */ if ((sock = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { rt_kprintf("Can not create udp setup socket.\n"); return; } lwip_setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&optval ,sizeof(optval)); /* init server socket address */ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SETUP_UDP_PORT); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero)); if (lwip_bind(sock,(struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { rt_kprintf("Bind error\n"); return; } rt_kprintf("UDPServer Waiting for client on port %d \n",SETUP_UDP_PORT); while (1) { int bytes_ret; rt_uint32_t addr_len = sizeof(struct sockaddr_in); FD_ZERO(&readset); FD_SET(sock, &readset); if( lwip_select(sock+1, &readset, 0, 0, 0) == 0 ) continue; rt_mutex_take(&setup_data_buf_mutex,RT_WAITING_FOREVER); bytes_read = lwip_recvfrom(sock, setup_data_buf, DATA_BUF_SIZE, MSG_DONTWAIT, (struct sockaddr *)&client_addr, &addr_len); rt_mutex_release(&setup_data_buf_mutex); if( bytes_read < 0 ) { continue; } rt_mutex_take(&setup_data_buf_mutex,RT_WAITING_FOREVER); setup_data_buf[bytes_read] = 0; bytes_ret = processCMD(setup_data_buf,bytes_read); rt_mutex_release(&setup_data_buf_mutex); if( bytes_ret > 0 ) { // command execute success, send reply in buffer. client_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); rt_mutex_take(&setup_data_buf_mutex,RT_WAITING_FOREVER); lwip_sendto(sock,setup_data_buf,bytes_ret,0,(struct sockaddr*)&client_addr, sizeof(struct sockaddr)); rt_mutex_release(&setup_data_buf_mutex); } } // Should not reach here!. }
/** * Send an SNTP request via sockets. * This is a very minimal implementation that does not fully conform * to the SNTPv4 RFC, especially regarding server load and error procesing. */ void sntp_request(void *arg) { int sock; struct sockaddr_in local; struct sockaddr_in to; int tolen; int size; int timeout; struct sntp_msg sntpmsg; ip_addr_t sntp_server_address; LWIP_UNUSED_ARG(arg); /* if we got a valid SNTP server address... */ if (ipaddr_aton(SNTP_SERVER_ADDRESS, &sntp_server_address)) { /* create new socket */ sock = lwip_socket(AF_INET, SOCK_DGRAM, 0); if (sock >= 0) { /* prepare local address */ memset(&local, 0, sizeof(local)); local.sin_family = AF_INET; local.sin_port = PP_HTONS(INADDR_ANY); local.sin_addr.s_addr = PP_HTONL(INADDR_ANY); /* bind to local address */ if (lwip_bind(sock, (struct sockaddr *)&local, sizeof(local)) == 0) { /* set recv timeout */ timeout = SNTP_RECV_TIMEOUT; lwip_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)); /* prepare SNTP request */ sntp_initialize_request(&sntpmsg); /* prepare SNTP server address */ memset(&to, 0, sizeof(to)); to.sin_family = AF_INET; to.sin_port = PP_HTONS(SNTP_PORT); inet_addr_from_ipaddr(&to.sin_addr, &sntp_server_address); /* send SNTP request to server */ if (lwip_sendto(sock, &sntpmsg, SNTP_MSG_LEN, 0, (struct sockaddr *)&to, sizeof(to)) >= 0) { /* receive SNTP server response */ tolen = sizeof(to); size = lwip_recvfrom(sock, &sntpmsg, SNTP_MSG_LEN, 0, (struct sockaddr *)&to, (socklen_t *)&tolen); /* if the response size is good */ if (size == SNTP_MSG_LEN) { /* if this is a SNTP response... */ if (((sntpmsg.li_vn_mode & SNTP_MODE_MASK) == SNTP_MODE_SERVER) || ((sntpmsg.li_vn_mode & SNTP_MODE_MASK) == SNTP_MODE_BROADCAST)) { /* do time processing */ sntp_process(sntpmsg.receive_timestamp); } else { LWIP_DEBUGF( SNTP_DEBUG_WARN, ("sntp_request: not response frame code\n")); } } } else { LWIP_DEBUGF( SNTP_DEBUG_WARN, ("sntp_request: not sendto==%i\n", errno)); } } /* close the socket */ closesocket(sock); } } }
static void socketUdpRead(uint16 sockNr) { BufReq_ReturnType result; switch (SocketAdminList[sockNr].SocketConnectionRef->AutosarConnectorType) { case SOAD_AUTOSAR_CONNECTOR_PDUR: if (SocketAdminList[sockNr].SocketRouteRef != NULL) { struct sockaddr_in fromAddr; socklen_t fromAddrLen = sizeof(fromAddr); int nBytes; PduInfoType pduInfo; if (SoAd_BufferGet(SOAD_RX_BUFFER_SIZE, &pduInfo.SduDataPtr)) { nBytes = lwip_recvfrom(SocketAdminList[sockNr].SocketHandle, pduInfo.SduDataPtr, SOAD_RX_BUFFER_SIZE, MSG_PEEK, (struct sockaddr*)&fromAddr, &fromAddrLen); SoAd_SocketStatusCheck(sockNr, SocketAdminList[sockNr].SocketHandle); if (nBytes > 0){ if(nBytes >= SocketAdminList[sockNr].SocketRouteRef->DestinationSduLength) { if (!SocketAdminList[sockNr].SocketConnectionRef->PduProvideBufferEnable) { // IF-type pduInfo.SduLength = lwip_recvfrom(SocketAdminList[sockNr].SocketHandle, pduInfo.SduDataPtr, SocketAdminList[sockNr].SocketRouteRef->DestinationSduLength,0, (struct sockaddr*)&fromAddr, &fromAddrLen); SocketAdminList[sockNr].RemotePort = fromAddr.sin_port; SocketAdminList[sockNr].RemoteIpAddress = fromAddr.sin_addr.s_addr; /* TODO Find out how autosar connector and user really shall be used. This is just one interpretation * support for XCP, CDD will have to be added later when supported */ switch(SocketAdminList[sockNr].SocketRouteRef->UserRxIndicationUL){ #if defined(USE_UDPNM) case SOAD_UL_UDPNM: (void)UdpNm_SoAdIfRxIndication(SocketAdminList[sockNr].SocketRouteRef->DestinationPduId, &pduInfo); break; #endif default: (void)PduR_SoAdIfRxIndication(SocketAdminList[sockNr].SocketRouteRef->DestinationPduId, &pduInfo); break; } } else { // TP-type PduLengthType len; result = PduR_SoAdTpStartOfReception(SocketAdminList[sockNr].SocketRouteRef->DestinationPduId, SocketAdminList[sockNr].SocketRouteRef->DestinationSduLength, &len); if (result == BUFREQ_OK && len > 0) { pduInfo.SduLength = SocketAdminList[sockNr].SocketRouteRef->DestinationSduLength; nBytes = lwip_recvfrom(SocketAdminList[sockNr].SocketHandle, pduInfo.SduDataPtr, SocketAdminList[sockNr].SocketRouteRef->DestinationSduLength,0, (struct sockaddr*)&fromAddr, &fromAddrLen); SocketAdminList[sockNr].RemotePort = fromAddr.sin_port; SocketAdminList[sockNr].RemoteIpAddress = fromAddr.sin_addr.s_addr; /* Let pdur copy received data */ if(len < SocketAdminList[sockNr].SocketRouteRef->DestinationSduLength) { PduInfoType pduInfoChunk; PduLengthType lenToSend = SocketAdminList[sockNr].SocketRouteRef->DestinationSduLength; /* We need to copy in smaller parts */ pduInfoChunk.SduDataPtr = pduInfo.SduDataPtr; while(lenToSend > 0) { if(lenToSend >= len){ PduR_SoAdTpCopyRxData(SocketAdminList[sockNr].SocketRouteRef->DestinationPduId, &pduInfoChunk, &len); lenToSend -= len; }else{ PduR_SoAdTpCopyRxData(SocketAdminList[sockNr].SocketRouteRef->DestinationPduId, &pduInfoChunk, &lenToSend); lenToSend = 0; } } }else{ PduR_SoAdTpCopyRxData(SocketAdminList[sockNr].SocketRouteRef->DestinationPduId, &pduInfo, &len); } /* Finished reception */ (void)PduR_SoAdTpRxIndication(SocketAdminList[sockNr].SocketRouteRef->DestinationPduId, NTFRSLT_OK); } else if (result != BUFREQ_BUSY) { DET_REPORTERROR(MODULE_ID_SOAD, 0, SOAD_SOCKET_UDP_READ_ID, SOAD_E_UL_RXBUFF); (void)PduR_SoAdTpRxIndication(SocketAdminList[sockNr].SocketRouteRef->DestinationPduId, NTFRSLT_E_NO_BUFFER); } } } } SoAd_BufferFree(pduInfo.SduDataPtr); } else { // No buffer available: continue poll() Report error SOAD_E_UPPERBUFF to DEM // DET_REPORTERROR(MODULE_ID_SOAD, 0, SOAD_SOCKET_UDP_READ_ID, SOAD_E_UPPERBUFF); (void)PduR_SoAdTpRxIndication(SocketAdminList[sockNr].SocketRouteRef->DestinationPduId, NTFRSLT_E_NO_BUFFER); } } else { DET_REPORTERROR(MODULE_ID_SOAD, 0, SOAD_SOCKET_UDP_READ_ID, SOAD_E_CONFIG_INVALID); } break; // SOAD_AUTOSAR_CONNECTOR_PDUR case SOAD_AUTOSAR_CONNECTOR_DOIP: DoIp_HandleUdpRx(sockNr); break; // SOAD_AUTOSAR_CONNECTOR_DOIP default: DET_REPORTERROR(MODULE_ID_SOAD, 0, SOAD_SOCKET_UDP_READ_ID, SOAD_E_NOCONNECTOR); break; } }
int recvfrom(int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) { BT_HANDLE hSocket = (BT_HANDLE)s; return lwip_recvfrom(hSocket->socket, mem, len, flags, from, fromlen); }
static void serve_thread(uint32_t a) { struct st_args *args = (struct st_args *)a; union Nsipc *req = args->req; int r; switch (args->reqno) { case NSREQ_ACCEPT: { struct Nsret_accept ret; r = lwip_accept(req->accept.req_s, &ret.ret_addr, &ret.ret_addrlen); memmove(req, &ret, sizeof ret); break; } case NSREQ_BIND: r = lwip_bind(req->bind.req_s, &req->bind.req_name, req->bind.req_namelen); break; case NSREQ_SHUTDOWN: r = lwip_shutdown(req->shutdown.req_s, req->shutdown.req_how); break; case NSREQ_CLOSE: r = lwip_close(req->close.req_s); break; case NSREQ_CONNECT: r = lwip_connect(req->connect.req_s, &req->connect.req_name, req->connect.req_namelen); break; case NSREQ_LISTEN: r = lwip_listen(req->listen.req_s, req->listen.req_backlog); break; case NSREQ_RECV: // Note that we read the request fields before we // overwrite it with the response data. r = lwip_recv(req->recv.req_s, req->recvRet.ret_buf, req->recv.req_len, req->recv.req_flags); break; case NSREQ_SEND: r = lwip_send(req->send.req_s, &req->send.req_buf, req->send.req_size, req->send.req_flags); break; case NSREQ_SOCKET: r = lwip_socket(req->socket.req_domain, req->socket.req_type, req->socket.req_protocol); break; case NSREQ_INPUT: jif_input(&nif, (void *)&req->pkt); r = 0; break; case NSREQ_RECVFROM: r = lwip_recvfrom(req->recvfrom.req_s, req->recvfromRet.ret_buf, req->recvfrom.req_len, req->recvfrom.req_flags, &(req->recvfromRet.from), &(req->recvfromRet.fromlen)); break; default: cprintf("Invalid request code %d from %08x\n", args->whom, args->req); r = -E_INVAL; break; } if (r == -1) { char buf[100]; snprintf(buf, sizeof buf, "ns req type %d", args->reqno); perror(buf); } if (args->reqno != NSREQ_INPUT) ipc_send(args->whom, r, 0, 0); put_buffer(args->req); sys_page_unmap(0, (void*) args->req); free(args); }