2 * Copyright (C) 2002 Mike McCormack
4 * CIFS implementation for WINE
6 * This is a WINE's implementation of the Common Internet File System
8 * for specification see:
10 * http://www.codefx.com/CIFS_Explained.htm
11 * http://www.ubiqx.org/cifs/rfc-draft/rfc1002.html
12 * http://www.ubiqx.org/cifs/rfc-draft/draft-leach-cifs-v1-spec-02.html
13 * http://ubiqx.org/cifs/
14 * http://www.samba.org
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "wine/port.h"
41 #ifdef HAVE_SYS_ERRNO_H
42 #include <sys/errno.h>
44 #include <sys/types.h>
46 #ifdef HAVE_SYS_MMAN_H
54 #ifdef HAVE_SYS_SOCKET_H
55 # include <sys/socket.h>
57 #include <sys/types.h>
58 #ifdef HAVE_NETINET_IN_SYSTM_H
59 #include <netinet/in_systm.h>
61 #ifdef HAVE_NETINET_IN_H
62 #include <netinet/in.h>
64 #ifdef HAVE_NETINET_IP_H
65 #include <netinet/ip.h>
67 #ifdef HAVE_ARPA_INET_H
68 #include <arpa/inet.h>
82 #include "wine/server.h"
83 #include "wine/debug.h"
85 WINE_DEFAULT_DEBUG_CHANNEL(file
);
87 #define MAX_HOST_NAME 15
88 #define NB_TIMEOUT 10000
90 USHORT SMB_MultiplexId
= 0;
92 static int netbios_name(const char *p
, unsigned char *buffer
)
98 for(i
=0; i
<=MAX_HOST_NAME
; i
++)
103 ch
= *p
++&0xdf; /* add character from hostname */
105 ch
= ' '; /* add padding */
108 ch
= 0; /* add terminator */
109 buffer
[len
++] = ((ch
&0xf0) >> 4) + 'A';
110 buffer
[len
++] = (ch
&0x0f) + 'A';
112 buffer
[len
++] = 0; /* add second terminator */
116 static DWORD
NB_NameReq(LPCSTR host
, unsigned char *buffer
, int len
)
120 NBR_ADDWORD(&buffer
[i
],trn
); i
+=2;
121 NBR_ADDWORD(&buffer
[i
],0x0110); i
+=2;
122 NBR_ADDWORD(&buffer
[i
],0x0001); i
+=2;
123 NBR_ADDWORD(&buffer
[i
],0x0000); i
+=2;
124 NBR_ADDWORD(&buffer
[i
],0x0000); i
+=2;
125 NBR_ADDWORD(&buffer
[i
],0x0000); i
+=2;
127 i
+= netbios_name(host
,&buffer
[i
]);
129 NBR_ADDWORD(&buffer
[i
],0x0020); i
+=2;
130 NBR_ADDWORD(&buffer
[i
],0x0001); i
+=2;
132 ERR("packet is %d bytes in length\n",i
);
137 printf("%02x%c",buffer
[j
],(((j
+1)%16)&&((j
+1)!=j
))?' ':'\n');
143 /* unc = \\hostname\share\file... */
144 static BOOL
UNC_SplitName(LPSTR unc
, LPSTR
*hostname
, LPSTR
*share
, LPSTR
*file
)
150 p
= strchr(unc
,'\\');
153 p
= strchr(p
+1,'\\');
173 static BOOL
NB_Lookup(LPCSTR host
, struct sockaddr_in
*addr
)
175 int fd
,on
=1,r
,len
,i
,fromsize
;
177 struct sockaddr_in sin
,fromaddr
;
178 unsigned char buffer
[256];
180 fd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
184 r
= setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof on
);
188 if(0==inet_aton("255.255.255.255", (struct in_addr
*)&sin
.sin_addr
.s_addr
))
190 FIXME("Error getting bcast address\n");
193 sin
.sin_family
= AF_INET
;
194 sin
.sin_port
= htons(137);
196 len
= NB_NameReq(host
,buffer
,sizeof buffer
);
200 r
= sendto(fd
, buffer
, len
, 0, &sin
, sizeof sin
);
203 FIXME("Error sending packet\n");
211 /* FIXME: this is simple and easily fooled logic
212 * we should loop until we receive the correct packet or timeout
214 r
= poll(&fds
,1,NB_TIMEOUT
);
218 TRACE("Got response!\n");
220 fromsize
= sizeof (fromaddr
);
221 r
= recvfrom(fd
, buffer
, sizeof buffer
, 0, &fromaddr
, &fromsize
);
225 ERR("%d bytes received\n",r
);
231 DPRINTF("%02X%c",buffer
[i
],(((i
+1)!=r
)&&((i
+1)%16))?' ':'\n');
237 ERR("packet is OK\n");
239 memcpy(&addr
->sin_addr
, &buffer
[58], sizeof addr
->sin_addr
);
249 #define NB_FIRST 0x40
253 #define NB_SESSION_MSG 0x00
254 #define NB_SESSION_REQ 0x81
256 /* RFC 1002, section 4.3.2 */
257 static BOOL
NB_SessionReq(int fd
, char *called
, char *calling
)
259 unsigned char buffer
[0x100];
263 ERR("called %s, calling %s\n",called
,calling
);
265 buffer
[0] = NB_SESSION_REQ
;
266 buffer
[1] = NB_FIRST
;
268 netbios_name(called
, &buffer
[NB_HDRSIZE
]);
270 netbios_name(calling
, &buffer
[NB_HDRSIZE
+len
]);
273 NBR_ADDWORD(&buffer
[2],len
);
275 /* for(i=0; i<(len+NB_HDRSIZE); i++)
276 DPRINTF("%02X%c",buffer[i],(((i+1)!=(len+4))&&((i+1)%16))?' ':'\n'); */
278 r
= write(fd
,buffer
,len
+4);
281 ERR("Write failed\n");
289 r
= poll(&fds
,1,NB_TIMEOUT
);
292 ERR("Poll failed\n");
296 r
= read(fd
, buffer
, NB_HDRSIZE
);
297 if((r
!=NB_HDRSIZE
) || (buffer
[0]!=0x82))
299 ERR("Received %d bytes\n",r
);
300 ERR("%02x %02x %02x %02x\n", buffer
[0],buffer
[1],buffer
[2],buffer
[3]);
307 static BOOL
NB_SendData(int fd
, unsigned char *data
, int size
)
309 unsigned char buffer
[NB_HDRSIZE
];
312 /* CHECK: is it always OK to do this in two writes? */
313 /* perhaps use scatter gather sendmsg instead? */
315 buffer
[0] = NB_SESSION_MSG
;
316 buffer
[1] = NB_FIRST
;
317 NBR_ADDWORD(&buffer
[2],size
);
319 r
= write(fd
, buffer
, NB_HDRSIZE
);
323 r
= write(fd
, data
, size
);
326 ERR("write failed\n");
333 static BOOL
NB_RecvData(int fd
, unsigned char *data
, int *outlen
)
336 unsigned char buffer
[NB_HDRSIZE
];
338 r
= read(fd
, buffer
, NB_HDRSIZE
);
339 if((r
!=NB_HDRSIZE
) || (buffer
[0]!=NB_SESSION_MSG
))
341 ERR("Received %d bytes\n",r
);
345 len
= NBR_GETWORD(&buffer
[2]);
346 r
= read(fd
, data
, len
);
349 ERR("Received %d bytes\n",r
);
357 static BOOL
NB_Transaction(int fd
, unsigned char *buffer
, int len
, int *outlen
)
362 DPRINTF("Sending request:\n");
364 DPRINTF("%02X%c",buffer
[i
],(((i
+1)!=len
)&&((i
+1)%16))?' ':'\n');
366 if(!NB_SendData(fd
,buffer
,len
))
373 r
= poll(&fds
,1,NB_TIMEOUT
);
376 ERR("Poll failed\n");
380 if(!NB_RecvData(fd
, buffer
, outlen
))
384 DPRINTF("Got response:\n");
386 DPRINTF("%02X%c",buffer
[i
],(((i
+1)!=len
)&&((i
+1)%16))?' ':'\n');
391 #define SMB_ADDHEADER(b,l) { b[(l)++]=0xff; b[(l)++]='S'; b[(l)++]='M'; b[(l)++]='B'; }
392 #define SMB_ADDERRINFO(b,l) { b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; }
393 #define SMB_ADDPADSIG(b,l) { memset(&b[l],0,12); l+=12; }
395 #define SMB_ERRCLASS 5
396 #define SMB_ERRCODE 7
397 #define SMB_TREEID 24
398 #define SMB_PROCID 26
399 #define SMB_USERID 28
400 #define SMB_PLEXID 30
401 #define SMB_PCOUNT 32
402 #define SMB_HDRSIZE 33
404 static DWORD
SMB_GetError(unsigned char *buffer
)
406 if(buffer
[SMB_ERRCLASS
]==0)
407 return STATUS_SUCCESS
;
408 /* FIXME: return propper error codes */
409 return STATUS_INVALID_PARAMETER
;
412 static int SMB_Header(unsigned char *buffer
, unsigned char command
, USHORT tree_id
, USHORT user_id
)
418 SMB_ADDHEADER(buffer
,len
);
421 buffer
[len
++] = command
;
424 SMB_ADDERRINFO(buffer
,len
)
427 buffer
[len
++] = 0x00; /* flags */
428 SMB_ADDWORD(&buffer
[len
],1); len
+= 2; /* flags2 */
431 SMB_ADDPADSIG(buffer
,len
)
434 SMB_ADDWORD(&buffer
[len
],tree_id
); len
+= 2; /* treeid */
435 id
= GetCurrentThreadId();
436 SMB_ADDWORD(&buffer
[len
],id
); len
+= 2; /* process id */
437 SMB_ADDWORD(&buffer
[len
],user_id
); len
+= 2; /* user id */
438 SMB_ADDWORD(&buffer
[len
],SMB_MultiplexId
); len
+= 2; /* multiplex id */
444 static const char *SMB_ProtocolDialect
= "NT LM 0.12";
445 /* = "Windows for Workgroups 3.1a"; */
447 /* FIXME: support multiple SMB dialects */
448 static BOOL
SMB_NegotiateProtocol(int fd
, USHORT
*dialect
)
450 unsigned char buffer
[0x100];
455 memset(buffer
,0,sizeof buffer
);
457 len
= SMB_Header(buffer
, SMB_COM_NEGOTIATE
, 0, 0);
460 buffer
[len
++] = 0; /* no parameters */
463 buflen
= strlen(SMB_ProtocolDialect
)+2; /* include type and nul byte */
464 SMB_ADDWORD(&buffer
[len
],buflen
); len
+= 2;
467 strcpy(&buffer
[len
+1],SMB_ProtocolDialect
);
470 if(!NB_Transaction(fd
, buffer
, len
, &len
))
476 /* FIXME: check response */
477 if(SMB_GetError(buffer
))
479 ERR("returned error\n");
488 #define SMB_PARAM_COUNT(buffer) ((buffer)[SMB_PCOUNT])
489 #define SMB_PARAM(buffer,n) SMB_GETWORD(&(buffer)[SMB_HDRSIZE+2*(n)])
490 #define SMB_BUFFER_COUNT(buffer) SMB_GETWORD(buffer+SMB_HDRSIZE+2*SMB_PARAM_COUNT(buffer))
491 #define SMB_BUFFER(buffer,n) ((buffer)[SMB_HDRSIZE + 2*SMB_PARAM_COUNT(buffer) + 2 + (n) ])
493 static BOOL
SMB_SessionSetup(int fd
, USHORT
*userid
)
495 unsigned char buffer
[0x100];
499 memset(buffer
,0,sizeof buffer
);
501 len
= SMB_Header(buffer
, SMB_COM_SESSION_SETUP_ANDX
, 0, 0);
503 buffer
[len
++] = 0; /* no parameters? */
505 buffer
[len
++] = 0xff; /* AndXCommand: secondary request */
506 buffer
[len
++] = 0x00; /* AndXReserved */
507 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* AndXOffset */
508 SMB_ADDWORD(&buffer
[len
],0x400); len
+= 2; /* MaxBufferSize */
509 SMB_ADDWORD(&buffer
[len
],1); len
+= 2; /* MaxMpxCount */
510 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* VcNumber */
511 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* SessionKey */
512 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* SessionKey */
513 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* Password length */
514 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* Reserved */
515 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* Reserved */
517 /* FIXME: add name and password here */
518 buffer
[len
++] = 0; /* number of bytes in password */
520 if(!NB_Transaction(fd
, buffer
, len
, &len
))
523 if(SMB_GetError(buffer
))
526 pcount
= SMB_PARAM_COUNT(buffer
);
528 if( (SMB_HDRSIZE
+pcount
*2) > len
)
530 ERR("Bad parameter count %d\n",pcount
);
534 DPRINTF("SMB_COM_SESSION_SETUP response, %d args: ",pcount
);
535 for(i
=0; i
<pcount
; i
++)
536 DPRINTF("%04x ",SMB_PARAM(buffer
,i
));
539 bcount
= SMB_BUFFER_COUNT(buffer
);
540 if( (SMB_HDRSIZE
+pcount
*2+2+bcount
) > len
)
542 ERR("parameter count %x, buffer count %x, len %x\n",pcount
,bcount
,len
);
546 DPRINTF("response buffer %d bytes: ",bcount
);
547 for(i
=0; i
<bcount
; i
++)
549 unsigned char ch
= SMB_BUFFER(buffer
,i
);
550 DPRINTF("%c", isprint(ch
)?ch
:' ');
554 *userid
= SMB_GETWORD(&buffer
[SMB_USERID
]);
559 static BOOL
SMB_TreeConnect(int fd
, USHORT user_id
, LPCSTR share_name
, USHORT
*treeid
)
561 unsigned char buffer
[0x100];
564 ERR("%s\n",share_name
);
566 memset(buffer
,0,sizeof buffer
);
568 len
= SMB_Header(buffer
, SMB_COM_TREE_CONNECT
, 0, user_id
);
570 buffer
[len
++] = 4; /* parameters */
572 buffer
[len
++] = 0xff; /* AndXCommand: secondary request */
573 buffer
[len
++] = 0x00; /* AndXReserved */
574 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* AndXOffset */
575 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* Flags */
576 SMB_ADDWORD(&buffer
[len
],1); len
+= 2; /* Password length */
578 /* SMB command buffer */
579 SMB_ADDWORD(&buffer
[len
],3); len
+= 2; /* command buffer len */
580 buffer
[len
++] = 0; /* null terminated password */
582 slen
= strlen(share_name
);
583 if(slen
<(sizeof buffer
-len
))
584 strcpy(&buffer
[len
], share_name
);
589 /* name of the service */
592 if(!NB_Transaction(fd
, buffer
, len
, &len
))
595 if(SMB_GetError(buffer
))
598 *treeid
= SMB_GETWORD(&buffer
[SMB_TREEID
]);
600 ERR("OK, treeid = %04x\n", *treeid
);
605 static BOOL
SMB_NtCreateOpen(int fd
, USHORT tree_id
, USHORT user_id
, USHORT dialect
,
606 LPCSTR filename
, DWORD access
, DWORD sharing
,
607 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
608 DWORD attributes
, HANDLE
template, USHORT
*file_id
)
610 unsigned char buffer
[0x100];
613 ERR("%s\n",filename
);
615 memset(buffer
,0,sizeof buffer
);
617 len
= SMB_Header(buffer
, SMB_COM_NT_CREATE_ANDX
, tree_id
, user_id
);
620 buffer
[len
++] = 24; /* parameters */
622 buffer
[len
++] = 0xff; /* AndXCommand: secondary request */
623 buffer
[len
++] = 0x00; /* AndXReserved */
624 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* AndXOffset */
626 buffer
[len
++] = 0; /* reserved */
627 slen
= strlen(filename
);
628 SMB_ADDWORD(&buffer
[len
],slen
); len
+= 2; /* name length */
631 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* flags */
632 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* root directory fid */
634 SMB_ADDDWORD(&buffer
[len
],access
); len
+= 4; /* access */
635 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* allocation size */
637 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* root directory fid */
640 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* initial allocation */
641 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4;
644 SMB_ADDDWORD(&buffer
[len
],attributes
); len
+= 4; /* ExtFileAttributes*/
647 SMB_ADDDWORD(&buffer
[len
],sharing
); len
+= 4; /* ShareAccess */
650 ERR("creation = %08lx\n",creation
);
651 SMB_ADDDWORD(&buffer
[len
],creation
); len
+= 4; /* CreateDisposition */
654 SMB_ADDDWORD(&buffer
[len
],creation
); len
+= 4; /* CreateOptions */
657 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* Impersonation */
660 buffer
[len
++] = 0; /* security flags */
663 SMB_ADDWORD(&buffer
[len
],slen
); len
+= 2; /* size of buffer */
665 if(slen
<(sizeof buffer
-len
))
666 strcpy(&buffer
[len
], filename
);
671 /* name of the file */
674 if(!NB_Transaction(fd
, buffer
, len
, &len
))
677 if(SMB_GetError(buffer
))
683 /* *file_id = SMB_GETWORD(&buffer[xxx]); */
690 static USHORT
SMB_GetMode(DWORD access
, DWORD sharing
)
694 switch(access
&(GENERIC_READ
|GENERIC_WRITE
))
702 case (GENERIC_READ
|GENERIC_WRITE
):
703 mode
|= OF_READWRITE
;
707 switch(sharing
&(FILE_SHARE_READ
|FILE_SHARE_WRITE
))
709 case (FILE_SHARE_READ
|FILE_SHARE_WRITE
):
710 mode
|= OF_SHARE_DENY_NONE
;
712 case FILE_SHARE_READ
:
713 mode
|= OF_SHARE_DENY_WRITE
;
715 case FILE_SHARE_WRITE
:
716 mode
|= OF_SHARE_DENY_READ
;
719 mode
|= OF_SHARE_EXCLUSIVE
;
726 /* inverse of FILE_ConvertOFMode */
727 static BOOL
SMB_OpenAndX(int fd
, USHORT tree_id
, USHORT user_id
, USHORT dialect
,
728 LPCSTR filename
, DWORD access
, DWORD sharing
,
729 DWORD creation
, DWORD attributes
, USHORT
*file_id
)
731 unsigned char buffer
[0x100];
735 ERR("%s\n",filename
);
737 mode
= SMB_GetMode(access
,sharing
);
739 memset(buffer
,0,sizeof buffer
);
741 len
= SMB_Header(buffer
, SMB_COM_OPEN_ANDX
, tree_id
, user_id
);
744 buffer
[len
++] = 15; /* parameters */
745 buffer
[len
++] = 0xff; /* AndXCommand: secondary request */
746 buffer
[len
++] = 0x00; /* AndXReserved */
747 SMB_ADDWORD(buffer
+len
,0); len
+=2; /* AndXOffset */
748 SMB_ADDWORD(buffer
+len
,0); len
+=2; /* Flags */
749 SMB_ADDWORD(buffer
+len
,mode
); len
+=2; /* desired access */
750 SMB_ADDWORD(buffer
+len
,0); len
+=2; /* search attributes */
751 SMB_ADDWORD(buffer
+len
,0); len
+=2;
757 static BOOL
SMB_Open(int fd
, USHORT tree_id
, USHORT user_id
, USHORT dialect
,
758 LPCSTR filename
, DWORD access
, DWORD sharing
,
759 DWORD creation
, DWORD attributes
, USHORT
*file_id
)
761 unsigned char buffer
[0x100];
762 int len
= 0,slen
,pcount
,i
;
763 USHORT mode
= SMB_GetMode(access
,sharing
);
765 ERR("%s\n",filename
);
767 memset(buffer
,0,sizeof buffer
);
769 len
= SMB_Header(buffer
, SMB_COM_OPEN
, tree_id
, user_id
);
772 buffer
[len
++] = 2; /* parameters */
773 SMB_ADDWORD(buffer
+len
,mode
); len
+=2;
774 SMB_ADDWORD(buffer
+len
,0); len
+=2; /* search attributes */
776 slen
= strlen(filename
)+2; /* inc. nul and BufferFormat */
777 SMB_ADDWORD(buffer
+len
,slen
); len
+=2;
779 buffer
[len
] = 0x04; /* BufferFormat */
780 strcpy(&buffer
[len
+1],filename
);
783 if(!NB_Transaction(fd
, buffer
, len
, &len
))
786 if(SMB_GetError(buffer
))
789 pcount
= SMB_PARAM_COUNT(buffer
);
791 if( (SMB_HDRSIZE
+pcount
*2) > len
)
793 ERR("Bad parameter count %d\n",pcount
);
797 ERR("response, %d args: ",pcount
);
798 for(i
=0; i
<pcount
; i
++)
799 DPRINTF("%04x ",SMB_PARAM(buffer
,i
));
802 *file_id
= SMB_PARAM(buffer
,0);
804 ERR("file_id = %04x\n",*file_id
);
809 static BOOL
SMB_Read(int fd
, USHORT tree_id
, USHORT user_id
, USHORT dialect
, USHORT file_id
, DWORD offset
, LPVOID out
, USHORT count
, LPUSHORT read
)
811 unsigned char *buffer
;
812 int len
,buf_size
,n
,i
;
814 ERR("user %04x tree %04x file %04x count %04x offset %08lx\n",
815 user_id
, tree_id
, file_id
, count
, offset
);
817 buf_size
= count
+0x100;
818 buffer
= (unsigned char *) HeapAlloc(GetProcessHeap(),0,buf_size
);
820 memset(buffer
,0,buf_size
);
822 len
= SMB_Header(buffer
, SMB_COM_READ
, tree_id
, user_id
);
825 SMB_ADDWORD(&buffer
[len
],file_id
); len
+= 2;
826 SMB_ADDWORD(&buffer
[len
],count
); len
+= 2;
827 SMB_ADDDWORD(&buffer
[len
],offset
); len
+= 4;
828 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* how many more bytes will be read */
832 if(!NB_Transaction(fd
, buffer
, len
, &len
))
834 HeapFree(GetProcessHeap(),0,buffer
);
838 if(SMB_GetError(buffer
))
840 HeapFree(GetProcessHeap(),0,buffer
);
844 n
= SMB_PARAM_COUNT(buffer
);
846 if( (SMB_HDRSIZE
+n
*2) > len
)
848 HeapFree(GetProcessHeap(),0,buffer
);
849 ERR("Bad parameter count %d\n",n
);
853 ERR("response, %d args: ",n
);
855 DPRINTF("%04x ",SMB_PARAM(buffer
,i
));
858 n
= SMB_PARAM(buffer
,5) - 3;
862 memcpy( out
, &SMB_BUFFER(buffer
,3), n
);
864 ERR("Read %d bytes\n",n
);
867 HeapFree(GetProcessHeap(),0,buffer
);
872 static int SMB_GetSocket(LPCSTR host
)
875 struct sockaddr_in sin
;
878 ERR("host %s\n",host
);
880 if(NB_Lookup(host
,&sin
))
883 he
= gethostbyname(host
);
886 memcpy(&sin
.sin_addr
,he
->h_addr
, sizeof (sin
.sin_addr
));
890 /* FIXME: resolve by WINS too */
892 ERR("couldn't resolve SMB host %s\n", host
);
897 sin
.sin_family
= AF_INET
;
898 sin
.sin_port
= htons(139); /* netbios session */
900 fd
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
905 unsigned char *x
= (unsigned char *)&sin
.sin_addr
;
906 ERR("Connecting to %d.%d.%d.%d ...\n", x
[0],x
[1],x
[2],x
[3]);
908 r
= connect(fd
, &sin
, sizeof sin
);
910 if(!NB_SessionReq(fd
, "*SMBSERVER", "WINE"))
919 static BOOL
SMB_LoginAndConnect(int fd
, LPCSTR host
, LPCSTR share
, USHORT
*tree_id
, USHORT
*user_id
, USHORT
*dialect
)
923 ERR("host %s share %s\n",host
,share
);
925 if(!SMB_NegotiateProtocol(fd
, dialect
))
928 if(!SMB_SessionSetup(fd
, user_id
))
931 name
= HeapAlloc(GetProcessHeap(),0,strlen(host
)+strlen(share
)+5);
935 sprintf(name
,"\\\\%s\\%s",host
,share
);
936 if(!SMB_TreeConnect(fd
,*user_id
,name
,tree_id
))
938 HeapFree(GetProcessHeap(),0,name
);
945 static HANDLE
SMB_RegisterFile( int fd
, USHORT tree_id
, USHORT user_id
, USHORT dialect
, USHORT file_id
)
950 wine_server_send_fd( fd
);
952 SERVER_START_REQ( create_smb
)
954 req
->tree_id
= tree_id
;
955 req
->user_id
= user_id
;
956 req
->file_id
= file_id
;
960 r
= wine_server_call_err( req
);
966 ERR("created wineserver smb object, handle = %04x\n",ret
);
968 SetLastError( ERROR_PATH_NOT_FOUND
);
973 HANDLE WINAPI
SMB_CreateFileA( LPCSTR uncname
, DWORD access
, DWORD sharing
,
974 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
975 DWORD attributes
, HANDLE
template )
978 USHORT tree_id
=0, user_id
=0, dialect
=0, file_id
=0;
979 LPSTR name
,host
,share
,file
;
980 HANDLE handle
= INVALID_HANDLE_VALUE
;
982 name
= HeapAlloc(GetProcessHeap(),0,lstrlenA(uncname
));
986 lstrcpyA(name
,uncname
);
988 if( !UNC_SplitName(name
, &host
, &share
, &file
) )
990 HeapFree(GetProcessHeap(),0,name
);
994 ERR("server is %s, share is %s, file is %s\n", host
, share
, file
);
996 fd
= SMB_GetSocket(host
);
1000 if(!SMB_LoginAndConnect(fd
, host
, share
, &tree_id
, &user_id
, &dialect
))
1004 if(!SMB_NtCreateOpen(fd
, tree_id
, user_id
, dialect
, file
,
1005 access
, sharing
, sa
, creation
, attributes
, template, &file_id
))
1008 ERR("CreateOpen failed\n");
1012 if(!SMB_Open(fd
, tree_id
, user_id
, dialect
, file
,
1013 access
, sharing
, creation
, attributes
, &file_id
))
1016 ERR("CreateOpen failed\n");
1020 handle
= SMB_RegisterFile(fd
, tree_id
, user_id
, dialect
, file_id
);
1023 ERR("register failed\n");
1028 HeapFree(GetProcessHeap(),0,name
);
1032 static BOOL
SMB_GetSmbInfo(HANDLE hFile
, USHORT
*tree_id
, USHORT
*user_id
, USHORT
*dialect
, USHORT
*file_id
, LPDWORD offset
)
1036 SERVER_START_REQ( get_smb_info
)
1038 req
->handle
= hFile
;
1041 r
= wine_server_call_err( req
);
1043 *tree_id
= reply
->tree_id
;
1045 *user_id
= reply
->user_id
;
1047 *file_id
= reply
->file_id
;
1049 *dialect
= reply
->dialect
;
1051 *offset
= reply
->offset
;
1058 static BOOL
SMB_SetOffset(HANDLE hFile
, DWORD offset
)
1062 ERR("offset = %08lx\n",offset
);
1064 SERVER_START_REQ( get_smb_info
)
1066 req
->handle
= hFile
;
1067 req
->flags
= SMBINFO_SET_OFFSET
;
1068 req
->offset
= offset
;
1070 r
= wine_server_call_err( req
);
1072 *offset = reply->offset; */
1079 BOOL WINAPI
SMB_ReadFile(HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
, LPDWORD bytesRead
, LPOVERLAPPED lpOverlapped
)
1082 DWORD total
, count
, offset
;
1083 USHORT user_id
, tree_id
, dialect
, file_id
, read
;
1086 ERR("%04x %p %ld %p\n", hFile
, buffer
, bytesToRead
, bytesRead
);
1088 if(!SMB_GetSmbInfo(hFile
, &tree_id
, &user_id
, &dialect
, &file_id
, &offset
))
1091 fd
= FILE_GetUnixHandle(hFile
, GENERIC_READ
);
1098 count
= bytesToRead
- total
;
1104 r
= SMB_Read(fd
, tree_id
, user_id
, dialect
, file_id
, offset
, buffer
, count
, &read
);
1112 if(total
>=bytesToRead
)
1120 if(!SMB_SetOffset(hFile
, offset
))