void pack_usb_device(int pack, struct usb_device *udev) { pack_uint32_t(pack, &udev->busnum); pack_uint32_t(pack, &udev->devnum); pack_uint32_t(pack, &udev->speed ); pack_uint16_t(pack, &udev->idVendor ); pack_uint16_t(pack, &udev->idProduct); pack_uint16_t(pack, &udev->bcdDevice); }
/* depending upon the connection type, assemble a service_reply_pkt and send it to the other side */ int send_service_reply_pkt(service_reply_pkt *srp, FDContext *fdc) { /* This will be the service_reply_pkt */ char netpkt[SREP_PKTSIZE_MAX]; /* The fields of the packet */ uint16_t req_status; struct in_addr server_addr; uint16_t port; uint32_t num_files_32 = 0; uint64_t num_files_64 = 0; char capacity_free_ACD[MAX_ASCII_CODED_DECIMAL_LENGTH]; int ret = -1; ASSERT(fdc->type != FDC_UNKNOWN); memset(netpkt, 0, SREP_PKTSIZE_MAX); /* get the right sized quantities I need depending upon what the client needs. Ensure to convert it to network byte order here too. */ req_status = srp->req_status; req_status = host_uint16_t_order_to_network_uint16_t_order(req_status); server_addr = srp->server_addr; server_addr.s_addr = host_uint32_t_order_to_network_uint32_t_order(server_addr.s_addr); port = srp->port; port = host_uint16_t_order_to_network_uint16_t_order(port); switch(fdc->type) { case FDC_32: num_files_32 = srp->num_files; num_files_32 = host_uint32_t_order_to_network_uint32_t_order(num_files_32); break; case FDC_64: num_files_64 = srp->num_files; num_files_64 = host_uint64_t_order_to_network_uint64_t_order(num_files_64); break; default: Server::Log("service_reply_pkt type conversion error!"); return NET_WRITE_FAIL; break; } memmove(capacity_free_ACD, srp->capacity_free_ACD, MAX_ASCII_CODED_DECIMAL_LENGTH); /* Assemble the packet according to what type of connection it is. Then send it. */ switch(fdc->type) { case FDC_32: pack_uint16_t(netpkt, SREP32_req_status, req_status); pack_in_addr(netpkt, SREP32_server_addr, server_addr); pack_uint16_t(netpkt, SREP32_port, port); pack_uint32_t(netpkt, SREP32_num_files, num_files_32); pack_char_array(netpkt, SREP32_capacity_free_ACD, capacity_free_ACD, MAX_ASCII_CODED_DECIMAL_LENGTH); ret = net_write(fdc->fd, netpkt, SREP_PKTSIZE_32); break; case FDC_64: pack_uint16_t(netpkt, SREP64_req_status, req_status); pack_in_addr(netpkt, SREP64_server_addr, server_addr); pack_uint16_t(netpkt, SREP64_port, port); pack_uint64_t(netpkt, SREP64_num_files, num_files_64); pack_char_array(netpkt, SREP64_capacity_free_ACD, capacity_free_ACD, MAX_ASCII_CODED_DECIMAL_LENGTH); ret = net_write(fdc->fd, netpkt, SREP_PKTSIZE_64); break; default: Server::Log("service_reply_pkt type packing error!"); return NET_WRITE_FAIL; break; } if (ret < 0) { return NET_WRITE_FAIL; } return NET_WRITE_OK; }
/* depending upon the connection type, assemble a store_reply_pkt and send it to the other side */ int send_restore_reply_pkt(restore_reply_pkt *rstrp, FDContext *fdc) { /* This will be the service_reply_pkt */ char netpkt[RSTREP_PKTSIZE_MAX]; /* The fields of the packet */ struct in_addr server_name; uint16_t port; uint32_t file_size_32 = 0 ; uint64_t file_size_64 = 0 ; uint16_t req_status; int ret = -1; ASSERT(fdc->type != FDC_UNKNOWN); memset(netpkt, 0, RSTREP_PKTSIZE_MAX); /* get the right sized quantities I need depending upon what the client needs. Ensure to convert it to network byte order here too. */ server_name = rstrp->server_name; server_name.s_addr = host_uint32_t_order_to_network_uint32_t_order(server_name.s_addr); port = rstrp->port; port = host_uint16_t_order_to_network_uint16_t_order(port); switch(fdc->type) { case FDC_32: file_size_32 = rstrp->file_size; /* If there is the possibility of a type narrowing, then check to see if bits were actually lost. */ if ((sizeof(file_size_32) < sizeof(rstrp->file_size)) && (uint64_t)file_size_32 != (uint64_t)rstrp->file_size) { Server::Log("restore_reply_pkt type narrowed from 64 to 32 " "bits with known loss of data and the ckpt restore will " "likely fail."); return NET_WRITE_FAIL; } file_size_32 = host_uint32_t_order_to_network_uint32_t_order(file_size_32); break; case FDC_64: file_size_64 = rstrp->file_size; file_size_64 = host_uint64_t_order_to_network_uint64_t_order(file_size_64); break; default: Server::Log("restore_reply_pkt type conversion error!"); return NET_WRITE_FAIL; break; } req_status = rstrp->req_status; req_status = host_uint16_t_order_to_network_uint16_t_order(req_status); /* Assemble the packet according to what type of connection it is. Then send it. */ switch(fdc->type) { case FDC_32: pack_in_addr(netpkt, RSTREP32_server_name, server_name); pack_uint16_t(netpkt, RSTREP32_port, port); pack_uint32_t(netpkt, RSTREP32_file_size, file_size_32); pack_uint16_t(netpkt, RSTREP32_req_status, req_status); ret = net_write(fdc->fd, netpkt, RSTREP_PKTSIZE_32); break; case FDC_64: pack_in_addr(netpkt, RSTREP64_server_name, server_name); pack_uint16_t(netpkt, RSTREP64_port, port); pack_uint64_t(netpkt, RSTREP64_file_size, file_size_64); pack_uint16_t(netpkt, RSTREP64_req_status, req_status); ret = net_write(fdc->fd, netpkt, RSTREP_PKTSIZE_64); break; default: Server::Log("restore_reply_pkt type packing error!"); return NET_WRITE_FAIL; break; } if (ret < 0) { return NET_WRITE_FAIL; } return NET_WRITE_OK; }