1/*
2 *  Copyright (c) 2000-2007 Apple Inc. All Rights Reserved.
3 *
4 *  @APPLE_LICENSE_HEADER_START@
5 *
6 *  This file contains Original Code and/or Modifications of Original Code
7 *  as defined in and that are subject to the Apple Public Source License
8 *  Version 2.0 (the 'License'). You may not use this file except in
9 *  compliance with the License. Please obtain a copy of the License at
10 *  http://www.opensource.apple.com/apsl/ and read it before using this
11 *  file.
12 *
13 *  The Original Code and all software distributed under the License are
14 *  distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 *  EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 *  INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 *  FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 *  Please see the License for the specific language governing rights and
19 *  limitations under the License.
20 *
21 *  @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 *  winscard_msg.h
26 *  SmartCardServices
27 */
28
29/*
30 * MUSCLE SmartCard Development ( http://www.linuxnet.com )
31 *
32 * Copyright (C) 2001-2004
33 *  David Corcoran <corcoran@linuxnet.com>
34 *  Damien Sauveron <damien.sauveron@labri.fr>
35 *  Ludovic Rousseau <ludovic.rousseau@free.fr>
36 *
37 * $Id: winscard_msg.h 123 2010-03-27 10:50:42Z ludovic.rousseau@gmail.com $
38 */
39
40/**
41 * @file
42 * @brief This defines some structures and \#defines to be used over
43 * the transport layer.
44 */
45
46#ifndef __winscard_msg_h__
47#define __winscard_msg_h__
48
49#include "pcscexport.h"
50
51/** Major version of the current message protocol */
52#define PROTOCOL_VERSION_MAJOR 3
53/** Minor version of the current message protocol */
54#define PROTOCOL_VERSION_MINOR 0
55
56#define WINSCARD_MSG_HEADER_TAG	0x12345678
57
58#ifdef __cplusplus
59extern "C"
60{
61#endif
62
63	/**
64	 * @brief General structure for client/serve message data exchange.
65	 *
66	 * It is used in the calls of \c SHMMessageSend and \c SHMMessageReceive.
67	 * The field \c data is interpreted according to the values of the fields
68	 * \c mtype and \c command. The possible structs the \c data field can
69	 * represent are: \c version_struct \c client_struct \c establish_struct
70	 * \c release_struct \c connect_struct \c reconnect_struct
71	 * \c disconnect_struct \c begin_struct \c end_struct \c cancel_struct
72	 * \c status_struct \c transmit_struct \c control_struct \c getset_struct
73	 */
74	typedef struct rxSharedSegment
75	{
76		uint32_t headerTag;		/** Always WINSCARD_MSG_HEADER_TAG */
77		uint32_t msgSize;		/** size of the whole message being sent/received */
78		uint32_t mtype;			/** one of the \c pcsc_adm_commands */
79		uint32_t user_id;
80		uint32_t group_id;
81		uint32_t command;		/** one of the \c pcsc_msg_commands */
82		uint64_t date;
83		unsigned char key[PCSCLITE_MSG_KEY_LEN];
84		unsigned char data[PCSCLITE_MAX_MESSAGE_SIZE];
85	}
86	sharedSegmentMsg, *psharedSegmentMsg;
87
88	/**
89	 * Command types available to use in the field \c sharedSegmentMsg.mtype.
90	 */
91	enum pcsc_adm_commands
92	{
93		CMD_FUNCTION = 0xF1,
94		CMD_FAILED = 0xF2,
95		CMD_SERVER_DIED = 0xF3,
96		CMD_CLIENT_DIED = 0xF4,
97		CMD_READER_EVENT = 0xF5,
98		CMD_SYN = 0xF6,
99		CMD_ACK = 0xF7,
100		CMD_VERSION = 0xF8	/**< version of the IPC */
101	};
102
103	/**
104	 * @brief Commands available to use in the field \c sharedSegmentMsg.command.
105	 */
106	enum pcsc_msg_commands
107	{
108		SCARD_ESTABLISH_CONTEXT = 0x01,	/**< used by SCardEstablishContext() */
109		SCARD_RELEASE_CONTEXT = 0x02,	/**< used by SCardReleaseContext() */
110		SCARD_LIST_READERS = 0x03,		/**< used by SCardListReaders() */
111		SCARD_CONNECT = 0x04,			/**< used by SCardConnect() */
112		SCARD_RECONNECT = 0x05,			/**< used by SCardReconnect() */
113		SCARD_DISCONNECT = 0x06,		/**< used by SCardDisconnect() */
114		SCARD_BEGIN_TRANSACTION = 0x07,	/**< used by SCardBeginTransaction() */
115		SCARD_END_TRANSACTION = 0x08,	/**< used by SCardEndTransaction() */
116		SCARD_TRANSMIT = 0x09,			/**< used by SCardTransmit() */
117		SCARD_CONTROL = 0x0A,			/**< used by SCardControl() */
118		SCARD_STATUS = 0x0B,			/**< used by SCardStatus() */
119		SCARD_GET_STATUS_CHANGE = 0x0C,	/**< used by SCardGetStatusChange() */
120		SCARD_CANCEL = 0x0D,			/**< used by SCardCancel() */
121		SCARD_CANCEL_TRANSACTION = 0x0E,
122		SCARD_GET_ATTRIB = 0x0F,		/**< used by SCardGetAttrib() */
123		SCARD_SET_ATTRIB = 0x10,		/**< used by SCardSetAttrib() */
124		SCARD_TRANSMIT_EXTENDED = 0x11,	/**< used by SCardTransmit() */
125		SCARD_CONTROL_EXTENDED = 0x12	/**< used by SCardControl() */
126	};
127
128	/**
129	 * @brief Information transmitted in \ref CMD_VERSION Messages.
130	 */
131	struct version_struct
132	{
133		int32_t major;	/**< IPC major \ref PROTOCOL_VERSION_MAJOR */
134		int32_t minor;	/**< IPC minor \ref PROTOCOL_VERSION_MINOR */
135		uint32_t rv;
136	};
137	typedef struct version_struct version_struct;
138
139	struct client_struct
140	{
141		uint32_t hContext;
142	};
143	typedef struct client_struct client_struct;
144
145	/**
146	 * @brief Information contained in \ref SCARD_ESTABLISH_CONTEXT Messages.
147	 *
148	 * These data are passed throw the field \c sharedSegmentMsg.data.
149	 */
150	struct establish_struct
151	{
152		uint32_t dwScope;
153		uint32_t phContext;
154		uint32_t rv;
155	};
156	typedef struct establish_struct establish_struct;
157
158	/**
159	 * @brief Information contained in \ref SCARD_RELEASE_CONTEXT Messages.
160	 *
161	 * These data are passed throw the field \c sharedSegmentMsg.data.
162	 */
163	struct release_struct
164	{
165		uint32_t hContext;
166		uint32_t rv;
167	};
168	typedef struct release_struct release_struct;
169
170	/**
171	 * @brief contained in \ref SCARD_CONNECT Messages.
172	 *
173	 * These data are passed throw the field \c sharedSegmentMsg.data.
174	 */
175	struct connect_struct
176	{
177		uint32_t hContext;
178		char szReader[MAX_READERNAME];
179		uint32_t dwShareMode;
180		uint32_t dwPreferredProtocols;
181		int32_t phCard;
182		uint32_t pdwActiveProtocol;
183		uint32_t rv;
184	};
185	typedef struct connect_struct connect_struct;
186
187	/**
188	 * @brief contained in \ref SCARD_RECONNECT Messages.
189	 *
190	 * These data are passed throw the field \c sharedSegmentMsg.data.
191	 */
192	struct reconnect_struct
193	{
194		int32_t hCard;
195		uint32_t dwShareMode;
196		uint32_t dwPreferredProtocols;
197		uint32_t dwInitialization;
198		uint32_t pdwActiveProtocol;
199		uint32_t rv;
200	};
201	typedef struct reconnect_struct reconnect_struct;
202
203	/**
204	 * @brief contained in \ref SCARD_DISCONNECT Messages.
205	 *
206	 * These data are passed throw the field \c sharedSegmentMsg.data.
207	 */
208	struct disconnect_struct
209	{
210		int32_t hCard;
211		uint32_t dwDisposition;
212		uint32_t rv;
213	};
214	typedef struct disconnect_struct disconnect_struct;
215
216	/**
217	 * @brief contained in \ref SCARD_BEGIN_TRANSACTION Messages.
218	 *
219	 * These data are passed throw the field \c sharedSegmentMsg.data.
220	 */
221	struct begin_struct
222	{
223		int32_t hCard;
224		uint32_t rv;
225	};
226	typedef struct begin_struct begin_struct;
227
228	/**
229	 * @brief contained in \ref SCARD_END_TRANSACTION Messages.
230	 *
231	 * These data are passed throw the field \c sharedSegmentMsg.data.
232	 */
233	struct end_struct
234	{
235		int32_t hCard;
236		uint32_t dwDisposition;
237		uint32_t rv;
238	};
239	typedef struct end_struct end_struct;
240
241	/**
242	 * @brief contained in \ref SCARD_CANCEL Messages.
243	 *
244	 * These data are passed throw the field \c sharedSegmentMsg.data.
245	 */
246	struct cancel_struct
247	{
248		int32_t hCard;
249		uint32_t rv;
250	};
251	typedef struct cancel_struct cancel_struct;
252
253	/**
254	 * @brief contained in \ref SCARD_STATUS Messages.
255	 *
256	 * These data are passed throw the field \c sharedSegmentMsg.data.
257	 */
258	struct status_struct
259	{
260		int32_t hCard;
261		char mszReaderNames[MAX_READERNAME];
262		uint32_t pcchReaderLen;
263		uint32_t pdwState;
264		uint32_t pdwProtocol;
265		uint8_t pbAtr[MAX_ATR_SIZE];
266		uint32_t pcbAtrLen;
267		uint32_t rv;
268	};
269	typedef struct status_struct status_struct;
270
271	/**
272	 * @brief contained in \ref SCARD_TRANSMIT Messages.
273	 *
274	 * These data are passed throw the field \c sharedSegmentMsg.data.
275	 */
276	struct transmit_struct
277	{
278		int32_t hCard;
279		uint32_t pioSendPciProtocol;
280		uint32_t pioSendPciLength;
281		uint8_t pbSendBuffer[MAX_BUFFER_SIZE];
282		uint32_t cbSendLength;
283		uint32_t pioRecvPciProtocol;
284		uint32_t pioRecvPciLength;
285		uint8_t pbRecvBuffer[MAX_BUFFER_SIZE];
286		uint32_t pcbRecvLength;
287		uint32_t rv;
288	};
289	typedef struct transmit_struct transmit_struct;
290
291	/**
292	 * @brief contained in \ref SCARD_TRANSMIT_EXTENDED Messages.
293	 *
294	 * These data are passed throw the field \c sharedSegmentMsg.data.
295	 */
296	struct transmit_struct_extended
297	{
298		int32_t hCard;
299		uint32_t pioSendPciProtocol;
300		uint32_t pioSendPciLength;
301		uint32_t cbSendLength;
302		uint32_t pioRecvPciProtocol;
303		uint32_t pioRecvPciLength;
304		uint32_t pcbRecvLength;
305		uint32_t rv;
306		uint64_t size;
307		uint8_t data[1];
308	};
309	typedef struct transmit_struct_extended transmit_struct_extended;
310
311	/**
312	 * @brief contained in \ref SCARD_CONTROL Messages.
313	 *
314	 * These data are passed throw the field \c sharedSegmentMsg.data.
315	 */
316	struct control_struct
317	{
318		int32_t hCard;
319		uint32_t dwControlCode;
320		uint8_t pbSendBuffer[MAX_BUFFER_SIZE];
321		uint32_t cbSendLength;
322		uint8_t pbRecvBuffer[MAX_BUFFER_SIZE];
323		uint32_t cbRecvLength;
324		uint32_t dwBytesReturned;
325		uint32_t rv;
326	};
327	typedef struct control_struct control_struct;
328
329	/**
330	 * @brief contained in \ref SCARD_CONTROL_EXTENDED Messages.
331	 *
332	 * These data are passed throw the field \c sharedSegmentMsg.data.
333	 */
334	struct control_struct_extended
335	{
336		int32_t hCard;
337		uint32_t dwControlCode;
338		uint32_t cbSendLength;
339		uint32_t cbRecvLength;
340		uint32_t pdwBytesReturned;
341		uint32_t rv;
342		uint64_t size;
343		uint8_t data[1];
344	};
345	typedef struct control_struct_extended control_struct_extended;
346
347	/**
348	 * @brief contained in \ref SCARD_GET_ATTRIB and \c  Messages.
349	 *
350	 * These data are passed throw the field \c sharedSegmentMsg.data.
351	 */
352	struct getset_struct
353	{
354		int32_t hCard;
355		uint32_t dwAttrId;
356		uint8_t pbAttr[MAX_BUFFER_SIZE];
357		uint32_t cbAttrLen;
358		uint32_t rv;
359	};
360	typedef struct getset_struct getset_struct;
361
362	/*
363	 * Now some function definitions
364	 */
365
366	int32_t SHMClientReadMessage(psharedSegmentMsg msgStruct, uint32_t dwClientID, size_t dataSize, int blockamount);
367
368	int32_t SHMClientRead(psharedSegmentMsg, uint32_t, int32_t);
369	int32_t SHMClientSetupSession(uint32_t *);
370	int32_t SHMClientCloseSession(uint32_t);
371	int32_t SHMInitializeCommonSegment(void);
372	int32_t SHMProcessEventsContext(uint32_t, psharedSegmentMsg, int32_t);
373	int32_t SHMProcessEventsServer(uint32_t *, int32_t);
374	int32_t SHMMessageSend(void *buffer, uint64_t buffer_size, int32_t filedes,
375		int32_t blockAmount);
376	int32_t SHMMessageReceive(void *buffer, uint64_t buffer_size,
377		int32_t filedes, int32_t blockAmount);
378	int32_t WrapSHMWrite(uint32_t command, uint32_t dwClientID, uint64_t dataSize,
379		uint32_t blockAmount, void *data);
380	void SHMCleanupSharedSegment(int32_t, const char *);
381
382	void SHSharedSegmentMsgToNetworkOrder(psharedSegmentMsg msg);
383	void SHSharedSegmentMsgToHostOrder(psharedSegmentMsg msg);
384	size_t SHMCalculateMessageSize(size_t dataSize);
385	int32_t SHMCommunicationTimeout();
386
387	// Fix up byte ordering
388	INTERNAL void htonlControlStructExtended(control_struct_extended *cs);
389	INTERNAL void ntohlControlStructExtended(control_struct_extended *cs);
390	INTERNAL void htonlTransmitStructExtended(transmit_struct_extended *ts);
391	INTERNAL void ntohlTransmitStructExtended(transmit_struct_extended *ts);
392	INTERNAL void htonlEstablishStruct(establish_struct *es);
393	INTERNAL void ntohlEstablishStruct(establish_struct *es);
394	INTERNAL void htonlTransmitStruct(transmit_struct *ts);
395	INTERNAL void ntohlTransmitStruct(transmit_struct *ts);
396	INTERNAL void htonlReleaseStruct(release_struct *rs);
397	INTERNAL void ntohlReleaseStruct(release_struct *rs);
398	INTERNAL void htonlConnectStruct(connect_struct *Cs);
399	INTERNAL void ntohlConnectStruct(connect_struct *cs);
400	INTERNAL void htonlReconnectStruct(reconnect_struct *rc);
401	INTERNAL void ntohlReconnectStruct(reconnect_struct *rc);
402	INTERNAL void htonlDisconnectStruct(disconnect_struct *dc);
403	INTERNAL void ntohlDisconnectStruct(disconnect_struct *dc);
404	INTERNAL void htonlBeginStruct(begin_struct *bs);
405	INTERNAL void ntohlBeginStruct(begin_struct *bs);
406	INTERNAL void htonlCancelStruct(cancel_struct *cs);
407	INTERNAL void ntohlCancelStruct(cancel_struct *cs);
408	INTERNAL void htonlEndStruct(end_struct *es);
409	INTERNAL void ntohlEndStruct(end_struct *es);
410	INTERNAL void htonlStatusStruct(status_struct *ss);
411	INTERNAL void ntohlStatusStruct(status_struct *ss);
412	INTERNAL void htonlControlStruct(control_struct *cs);
413	INTERNAL void ntohlControlStruct(control_struct *cs);
414	INTERNAL void htonlGetSetStruct(getset_struct *gs);
415	INTERNAL void ntohlGetSetStruct(getset_struct *gs);
416	INTERNAL void htonlVersionStruct(version_struct *vs);
417	INTERNAL void ntohlVersionStruct(version_struct *vs);
418
419#ifdef __cplusplus
420}
421#endif
422
423#endif
424