int zts_read(int fd, void *buf, size_t len) { if (!buf) { return ZTS_ERR_INVALID_ARG; } return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_read(fd, buf, len); }
int socket_read(int sock, void* buf, size_t bufsz) { int result; result = lwip_read(sock, buf, bufsz); return result; }
void rx_application_thread() { int sock, new_sd; struct sockaddr_in address, remote; int size; if ((sock = lwip_socket(AF_INET, SOCK_STREAM, 0)) < 0) return; address.sin_family = AF_INET; address.sin_port = htons(rxperf_port); address.sin_addr.s_addr = INADDR_ANY; if (lwip_bind(sock, (struct sockaddr *)&address, sizeof (address)) < 0) { #ifdef OS_IS_FREERTOS vTaskDelete(NULL); #endif return; } lwip_listen(sock, 0); size = sizeof(remote); new_sd = lwip_accept(sock, (struct sockaddr *)&remote, (socklen_t*)&size); while (1) { #if (USE_JUMBO_FRAMES==1) char recv_buf[9700]; /* keep reading data */ if (lwip_read(new_sd, recv_buf, 8000) <= 0) break; #else char recv_buf[1500]; /* keep reading data */ if (lwip_read(new_sd, recv_buf, 1460) <= 0) break; #endif } print("Connection closed. RXPERF exiting.\r\n"); lwip_close(new_sd); #ifdef OS_IS_FREERTOS xil_printf("Rx IPERF Thread is being DELETED\r\n"); vTaskDelete(NULL); #endif }
static msg_t socket_stream_get(void *ip) { uint8_t b; if (lwip_read(((SocketStream*) ip)->fd, (uint8_t *) &b, sizeof(b)) < 0) return -1; return b; }
int zts_read_offset(int fd, void *buf, size_t offset, size_t len) { if (!buf) { return ZTS_ERR_INVALID_ARG; } char *cbuf = (char*)buf; return (!_run_service || !_run_lwip_tcpip) ? ZTS_ERR_SERVICE : lwip_read(fd, &(cbuf[offset]), len); }
BST_IP_ERR_T BST_IP_BsdReceive( BST_FD_T fd, BST_UINT8* pData, BST_UINT16 usLength ) { BST_IP_ERR_T lRtnVal; BST_IP_ERR_T lTmpVal; BST_UINT8 *pucTmpData; BST_UINT16 usDefLength; usDefLength = BST_IP_MTU_SIZE; if( !BST_IP_IsBsdFdValid(fd) ) { return BST_IP_ERR_MEM; } if( BST_NULL_PTR == pData || usLength <= 0 ) { return BST_IP_ERR_MEM; } lRtnVal = (BST_IP_ERR_T)lwip_read( fd.lFd, pData, usLength ); if ( lRtnVal < usLength ) { return lRtnVal; } /* 读空socket缓冲区里的数据 */ pucTmpData = (BST_UINT8 *)BST_OS_MALLOC( usDefLength ); if ( BST_NULL_PTR == pucTmpData ) { return lRtnVal; } BST_OS_MEMSET( pucTmpData, 0, usDefLength ); while( 1 ) { lTmpVal = (BST_IP_ERR_T)lwip_read( fd.lFd, pucTmpData, usDefLength ); if ( lTmpVal < usDefLength ) { break; } } BST_OS_FREE( pucTmpData ); return lRtnVal; }
/* ------------------------------------------------------------------------------------------------------ * sockex_app() * * Description : Handing socket receive data. * * Argument(s) : none. * */ void sockex_app(void *arg) { int opt, ret; INT8U sock_rxbuf[50]; // INT8U len; LWIP_UNUSED_ARG(arg); for(;;) { if(connfd > 0) { opt = 100; /* set recv timeout (100 ms) */ lwip_setsockopt(connfd, SOL_SOCKET, SO_RCVTIMEO, &opt, sizeof(int)); ret = lwip_read(connfd, sock_rxbuf, 8); if(ret == -1) { OSTimeDly(2); continue; } if((sock_rxbuf[0] == 'C')&&(sock_rxbuf[1] == 'o')) /* Compare start frame.*/ { // address_t addr; // INT8U mac[8] = {0x00, 0x12, 0x4B, 0x00, 0x01, 0xC0, 0xB7, 0xE0}; // // len = sock_rxbuf[8]; /* Set frame length.*/ /* if(len != 0x0F) { OSTimeDly(2); continue; } */ // addr.mode = LONG_ADDR; /* Using device long address.*/ // lwip_setsockopt(connfd, SOL_SOCKET, SO_RCVTIMEO, &opt, sizeof(int)); // ret = lwip_read(connfd, sock_rxbuf, len); /* Read other frame data.*/ /* if(ret == -1) { OSTimeDly(2); continue; } */ // utilReverseBuf(mac, 8); // memcpy(addr.long_addr, mac, 8); /* 提取MAC地址*/ // // // DOTO: MAC layer send frame. Using deveice MAC address. // mac_tx_handle(&addr, &sock_rxbuf[7], 1, MAC_DATA); /* Send command frame.*/ } } OSTimeDly(2); } }
/************************************************************** * void do_read(struct charcb *p_charcb) * * Socket definitely is ready for reading. Read a buffer from the socket and * discard the data. If no data is read, then the socket is closed and the * charcb is removed from the list and freed. **************************************************************/ static int do_read(struct charcb *p_charcb) { char buffer[80]; int readcount; /* Read some data */ readcount = lwip_read(p_charcb->socket, &buffer, 80); if (readcount <= 0) { close_chargen(p_charcb); return -1; } return 0; }
int lwip_readv(int s, struct iovec *vector, int count) { int totsize=0; int i; int pos; char *temp_buf; int ret; /* Check for invalid parameter */ if (count < 0 || count > UIO_MAXIOV) { set_errno(EINVAL); return -1; } /* FIX: check overflow of totsize and set EINVAL */ for (i=0; i<count; i++) totsize += vector[i].iov_len; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_readv(%d, %p, %d), iovec totlen=$d\n", s, vector, count, totsize)); if (totsize == 0) return 0; temp_buf = mem_malloc(totsize); if (temp_buf == NULL) { set_errno(ENOMEM); return -1; } ret = lwip_read(s, temp_buf, totsize); if (ret != -1) { i = 0; pos = 0; while (pos < ret) { memcpy( vector[i].iov_base, &temp_buf[pos], vector[i].iov_len); i++; pos += vector[i].iov_len; } } mem_free(temp_buf); return ret; }
/** This is an example function that tests the recv function (timeout etc.). */ static void sockex_testrecv(void *arg) { int s; int ret; int err; int opt; struct sockaddr_in addr; size_t len; char rxbuf[1024]; fd_set readset; fd_set errset; struct timeval tv; LWIP_UNUSED_ARG(arg); /* set up address to connect to */ memset(&addr, 0, sizeof(addr)); addr.sin_len = sizeof(addr); addr.sin_family = AF_INET; addr.sin_port = PP_HTONS(SOCK_TARGET_PORT); addr.sin_addr.s_addr = inet_addr(SOCK_TARGET_HOST); /* first try blocking: */ /* create the socket */ s = lwip_socket(AF_INET, SOCK_STREAM, 0); LWIP_ASSERT("s >= 0", s >= 0); /* connect */ ret = lwip_connect(s, (struct sockaddr*)&addr, sizeof(addr)); /* should succeed */ LWIP_ASSERT("ret == 0", ret == 0); /* set recv timeout (100 ms) */ opt = 100; ret = lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &opt, sizeof(int)); LWIP_ASSERT("ret == 0", ret == 0); /* write the start of a GET request */ #define SNDSTR1 "G" len = strlen(SNDSTR1); ret = lwip_write(s, SNDSTR1, len); LWIP_ASSERT("ret == len", ret == (int)len); /* should time out if the other side is a good HTTP server */ ret = lwip_read(s, rxbuf, 1); LWIP_ASSERT("ret == -1", ret == -1); err = errno; LWIP_ASSERT("errno == EAGAIN", err == EAGAIN); /* write the rest of a GET request */ #define SNDSTR2 "ET / HTTP_1.1\r\n\r\n" len = strlen(SNDSTR2); ret = lwip_write(s, SNDSTR2, len); LWIP_ASSERT("ret == len", ret == (int)len); /* wait a while: should be enough for the server to send a response */ sys_msleep(1000); /* should not time out but receive a response */ ret = lwip_read(s, rxbuf, 1024); LWIP_ASSERT("ret > 0", ret > 0); /* now select should directly return because the socket is readable */ FD_ZERO(&readset); FD_ZERO(&errset); FD_SET(s, &readset); FD_SET(s, &errset); tv.tv_sec = 10; tv.tv_usec = 0; ret = lwip_select(s + 1, &readset, NULL, &errset, &tv); LWIP_ASSERT("ret == 1", ret == 1); LWIP_ASSERT("!FD_ISSET(s, &errset)", !FD_ISSET(s, &errset)); LWIP_ASSERT("FD_ISSET(s, &readset)", FD_ISSET(s, &readset)); /* should not time out but receive a response */ ret = lwip_read(s, rxbuf, 1024); /* might receive a second packet for HTTP/1.1 servers */ if (ret > 0) { /* should return 0: closed */ ret = lwip_read(s, rxbuf, 1024); LWIP_ASSERT("ret == 0", ret == 0); } /* close */ ret = lwip_close(s); LWIP_ASSERT("ret == 0", ret == 0); printf("sockex_testrecv finished successfully\n"); }
int read(int fd, void *buf, size_t nbytes) { switch (files[fd].type) { case FTYPE_SAVEFILE: case FTYPE_CONSOLE: { int ret; DEFINE_WAIT(w); while(1) { add_waiter(w, console_queue); ret = xencons_ring_recv(files[fd].cons.dev, buf, nbytes); if (ret) break; schedule(); } remove_waiter(w, console_queue); return ret; } #ifdef HAVE_LWIP case FTYPE_SOCKET: return lwip_read(files[fd].socket.fd, buf, nbytes); #endif #ifdef CONFIG_NETFRONT case FTYPE_TAP: { ssize_t ret; ret = netfront_receive(files[fd].tap.dev, buf, nbytes); if (ret <= 0) { errno = EAGAIN; return -1; } return ret; } #endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD: { int ret, n; n = nbytes / sizeof(union xenkbd_in_event); ret = kbdfront_receive(files[fd].kbd.dev, buf, n); if (ret <= 0) { errno = EAGAIN; return -1; } return ret * sizeof(union xenkbd_in_event); } #endif #ifdef CONFIG_FBFRONT case FTYPE_FB: { int ret, n; n = nbytes / sizeof(union xenfb_in_event); ret = fbfront_receive(files[fd].fb.dev, buf, n); if (ret <= 0) { errno = EAGAIN; return -1; } return ret * sizeof(union xenfb_in_event); } #endif #ifdef CONFIG_BLKFRONT case FTYPE_BLK: { return blkfront_posix_read(fd, buf, nbytes); } #endif #ifdef CONFIG_TPMFRONT case FTYPE_TPMFRONT: { return tpmfront_posix_read(fd, buf, nbytes); } #endif #ifdef CONFIG_TPM_TIS case FTYPE_TPM_TIS: { return tpm_tis_posix_read(fd, buf, nbytes); } #endif default: break; } printk("read(%d): Bad descriptor\n", fd); errno = EBADF; return -1; }
/* ------------------------------------------------------------------------------------------------------ * sockex_selects() * * Description : socket selects test. * * Argument(s) : none. * */ void sockex_selects(void *arg) { int sock_fd, new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; socklen_t sin_size; int yes; INT8U buf[BUF_SIZE]; int ret; int i; fd_set fdsr; /* Create file descriptor.*/ int maxsock; struct timeval tv; conn_amount = 0; LWIP_UNUSED_ARG(arg); sock_fd = lwip_socket(AF_INET, SOCK_STREAM, 0); yes = 1; ret = lwip_setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); if(ret == -1) { return; } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_len = sizeof(server_addr); server_addr.sin_port = PP_HTONS(SOCK_HOSR_PORT); server_addr.sin_addr.s_addr = lwIPLocalIPAddrGet(); /* IP_ADDR_ANY is '0.0.0.0'.*/ lwip_bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); lwip_listen(sock_fd, BACKLOG + 1); /* MAX TCP client is BACKLOG.*/ sin_size = sizeof(client_addr); maxsock = sock_fd; while(1) { FD_ZERO(&fdsr); /* Initialize file descriptor set.*/ FD_SET(sock_fd, &fdsr); tv.tv_sec = 10; /* Timeout setting.*/ tv.tv_usec = 0; for (i = 0; i < BACKLOG; i++) /* Add active connection to fd set.*/ { if (fd_A[i] != 0) { FD_SET(fd_A[i], &fdsr); } } ret = lwip_select(maxsock + 1, &fdsr, NULL, NULL, &tv); if(ret < 0) { break; } else if(ret == 0) { continue; } for (i = 0; i < conn_amount; i++) /* Check every fd in the set.*/ { if (FD_ISSET(fd_A[i], &fdsr)) { int opt = 100; /* set recv timeout (100 ms) */ lwip_setsockopt(fd_A[i], SOL_SOCKET, SO_RCVTIMEO, &opt, sizeof(int)); ret = lwip_read(fd_A[i], buf, 8); if (ret <= 0) { // lwip_close(fd_A[i]); // FD_CLR(fd_A[i], &fdsr); // fd_A[i] = 0; } else /* receive data.*/ { if((buf[0] == 'C')&&(buf[1] == 'o')) { // address_t addr; // INT8U mac[8] = {0x00, 0x12, 0x4B, 0x00, 0x01, 0xC0, 0xB7, 0xE0}; // addr.mode = LONG_ADDR; // utilReverseBuf(mac, 8); // memcpy(addr.long_addr, mac, 8); // mac_tx_handle(&addr, &buf[7], 1, MAC_DATA); } if (ret < BUF_SIZE) memset(&buf[ret], '\0', 1); } } } if(FD_ISSET(sock_fd, &fdsr)) /* Check whether a new connection comes.*/ { new_fd = lwip_accept(sock_fd, (struct sockaddr *)&client_addr, &sin_size); if(new_fd <= 0) { continue; } // lwip_send(new_fd, "con", 4, 0); if(conn_amount < BACKLOG) /* Add to fd queue.*/ { fd_A[conn_amount++] = new_fd; if(new_fd > maxsock) maxsock = new_fd; } else { // conn_amount = 0; lwip_close(fd_A[conn_amount-1]); fd_A[conn_amount-1] = new_fd; if(new_fd > maxsock) maxsock = new_fd; // lwip_send(new_fd, "bye", 4, 0); // lwip_close(new_fd); /* Close larger than 5 socket.*/ } } // for (i = 0; i < BACKLOG; i++) /* Close other connections.*/ // { // if (fd_A[i] != 0) { // lwip_close(fd_A[i]); // } // } } }
int read(int s, void *mem, size_t len) { BT_HANDLE hSocket = (BT_HANDLE)s; return lwip_read(hSocket->socket, mem, len); }
static size_t socket_stream_read(void *ip, uint8_t *bp, size_t n) { return lwip_read(((SocketStream*) ip)->fd, bp, n); }
int read(int fd, void *buf, size_t nbytes) { if (fd < 0 || fd >= NOFILE) { fd = EBADF; return -1; } switch (files[fd].type) { case FTYPE_SAVEFILE: case FTYPE_CONSOLE: { int ret; DEFINE_WAIT(w); while(1) { add_waiter(w, console_queue); ret = xencons_ring_recv(files[fd].cons.dev, buf, nbytes); if (ret) break; schedule(); } remove_waiter(w); return ret; } #ifdef HAVE_LWIP case FTYPE_SOCKET: return lwip_read(files[fd].socket.fd, buf, nbytes); #endif case FTYPE_TAP: { ssize_t ret; ret = netfront_receive(files[fd].tap.dev, buf, nbytes); if (ret <= 0) { errno = EAGAIN; return -1; } return ret; } case FTYPE_KBD: { int ret, n; n = nbytes / sizeof(union xenkbd_in_event); ret = kbdfront_receive(files[fd].kbd.dev, buf, n); if (ret <= 0) { errno = EAGAIN; return -1; } return ret * sizeof(union xenkbd_in_event); } case FTYPE_FB: { int ret, n; n = nbytes / sizeof(union xenfb_in_event); ret = fbfront_receive(files[fd].fb.dev, buf, n); if (ret <= 0) { errno = EAGAIN; return -1; } return ret * sizeof(union xenfb_in_event); } case FTYPE_COMPILED_FILE: { int n; if (files[fd].compiled_file.offset >= files[fd].compiled_file.size) n = 0; else n = files[fd].compiled_file.size - files[fd].compiled_file.offset; if (n >= nbytes) n = nbytes; printf("Request %d on %d, get %d\n", nbytes, fd, n); memcpy(buf, files[fd].compiled_file.content + files[fd].compiled_file.offset, n); files[fd].compiled_file.offset += n; return n; } default: break; } printk("read(%d): Bad descriptor\n", fd); errno = EBADF; return -1; }
END_TEST static void test_sockets_allfunctions_basic_domain(int domain) { int s, s2, s3, ret; struct sockaddr_storage addr, addr2; socklen_t addrlen, addr2len; char buf[4]; /* listen socket */ s = lwip_socket(domain, SOCK_STREAM, 0); fail_unless(s >= 0); ret = lwip_listen(s, 0); fail_unless(ret == 0); addrlen = sizeof(addr); ret = lwip_getsockname(s, (struct sockaddr*)&addr, &addrlen); fail_unless(ret == 0); s2 = test_sockets_alloc_socket_nonblocking(domain, SOCK_STREAM); fail_unless(s2 >= 0); /* nonblocking connect s2 to s (but use loopback address) */ if (domain == AF_INET) { #if LWIP_IPV4 struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; addr4->sin_addr.s_addr = PP_HTONL(INADDR_LOOPBACK); #endif } else { #if LWIP_IPV6 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; struct in6_addr lo6 = IN6ADDR_LOOPBACK_INIT; addr6->sin6_addr = lo6; #endif } ret = lwip_connect(s2, (struct sockaddr*)&addr, addrlen); fail_unless(ret == -1); fail_unless(errno == EINPROGRESS); ret = lwip_connect(s2, (struct sockaddr*)&addr, addrlen); fail_unless(ret == -1); fail_unless(errno == EALREADY); while(tcpip_thread_poll_one()); s3 = lwip_accept(s, (struct sockaddr*)&addr2, &addr2len); fail_unless(s3 >= 0); ret = lwip_connect(s2, (struct sockaddr*)&addr, addrlen); fail_unless(ret == -1); fail_unless(errno == EISCONN); /* write from server to client */ ret = write(s3, "test", 4); fail_unless(ret == 4); ret = lwip_shutdown(s3, SHUT_WR); fail_unless(ret == 0); while(tcpip_thread_poll_one()); ret = lwip_recv(s2, buf, 3, MSG_PEEK); fail_unless(ret == 3); ret = lwip_recv(s2, buf, 3, MSG_PEEK); fail_unless(ret == 3); ret = lwip_read(s2, buf, 4); fail_unless(ret == 4); ret = lwip_read(s2, buf, 1); fail_unless(ret == 0); ret = lwip_read(s2, buf, 1); fail_unless(ret == -1); ret = lwip_write(s2, "foo", 3); fail_unless(ret == 3); ret = lwip_close(s2); fail_unless(ret == 0); while(tcpip_thread_poll_one()); /* read one byte more than available to check handling FIN */ ret = lwip_read(s3, buf, 4); fail_unless(ret == 3); ret = lwip_read(s3, buf, 1); fail_unless(ret == 0); ret = lwip_read(s3, buf, 1); fail_unless(ret == -1); while(tcpip_thread_poll_one()); ret = lwip_close(s); fail_unless(ret == 0); ret = lwip_close(s3); fail_unless(ret == 0); }