From 1eeb22ebc994b41bba67b52cb2451c778c667de6 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 13 Oct 2019 22:41:12 +0100 Subject: [PATCH] Handle open device packet. --- CMakeLists.txt | 2 +- device.c | 18 ++++++++++++++++++ dicmote.h | 3 ++- main.c | 25 +++++++++++++++++++++---- 4 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 device.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f91f3db..ff33e56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ project(dicremote C) set(CMAKE_C_STANDARD 90) -set(PLATFORM_SOURCES) +set(PLATFORM_SOURCES device.c) if("${CMAKE_SYSTEM}" MATCHES "Linux") list(APPEND PLATFORM_SOURCES linux/list_devices.c linux/linux.h) diff --git a/device.c b/device.c new file mode 100644 index 0000000..b135229 --- /dev/null +++ b/device.c @@ -0,0 +1,18 @@ +/* + * This file is part of the DiscImageChef Remote Server. + * Copyright (c) 2019 Natalia Portillo. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +int DeviceOpen(const char* devicePath) { return -1; } \ No newline at end of file diff --git a/dicmote.h b/dicmote.h index 796a60f..69269a7 100644 --- a/dicmote.h +++ b/dicmote.h @@ -94,7 +94,7 @@ typedef struct uint8_t reason_code; char spare[3]; char reason[256]; - int32_t errno; + int32_t errorNo; } DicPacketNop; typedef struct @@ -108,5 +108,6 @@ typedef struct DeviceInfoList* ListDevices(); void FreeDeviceInfoList(DeviceInfoList* start); uint16_t DeviceInfoListCount(DeviceInfoList* start); +int DeviceOpen(const char* devicePath); #endif diff --git a/main.c b/main.c index d6e8965..f8dd475 100644 --- a/main.c +++ b/main.c @@ -47,6 +47,8 @@ int main() int i; uint64_t n; DicPacketNop* pkt_nop; + DicPacketCmdOpen* pkt_dev_open; + int device_fd; printf("DiscImageChef Remote Server %s\n", DICMOTE_VERSION); printf("Copyright (C) 2019 Natalia Portillo\n"); @@ -388,12 +390,27 @@ int main() skip_next_hdr = 1; continue; case DICMOTE_PACKET_TYPE_COMMAND_OPEN_DEVICE: - pkt_nop->reason_code = DICMOTE_PACKET_NOP_REASON_NOT_IMPLEMENTED; + pkt_dev_open = malloc(pkt_hdr->len); + + if(!pkt_dev_open) + { + printf("Fatal error %d allocating memory for packet, closing connection...\n", errno); + free(pkt_hdr); + close(cli_sock); + continue; + } + + recv(cli_sock, pkt_dev_open, pkt_hdr->len, 0); + + device_fd = DeviceOpen(pkt_dev_open->device_path); + + pkt_nop->reason_code = + device_fd == -1 ? DICMOTE_PACKET_NOP_REASON_OPEN_ERROR : DICMOTE_PACKET_NOP_REASON_OPEN_OK; + pkt_nop->errorNo = errno; memset(&pkt_nop->reason, 0, 256); - strncpy(pkt_nop->reason, "Opening devices not yet implemented...", 256); write(cli_sock, pkt_nop, sizeof(DicPacketNop)); - printf("%s...\n", pkt_nop->reason); - skip_next_hdr = 1; + + free(pkt_dev_open); continue; default: pkt_nop->reason_code = DICMOTE_PACKET_NOP_REASON_NOT_RECOGNIZED;