2011-10-11 11:16:05 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-10-11 11:16:05 +04:00
|
|
|
* Redirected Parallel Port Device Service
|
|
|
|
*
|
|
|
|
* Copyright 2010 O.S. Systems Software Ltda.
|
|
|
|
* Copyright 2010 Eduardo Fiss Beloni <beloni@ossystems.com.br>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-10-21 16:11:33 +04:00
|
|
|
|
2012-07-29 06:24:14 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <unistd.h>
|
2012-07-29 06:24:14 +04:00
|
|
|
#endif
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2012-07-29 06:24:14 +04:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <termios.h>
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <strings.h>
|
|
|
|
#include <sys/ioctl.h>
|
2012-07-29 06:24:14 +04:00
|
|
|
#endif
|
|
|
|
|
2011-10-24 22:08:39 +04:00
|
|
|
#ifdef __LINUX__
|
2011-10-21 16:11:33 +04:00
|
|
|
#include <linux/ppdev.h>
|
|
|
|
#include <linux/parport.h>
|
2011-10-24 22:08:39 +04:00
|
|
|
#endif
|
2011-10-21 16:11:33 +04:00
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
|
|
|
#include <winpr/interlocked.h>
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <freerdp/types.h>
|
|
|
|
#include <freerdp/constants.h>
|
|
|
|
#include <freerdp/utils/list.h>
|
|
|
|
#include <freerdp/utils/thread.h>
|
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
#include <freerdp/utils/stream.h>
|
2012-09-24 02:41:07 +04:00
|
|
|
#include <freerdp/utils/unicode.h>
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <freerdp/utils/svc_plugin.h>
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
struct _PARALLEL_DEVICE
|
|
|
|
{
|
|
|
|
DEVICE device;
|
|
|
|
|
2011-10-11 19:43:54 +04:00
|
|
|
int file;
|
2011-10-11 11:16:05 +04:00
|
|
|
char* path;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 id;
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
PSLIST_HEADER pIrpList;
|
2011-10-11 11:16:05 +04:00
|
|
|
freerdp_thread* thread;
|
|
|
|
};
|
|
|
|
typedef struct _PARALLEL_DEVICE PARALLEL_DEVICE;
|
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
static void parallel_process_irp_create(PARALLEL_DEVICE* parallel, IRP* irp)
|
2011-10-11 19:43:54 +04:00
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
char* path;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 PathLength;
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
stream_seek(irp->input, 28);
|
|
|
|
/* DesiredAccess(4) AllocationSize(8), FileAttributes(4) */
|
|
|
|
/* SharedAccess(4) CreateDisposition(4), CreateOptions(4) */
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_read_UINT32(irp->input, PathLength);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2012-09-24 04:11:50 +04:00
|
|
|
freerdp_UnicodeToAsciiAlloc((WCHAR*) stream_get_tail(irp->input), &path, PathLength / 2);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
parallel->id = irp->devman->id_sequence++;
|
2011-10-11 19:43:54 +04:00
|
|
|
parallel->file = open(parallel->path, O_RDWR);
|
2012-07-29 06:24:14 +04:00
|
|
|
|
2011-10-11 19:43:54 +04:00
|
|
|
if (parallel->file < 0)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_ACCESS_DENIED;
|
2011-10-21 16:11:33 +04:00
|
|
|
parallel->id = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("failed to create %s: %s", parallel->path, strerror(errno));
|
2011-10-11 19:43:54 +04:00
|
|
|
}
|
2011-10-21 16:11:33 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* all read and write operations should be non-blocking */
|
|
|
|
if (fcntl(parallel->file, F_SETFL, O_NONBLOCK) == -1)
|
|
|
|
DEBUG_WARN("%s fcntl %s", path, strerror(errno));
|
|
|
|
|
|
|
|
DEBUG_SVC("%s(%d) created", parallel->path, parallel->file);
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_write_UINT32(irp->output, parallel->id);
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_write_BYTE(irp->output, 0);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(path);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parallel_process_irp_close(PARALLEL_DEVICE* parallel, IRP* irp)
|
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
if (close(parallel->file) < 0)
|
|
|
|
DEBUG_SVC("failed to close %s(%d)", parallel->path, parallel->id);
|
|
|
|
else
|
|
|
|
DEBUG_SVC("%s(%d) closed", parallel->path, parallel->id);
|
|
|
|
|
|
|
|
stream_write_zero(irp->output, 5); /* Padding(5) */
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parallel_process_irp_read(PARALLEL_DEVICE* parallel, IRP* irp)
|
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT64 Offset;
|
2011-10-11 19:43:54 +04:00
|
|
|
ssize_t status;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* buffer = NULL;
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_read_UINT32(irp->input, Length);
|
|
|
|
stream_read_UINT64(irp->input, Offset);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
buffer = (BYTE*) malloc(Length);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
status = read(parallel->file, irp->output->p, Length);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
2012-10-09 07:21:26 +04:00
|
|
|
free(buffer);
|
2011-10-21 16:11:33 +04:00
|
|
|
buffer = NULL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("read %s(%d) failed", parallel->path, parallel->id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUG_SVC("read %llu-%llu from %d", Offset, Offset + Length, parallel->id);
|
2011-10-11 19:43:54 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_write_UINT32(irp->output, Length);
|
2011-10-21 16:11:33 +04:00
|
|
|
if (Length > 0)
|
|
|
|
{
|
|
|
|
stream_check_size(irp->output, Length);
|
|
|
|
stream_write(irp->output, buffer, Length);
|
|
|
|
}
|
2012-10-09 07:21:26 +04:00
|
|
|
free(buffer);
|
2011-10-21 16:11:33 +04:00
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parallel_process_irp_write(PARALLEL_DEVICE* parallel, IRP* irp)
|
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT64 Offset;
|
2011-10-11 19:43:54 +04:00
|
|
|
ssize_t status;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 len;
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_read_UINT32(irp->input, Length);
|
|
|
|
stream_read_UINT64(irp->input, Offset);
|
2011-10-11 19:43:54 +04:00
|
|
|
stream_seek(irp->input, 20); /* Padding */
|
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
DEBUG_SVC("Length %u Offset %llu", Length, Offset);
|
|
|
|
|
|
|
|
len = Length;
|
2012-07-29 06:24:14 +04:00
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
while (len > 0)
|
2011-10-11 19:43:54 +04:00
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
status = write(parallel->file, stream_get_tail(irp->input), len);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("write %s(%d) failed.", parallel->path, parallel->id);
|
|
|
|
break;
|
2011-10-11 19:43:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
stream_seek(irp->input, status);
|
2011-10-21 16:11:33 +04:00
|
|
|
len -= status;
|
2011-10-11 19:43:54 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_write_UINT32(irp->output, Length);
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_write_BYTE(irp->output, 0); /* Padding */
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parallel_process_irp_device_control(PARALLEL_DEVICE* parallel, IRP* irp)
|
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
DEBUG_SVC("in");
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_write_UINT32(irp->output, 0); /* OutputBufferLength */
|
2011-10-11 11:16:05 +04:00
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parallel_process_irp(PARALLEL_DEVICE* parallel, IRP* irp)
|
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
DEBUG_SVC("MajorFunction %u", irp->MajorFunction);
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
switch (irp->MajorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MJ_CREATE:
|
|
|
|
parallel_process_irp_create(parallel, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_CLOSE:
|
|
|
|
parallel_process_irp_close(parallel, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_READ:
|
|
|
|
parallel_process_irp_read(parallel, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_WRITE:
|
|
|
|
parallel_process_irp_write(parallel, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_DEVICE_CONTROL:
|
|
|
|
parallel_process_irp_device_control(parallel, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_WARN("MajorFunction 0x%X not supported", irp->MajorFunction);
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
|
|
|
irp->Complete(irp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parallel_process_irp_list(PARALLEL_DEVICE* parallel)
|
|
|
|
{
|
|
|
|
IRP* irp;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (freerdp_thread_is_stopped(parallel->thread))
|
|
|
|
break;
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
irp = (IRP*) InterlockedPopEntrySList(parallel->pIrpList);
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
if (irp == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
parallel_process_irp(parallel, irp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* parallel_thread_func(void* arg)
|
|
|
|
{
|
|
|
|
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) arg;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
freerdp_thread_wait(parallel->thread);
|
|
|
|
|
|
|
|
if (freerdp_thread_is_stopped(parallel->thread))
|
|
|
|
break;
|
|
|
|
|
|
|
|
freerdp_thread_reset(parallel->thread);
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
parallel_process_irp_list(parallel);
|
|
|
|
}
|
|
|
|
|
|
|
|
freerdp_thread_quit(parallel->thread);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parallel_irp_request(DEVICE* device, IRP* irp)
|
|
|
|
{
|
|
|
|
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) device;
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
InterlockedPushEntrySList(parallel->pIrpList, &(irp->ItemEntry));
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
freerdp_thread_signal(parallel->thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parallel_free(DEVICE* device)
|
|
|
|
{
|
|
|
|
IRP* irp;
|
|
|
|
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) device;
|
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
DEBUG_SVC("freeing device");
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
freerdp_thread_stop(parallel->thread);
|
|
|
|
freerdp_thread_free(parallel->thread);
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
while ((irp = (IRP*) InterlockedPopEntrySList(parallel->pIrpList)) != NULL)
|
2011-10-11 11:16:05 +04:00
|
|
|
irp->Discard(irp);
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
_aligned_free(parallel->pIrpList);
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(parallel);
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
2012-10-14 10:38:58 +04:00
|
|
|
#ifdef STATIC_CHANNELS
|
|
|
|
#define DeviceServiceEntry parallel_DeviceServiceEntry
|
|
|
|
#endif
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
int DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
|
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
char* path;
|
|
|
|
int i, length;
|
|
|
|
PARALLEL_DEVICE* parallel;
|
|
|
|
|
|
|
|
name = (char*) pEntryPoints->plugin_data->data[1];
|
|
|
|
path = (char*) pEntryPoints->plugin_data->data[2];
|
|
|
|
|
|
|
|
if (name[0] && path[0])
|
|
|
|
{
|
|
|
|
parallel = xnew(PARALLEL_DEVICE);
|
|
|
|
|
|
|
|
parallel->device.type = RDPDR_DTYP_PARALLEL;
|
|
|
|
parallel->device.name = name;
|
|
|
|
parallel->device.IRPRequest = parallel_irp_request;
|
|
|
|
parallel->device.Free = parallel_free;
|
|
|
|
|
|
|
|
length = strlen(name);
|
|
|
|
parallel->device.data = stream_new(length + 1);
|
|
|
|
|
|
|
|
for (i = 0; i <= length; i++)
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_write_BYTE(parallel->device.data, name[i] < 0 ? '_' : name[i]);
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
parallel->path = path;
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
parallel->pIrpList = (PSLIST_HEADER) _aligned_malloc(sizeof(SLIST_HEADER), MEMORY_ALLOCATION_ALIGNMENT);
|
|
|
|
InitializeSListHead(parallel->pIrpList);
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
parallel->thread = freerdp_thread_new();
|
|
|
|
|
|
|
|
pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*) parallel);
|
|
|
|
|
|
|
|
freerdp_thread_start(parallel->thread, parallel_thread_func, parallel);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|