94 lines
2.9 KiB
C
94 lines
2.9 KiB
C
/* *****************************************************************************
|
|
* usage/mcs/request.c -- request a file from the main memory.
|
|
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
*
|
|
* This file is part of libp7.
|
|
* libp7 is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation; either version 3.0 of the License,
|
|
* or (at your option) any later version.
|
|
*
|
|
* libp7 is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with libp7; if not, see <http://www.gnu.org/licenses/>.
|
|
* ************************************************************************** */
|
|
#include <libp7/internals.h>
|
|
|
|
/**
|
|
* p7_mcs_request:
|
|
* Requests a file.
|
|
*
|
|
* @arg handle the libp7 handle.
|
|
* @arg file the file handle to create.
|
|
* @arg reqhead the request head.
|
|
* @arg disp the progression display callback.
|
|
* @return the libp7 error (0 if ok)
|
|
*/
|
|
|
|
int p7_mcs_request(p7_handle_t *handle, g1m_mcsfile_t **file,
|
|
g1m_mcshead_t *reqhead, p7_disp_t disp)
|
|
{
|
|
int err;
|
|
|
|
/* make checks */
|
|
chk_handle(handle);
|
|
chk_active(handle);
|
|
chk_head(reqhead);
|
|
|
|
/* send command */
|
|
log_info("sending file transfer request");
|
|
if ((err = p7_send_cmdmcs_reqfile(handle, reqhead))) {
|
|
log_fatal("couldn't send file transfer request/didn't receive answer");
|
|
return (err);
|
|
} if (response.type == p7_seven_type_nak
|
|
&& response.code == p7_seven_err_other) {
|
|
log_fatal("requested file didn't exist");
|
|
return (p7_error_notfound);
|
|
} else if (response.type != p7_seven_type_ack) {
|
|
log_fatal("didn't receive ack or known error...");
|
|
return (p7_error_unknown);
|
|
}
|
|
|
|
/* swap roles */
|
|
log_info("sending roleswap");
|
|
if ((err = p7_seven_send_swp(handle))) {
|
|
log_fatal("couldn't swap roles");
|
|
return (err);
|
|
} else if (response.type != p7_seven_type_cmd || response.code != 0x25) {
|
|
log_fatal("didn't receive expected command");
|
|
return (p7_error_unknown);
|
|
}
|
|
|
|
/* save the head */
|
|
g1m_mcshead_t head;
|
|
g1m_decode_mcsfile_head(&head, response.mcstype,
|
|
(unsigned char*)response.args[2], (unsigned char*)response.args[0],
|
|
(unsigned char*)response.args[1], response.filesize);
|
|
|
|
/* get data */
|
|
unsigned char *tmp = malloc(head.size);
|
|
if (!tmp) return (p7_error_alloc);
|
|
if ((err = p7_seven_get_data(handle, tmp, head.size, 0, disp)))
|
|
goto fail;
|
|
|
|
/* check if last answer was roleswap */
|
|
if (response.type != p7_seven_type_swp) {
|
|
err = p7_error_unknown;
|
|
goto fail;
|
|
}
|
|
|
|
/* decode */
|
|
if (g1m_decode_mcsfile_data(file, &head, tmp, head.size)) {
|
|
err = p7_error_unknown;
|
|
goto fail;
|
|
}
|
|
|
|
fail:
|
|
free(tmp);
|
|
return (err);
|
|
}
|