/**************************************************************************** * NetworkWait ***************************************************************************/ int NetworkWait() { if (!checkincomming) return -3; struct sockaddr_in sin; struct sockaddr_in client_address; socklen_t addrlen = sizeof(client_address); //Open socket socket = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (socket == INVALID_SOCKET) { return socket; } sin.sin_family = AF_INET; sin.sin_port = htons(PORT); sin.sin_addr.s_addr = htonl(INADDR_ANY); if (net_bind(socket, (struct sockaddr*)&sin, sizeof(sin)) < 0) { net_close(socket); return -1; } if (net_listen(socket, 3) < 0) { net_close(socket); return -1; } connection = net_accept(socket, (struct sockaddr*)&client_address, &addrlen); sprintf(incommingIP, "%s", inet_ntoa(client_address.sin_addr)); if (connection < 0) { net_close(connection); net_close(socket); return -4; } else { unsigned char haxx[9]; //skip haxx net_read(connection, &haxx, 8); wiiloadVersion[0] = haxx[4]; wiiloadVersion[1] = haxx[5]; net_read(connection, &infilesize, 4); if (haxx[4] > 0 || haxx[5] > 4) { net_read(connection, &uncfilesize, 4); // Compressed protocol, read another 4 bytes } waitforanswer = true; checkincomming = false; networkHalt = true; } return 1; }
int message_handler_handle_player_message(void) { char *data; char *message; size_t message_size; ssize_t bytes_read; struct player player; if ((bytes_read = net_read(werld_client.message_handler_fds[0], &message_size, sizeof(size_t))) == -1) { werld_client_kill(&werld_client); werld_client_log(WERLD_CLIENT_ERROR, "+message_handler+handle_player_message read failed\n"); exit(-1); } if (bytes_read == 0) return(-1); werld_client_log_binary(WERLD_CLIENT_DEBUG, (uint8_t *) &message_size, sizeof(size_t), "+message_handler+handle_player_message bytes read: %zd ", bytes_read); if (!(data = malloc(WERLD_MESSAGE_HANDLER_READ_BUFSIZ(message_size)))) { perror("malloc"); exit(errno); } if ((bytes_read = net_read(werld_client.message_handler_fds[0], data, WERLD_MESSAGE_HANDLER_READ_BUFSIZ(message_size))) == -1) { werld_client_kill(&werld_client); werld_client_log(WERLD_CLIENT_ERROR, "+message_handler+handle_player_message read failed\n"); exit(-1); } if (bytes_read == 0) return(-1); if (!(message = malloc(message_size))) { perror("malloc"); exit(errno); } memcpy(&player, data, sizeof(struct player)); memcpy(message, data + sizeof(struct player), message_size); message[message_size] = '\0'; ui_erase_player_message_list(&player); ui_draw_map(werld_client.world_map); player_list_add_message(&(werld_client.player_list), message, player.id); ui_draw_player_message_list(&player); ui_draw_player(player); main_window_refresh(werld_client.main_window); free(message); free(data); return(0); }
/* mca_network_cmd_wait_for_cmd(MCACnxn *cnxn) Wait for and read command sent from the server Returns the received command, but also updates cmd field in cnxn */ gint32 mca_network_wait_for_cmd (MCACnxn *cnxn) { // GInputStream *server = NULL; gint32 cmd = 0, cmd_data_size = 0; gint32 cmd_n = 0, cmd_data_size_n = 0; gssize retsize = 0; assert(cnxn->connected == TRUE); DEBUG_PRINT("mca_network_wait_for_cmd(): **** start **** %d\n", retsize); cnxn->cmd = 0; cnxn->cmd_data_len = 0; retsize = net_read(cnxn, (void *)&cmd_n, sizeof(cmd_n)); DEBUG_PRINT("mca_network_wait_for_cmd(): server command: %08X\n", ntohl(cmd_n)); if (retsize <= 0){ //Unable to read the command DEBUG_PRINT("mca_network_wait_for_cmd(): Unable to read command: [%d]\n", retsize); return 0; } //the command was read cmd = ntohl(cmd_n); cnxn->cmd = cmd; //now read the size of command payload ( data ); retsize = net_read(cnxn, (void *)&cmd_data_size_n, sizeof(cmd_data_size_n)); if (retsize <= 0){ DEBUG_PRINT("mca_network_wait_for_cmd(): command: %08X, BUT error reading data size\n", cmd); return 0; } cmd_data_size = ntohl(cmd_data_size_n); cnxn->cmd_data_len = cmd_data_size; DEBUG_PRINT("mca_network_wait_for_cmd(): server command [%08X] data size [%d bytes]\n", cmd, cmd_data_size); if (!cmd_data_size){ DEBUG_PRINT("mca_network_wait_for_cmd(): command: %08X BUT size is [%d bytes]\n", cmd, cmd_data_size); return cmd; } //now read the command's payload ( data ) if (cnxn->cmd_data_len > MAX_RCV_BUF_SIZE) cnxn->cmd_data_len = MAX_RCV_BUF_SIZE; retsize = net_read(cnxn, cnxn->cmd_data, cnxn->cmd_data_len); if (retsize>=0) DEBUG_PRINT("mca_network_wait_cmd(): command data [%d bytes]\n", retsize); else { DEBUG_PRINT("mca_network_wait_cmd(): reading command data failed: %d\n", retsize); } return cmd; }
int slip_user_read(int fd, void *buf, int len, struct slip_data *pri) { int i, n, size, start; if(pri->more>0) { i = 0; while(i < pri->more) { size = slip_unesc(pri->ibuf[i++], pri->ibuf, &pri->pos, &pri->esc); if(size){ memcpy(buf, pri->ibuf, size); memmove(pri->ibuf, &pri->ibuf[i], pri->more-i); pri->more=pri->more-i; return(size); } } pri->more=0; } n = net_read(fd, &pri->ibuf[pri->pos], sizeof(pri->ibuf) - pri->pos); if(n <= 0) return(n); start = pri->pos; for(i = 0; i < n; i++){ size = slip_unesc(pri->ibuf[start + i], pri->ibuf, &pri->pos, &pri->esc); if(size){ memcpy(buf, pri->ibuf, size); memmove(pri->ibuf, &pri->ibuf[start+i+1], n-(i+1)); pri->more=n-(i+1); return(size); } } return(0); }
static int NetRead(int connection, u8 *buf, u32 len, u32 tout) // timeout in msec { u32 read = 0; s32 ret = 0; u32 t; t = ticks_to_millisecs(gettime()) + tout; while (read < len) { ret = net_read(connection, buf + read, len - read); if (ret <= 0) usleep (10 * 1000); else { read += ret; } if (ticks_to_millisecs(gettime()) > t) break; } return read; }
static int sed_request_read(FilterLayer *layer, void *buf, int amount, int rdtimeout) { SedData *seddata = (SedData *)layer->context->data; PRStatus rv; for (;;) { int n; n = seddata->fifo->read(buf, amount); if (n > 0) return n; n = net_read(layer->lower, buf, amount, rdtimeout); if (n == 0) break; if (n < 0) return n; rv = sed_eval_buffer(&seddata->eval, (const char *)buf, n, (PRFileDesc *)*seddata->fifo); if (rv != PR_SUCCESS) { NsprError::setErrorf(PR_INVALID_ARGUMENT_ERROR, XP_GetAdminStr(DBT_FnError), seddata->name); return -1; } } rv = sed_finalize_eval(&seddata->eval, (PRFileDesc *)*seddata->fifo); if (rv != PR_SUCCESS) { NsprError::setErrorf(PR_INVALID_ARGUMENT_ERROR, XP_GetAdminStr(DBT_FnError), seddata->name); return -1; } return seddata->fifo->read(buf, amount); }
int netctrl_listen(SERVER_LISTEN_CALLBACK_FUNC func) { int readsize; char buffer[NET_BUFFER_LEN]; if (!func) return ERROR; while(TRUE) { int ret = net_read(buffer, NET_BUFFER_LEN, &readsize); if (ret == FATAL) return FATAL; else if (ret == ERROR) return ERROR; else { if (readsize == 0) continue; if (func(buffer) != SUCCESS) break; } } return SUCCESS; }
NSAPI_PUBLIC int netbuf_grab(netbuf *buf, int sz) { int n; if(!buf->inbuf) { buf->inbuf = (unsigned char *) MALLOC(sz); buf->maxsize = sz; } else if(sz > buf->maxsize) { buf->inbuf = (unsigned char *) REALLOC(buf->inbuf, sz); buf->maxsize = sz; } PR_ASSERT(buf->pos == buf->cursize); buf->pos = 0; buf->cursize = 0; while(1) { switch(n = net_read(buf->sd,(char *)(buf->inbuf),sz,buf->rdtimeout)) { case IO_EOF: return IO_EOF; case IO_ERROR: { buf->errmsg = system_errmsg(); return IO_ERROR; } default: buf->cursize = n; return n; } } }
/* * HttpSendCmd - send a command * * return 1 if proper response received, 0 otherwise */ static int HttpSendCmd(const char *cmd, char expresp, netbuf *nControl) { int ret = 0; char *buf = nControl->response; /* if (nControl->dir != FTPLIB_CONTROL) return 0; */ if (ftplib_debug > 2) fprintf(stderr,"%s\n",cmd); if (net_write(nControl->handle,cmd,strlen(cmd)) <= 0) { perror("write"); return 0; } while (ret < 256) { if (socket_wait(nControl) != 1) return 0; if (net_read(nControl->handle,buf,1) != 1) { break; } ret++; if (*buf == '\r') continue; if (*buf == '\n') break; buf++; } *buf = 0; if (nControl->response[9] == expresp) return 1; return 0; }
/** * This function reads all the data from a connection into a buffer which it returns. * It will return an empty buffer if something doesn't go as planned * * @param s32 connection The connection identifier to suck the response out of * @return block A 'block' struct (see http.h) in which the buffer is located */ static struct block read_message(s32 connection, struct block buffer, bool (*f)(void *, int, int), void *ud) { static char tmpHdr[512]; bool hdr = false, fail = true; u32 fileSize = 0, step = 0, offset = 0; s64 t = gettime(); //The offset variable always points to the first byte of memory that is free in the buffer while (true) { if(ticks_to_millisecs(diff_ticks(t, gettime())) > TCP_TIMEOUT || buffer.size <= offset) break; //Fill the buffer with a new batch of bytes from the connection, //starting from where we left of in the buffer till the end of the buffer u32 len = buffer.size - offset; s32 bytes_read = net_read(connection, buffer.data + offset, len > HTTP_BUFFER_GROWTH ? HTTP_BUFFER_GROWTH : len); //Anything below 0 is an error in the connection if(bytes_read > 0) { t = gettime (); offset += bytes_read; // Not enough memory if(buffer.size <= offset) return emptyblock; if(!hdr && offset >= sizeof tmpHdr) { hdr = true; memcpy(tmpHdr, buffer.data, sizeof tmpHdr - 1); tmpHdr[sizeof tmpHdr - 1] = 0; const char *p = strstr(tmpHdr, "Content-Length:"); if(p != 0) { p += sizeof "Content-Length:"; fileSize = strtol(p, 0, 10); } } if(step * HTTP_BUFFER_GROWTH < offset) { ++step; if(f != 0) { if((fileSize != 0 && !f(ud, fileSize, offset <= fileSize ? offset : fileSize)) || (fileSize == 0 && !f(ud, buffer.size, offset))) return emptyblock; } } fail = false; } else { if(bytes_read < 0) fail = true; break; // Need to translate the error messages here instead of just breaking. } } if(fail) return emptyblock; //At the end of above loop offset should be precisely the amount of bytes that were read from the connection buffer.size = offset; return buffer; }
/* * read a line of text * * return -1 on error or bytecount */ static int readline (char *buf, int max, netbuf *ctl) { int x, retval = 0; char *end, *bp = buf; int eof = 0; if ((ctl->dir != FTPLIB_CONTROL) && (ctl->dir != FTPLIB_READ)) return -1; if (max == 0) return 0; do { if (ctl->cavail > 0) { x = (max >= ctl->cavail) ? ctl->cavail : max - 1; end = (char *) memccpy(bp, ctl->cget, '\n', x); if (end != NULL) x = end - bp; retval += x; bp += x; *bp = '\0'; max -= x; ctl->cget += x; ctl->cavail -= x; if (end != NULL) { bp -= 2; if (strcmp(bp, "\r\n") == 0) { *bp++ = '\n'; *bp++ = '\0'; --retval; } break; } } if (max == 1) { *buf = '\0'; break; } if (ctl->cput == ctl->cget) { ctl->cput = ctl->cget = ctl->buf; ctl->cavail = 0; ctl->cleft = FTPLIB_BUFSIZ; } if (eof) { if (retval == 0) retval = -1; break; } if (!socket_wait(ctl)) return retval; if ((x = net_read(ctl->handle, ctl->cput, ctl->cleft)) == -1) { perror("read"); retval = -1; break; } if (x == 0) eof = 1; ctl->cleft -= x; ctl->cavail += x; ctl->cput += x; } while (1); return retval; }
s32 recv_to_file(s32 s, FILE *f) { char * buf = (char *) malloc(NET_BUFFER_SIZE); if(!buf) return -1; s32 bytes_read; while (1) { try_again_with_smaller_buffer: bytes_read = net_read(s, buf, NET_BUFFER_SIZE); if (bytes_read < 0) { if (bytes_read == -EINVAL && NET_BUFFER_SIZE == MAX_NET_BUFFER_SIZE) { NET_BUFFER_SIZE = MIN_NET_BUFFER_SIZE; usleep(100); goto try_again_with_smaller_buffer; } free(buf); return bytes_read; } else if (bytes_read == 0) { free(buf); return 0; } s32 bytes_written = fwrite(buf, 1, bytes_read, f); if (bytes_written < bytes_read) { free(buf); return -1; } usleep(100); } return -1; }
int run_established(fd_set *ips, fd_set *ops, int streamfd, int inpipe, int outpipe) { int n = 0; if (streamfd < 0) { /* local loopback */ if (FD_ISSET(inpipe, ips) && FD_ISSET(outpipe, ops)) { n = read(inpipe, netbuf, NETBUF_SIZE); n = write(outpipe, netbuf, n); } return n; } if (FD_ISSET(inpipe, ips) && FD_ISSET(streamfd, ops)) { n = net_write(streamfd, inpipe); if (n <= 0) return -1; } if (FD_ISSET(outpipe, ops) && FD_ISSET(streamfd, ips)) { n = net_read(streamfd, outpipe); if (n <= 0) return -1; } return n; }
/* * FtpRead - read from a data connection */ GLOBALDEF int FtpRead(void *buf, int max, netbuf *nData) { int i; if (nData->dir != FTPLIB_READ) return 0; if (nData->buf) i = readline(buf, max, nData); else { i = socket_wait(nData); if (i != 1) return 0; i = net_read(nData->handle, buf, max); } if (i == -1) return 0; nData->xfered += i; if (nData->idlecb && nData->cbbytes) { nData->xfered1 += i; if (nData->xfered1 > nData->cbbytes) { if (nData->idlecb(nData, nData->xfered, nData->idlearg) == 0) return 0; nData->xfered1 = 0; } } return i; }
NSAPI_PUBLIC int netbuf_getbytes(netbuf *buf, char *buffer, int size) { int bytes; if (!buf->inbuf) { buf->inbuf = (unsigned char *) MALLOC(buf->maxsize); } else { if (buf->pos < buf->cursize) { int bytes_in_buffer = buf->cursize - buf->pos; if (bytes_in_buffer > size) bytes_in_buffer = size; memcpy(buffer, &(buf->inbuf[buf->pos]), bytes_in_buffer); buf->pos += bytes_in_buffer; return bytes_in_buffer; } } /* The netbuf is empty. Read data directly into the caller's buffer */ bytes = net_read(buf->sd, buffer, size, buf->rdtimeout); if (bytes == 0) return NETBUF_EOF; if (bytes < 0) { buf->errmsg = system_errmsg(); return NETBUF_ERROR; } return bytes; }
static int tcp_readln(const s32 s, char *buf, const u16 max_length) { s32 res = -1; s32 ret; u64 start_time = gettime(); u16 c = 0; while (c < max_length) { if (ticks_to_millisecs(diff_ticks(start_time, gettime())) > HTTP_TIMEOUT) break; ret = net_read(s, &buf[c], 1); if (ret == -EAGAIN) { usleep(20 * 1000); continue; } if (ret <= 0) break; if (c > 0 && buf[c - 1] == '\r' && buf[c] == '\n') { res = 0; buf[c-1] = 0; break; } c++; start_time = gettime(); usleep(100); } return res; }
void net_test_recv() { uint8_t data[256]; size_t len; while(1) { //io_wait(10000000); //io_wait(1000000); len = net_read(data, sizeof(data)); if(!len) continue; /* debug(NET, "net rcv (%D):\n", len); */ /* #ifdef CONFIG_NET_DBG */ /* { */ /* size_t i; */ /* for(i=0 ; i<len ; i++) */ /* debug(NET, "%c", data[i]); */ /* debug(NET,"\n"); */ /* } */ /* #endif */ } }
static int filtermethod_callback_read(FilterLayer *layer, void *buf, int amount, int timeout) { PRFileDesc *fd = (PRFileDesc *)layer; filter_read_callback(layer->context->sn); return net_read(fd->lower, buf, amount, timeout); }
DWORD WINAPI net_thread(LPVOID pParam) { while(true) { net_read(); } return 0; }
/** * This function reads all the data from a connection into a buffer which it returns. * It will return an empty buffer if something doesn't go as planned * * @param s32 connection The connection identifier to suck the response out of * @return block A 'block' struct (see http.h) in which the buffer is located */ struct block read_message(s32 connection) { //Create a block of memory to put in the response struct block buffer; buffer.data = CFMalloc(HTTP_BUFFER_SIZE); buffer.size = HTTP_BUFFER_SIZE; if(buffer.data == NULL) { return emptyblock; } //The offset variable always points to the first byte of memory that is free in the buffer u32 offset = 0; while(1) { //Fill the buffer with a new batch of bytes from the connection, //starting from where we left of in the buffer till the end of the buffer s32 bytes_read = net_read(connection, buffer.data + offset, buffer.size - offset); //Anything below 0 is an error in the connection if(bytes_read < 0) { printf(TX.errorConnect, bytes_read); return emptyblock; } //No more bytes were read into the buffer, //we assume this means the HTTP response is done if(bytes_read == 0) { break; } offset += bytes_read; //Check if we have enough buffer left over, //if not expand it with an additional HTTP_BUFFER_GROWTH worth of bytes if(offset >= buffer.size) { buffer.size += HTTP_BUFFER_GROWTH; buffer.data = CFRealloc(buffer.data, buffer.size); if(buffer.data == NULL) { return emptyblock; } } } //At the end of above loop offset should be precisely the amount of bytes that were read from the connection buffer.size = offset; //Shrink the size of the buffer so the data fits exactly in it CFRealloc(buffer.data, buffer.size); return buffer; }
static s32 helper_readtcpip( DebugHelper_t *helper) { void *buffer = helper->param[0].pointer; s32 size = helper->param[1].intval; s32 ret = net_read( helper->clientsock, buffer, size); return ret; }
/** * Read \a len bytes from socket \a p_fd into buffer \a buf. * Block until \a len bytes are read or until an error. * * @return If successful, the number of bytes read: \a len. * On end-of-file, 0. * On error, less than 0 (if single-threaded, the error can be found * in the errno global variable). */ KRB5_LIB_FUNCTION krb5_ssize_t KRB5_LIB_CALL krb5_net_read (krb5_context context, void *p_fd, void *buf, size_t len) { krb5_socket_t fd = *((krb5_socket_t *)p_fd); return net_read(fd, buf, len); }
/** * This function reads all the data from a connection into a buffer. * * @param s32 connection The connection identifier to suck the response out of * @return bool True if data downloaded succesfully. */ bool tcp_readData(const s32 connection, u8 **buffer, const u32 length) { u8 *p; u32 left, block, received; s64 t; s32 res; p = *buffer; left = length; received = 0; t = gettime (); while (left) { if (ticks_to_millisecs (diff_ticks (t, gettime ())) > TCP_BLOCK_RECV_TIMEOUT) break; // Update the progress bar if(displayProgressWindow) { ShowProgress(received, length); if(ProgressCanceled()) { ProgressStop(); break; } } // Get next block size block = left; if (block > TCP_BLOCK_SIZE) block = TCP_BLOCK_SIZE; if(http_port == 443) res = ssl_read (connection, p, block); else res = net_read (connection, p, block); if ((res == 0) || (res == -EAGAIN)) { usleep (20 * 1000); continue; } if (res < 0) break; received += res; left -= res; p += res; // update timing after each downloaded block t = gettime (); } return left == 0; }
int net_read_16bit(int socketfd, uint16_t *u16, GError **gerror) { size_t size = 2; int result = net_read(socketfd, u16, &size, gerror); *u16 = ntohs(*u16); return result; }
int net_read_32bit(int socketfd, uint32_t *u32, GError **gerror) { size_t size = 4; int result = net_read(socketfd, u32, &size, gerror); *u32 = ntohl(*u32); return result; }
ssize_t ROKEN_LIB_FUNCTION eread (int fd, void *buf, size_t nbytes) { ssize_t ret; ret = net_read (fd, buf, nbytes); if (ret < 0) err (1, "read"); return ret; }
int receive_cmd(int sock, char *data, int max_len, int *p_cmd) { struct nestor_header header; int ret = 0, remaining; /* DEBUG("receive_cmd"); */ while (ret != HEADER_LEN) { ret = net_read(sock, (char *)&header, HEADER_LEN, -1 /* TIMEOUT_LONG */); if (ret <= 0) return ret; if (ret != HEADER_LEN) { ERROR("header read failed, ret: %d", ret); net_flush(sock); /* try to read again */ } } *p_cmd = header.cmd; //DEBUG("%s cmd %d len %d", __func__, header.cmd, header.len); if (header.len >= max_len) { ERROR("command too long, max length is: %d", max_len); net_flush(sock); return INVALID_DATA; } remaining = header.len; while (remaining > 0) { ret = net_read(sock, data, remaining, TIMEOUT_SHORT); if (ret <= 0) return ret; remaining -= ret; } /* set cmd terminate byte */ if (*p_cmd == CMD_XML) data[header.len] = '\0'; return header.len; }
/** * read a string from a socket until a char from delim str found. */ int net_input(socket_t s, char *buf, int size, const char *delim) { // wait for remote input without eating cpu fd_set readfds; struct timeval tv; char ch; int count = 0; // clear the set FD_ZERO(&readfds); while (1) { tv.tv_sec = 0; tv.tv_usec = BLOCK_INTERVAL; // time is reset in select() call in linux FD_SET(s, &readfds); int rv = select(s + 1, &readfds, NULL, NULL, &tv); if (rv == -1) { return 0; // an error occured } else if (rv == 0) { // timeout occured - check for program break if (0 != dev_events(0)) { return 0; } } else if (FD_ISSET(s, &readfds)) { // ready for reading break; } } FD_ZERO(&readfds); memset(buf, 0, size); while (count < size) { int bytes = net_read(s, &ch, 1); if (bytes <= 0) { return count; // no more data } else { if (ch == 0) { return count; } if (delim) { if ((strchr(delim, ch) != NULL)) { return count; // delimiter found } } if (ch != '\015') { // ignore it buf[count] = ch; count += bytes; // actually ++ } } } return count; }
static void enet_read(int fd, void *buf, size_t len) { ssize_t ret; ret = net_read (fd, buf, len); if (ret == 0) errx (1, "EOF in read"); else if (ret < 0) errx (1, "read"); }
bool tcp_read (const s32 s, u8 **buffer, const u32 length) { u8 *p; u32 step, left, block, received; s64 t; s32 res; step = 0; p = *buffer; left = length; received = 0; t = gettime (); while (left) { if (ticks_to_millisecs (diff_ticks (t, gettime ())) > TCP_BLOCK_RECV_TIMEOUT) { printDebugMsg(NORMAL_DEBUG_MESSAGE,"tcp_read timeout\n"); break; } block = left; if (block > 2048) block = 2048; res = net_read (s, p, block); if ((res == 0) || (res == -EAGAIN)) { usleep (20 * 1000); continue; } if (res < 0) { printDebugMsg(NORMAL_DEBUG_MESSAGE,"net_read failed: %d\n", res); break; } received += res; left -= res; p += res; if ((received / TCP_BLOCK_SIZE) > step) { t = gettime (); step++; } } return left == 0; }