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
);
606 static BOOL
SMB_NtCreateOpen(int fd
, USHORT tree_id
, USHORT user_id
, USHORT dialect
,
607 LPCSTR filename
, DWORD access
, DWORD sharing
,
608 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
609 DWORD attributes
, HANDLE
template, USHORT
*file_id
)
611 unsigned char buffer
[0x100];
614 ERR("%s\n",filename
);
616 memset(buffer
,0,sizeof buffer
);
618 len
= SMB_Header(buffer
, SMB_COM_NT_CREATE_ANDX
, tree_id
, user_id
);
621 buffer
[len
++] = 24; /* parameters */
623 buffer
[len
++] = 0xff; /* AndXCommand: secondary request */
624 buffer
[len
++] = 0x00; /* AndXReserved */
625 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* AndXOffset */
627 buffer
[len
++] = 0; /* reserved */
628 slen
= strlen(filename
);
629 SMB_ADDWORD(&buffer
[len
],slen
); len
+= 2; /* name length */
632 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* flags */
633 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* root directory fid */
635 SMB_ADDDWORD(&buffer
[len
],access
); len
+= 4; /* access */
636 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* allocation size */
638 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* root directory fid */
641 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* initial allocation */
642 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4;
645 SMB_ADDDWORD(&buffer
[len
],attributes
); len
+= 4; /* ExtFileAttributes*/
648 SMB_ADDDWORD(&buffer
[len
],sharing
); len
+= 4; /* ShareAccess */
651 ERR("creation = %08lx\n",creation
);
652 SMB_ADDDWORD(&buffer
[len
],creation
); len
+= 4; /* CreateDisposition */
655 SMB_ADDDWORD(&buffer
[len
],creation
); len
+= 4; /* CreateOptions */
658 SMB_ADDDWORD(&buffer
[len
],0); len
+= 4; /* Impersonation */
661 buffer
[len
++] = 0; /* security flags */
664 SMB_ADDWORD(&buffer
[len
],slen
); len
+= 2; /* size of buffer */
666 if(slen
<(sizeof buffer
-len
))
667 strcpy(&buffer
[len
], filename
);
672 /* name of the file */
675 if(!NB_Transaction(fd
, buffer
, len
, &len
))
678 if(SMB_GetError(buffer
))
684 /* *file_id = SMB_GETWORD(&buffer[xxx]); */
692 static USHORT
SMB_GetMode(DWORD access
, DWORD sharing
)
696 switch(access
&(GENERIC_READ
|GENERIC_WRITE
))
704 case (GENERIC_READ
|GENERIC_WRITE
):
705 mode
|= OF_READWRITE
;
709 switch(sharing
&(FILE_SHARE_READ
|FILE_SHARE_WRITE
))
711 case (FILE_SHARE_READ
|FILE_SHARE_WRITE
):
712 mode
|= OF_SHARE_DENY_NONE
;
714 case FILE_SHARE_READ
:
715 mode
|= OF_SHARE_DENY_WRITE
;
717 case FILE_SHARE_WRITE
:
718 mode
|= OF_SHARE_DENY_READ
;
721 mode
|= OF_SHARE_EXCLUSIVE
;
729 /* inverse of FILE_ConvertOFMode */
730 static BOOL
SMB_OpenAndX(int fd
, USHORT tree_id
, USHORT user_id
, USHORT dialect
,
731 LPCSTR filename
, DWORD access
, DWORD sharing
,
732 DWORD creation
, DWORD attributes
, USHORT
*file_id
)
734 unsigned char buffer
[0x100];
738 ERR("%s\n",filename
);
740 mode
= SMB_GetMode(access
,sharing
);
742 memset(buffer
,0,sizeof buffer
);
744 len
= SMB_Header(buffer
, SMB_COM_OPEN_ANDX
, tree_id
, user_id
);
747 buffer
[len
++] = 15; /* parameters */
748 buffer
[len
++] = 0xff; /* AndXCommand: secondary request */
749 buffer
[len
++] = 0x00; /* AndXReserved */
750 SMB_ADDWORD(buffer
+len
,0); len
+=2; /* AndXOffset */
751 SMB_ADDWORD(buffer
+len
,0); len
+=2; /* Flags */
752 SMB_ADDWORD(buffer
+len
,mode
); len
+=2; /* desired access */
753 SMB_ADDWORD(buffer
+len
,0); len
+=2; /* search attributes */
754 SMB_ADDWORD(buffer
+len
,0); len
+=2;
761 static BOOL
SMB_Open(int fd
, USHORT tree_id
, USHORT user_id
, USHORT dialect
,
762 LPCSTR filename
, DWORD access
, DWORD sharing
,
763 DWORD creation
, DWORD attributes
, USHORT
*file_id
)
765 unsigned char buffer
[0x100];
766 int len
= 0,slen
,pcount
,i
;
767 USHORT mode
= SMB_GetMode(access
,sharing
);
769 ERR("%s\n",filename
);
771 memset(buffer
,0,sizeof buffer
);
773 len
= SMB_Header(buffer
, SMB_COM_OPEN
, tree_id
, user_id
);
776 buffer
[len
++] = 2; /* parameters */
777 SMB_ADDWORD(buffer
+len
,mode
); len
+=2;
778 SMB_ADDWORD(buffer
+len
,0); len
+=2; /* search attributes */
780 slen
= strlen(filename
)+2; /* inc. nul and BufferFormat */
781 SMB_ADDWORD(buffer
+len
,slen
); len
+=2;
783 buffer
[len
] = 0x04; /* BufferFormat */
784 strcpy(&buffer
[len
+1],filename
);
787 if(!NB_Transaction(fd
, buffer
, len
, &len
))
790 if(SMB_GetError(buffer
))
793 pcount
= SMB_PARAM_COUNT(buffer
);
795 if( (SMB_HDRSIZE
+pcount
*2) > len
)
797 ERR("Bad parameter count %d\n",pcount
);
801 ERR("response, %d args: ",pcount
);
802 for(i
=0; i
<pcount
; i
++)
803 DPRINTF("%04x ",SMB_PARAM(buffer
,i
));
806 *file_id
= SMB_PARAM(buffer
,0);
808 ERR("file_id = %04x\n",*file_id
);
813 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
)
815 unsigned char *buffer
;
816 int len
,buf_size
,n
,i
;
818 ERR("user %04x tree %04x file %04x count %04x offset %08lx\n",
819 user_id
, tree_id
, file_id
, count
, offset
);
821 buf_size
= count
+0x100;
822 buffer
= (unsigned char *) HeapAlloc(GetProcessHeap(),0,buf_size
);
824 memset(buffer
,0,buf_size
);
826 len
= SMB_Header(buffer
, SMB_COM_READ
, tree_id
, user_id
);
829 SMB_ADDWORD(&buffer
[len
],file_id
); len
+= 2;
830 SMB_ADDWORD(&buffer
[len
],count
); len
+= 2;
831 SMB_ADDDWORD(&buffer
[len
],offset
); len
+= 4;
832 SMB_ADDWORD(&buffer
[len
],0); len
+= 2; /* how many more bytes will be read */
836 if(!NB_Transaction(fd
, buffer
, len
, &len
))
838 HeapFree(GetProcessHeap(),0,buffer
);
842 if(SMB_GetError(buffer
))
844 HeapFree(GetProcessHeap(),0,buffer
);
848 n
= SMB_PARAM_COUNT(buffer
);
850 if( (SMB_HDRSIZE
+n
*2) > len
)
852 HeapFree(GetProcessHeap(),0,buffer
);
853 ERR("Bad parameter count %d\n",n
);
857 ERR("response, %d args: ",n
);
859 DPRINTF("%04x ",SMB_PARAM(buffer
,i
));
862 n
= SMB_PARAM(buffer
,5) - 3;
866 memcpy( out
, &SMB_BUFFER(buffer
,3), n
);
868 ERR("Read %d bytes\n",n
);
871 HeapFree(GetProcessHeap(),0,buffer
);
876 static int SMB_GetSocket(LPCSTR host
)
879 struct sockaddr_in sin
;
882 ERR("host %s\n",host
);
884 if(NB_Lookup(host
,&sin
))
887 he
= gethostbyname(host
);
890 memcpy(&sin
.sin_addr
,he
->h_addr
, sizeof (sin
.sin_addr
));
894 /* FIXME: resolve by WINS too */
896 ERR("couldn't resolve SMB host %s\n", host
);
901 sin
.sin_family
= AF_INET
;
902 sin
.sin_port
= htons(139); /* netbios session */
904 fd
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
909 unsigned char *x
= (unsigned char *)&sin
.sin_addr
;
910 ERR("Connecting to %d.%d.%d.%d ...\n", x
[0],x
[1],x
[2],x
[3]);
912 r
= connect(fd
, &sin
, sizeof sin
);
914 if(!NB_SessionReq(fd
, "*SMBSERVER", "WINE"))
923 static BOOL
SMB_LoginAndConnect(int fd
, LPCSTR host
, LPCSTR share
, USHORT
*tree_id
, USHORT
*user_id
, USHORT
*dialect
)
927 ERR("host %s share %s\n",host
,share
);
929 if(!SMB_NegotiateProtocol(fd
, dialect
))
932 if(!SMB_SessionSetup(fd
, user_id
))
935 name
= HeapAlloc(GetProcessHeap(),0,strlen(host
)+strlen(share
)+5);
939 sprintf(name
,"\\\\%s\\%s",host
,share
);
940 if(!SMB_TreeConnect(fd
,*user_id
,name
,tree_id
))
942 HeapFree(GetProcessHeap(),0,name
);
949 static HANDLE
SMB_RegisterFile( int fd
, USHORT tree_id
, USHORT user_id
, USHORT dialect
, USHORT file_id
)
954 wine_server_send_fd( fd
);
956 SERVER_START_REQ( create_smb
)
958 req
->tree_id
= tree_id
;
959 req
->user_id
= user_id
;
960 req
->file_id
= file_id
;
964 r
= wine_server_call_err( req
);
970 ERR("created wineserver smb object, handle = %04x\n",ret
);
972 SetLastError( ERROR_PATH_NOT_FOUND
);
977 HANDLE WINAPI
SMB_CreateFileA( LPCSTR uncname
, DWORD access
, DWORD sharing
,
978 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
979 DWORD attributes
, HANDLE
template )
982 USHORT tree_id
=0, user_id
=0, dialect
=0, file_id
=0;
983 LPSTR name
,host
,share
,file
;
984 HANDLE handle
= INVALID_HANDLE_VALUE
;
986 name
= HeapAlloc(GetProcessHeap(),0,lstrlenA(uncname
));
990 lstrcpyA(name
,uncname
);
992 if( !UNC_SplitName(name
, &host
, &share
, &file
) )
994 HeapFree(GetProcessHeap(),0,name
);
998 ERR("server is %s, share is %s, file is %s\n", host
, share
, file
);
1000 fd
= SMB_GetSocket(host
);
1004 if(!SMB_LoginAndConnect(fd
, host
, share
, &tree_id
, &user_id
, &dialect
))
1008 if(!SMB_NtCreateOpen(fd
, tree_id
, user_id
, dialect
, file
,
1009 access
, sharing
, sa
, creation
, attributes
, template, &file_id
))
1012 ERR("CreateOpen failed\n");
1016 if(!SMB_Open(fd
, tree_id
, user_id
, dialect
, file
,
1017 access
, sharing
, creation
, attributes
, &file_id
))
1020 ERR("CreateOpen failed\n");
1024 handle
= SMB_RegisterFile(fd
, tree_id
, user_id
, dialect
, file_id
);
1027 ERR("register failed\n");
1032 HeapFree(GetProcessHeap(),0,name
);
1036 static BOOL
SMB_GetSmbInfo(HANDLE hFile
, USHORT
*tree_id
, USHORT
*user_id
, USHORT
*dialect
, USHORT
*file_id
, LPDWORD offset
)
1040 SERVER_START_REQ( get_smb_info
)
1042 req
->handle
= hFile
;
1045 r
= wine_server_call_err( req
);
1047 *tree_id
= reply
->tree_id
;
1049 *user_id
= reply
->user_id
;
1051 *file_id
= reply
->file_id
;
1053 *dialect
= reply
->dialect
;
1055 *offset
= reply
->offset
;
1062 static BOOL
SMB_SetOffset(HANDLE hFile
, DWORD offset
)
1066 ERR("offset = %08lx\n",offset
);
1068 SERVER_START_REQ( get_smb_info
)
1070 req
->handle
= hFile
;
1071 req
->flags
= SMBINFO_SET_OFFSET
;
1072 req
->offset
= offset
;
1074 r
= wine_server_call_err( req
);
1076 *offset = reply->offset; */
1083 BOOL WINAPI
SMB_ReadFile(HANDLE hFile
, LPVOID buffer
, DWORD bytesToRead
, LPDWORD bytesRead
, LPOVERLAPPED lpOverlapped
)
1086 DWORD total
, count
, offset
;
1087 USHORT user_id
, tree_id
, dialect
, file_id
, read
;
1090 ERR("%04x %p %ld %p\n", hFile
, buffer
, bytesToRead
, bytesRead
);
1092 if(!SMB_GetSmbInfo(hFile
, &tree_id
, &user_id
, &dialect
, &file_id
, &offset
))
1095 fd
= FILE_GetUnixHandle(hFile
, GENERIC_READ
);
1102 count
= bytesToRead
- total
;
1108 r
= SMB_Read(fd
, tree_id
, user_id
, dialect
, file_id
, offset
, buffer
, count
, &read
);
1114 buffer
= (char*)buffer
+ read
;
1116 if(total
>=bytesToRead
)
1124 if(!SMB_SetOffset(hFile
, offset
))