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
|
2015-06-08 18:51:01 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2011-08-17 17:57:09 +04:00
|
|
|
*
|
|
|
|
* 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>
|
2015-07-03 14:26:15 +03:00
|
|
|
#include <winpr/string.h>
|
2012-10-03 05:52:27 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
2014-06-11 23:09:28 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-10-03 05:52:27 +04:00
|
|
|
#include <winpr/interlocked.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
|
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
#include <freerdp/channels/log.h>
|
|
|
|
|
|
|
|
#define TAG CHANNELS_TAG("printer.client")
|
|
|
|
|
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;
|
2013-03-22 01:40:04 +04:00
|
|
|
|
|
|
|
HANDLE event;
|
|
|
|
HANDLE stopEvent;
|
|
|
|
|
|
|
|
HANDLE thread;
|
2015-07-15 10:50:35 +03:00
|
|
|
rdpContext* rdpcontext;
|
2011-08-17 17:57:09 +04:00
|
|
|
};
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_process_irp_create(PRINTER_DEVICE* printer_dev, IRP* irp)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
rdpPrintJob* printjob = NULL;
|
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (printer_dev->printer)
|
2011-08-17 17:57:09 +04:00
|
|
|
printjob = printer_dev->printer->CreatePrintJob(printer_dev->printer, irp->devman->id_sequence++);
|
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (printjob)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, printjob->id); /* FileId */
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, 0); /* FileId */
|
2011-08-17 17:57:09 +04:00
|
|
|
irp->IoStatus = STATUS_PRINT_QUEUE_FULL;
|
|
|
|
}
|
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
return irp->Complete(irp);
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_process_irp_close(PRINTER_DEVICE* printer_dev, IRP* irp)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
rdpPrintJob* printjob = NULL;
|
|
|
|
|
2014-06-11 23:09:28 +04:00
|
|
|
if (printer_dev->printer)
|
2011-08-17 17:57:09 +04:00
|
|
|
printjob = printer_dev->printer->FindPrintJob(printer_dev->printer, irp->FileId);
|
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (!printjob)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printjob->Close(printjob);
|
|
|
|
}
|
|
|
|
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Zero(irp->output, 4); /* Padding(4) */
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
return irp->Complete(irp);
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_process_irp_write(PRINTER_DEVICE* printer_dev, IRP* irp)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT64 Offset;
|
2012-10-03 05:52:27 +04:00
|
|
|
rdpPrintJob* printjob = NULL;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
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-08-17 17:57:09 +04:00
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (printer_dev->printer)
|
2011-08-17 17:57:09 +04:00
|
|
|
printjob = printer_dev->printer->FindPrintJob(printer_dev->printer, irp->FileId);
|
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (!printjob)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-08 18:51:01 +03:00
|
|
|
error = printjob->Write(printjob, Stream_Pointer(irp->input), Length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "printjob->Write failed with error %lu!", error);
|
|
|
|
return error;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, Length);
|
|
|
|
Stream_Write_UINT8(irp->output, 0); /* Padding */
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
return irp->Complete(irp);
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_process_irp_device_control(PRINTER_DEVICE* printer_dev, IRP* irp)
|
2013-05-20 23:50:22 +04:00
|
|
|
{
|
|
|
|
Stream_Write_UINT32(irp->output, 0); /* OutputBufferLength */
|
2015-06-08 18:51:01 +03:00
|
|
|
return irp->Complete(irp);
|
2013-05-20 23:50:22 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_process_irp(PRINTER_DEVICE* printer_dev, IRP* irp)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2011-08-17 17:57:09 +04:00
|
|
|
switch (irp->MajorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MJ_CREATE:
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_process_irp_create(printer_dev, irp)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "printer_process_irp_create failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_CLOSE:
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_process_irp_close(printer_dev, irp)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "printer_process_irp_close failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_WRITE:
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_process_irp_write(printer_dev, irp)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "printer_process_irp_write failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
|
|
|
|
2013-05-20 23:50:22 +04:00
|
|
|
case IRP_MJ_DEVICE_CONTROL:
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_process_irp_device_control(printer_dev, irp)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "printer_process_irp_device_control failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2013-05-20 23:50:22 +04:00
|
|
|
break;
|
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
default:
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
2015-06-08 18:51:01 +03:00
|
|
|
return irp->Complete(irp);
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
|
|
|
}
|
2015-06-08 18:51:01 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
static void* printer_thread_func(void* arg)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
IRP* irp;
|
2013-03-22 01:40:04 +04:00
|
|
|
PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*) arg;
|
2013-08-08 16:46:21 +04:00
|
|
|
HANDLE obj[] = {printer_dev->event, printer_dev->stopEvent};
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2013-08-08 16:46:21 +04:00
|
|
|
DWORD rc = WaitForMultipleObjects(2, obj, FALSE, INFINITE);
|
2015-07-30 16:49:21 +03:00
|
|
|
if (rc == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error);
|
|
|
|
break;
|
|
|
|
}
|
2013-03-22 01:40:04 +04:00
|
|
|
|
2013-08-08 16:46:21 +04:00
|
|
|
if (rc == WAIT_OBJECT_0 + 1)
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
2013-08-08 16:46:21 +04:00
|
|
|
else if( rc != WAIT_OBJECT_0 )
|
|
|
|
continue;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
ResetEvent(printer_dev->event);
|
|
|
|
|
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)
|
2015-06-08 18:51:01 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "InterlockedPopEntrySList failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
2015-06-08 18:51:01 +03:00
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_process_irp(printer_dev, irp)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "printer_process_irp failed with error %d!", error);
|
|
|
|
break;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
2015-07-15 10:50:35 +03:00
|
|
|
if (error && printer_dev->rdpcontext)
|
|
|
|
setChannelError(printer_dev->rdpcontext, error, "printer_thread_func reported an error");
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
ExitThread((DWORD) error);
|
2013-09-05 15:38:56 +04:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_irp_request(DEVICE* device, IRP* irp)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
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
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
SetEvent(printer_dev->event);
|
2015-06-08 18:51:01 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_free(DEVICE* device)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
IRP* irp;
|
2012-10-03 05:52:27 +04:00
|
|
|
PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*) device;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
SetEvent(printer_dev->stopEvent);
|
2015-07-30 16:49:21 +03:00
|
|
|
if (WaitForSingleObject(printer_dev->thread, INFINITE) == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error);
|
|
|
|
return error;
|
|
|
|
}
|
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
|
|
|
|
2013-09-05 15:38:56 +04:00
|
|
|
CloseHandle(printer_dev->thread);
|
|
|
|
CloseHandle(printer_dev->stopEvent);
|
|
|
|
CloseHandle(printer_dev->event);
|
|
|
|
|
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);
|
2015-07-30 16:49:21 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT printer_register(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints, rdpPrinter* printer)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
char* port;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Flags;
|
2012-09-24 03:49:13 +04:00
|
|
|
int DriverNameLen;
|
2013-01-11 00:30:32 +04:00
|
|
|
WCHAR* DriverName = NULL;
|
2012-09-24 03:49:13 +04:00
|
|
|
int PrintNameLen;
|
2013-01-11 00:30:32 +04:00
|
|
|
WCHAR* PrintName = NULL;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 CachedFieldsLen;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* CachedPrinterConfigData;
|
2012-09-24 02:41:07 +04:00
|
|
|
PRINTER_DEVICE* printer_dev;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
port = malloc(10);
|
2015-06-08 18:51:01 +03:00
|
|
|
if (!port)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "malloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2015-07-03 14:26:15 +03:00
|
|
|
sprintf_s(port, 10, "PRN%d", printer->id);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
printer_dev = (PRINTER_DEVICE*) calloc(1, sizeof(PRINTER_DEVICE));
|
|
|
|
if (!printer_dev)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
free(port);
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
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;
|
2015-07-15 10:50:35 +03:00
|
|
|
printer_dev->rdpcontext = pEntryPoints->rdpcontext;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
printer_dev->printer = printer;
|
|
|
|
|
|
|
|
CachedFieldsLen = 0;
|
|
|
|
CachedPrinterConfigData = NULL;
|
|
|
|
|
|
|
|
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-12-17 19:20:25 +04:00
|
|
|
DriverNameLen = ConvertToUnicode(CP_UTF8, 0, printer->driver, -1, &DriverName, 0) * 2;
|
|
|
|
PrintNameLen = ConvertToUnicode(CP_UTF8, 0, printer->name, -1, &PrintName, 0) * 2;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2013-05-09 01:48:30 +04:00
|
|
|
printer_dev->device.data = Stream_New(NULL, 28 + DriverNameLen + PrintNameLen + CachedFieldsLen);
|
2015-06-08 18:51:01 +03:00
|
|
|
if (!printer_dev->device.data)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
free(DriverName);
|
|
|
|
free(PrintName);
|
|
|
|
goto error_out;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
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)
|
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write(printer_dev->device.data, CachedPrinterConfigData, CachedFieldsLen);
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
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);
|
2015-06-08 18:51:01 +03:00
|
|
|
if (!printer_dev->pIrpList)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "_aligned_malloc failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2012-10-03 05:52:27 +04:00
|
|
|
InitializeSListHead(printer_dev->pIrpList);
|
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
if (!(printer_dev->event = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
if (!(printer_dev->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*) printer_dev)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "RegisterDevice failed with error %d!", error);
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(printer_dev->thread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) printer_thread_func, (void*) printer_dev, 0, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateThread failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
error_out:
|
|
|
|
CloseHandle(printer_dev->stopEvent);
|
|
|
|
CloseHandle(printer_dev->event);
|
|
|
|
_aligned_free(printer_dev->pIrpList);
|
|
|
|
Stream_Free(printer_dev->device.data, TRUE);
|
|
|
|
free(printer_dev);
|
|
|
|
free(port);
|
|
|
|
return error;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 06:53:05 +04:00
|
|
|
#ifdef STATIC_CHANNELS
|
2012-10-14 10:38:58 +04:00
|
|
|
#define DeviceServiceEntry printer_DeviceServiceEntry
|
2012-10-02 11:10:21 +04:00
|
|
|
#endif
|
2012-10-14 10:38:58 +04:00
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
|
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;
|
2012-11-09 04:01:52 +04:00
|
|
|
RDPDR_PRINTER* device;
|
2012-10-03 05:52:27 +04:00
|
|
|
rdpPrinterDriver* driver = NULL;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
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
|
2013-03-22 01:40:04 +04:00
|
|
|
|
2012-10-02 11:10:21 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
driver = printer_win_get_driver();
|
2011-08-17 17:57:09 +04:00
|
|
|
#endif
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2014-06-11 23:09:28 +04:00
|
|
|
if (!driver)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
2015-06-08 18:51:01 +03:00
|
|
|
WLog_ERR(TAG, "Could not get a printer driver!");
|
|
|
|
return CHANNEL_RC_INITIALIZATION_ERROR;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
device = (RDPDR_PRINTER*) pEntryPoints->device;
|
|
|
|
name = device->Name;
|
|
|
|
driver_name = device->DriverName;
|
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
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (!printer)
|
2015-06-08 18:51:01 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Could not get printer %s!", name);
|
|
|
|
return CHANNEL_RC_INITIALIZATION_ERROR;
|
|
|
|
}
|
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;
|
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_register(pEntryPoints, printer)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "printer_register failed with error %lu!", error);
|
|
|
|
return error;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
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];
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_register(pEntryPoints, printer)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "printer_register failed with error %lu!", error);
|
|
|
|
free(printers);
|
|
|
|
return error;
|
|
|
|
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|