2011-08-17 17:57:09 +04:00
|
|
|
/**
|
2012-10-09 05:00:07 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-17 17:57:09 +04:00
|
|
|
* Print Virtual Channel
|
|
|
|
*
|
|
|
|
* Copyright 2010-2011 Vic Lee
|
|
|
|
*
|
|
|
|
* 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
|
2011-08-17 17:57:09 +04:00
|
|
|
#include "config.h"
|
2012-08-15 01:09:01 +04:00
|
|
|
#endif
|
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +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-08-17 17:57:09 +04:00
|
|
|
#include <freerdp/utils/stream.h>
|
|
|
|
#include <freerdp/utils/unicode.h>
|
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
#include <freerdp/utils/thread.h>
|
|
|
|
#include <freerdp/utils/svc_plugin.h>
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2012-01-23 17:19:40 +04:00
|
|
|
#ifdef WITH_CUPS
|
2011-08-17 17:57:09 +04:00
|
|
|
#include "printer_cups.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "printer_main.h"
|
|
|
|
|
2012-10-02 11:10:21 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
#include "printer_win.h"
|
|
|
|
#endif
|
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
typedef struct _PRINTER_DEVICE PRINTER_DEVICE;
|
|
|
|
struct _PRINTER_DEVICE
|
|
|
|
{
|
|
|
|
DEVICE device;
|
|
|
|
|
|
|
|
rdpPrinter* printer;
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
PSLIST_HEADER pIrpList;
|
2011-08-17 17:57:09 +04:00
|
|
|
freerdp_thread* thread;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void printer_process_irp_create(PRINTER_DEVICE* printer_dev, IRP* irp)
|
|
|
|
{
|
|
|
|
rdpPrintJob* printjob = NULL;
|
|
|
|
|
|
|
|
if (printer_dev->printer != NULL)
|
|
|
|
printjob = printer_dev->printer->CreatePrintJob(printer_dev->printer, irp->devman->id_sequence++);
|
|
|
|
|
|
|
|
if (printjob != NULL)
|
|
|
|
{
|
|
|
|
stream_write_uint32(irp->output, printjob->id); /* FileId */
|
|
|
|
|
|
|
|
DEBUG_SVC("printjob id: %d", printjob->id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stream_write_uint32(irp->output, 0); /* FileId */
|
|
|
|
irp->IoStatus = STATUS_PRINT_QUEUE_FULL;
|
|
|
|
|
|
|
|
DEBUG_WARN("error creating print job.");
|
|
|
|
}
|
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printer_process_irp_close(PRINTER_DEVICE* printer_dev, IRP* irp)
|
|
|
|
{
|
|
|
|
rdpPrintJob* printjob = NULL;
|
|
|
|
|
|
|
|
if (printer_dev->printer != NULL)
|
|
|
|
printjob = printer_dev->printer->FindPrintJob(printer_dev->printer, irp->FileId);
|
|
|
|
|
|
|
|
if (printjob == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
|
|
|
|
DEBUG_WARN("printjob id %d not found.", irp->FileId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printjob->Close(printjob);
|
|
|
|
|
|
|
|
DEBUG_SVC("printjob id %d closed.", irp->FileId);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_write_zero(irp->output, 4); /* Padding(4) */
|
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printer_process_irp_write(PRINTER_DEVICE* printer_dev, IRP* irp)
|
|
|
|
{
|
|
|
|
uint32 Length;
|
|
|
|
uint64 Offset;
|
2012-10-03 05:52:27 +04:00
|
|
|
rdpPrintJob* printjob = NULL;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
stream_read_uint32(irp->input, Length);
|
|
|
|
stream_read_uint64(irp->input, Offset);
|
|
|
|
stream_seek(irp->input, 20); /* Padding */
|
|
|
|
|
|
|
|
if (printer_dev->printer != NULL)
|
|
|
|
printjob = printer_dev->printer->FindPrintJob(printer_dev->printer, irp->FileId);
|
|
|
|
|
|
|
|
if (printjob == NULL)
|
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
|
|
|
|
DEBUG_WARN("printjob id %d not found.", irp->FileId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printjob->Write(printjob, stream_get_tail(irp->input), Length);
|
|
|
|
|
|
|
|
DEBUG_SVC("printjob id %d written %d bytes.", irp->FileId, Length);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_write_uint32(irp->output, Length);
|
|
|
|
stream_write_uint8(irp->output, 0); /* Padding */
|
|
|
|
|
|
|
|
irp->Complete(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printer_process_irp(PRINTER_DEVICE* printer_dev, IRP* irp)
|
|
|
|
{
|
|
|
|
switch (irp->MajorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MJ_CREATE:
|
|
|
|
printer_process_irp_create(printer_dev, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_CLOSE:
|
|
|
|
printer_process_irp_close(printer_dev, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_WRITE:
|
|
|
|
printer_process_irp_write(printer_dev, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_WARN("MajorFunction 0x%X not supported", irp->MajorFunction);
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
|
|
|
irp->Complete(irp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printer_process_irp_list(PRINTER_DEVICE* printer_dev)
|
|
|
|
{
|
|
|
|
IRP* irp;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (freerdp_thread_is_stopped(printer_dev->thread))
|
|
|
|
break;
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
irp = (IRP*) InterlockedPopEntrySList(printer_dev->pIrpList);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
if (irp == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
printer_process_irp(printer_dev, irp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* printer_thread_func(void* arg)
|
|
|
|
{
|
|
|
|
PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*)arg;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
freerdp_thread_wait(printer_dev->thread);
|
|
|
|
|
|
|
|
if (freerdp_thread_is_stopped(printer_dev->thread))
|
|
|
|
break;
|
|
|
|
|
|
|
|
freerdp_thread_reset(printer_dev->thread);
|
|
|
|
printer_process_irp_list(printer_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
freerdp_thread_quit(printer_dev->thread);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printer_irp_request(DEVICE* device, IRP* irp)
|
|
|
|
{
|
2012-10-03 05:52:27 +04:00
|
|
|
PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*) device;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
InterlockedPushEntrySList(printer_dev->pIrpList, &(irp->ItemEntry));
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
freerdp_thread_signal(printer_dev->thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printer_free(DEVICE* device)
|
|
|
|
{
|
|
|
|
IRP* irp;
|
2012-10-03 05:52:27 +04:00
|
|
|
PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*) device;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
freerdp_thread_stop(printer_dev->thread);
|
2011-09-19 18:52:16 +04:00
|
|
|
freerdp_thread_free(printer_dev->thread);
|
2012-10-03 05:52:27 +04:00
|
|
|
|
|
|
|
while ((irp = (IRP*) InterlockedPopEntrySList(printer_dev->pIrpList)) != NULL)
|
2011-08-17 17:57:09 +04:00
|
|
|
irp->Discard(irp);
|
2012-10-03 05:52:27 +04:00
|
|
|
|
|
|
|
_aligned_free(printer_dev->pIrpList);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
if (printer_dev->printer)
|
|
|
|
printer_dev->printer->Free(printer_dev->printer);
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(printer_dev->device.name);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(printer_dev);
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void printer_register(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints, rdpPrinter* printer)
|
|
|
|
{
|
|
|
|
char* port;
|
|
|
|
uint32 Flags;
|
2012-09-24 03:49:13 +04:00
|
|
|
int DriverNameLen;
|
|
|
|
WCHAR* DriverName;
|
|
|
|
int PrintNameLen;
|
|
|
|
WCHAR* PrintName;
|
2011-08-17 17:57:09 +04:00
|
|
|
uint32 CachedFieldsLen;
|
|
|
|
uint8* CachedPrinterConfigData;
|
2012-09-24 02:41:07 +04:00
|
|
|
PRINTER_DEVICE* printer_dev;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
port = malloc(10);
|
2011-08-17 17:57:09 +04:00
|
|
|
snprintf(port, 10, "PRN%d", printer->id);
|
|
|
|
|
|
|
|
printer_dev = xnew(PRINTER_DEVICE);
|
|
|
|
|
|
|
|
printer_dev->device.type = RDPDR_DTYP_PRINT;
|
|
|
|
printer_dev->device.name = port;
|
|
|
|
printer_dev->device.IRPRequest = printer_irp_request;
|
|
|
|
printer_dev->device.Free = printer_free;
|
|
|
|
|
|
|
|
printer_dev->printer = printer;
|
|
|
|
|
|
|
|
CachedFieldsLen = 0;
|
|
|
|
CachedPrinterConfigData = NULL;
|
|
|
|
|
2011-08-18 06:21:00 +04:00
|
|
|
DEBUG_SVC("Printer %s registered", printer->name);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
Flags = 0;
|
2012-09-24 03:49:13 +04:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
if (printer->is_default)
|
|
|
|
Flags |= RDPDR_PRINTER_ANNOUNCE_FLAG_DEFAULTPRINTER;
|
|
|
|
|
2012-09-24 03:49:13 +04:00
|
|
|
DriverNameLen = freerdp_AsciiToUnicodeAlloc(printer->driver, &DriverName, 0) * 2;
|
|
|
|
PrintNameLen = freerdp_AsciiToUnicodeAlloc(printer->name, &PrintName, 0) * 2;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
printer_dev->device.data = stream_new(28 + DriverNameLen + PrintNameLen + CachedFieldsLen);
|
|
|
|
|
|
|
|
stream_write_uint32(printer_dev->device.data, Flags);
|
|
|
|
stream_write_uint32(printer_dev->device.data, 0); /* CodePage, reserved */
|
|
|
|
stream_write_uint32(printer_dev->device.data, 0); /* PnPNameLen */
|
|
|
|
stream_write_uint32(printer_dev->device.data, DriverNameLen + 2);
|
|
|
|
stream_write_uint32(printer_dev->device.data, PrintNameLen + 2);
|
|
|
|
stream_write_uint32(printer_dev->device.data, CachedFieldsLen);
|
|
|
|
stream_write(printer_dev->device.data, DriverName, DriverNameLen);
|
|
|
|
stream_write_uint16(printer_dev->device.data, 0);
|
|
|
|
stream_write(printer_dev->device.data, PrintName, PrintNameLen);
|
|
|
|
stream_write_uint16(printer_dev->device.data, 0);
|
2012-09-24 03:49:13 +04:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
if (CachedFieldsLen > 0)
|
|
|
|
{
|
|
|
|
stream_write(printer_dev->device.data, CachedPrinterConfigData, CachedFieldsLen);
|
|
|
|
}
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(DriverName);
|
|
|
|
free(PrintName);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
printer_dev->pIrpList = (PSLIST_HEADER) _aligned_malloc(sizeof(SLIST_HEADER), MEMORY_ALLOCATION_ALIGNMENT);
|
|
|
|
InitializeSListHead(printer_dev->pIrpList);
|
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
printer_dev->thread = freerdp_thread_new();
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*) printer_dev);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
freerdp_thread_start(printer_dev->thread, printer_thread_func, printer_dev);
|
|
|
|
}
|
|
|
|
|
2012-10-09 06:53:05 +04:00
|
|
|
#ifdef STATIC_CHANNELS
|
2012-10-02 11:10:21 +04:00
|
|
|
int printer_entry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
|
|
|
|
#else
|
2011-08-17 17:57:09 +04:00
|
|
|
int DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
|
2012-10-02 11:10:21 +04:00
|
|
|
#endif
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char* name;
|
|
|
|
char* driver_name;
|
2012-10-03 05:52:27 +04:00
|
|
|
rdpPrinter* printer;
|
|
|
|
rdpPrinter** printers;
|
|
|
|
rdpPrinterDriver* driver = NULL;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2012-01-23 17:19:40 +04:00
|
|
|
#ifdef WITH_CUPS
|
2011-08-17 17:57:09 +04:00
|
|
|
driver = printer_cups_get_driver();
|
2012-10-02 11:10:21 +04:00
|
|
|
#endif
|
|
|
|
#ifdef WIN32
|
|
|
|
driver = printer_win_get_driver();
|
2011-08-17 17:57:09 +04:00
|
|
|
#endif
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
if (driver == NULL)
|
|
|
|
{
|
2012-10-03 05:52:27 +04:00
|
|
|
DEBUG_WARN("no driver");
|
2011-08-17 17:57:09 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
name = (char*) pEntryPoints->plugin_data->data[1];
|
|
|
|
driver_name = (char*) pEntryPoints->plugin_data->data[2];
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
if (name && name[0])
|
|
|
|
{
|
|
|
|
printer = driver->GetPrinter(driver, name);
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
if (printer == NULL)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("printer %s not found.", name);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
if (driver_name && driver_name[0])
|
|
|
|
printer->driver = driver_name;
|
|
|
|
|
|
|
|
printer_register(pEntryPoints, printer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printers = driver->EnumPrinters(driver);
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
for (i = 0; printers[i]; i++)
|
|
|
|
{
|
|
|
|
printer = printers[i];
|
|
|
|
printer_register(pEntryPoints, printer);
|
|
|
|
}
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(printers);
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|