Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

478 lines
11 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: xfer.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef _XFER_H_
  11. #define _XFER_H_
  12. #include <stdio.h>
  13. #define SERVICE_NAME_1 "OBEX:IrXfer"
  14. #define SERVICE_NAME_2 "OBEX"
  15. #define GUARD_MAGIC 0x45454545
  16. #define TIMEOUT_INFINITE (~0UL)
  17. #define ERROR_DESCRIPTION_LENGTH 1000
  18. #define IRDA_DEVICE_NAME_LENGTH 22
  19. //
  20. // OBEX parameter codes
  21. //
  22. #define OBEX_PARAM_UNICODE 0x00
  23. #define OBEX_PARAM_STREAM 0x40
  24. #define OBEX_PARAM_1BYTE 0x80
  25. #define OBEX_PARAM_4BYTE 0xC0
  26. #define OBEX_PARAM_TYPE_MASK 0xC0
  27. #define OBEX_PARAM_COUNT ( 0x00 | OBEX_PARAM_4BYTE )
  28. #define OBEX_PARAM_NAME ( 0x01 | OBEX_PARAM_UNICODE )
  29. #define OBEX_PARAM_LENGTH ( 0x03 | OBEX_PARAM_4BYTE )
  30. #define OBEX_PARAM_UNIX_TIME ( 0x04 | OBEX_PARAM_4BYTE )
  31. #define OBEX_PARAM_ISO_TIME ( 0x04 | OBEX_PARAM_STREAM )
  32. #define OBEX_PARAM_BODY ( 0x08 | OBEX_PARAM_STREAM )
  33. #define OBEX_PARAM_BODY_END ( 0x09 | OBEX_PARAM_STREAM )
  34. #define OBEX_PARAM_WHO ( 0x0A | OBEX_PARAM_STREAM )
  35. // #define OBEX_PARAM_LEN 1
  36. #define PRIVATE_PARAM_WIN32_ERROR ( 0x30 | OBEX_PARAM_4BYTE )
  37. // for xfer.c
  38. typedef struct {
  39. BOOL fXferInProgress; // transfer in progress
  40. __int64 dwTotalSize; // total size of transfer
  41. __int64 dwTotalSent; // number of bytes sent in this transfer
  42. __int64 dwFileSize; // total size of current file
  43. __int64 dwFileSent; // number of bytes sent of the current file
  44. } DATA_XFER, *LPDATA_XFER;
  45. typedef struct {
  46. FILETIME filetime; // file time
  47. WCHAR szFileName[MAX_PATH]; // name of file
  48. WCHAR szFileSave[MAX_PATH]; // path+name of final file
  49. WCHAR szFileTemp[MAX_PATH]; // path+name of temp file used
  50. HANDLE hFile; // file handle (of szFileTemp)
  51. } DATA_FILE, *LPDATA_FILE;
  52. // for obex.c
  53. typedef struct {
  54. BYTE1 b1Flags; // setpath flags
  55. BYTE1 b1Constants; // setpath constants
  56. } DATA_PATH, *LPDATA_PATH;
  57. typedef struct {
  58. BOOL fWaiting; // indicates if waiting for a reply
  59. BYTE1 b1Status; // response status (error/success)
  60. } DATA_REPLY, *LPDATA_REPLY;
  61. typedef enum {
  62. osIDLE = 0,
  63. osCONN = 1,
  64. osFILE = 2
  65. } OBEXSTATE;
  66. typedef struct {
  67. LPSTORE lpStore;
  68. OBEXSTATE state;
  69. BYTE1 b1Version; // peer's version of obex
  70. BYTE1 b1Flags; // connection flags
  71. BYTE2 b2MaxPacket; // peer's maximum packet size
  72. } DATA_CONN, *LPDATA_CONN;
  73. // for status.c
  74. typedef struct {
  75. DWORD dwDeviceID;
  76. BOOL fClear;
  77. } TARGET_ITEM, *LPTARGET_ITEM;
  78. //-------------------------------------
  79. enum TRANSFER_STATE
  80. {
  81. BLANK,
  82. CONNECTING,
  83. ACCEPTING,
  84. READING,
  85. WRITING,
  86. CLOSING
  87. };
  88. enum OBEX_DIALECT
  89. {
  90. dialUnknown = 0,
  91. dialWin95,
  92. dialNt5
  93. };
  94. typedef class FILE_TRANSFER * PFILE_TRANSFER;
  95. class FILE_TRANSFER
  96. {
  97. public:
  98. FILE_TRANSFER( );
  99. ~FILE_TRANSFER();
  100. BOOL
  101. Xfer_Init(
  102. wchar_t * files,
  103. unsigned length,
  104. OBEX_DIALECT dialect ,
  105. OBEX_DEVICE_TYPE DeviceType,
  106. BOOL CreateSocket,
  107. SOCKET ListenSocket
  108. );
  109. BOOL
  110. SendReplyObex(
  111. BYTE1 ObexCode
  112. );
  113. BOOL
  114. SendReplyWin32(
  115. BYTE1 b1Opcode,
  116. DWORD status
  117. );
  118. void
  119. BeginSend(
  120. DWORD DeviceId,
  121. OBEX_DEVICE_TYPE DeviceType,
  122. error_status_t * pStatus,
  123. FAILURE_LOCATION * pLocation
  124. );
  125. void Deactivate();
  126. DWORD
  127. SyncAccept(
  128. VOID
  129. );
  130. handle_t _rpcHandle;
  131. COOKIE _cookie;
  132. MUTEX * _mutex;
  133. inline long
  134. DecrementRefCount()
  135. {
  136. EnterCriticalSection(&m_Lock);
  137. long count = --_refs;
  138. DbgLog3(SEV_INFO, "[%x] %p: refs = %d\n", (DWORD) _cookie, this, count);
  139. if (0 == count) {
  140. #if DBG
  141. DbgPrint("irmon: freeing transfer\n");
  142. #endif
  143. LeaveCriticalSection(&m_Lock);
  144. delete this;
  145. return count;
  146. } else {
  147. LeaveCriticalSection(&m_Lock);
  148. }
  149. return count;
  150. }
  151. inline BOOL
  152. IsActive(
  153. VOID
  154. )
  155. {
  156. return (_state != ACCEPTING);
  157. }
  158. inline long
  159. IncrementRefCount()
  160. {
  161. EnterCriticalSection(&m_Lock);
  162. long count = ++_refs;
  163. DbgLog3(SEV_INFO, "[%x] %p: refs = %d\n", (DWORD) _cookie, this, count);
  164. LeaveCriticalSection(&m_Lock);
  165. return count;
  166. }
  167. void Cancel()
  168. {
  169. _fCancelled = TRUE;
  170. }
  171. COOKIE
  172. GetCookie(
  173. VOID
  174. )
  175. {
  176. return _cookie;
  177. }
  178. void Send();
  179. void RecordDeviceName( SOCKADDR_IRDA * s );
  180. VOID
  181. StopListening(
  182. VOID
  183. )
  184. {
  185. IncrementRefCount();
  186. EnterCriticalSection(&m_Lock);
  187. m_StopListening=TRUE;
  188. if (m_ListenSocket != INVALID_SOCKET) {
  189. closesocket(m_ListenSocket);
  190. m_ListenSocket=INVALID_SOCKET;
  191. }
  192. LeaveCriticalSection(&m_Lock);
  193. DecrementRefCount();
  194. return;
  195. }
  196. void
  197. RecordIpDeviceName(
  198. sockaddr_in * Address
  199. );
  200. private:
  201. CRITICAL_SECTION m_Lock;
  202. BOOL m_StopListening;
  203. SOCKET m_ListenSocket;
  204. OBEX_DEVICE_TYPE m_DeviceType;
  205. BOOL _fCancelled;
  206. BOOL _fInUiReceiveList;
  207. XFER_TYPE _xferType;
  208. TRANSFER_STATE _state;
  209. SOCKET _socket;
  210. wchar_t * _files;
  211. long _refs;
  212. WSABUF _buffers;
  213. HANDLE _event;
  214. BOOL _fWriteable;
  215. OBEX_DIALECT _dialect;
  216. wchar_t _DeviceName[MAX_PATH];
  217. WSAOVERLAPPED _overlapped;
  218. HANDLE _waitHandle;
  219. BYTE _buffer[ cbSOCK_BUFFER_SIZE + 16 + sizeof(SOCKADDR_IRDA) + 16 + sizeof(SOCKADDR_IRDA) ];
  220. DWORD _guard;
  221. void
  222. HandleClosure(
  223. DWORD error
  224. );
  225. // for sock.c
  226. error_status_t Sock_Request( LPVOID lpvData, DWORD dwDataSize );
  227. error_status_t Sock_Respond( LPVOID lpvData, DWORD dwDataSize );
  228. error_status_t Sock_EstablishConnection( DWORD dwDeviceID ,OBEX_DEVICE_TYPE DeviceType);
  229. VOID Sock_BreakConnection( SOCKET * pSock );
  230. error_status_t _SendDataOnSocket( SOCKET sock, LPVOID lpvData, DWORD dwDataSize );
  231. VOID _BreakConnection( SOCKET sock );
  232. VOID _ReadConnection( SOCKET sock );
  233. error_status_t Sock_CheckForReply( long Timeout );
  234. // for xfer.c
  235. UINT _uObjsReceived;
  236. WCHAR _szRecvFolder[MAX_PATH];
  237. DATA_FILE _dataFileRecv;
  238. DATA_XFER _dataXferRecv;
  239. BOOL Xfer_FileSetSize( BYTE4 b4Size );
  240. error_status_t Xfer_FileWriteBody( LPVOID lpvData, BYTE2 b2Size, BOOL fFinal );
  241. VOID Xfer_ConnEnd( VOID );
  242. error_status_t Xfer_ConnStart( VOID );
  243. VOID Xfer_FileAbort( VOID );
  244. VOID Xfer_FileInit( VOID );
  245. error_status_t Xfer_FileSetName( LPWSTR szName );
  246. VOID Xfer_FileSetTime( FILETIME * FileTime );
  247. error_status_t Xfer_SetPath( LPWSTR szPath );
  248. VOID Xfer_SetSize( BYTE4 b4Size );
  249. error_status_t _PutFileBody( HANDLE hFile, wchar_t FileName[] );
  250. error_status_t _SendFile( LPWSTR szFile );
  251. error_status_t _SendFolder( LPWSTR szFolder );
  252. error_status_t _FileStart( VOID );
  253. error_status_t _FileEnd( BOOL fSave );
  254. VOID _Send_StartXfer( __int64 dwTotalSize, LPWSTR szDst );
  255. VOID _Send_EndXfer( VOID );
  256. error_status_t _SetReceiveFolder( LPWSTR szFolder );
  257. // for progress.c
  258. DWORD _dwTimeStart;
  259. DWORD _dwSecondsLeft;
  260. int _CurrentPercentage;
  261. VOID _FormatTime( LPWSTR sz, DWORD dwSeconds );
  262. // for obex.c
  263. DATA_CONN _dataRecv;
  264. DATA_PATH _dataPath;
  265. DATA_REPLY _dataReply;
  266. ULONG _blocksSent;
  267. ULONG _blocksAcked;
  268. ULONG _blockSize;
  269. __int64 _completedFilesSize;
  270. __int64 _currentFileSize;
  271. __int64 _currentFileAcked;
  272. DWORD _lastAckTime;
  273. error_status_t Obex_Abort( error_status_t status );
  274. error_status_t Obex_Connect( __int64 dwTotalSize );
  275. error_status_t Obex_Disconnect( error_status_t status );
  276. error_status_t Obex_PutBegin( LPWSTR wszObj, __int64 dwObjSize, FILETIME * pFileTime );
  277. error_status_t Obex_PutBody( wchar_t FileName[], LPBYTE1 pb1Data, BYTE2 b2DataSize, BOOL fFinal );
  278. error_status_t Obex_SetPath( LPWSTR wszPath );
  279. BOOL Obex_ConsumePackets( XFER_TYPE xferType, error_status_t * pStatus );
  280. BOOL Obex_Init( VOID );
  281. BOOL Obex_ReceiveData( XFER_TYPE xferType, LPVOID lpvData, DWORD dwDataSize );
  282. VOID Obex_Reset( VOID );
  283. error_status_t _WaitForReply( DWORD dwTimeout, BYTE1 b1NeededReply );
  284. error_status_t _Put( LPWSTR wszObj, __int64 dwObjSize, FILETIME * Time, LPBYTE1 pb1Data, BYTE2 b2DataSize, BOOL fFinal );
  285. error_status_t _Request( LPSTORE lpStore, BYTE1 b1NeededReply );
  286. error_status_t _Respond( LPSTORE lpStore );
  287. INT _ValidOpcode( OBEXSTATE state, BYTE1 b1Opcode );
  288. BOOL _HandleAbort( BYTE1 b1Opcode, BYTE2 b2Length );
  289. BOOL _HandleBadRequest( BYTE1 b1Opcode );
  290. BOOL _HandleConnect( BYTE1 b1Opcode, BYTE2 b2Length );
  291. BOOL _HandleDisconnect( BYTE1 b1Opcode, BYTE2 b2Length );
  292. BOOL _HandleNotImplemented( BYTE1 b1Opcode );
  293. error_status_t _HandlePut( BYTE1 b1Opcode, BYTE2 b2Length, BOOL fFinal );
  294. error_status_t _HandleResponse( BYTE1 b1Status, BYTE2 b2Length );
  295. error_status_t _HandleRequest( BYTE1 b1Opcode, BYTE2 b2Length );
  296. error_status_t _HandleSetPath( BYTE1 b1Opcode, BYTE2 b2Length );
  297. error_status_t _ParseParams( BYTE1 b1Opcode, BYTE2 b2Length );
  298. BOOL _PokePacketSizeIntoStore( LPSTORE lpStore );
  299. VOID _SetState( LPDATA_CONN lpDataConn, OBEXSTATE os );
  300. VOID _WaitInit( VOID );
  301. VOID _WriteBody( LPVOID lpvData, BYTE2 b2Size, BOOL fFinal );
  302. BYTE2 _SkipHeader( BYTE1 b1Param, LPSTORE lpStore );
  303. BYTE1
  304. StatusToReplyCode(
  305. BYTE1 b1Opcode,
  306. DWORD status
  307. );
  308. error_status_t
  309. ObexStatusToWin32(
  310. BYTE1 ObexStatus
  311. );
  312. BOOL Activate();
  313. };
  314. error_status_t
  315. _GetObjListStats(
  316. LPWSTR lpszObjList,
  317. LPDWORD lpdwFiles,
  318. LPDWORD lpdwFolders,
  319. __int64 * pTotalSize
  320. );
  321. DWORD
  322. ReportFileError( DWORD mc,
  323. WCHAR * file,
  324. DWORD error
  325. );
  326. BYTE1 WinErrorToObexError( DWORD Win32Error );
  327. BOOL IsWorkstationLocked();
  328. extern RPC_BINDING_HANDLE rpcBinding;
  329. extern BOOL g_fAllowReceives;
  330. extern wchar_t g_DuplicateFileTemplate[];
  331. extern wchar_t g_UnknownDeviceName[];
  332. extern MUTEX * g_Mutex;
  333. #include <stdio.h>
  334. extern "C" {
  335. FILE_TRANSFER* InitializeSocket(
  336. char ServiceName[]
  337. );
  338. FILE_TRANSFER *
  339. ListenForTransfer(
  340. SOCKET ListenSocket,
  341. OBEX_DEVICE_TYPE DeviceType
  342. );
  343. }
  344. FILE_TRANSFER*
  345. InitializeSocket(
  346. char ServiceName[]
  347. );
  348. #endif // _XFER_H_