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>
|
2014-06-11 23:09:28 +04:00
|
|
|
#include <winpr/stream.h>
|
2013-03-21 04:57:51 +04:00
|
|
|
#include <winpr/collections.h>
|
2012-10-03 05:52:27 +04:00
|
|
|
#include <winpr/interlocked.h>
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
#include <freerdp/types.h>
|
|
|
|
#include <freerdp/constants.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
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
HANDLE thread;
|
|
|
|
wMessageQueue* queue;
|
2011-10-11 11:16:05 +04:00
|
|
|
};
|
|
|
|
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
|
|
|
{
|
2013-01-16 04:14:03 +04:00
|
|
|
char* path = NULL;
|
2012-12-17 08:34:07 +04:00
|
|
|
int status;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 PathLength;
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 28);
|
2011-10-21 16:11:33 +04:00
|
|
|
/* DesiredAccess(4) AllocationSize(8), FileAttributes(4) */
|
|
|
|
/* SharedAccess(4) CreateDisposition(4), CreateOptions(4) */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, PathLength);
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
status = ConvertFromUnicode(CP_UTF8, 0, (WCHAR*) Stream_Pointer(irp->input),
|
2012-12-17 08:34:07 +04:00
|
|
|
PathLength / 2, &path, 0, NULL, NULL);
|
|
|
|
|
|
|
|
if (status < 1)
|
|
|
|
path = (char*) calloc(1, 1);
|
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;
|
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)
|
2014-06-11 23:09:28 +04:00
|
|
|
{
|
2011-10-21 16:11:33 +04:00
|
|
|
|
2014-06-11 23:09:28 +04:00
|
|
|
}
|
2011-10-21 16:11:33 +04:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, parallel->id);
|
|
|
|
Stream_Write_UINT8(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)
|
2014-06-11 23:09:28 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2011-10-21 16:11:33 +04:00
|
|
|
else
|
2014-06-11 23:09:28 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2011-10-21 16:11:33 +04:00
|
|
|
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Zero(irp->output, 5); /* Padding(5) */
|
2011-10-21 16:11:33 +04:00
|
|
|
|
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
|
|
|
|
2013-05-09 00:09:16 +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
|
|
|
|
2013-08-29 13:01:58 +04:00
|
|
|
status = read(parallel->file, buffer, 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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-11 23:09:28 +04:00
|
|
|
|
2011-10-11 19:43:54 +04:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, Length);
|
2013-03-21 04:57:51 +04:00
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
if (Length > 0)
|
|
|
|
{
|
2013-05-02 02:15:55 +04:00
|
|
|
Stream_EnsureRemainingCapacity(irp->output, Length);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write(irp->output, buffer, Length);
|
2011-10-21 16:11:33 +04:00
|
|
|
}
|
2013-03-21 04:57:51 +04:00
|
|
|
|
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)
|
|
|
|
{
|
2013-03-21 04:57:51 +04:00
|
|
|
UINT32 len;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT64 Offset;
|
2011-10-11 19:43:54 +04:00
|
|
|
ssize_t status;
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, Length);
|
|
|
|
Stream_Read_UINT64(irp->input, Offset);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 20); /* Padding */
|
2011-10-11 19:43:54 +04:00
|
|
|
|
2011-10-21 16:11:33 +04:00
|
|
|
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
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
status = write(parallel->file, Stream_Pointer(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;
|
|
|
|
break;
|
2011-10-11 19:43:54 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +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
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, Length);
|
|
|
|
Stream_Write_UINT8(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)
|
|
|
|
{
|
2013-05-09 00:09:16 +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)
|
|
|
|
{
|
|
|
|
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:
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
|
|
|
irp->Complete(irp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
static void* parallel_thread_func(void* arg)
|
2011-10-11 11:16:05 +04:00
|
|
|
{
|
|
|
|
IRP* irp;
|
2013-03-21 04:57:51 +04:00
|
|
|
wMessage message;
|
|
|
|
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) arg;
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2013-03-21 04:57:51 +04:00
|
|
|
if (!MessageQueue_Wait(parallel->queue))
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
if (!MessageQueue_Peek(parallel->queue, &message, TRUE))
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
if (message.id == WMQ_QUIT)
|
2011-10-11 11:16:05 +04:00
|
|
|
break;
|
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
irp = (IRP*) message.wParam;
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
parallel_process_irp(parallel, irp);
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parallel_irp_request(DEVICE* device, IRP* irp)
|
|
|
|
{
|
|
|
|
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) device;
|
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
MessageQueue_Post(parallel->queue, NULL, 0, (void*) irp, NULL);
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void parallel_free(DEVICE* device)
|
|
|
|
{
|
|
|
|
PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) device;
|
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
MessageQueue_PostQuit(parallel->queue, 0);
|
|
|
|
WaitForSingleObject(parallel->thread, INFINITE);
|
2014-03-26 19:05:12 +04:00
|
|
|
CloseHandle(parallel->thread);
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2013-08-13 18:04:19 +04:00
|
|
|
Stream_Free(parallel->device.data, TRUE);
|
2013-03-21 04:57:51 +04:00
|
|
|
MessageQueue_Free(parallel->queue);
|
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;
|
2012-11-09 04:01:52 +04:00
|
|
|
RDPDR_PARALLEL* device;
|
2011-10-11 11:16:05 +04:00
|
|
|
PARALLEL_DEVICE* parallel;
|
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
device = (RDPDR_PARALLEL*) pEntryPoints->device;
|
|
|
|
name = device->Name;
|
|
|
|
path = device->Path;
|
2011-10-11 11:16:05 +04:00
|
|
|
|
2014-03-26 19:05:12 +04:00
|
|
|
if (!name || (name[0] == '*'))
|
|
|
|
{
|
|
|
|
/* TODO: implement auto detection of parallel ports */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-11 11:16:05 +04:00
|
|
|
if (name[0] && path[0])
|
|
|
|
{
|
2014-03-26 19:05:12 +04:00
|
|
|
parallel = (PARALLEL_DEVICE*) calloc(1, sizeof(PARALLEL_DEVICE));
|
|
|
|
|
|
|
|
if (!parallel)
|
|
|
|
return -1;
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
parallel->device.type = RDPDR_DTYP_PARALLEL;
|
|
|
|
parallel->device.name = name;
|
|
|
|
parallel->device.IRPRequest = parallel_irp_request;
|
|
|
|
parallel->device.Free = parallel_free;
|
|
|
|
|
|
|
|
length = strlen(name);
|
2013-05-09 01:48:30 +04:00
|
|
|
parallel->device.data = Stream_New(NULL, length + 1);
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
for (i = 0; i <= length; i++)
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(parallel->device.data, name[i] < 0 ? '_' : name[i]);
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
parallel->path = path;
|
|
|
|
|
2013-11-12 18:18:35 +04:00
|
|
|
parallel->queue = MessageQueue_New(NULL);
|
2011-10-11 11:16:05 +04:00
|
|
|
|
|
|
|
pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*) parallel);
|
|
|
|
|
2013-03-21 04:57:51 +04:00
|
|
|
parallel->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) parallel_thread_func, (void*) parallel, 0, NULL);
|
2011-10-11 11:16:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|