void attacker_send_concatenation(havege_state *havege_state, void* socket) //@ requires attacker_invariant(?pub, ?pred, ?kc, havege_state, socket, ?attacker); //@ ensures attacker_invariant(pub, pred, kc, havege_state, socket, attacker); { int size1; int size2; char buffer1[MAX_MESSAGE_SIZE]; char buffer2[MAX_MESSAGE_SIZE]; char buffer3[MAX_MESSAGE_SIZE]; //@ open attacker_invariant(pub, pred, kc, havege_state, socket, attacker); size1 = net_recv(socket, buffer1, MAX_MESSAGE_SIZE); size2 = net_recv(socket, buffer2, MAX_MESSAGE_SIZE); if (size1 <= 0 || size2 <= 0 || MAX_MESSAGE_SIZE - size1 <= size2) { //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker); return; } //@ chars_to_crypto_chars(buffer1, size1); memcpy(buffer3, buffer1, (unsigned int) size1); //@ chars_to_crypto_chars(buffer2, size2); memcpy((char*) buffer3 + size1, buffer2, (unsigned int) size2); //@ crypto_chars_join(buffer3); //@ crypto_chars_to_chars(buffer3, size1 + size2); net_send(socket, buffer3, (unsigned int) (size1 + size2)); //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker); //@ crypto_chars_to_chars(buffer1, size1); //@ crypto_chars_to_chars(buffer2, size2); }
void ReadCommandPatch(int socket, std::vector<FifoFrameData>& frames) { char cmd; u32 frame_idx; u32 size; u32 offset; char data[12]; ssize_t numread = 0; while (numread != sizeof(data)) numread += net_recv(socket, data+numread, sizeof(data)-numread, 0); frame_idx = ntohl(*(u32*)&data[0]); offset = ntohl(*(u32*)&data[4]); size = ntohl(*(u32*)&data[8]); printf("Patching %d bytes of frame %d at offset %d;\n", size, frame_idx, offset); numread = 0; while (numread != size) { ssize_t numread_now = 0; if (0 != (numread_now = net_recv(socket, data, 1, 0))) { frames[frame_idx].fifoData[offset + numread] = data[0]; ++numread; } } // TODO: Need to update frame analysis here.. }
extern int net_recv_packet(int sock, t_packet * packet, unsigned int * currsize) { int addlen; unsigned int header_size; void * temp; if (!packet) { eventlog(eventlog_level_error,__FUNCTION__,"[%d] got NULL packet (closing connection)",sock); return -1; } if (!currsize) { eventlog(eventlog_level_error,__FUNCTION__,"[%d] got NULL currsize (closing connection)",sock); return -1; } if ((header_size = packet_get_header_size(packet))>=MAX_PACKET_SIZE) { eventlog(eventlog_level_error,__FUNCTION__,"[%d] could not determine header size (closing connection)",sock); return -1; } if (!(temp = packet_get_raw_data_build(packet,*currsize))) { eventlog(eventlog_level_error,__FUNCTION__,"[%d] could not obtain raw data pointer at offset %u (closing connection)",sock,*currsize); return -1; } if (*currsize<header_size) addlen = net_recv(sock, temp, header_size-*currsize); else { unsigned int total_size=packet_get_size(packet); if (total_size<header_size) { eventlog(eventlog_level_warn,__FUNCTION__,"[%d] corrupted packet received (total_size=%u currsize=%u) (closing connection)",sock,total_size,*currsize); return -1; } if (*currsize>=total_size) { eventlog(eventlog_level_warn,__FUNCTION__,"[%d] more data requested for already complete packet (total_size=%u currsize=%u) (closing connection)",sock,total_size,*currsize); return -1; } addlen = net_recv(sock, temp, total_size-*currsize); } if (addlen<=0) return addlen; *currsize += addlen; if (*currsize>=header_size && *currsize==packet_get_size(packet)) return 1; return 0; }
static void recvInstructions(int fd, Instructions *insts, block **offsets) { int noffsets; net_recv(fd, &insts->size, sizeof(int), 0); net_recv(fd, &noffsets, sizeof(int), 0); insts->instr = malloc(insts->size * sizeof(Instruction)); *offsets = garble_allocate_blocks(noffsets); net_recv(fd, insts->instr, sizeof(Instruction) * insts->size, 0); net_recv(fd, *offsets, sizeof(block) * noffsets, 0); }
void receiver(){ net_event_t *event; comBuf *recv_pkt; uint8_t port = 2; uint8_t *payload; com_mode (IFACE_RADIO, IF_LISTEN); while(1){ recv_pkt = net_recv(port); event = (net_event_t *)recv_pkt->data; //packet meant for us if(event->to == 0) { if(event->event == THERMAL) { payload = &(recv_pkt->data[6]); printf("Node: %d, Temp: %C\n", event->from, *payload); } else if(event->event == LIGHT) { payload = &(recv_pkt->data[6]); printf("Node: %d, Light: %C\n", event->from, *payload); } else { printf("No event handler for given event!\n"); } } com_free_buf(recv_pkt); } }
int tread(spdid_t spdid, td_t td, int cbid, int sz) { net_connection_t nc; struct torrent *t; char *buf; int ret; buf = cbuf2buf(cbid, sz); if (!buf) return -EINVAL; if (tor_isnull(td)) return -EINVAL; NET_LOCK_TAKE(); t = tor_lookup(td); if (!t) ERR_THROW(-EINVAL, done); if (!(t->flags & TOR_READ)) ERR_THROW(-EACCES, done); assert(t->data); nc = (net_connection_t)t->data; ret = net_recv(spdid, nc, buf, sz); done: NET_LOCK_RELEASE(); assert(lock_contested(&net_lock) != cos_get_thd_id()); return ret; }
/***************************** * ScanHeader * Download the data until the header is finished * Returns: Success if return header has a 200 status */ uint32_t ScanHeader(int32_t sockDesc, std::map<std::string, std::string> & header_value, std::string & urlData ) { int32_t length = -1; std::string::size_type cut_at = 0; std::string header = ""; uint8_t rev[RCVBUFSIZE]; #if defined (__GAMECUBE__) || defined (__WII__) while ( ( length = net_recv(sockDesc, (char*)rev, RCVBUFSIZE, 0)) > 0 ) #else while ( ( length = recv(sockDesc, (char*)rev, RCVBUFSIZE, 0)) > 0 ) #endif { urlData.append((char*)rev, length); memset(rev, 0, RCVBUFSIZE); cut_at = urlData.find(http_split); if ( cut_at != urlData.npos ) { header = urlData.substr( 0, cut_at ); urlData.erase( 0, cut_at+4 ); return (ParseHeader( header, header_value ) == 200); } } return false; }
void handle_client(FILE *fp, int sockfd, char *cmd) { int n; char sendline[MAXLINE]; char recvline[MAXLINE+1]; int quit = 0; while (!quit) { if (cmd) { strcpy(sendline, cmd); /* one shot command */ quit = 1; } else if (fgets(sendline, MAXLINE, fp) == NULL) { break; } n = strlen(sendline); if (net_send(sockfd, sendline, n) != n) error_abort("handle_client: write error on socket"); while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) { recvline[n] = 0; fputs(recvline, stdout); } if (n < 0) { char msg[200]; sprintf(msg, "handle_client: net_recv error: %s\n", strerror(-n)); error_abort(msg); } } }
int main(int argc, char** argv) { net_init(); net_send(); net_recv(); return 0; }
void ReadCommandEnable(int socket, std::vector<AnalyzedFrameInfo>& analyzed_frames, bool enable) { char cmd; u32 frame_idx; u32 object; u32 offset; char data[12]; ssize_t numread = 0; while (numread != sizeof(data)) numread += net_recv(socket, data+numread, sizeof(data)-numread, 0); frame_idx = ntohl(*(u32*)&data[0]); object = ntohl(*(u32*)&data[4]); offset = ntohl(*(u32*)&data[8]); printf("%s command %d in frame %d;\n", (enable)?"Enabled":"Disabled", offset, frame_idx); AnalyzedFrameInfo& frame = analyzed_frames[frame_idx]; AnalyzedObject& obj = frame.objects[object]; for (int i = 0; i < obj.cmd_starts.size(); ++i) { if (obj.cmd_starts[i] == offset) { obj.cmd_enabled[i] = enable; printf("%s command %d in frame %d, %d\n", (enable)?"Enabled":"Disabled", i, frame_idx, obj.cmd_enabled.size()); break; } } }
void attacker_send_hash(havege_state *havege_state, void* socket) //@ requires attacker_invariant(?pub, ?pred, ?kc, havege_state, socket, ?attacker); //@ ensures attacker_invariant(pub, pred, kc, havege_state, socket, attacker); { int temp; int size; char buffer[MAX_MESSAGE_SIZE]; //@ open attacker_invariant(pub, pred, kc, havege_state, socket, attacker); size = net_recv(socket, buffer, MAX_MESSAGE_SIZE); if (size < MINIMAL_STRING_SIZE) { //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker); return; } //@ assert chars(buffer, size, ?pay); char hash[64]; //@ chars_to_crypto_chars(buffer, size); //@ HASH_PUB_PAYLOAD(pay) sha512(buffer, (unsigned int) size, hash, 0); //@ assert cryptogram(hash, 64, ?h_ccs, ?h_cg); //@ assert h_cg == cg_hash(cs_to_ccs(pay)); //@ assert is_hash_is_public(?proof, pub, pred); //@ crypto_chars_to_chars(buffer, size); //@ public_chars(buffer, size); //@ proof(h_cg); //@ public_cryptogram(hash, h_cg); net_send(socket, hash, 64); //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker); }
void attacker_send_split(havege_state *havege_state, void* socket) //@ requires attacker_invariant(?pub, ?pred, ?kc, havege_state, socket, ?attacker); //@ ensures attacker_invariant(pub, pred, kc, havege_state, socket, attacker); { int temp; int size1; int size2; char buffer[MAX_MESSAGE_SIZE]; //@ open attacker_invariant(pub, pred, kc, havege_state, socket, attacker); size1 = net_recv(socket, buffer, MAX_MESSAGE_SIZE); if (size1 <= 0) { //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker); return; } //@ assert chars(buffer, size1, ?cs); //@ close_havege_util(pub, pred, attacker); r_int_with_bounds(havege_state, &temp, 0, size1); //@ open_havege_util(pub, pred, attacker); size2 = temp; net_send(socket, buffer, (unsigned int) (size2)); net_send(socket, (void*) buffer + size2, (unsigned int) (size1 - size2)); //@ chars_join(buffer); //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker); }
ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len) { int ret; #ifdef SSL_SUPPORT if (!con->ssl) { #endif ret = net_recv(con->sd, buf, len, 0); if (ret == -1) { if (is_blocked_or_interrupted()) return 0; return -net_error(); } else if (ret == 0) { return -1; } #ifdef SSL_SUPPORT } else { ret = net_ssl_recv(con, buf, len); } #endif /* SSL_SUPPORT */ return ret; }
// // get one response line // static int get_line(int sock, char line[], short linesize) { // get the line length short sz; if (!net_recv(sock, &sz, sizeof(sz))) return -1; sz = ntohs(sz); if (!sz) return 0; // get the line while (sz > linesize) { // this is just a hack (being lazy), this should not happen anyway net_recv(sock, line, linesize); sz -= sizeof(line); } if (!net_recv(sock, line, sz)) return 0; line[sz] = 0; return sz; }
static int response(int *cfd, unsigned char * cmd, size_t size) { int ret; if( ( ret = net_recv(cfd, cmd, size) ) != size ) { printf("\n FAILED net_recv (%d)", ret); return -1; } return 0; }
// Entry point int main(int argc, char *argv[]) { // Initialize SDL's subsystems - in this case, only video. if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); exit(1); } // Register SDL_Quit to be called at exit; makes sure things are // cleaned up when we quit. atexit(SDL_Quit); // Attempt to create a window with 32bit pixels. plane = SDL_SetVideoMode(SIZEX, SIZEY, BITSIZE, SDL_SWSURFACE); // If we fail, return error. if ( plane == NULL ) { fprintf(stderr, "Unable to set 896x640 video: %s\n", SDL_GetError()); exit(1); } net_init(); // Main loop: loop forever. while (1) { net_recv(); render(); // Poll for events, and handle the ones we care about. SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: break; case SDL_KEYUP: // If escape is pressed, return (and thus, quit) if (event.key.keysym.sym == SDLK_ESCAPE) return 0; break; case SDL_QUIT: return(0); } } } return 0; }
/* dbs_server_read_data * Data came in on a client socket, so read it into the buffer. Returns * false on failure, or when the client closes its half of the * connection. (EAGAIN doesn't count as a failure.) */ bool dbs_server_read_data(t_d2dbs_connection* conn) { int nBytes; nBytes = net_recv(conn->sd, conn->ReadBuf + conn->nCharsInReadBuffer, kBufferSize - conn->nCharsInReadBuffer); if (nBytes < 0) return false; conn->nCharsInReadBuffer += nBytes; return true; }
int ReadHandshake(int socket) { char data[4]; net_recv(socket, data, sizeof(data), 0); uint32_t received_handshake = ntohl(*(uint32_t*)&data[0]); if (received_handshake != handshake) return RET_FAIL; return RET_SUCCESS; }
/* dbs_server_read_data * Data came in on a client socket, so read it into the buffer. Returns * false on failure, or when the client closes its half of the * connection. (EAGAIN doesn't count as a failure.) */ BOOL dbs_server_read_data(t_d2dbs_connection* conn) { int nBytes; nBytes = net_recv(conn->sd, conn->ReadBuf + conn->nCharsInReadBuffer, kBufferSize - conn->nCharsInReadBuffer); if (nBytes < 0) return FALSE; conn->nCharsInReadBuffer += nBytes; return TRUE; }
int socket_receive(unsigned char *buff, unsigned int bytes) { int recv; memset(buff, 0, bytes); recv = net_recv(sock.sock, buff, bytes); if (recv != bytes) { printf("%d\t%d\n", recv, bytes); warning("%s: failed for TCP connection to host '%s' on port %d\n", __PRETTY_FUNCTION__, sock.host, sock.port, strerror(errno)); } return recv; }
void recv() { comBuf * buffer; while (1) { buffer = net_recv(LISTEN_PORT); mos_led_toggle (1); com_free_buf(buffer); printf("App:::\n"); } }
void attacker_send_hmac(havege_state *havege_state, void* socket) //@ requires attacker_invariant(?pub, ?pred, ?kc, havege_state, socket, ?attacker); //@ ensures attacker_invariant(pub, pred, kc, havege_state, socket, attacker); { int temp; int size1; int size2; char buffer1[MAX_MESSAGE_SIZE]; char buffer2[MAX_MESSAGE_SIZE]; char buffer3[64]; //@ open attacker_invariant(pub, pred, kc, havege_state, socket, attacker); size1 = net_recv(socket, buffer1, MAX_MESSAGE_SIZE); size2 = net_recv(socket, buffer2, MAX_MESSAGE_SIZE); if (size1 <= 0 || size2 < MINIMAL_STRING_SIZE) { //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker); return; } //@ interpret_symmetric_key(buffer1, size1); //@ assert cryptogram(buffer1, size1, ?ccs1, cg_symmetric_key(?p, ?c)); //@ assert chars(buffer2, size2, ?pay); //@ chars_to_crypto_chars(buffer2, size2); //@ public_cs(pay); //@ HASH_PUB_PAYLOAD(pay) sha512_hmac(buffer1, (unsigned int) size1, buffer2, (unsigned int) size2, buffer3, 0); //@ assert cryptogram(buffer3, 64, ?ccs_hmac, ?hmac); //@ assert is_public_hmac_is_public(?proof2, pub, pred); //@ crypto_chars_to_chars(buffer2, size2); //@ public_chars(buffer2, size2); //@ proof2(hmac); //@ public_cryptogram(buffer3, hmac); net_send(socket, buffer3, 64); //@ public_cryptogram(buffer1, cg_symmetric_key(p, c)); //@ close attacker_invariant(pub, pred, kc, havege_state, socket, attacker); }
int net_recv_flush(struct connection *c) { cyon_debug("net_recv_flush(%p)", c); while (!TAILQ_EMPTY(&(c->recv_queue)) && (c->flags & CONN_READ_POSSIBLE)) { if (!net_recv(c)) return (CYON_RESULT_ERROR); } return (CYON_RESULT_OK); }
void ReadStreamedDff(int socket, bool (*recv_callback)(void)) { int32_t n_size; net_recv(socket, &n_size, 4, 0); int32_t size = ntohl(n_size); printf("About to read %d bytes of dff data!", size); mkdir("sd:/dff", 0777); FILE* file = fopen("sd:/dff/test.dff", "wb"); // TODO: Change! if (file == NULL) { printf("Failed to open output file!\n"); } for (; size > 0; ) { char data[dff_stream_chunk_size]; ssize_t num_received = net_recv(socket, data, std::min(size,dff_stream_chunk_size), 0); if (num_received == -1) { printf("Error in recv!\n"); } else if (num_received > 0) { fwrite(data, num_received, 1, file); size -= num_received; } // printf("%d bytes left to be read!\n", size); if (recv_callback()) { printf ("Pressed Home button, aborting...\n"); break; } } printf ("Done reading :)\n"); fclose(file); }
int proxy_transfer(char mode, nethost_t* host, objlist_t* data, size_t datasize) { if(mode == MODE_RECV) { data->dataptr = pool_malloc(NULL, datasize); if(net_recv(host->s, (char*)data->dataptr, datasize) != NETOK) return PROXY_ERROR; } else { if(net_send(host->s, (char*)data->dataptr, datasize) != NETOK) return PROXY_ERROR; } return PROXY_OK; }
ssize_t net_con_peek(struct net_connection* con, void* buf, size_t len) { int ret = net_recv(con->sd, buf, len, MSG_PEEK); if (ret == -1) { if (is_blocked_or_interrupted()) return 0; return -net_error(); } else if (ret == 0) return -1; return ret; }
/* ---------------------------------------------------------------------- * Receive * ---------------------------------------------------------------------- */ int rx_task(void) { uint8_t buf[256]; int n; int i; for (;;) { n = net_recv(buf, 256); for (i = 0; i < n; ++i) { printf("%c", buf[i]); } } }
s32 network_request(s32 connect, const char * request, char * filename) { if(connect == NET_DEFAULT_SOCK) connect = connection; char buf[1024]; char *ptr = NULL; u32 cnt, size; s32 ret; /* Send request */ ret = net_send(connect, request, strlen(request), 0); if (ret < 0) return ret; /* Clear buffer */ memset(buf, 0, sizeof(buf)); /* Read HTTP header */ for (cnt = 0; !strstr(buf, "\r\n\r\n"); cnt++) if (net_recv(connect, buf + cnt, 1, 0) <= 0) return -1; /* HTTP request OK? */ if (!strstr(buf, "HTTP/1.1 200 OK")) return -1; if (filename) { /* Get filename */ ptr = strstr(buf, "filename=\""); if (ptr) { ptr += sizeof("filename=\"") - 1; for (cnt = 0; ptr[cnt] != '\r' && ptr[cnt] != '\n' && ptr[cnt] != '"'; cnt++) { filename[cnt] = ptr[cnt]; filename[cnt + 1] = '\0'; } } } /* Retrieve content size */ ptr = strstr(buf, "Content-Length:"); if (!ptr) return NET_SIZE_UNKNOWN; sscanf(ptr, "Content-Length: %u", &size); return size; }
s32 Network_Request(const char *filepath, u32 *len) { char buf[1024], request[256]; char *ptr = NULL; u32 cnt, length; s32 ret; /* Generate HTTP request */ sprintf( request, "GET " NusPath "%s HTTP/1.1\r\nHost: " NusHostname "\r\nConnection: close\r\n\r\n", filepath); /* Connect to server */ ret = Network_Connect(); if (ret < 0) return ret; /* Send request */ ret = net_send(sockfd, request, strlen(request), 0); if (ret < 0) return ret; /* Clear buffer */ memset(buf, 0, sizeof(buf)); /* Read HTTP header */ for (cnt = 0; !strstr(buf, "\r\n\r\n"); cnt++) { if (net_recv(sockfd, buf + cnt, 1, 0) <= 0) return -1; } /* HTTP 1.1 request OK? */ if (!strstr(buf, "HTTP/1.1 200 OK")) { /* HTTP 1.0 request OK? */ if (!strstr(buf, "HTTP/1.0 200 OK")) { return -1; } } /* Retrieve content size */ ptr = strstr(buf, "Content-Length:"); if (!ptr) return -1; sscanf(ptr, "Content-Length: %u", &length); /* Set length */ *len = length; return 0; }
/* * Test recv/send functions that make sure each try returns * WANT_READ/WANT_WRITE at least once before sucesseding */ static int my_recv( void *ctx, unsigned char *buf, size_t len ) { static int first_try = 1; int ret; if( first_try ) { first_try = 0; return( POLARSSL_ERR_NET_WANT_READ ); } ret = net_recv( ctx, buf, len ); if( ret != POLARSSL_ERR_NET_WANT_READ ) first_try = 1; /* Next call will be a new operation */ return( ret ); }