SMB - free handles when connection fails, replace BOOL with bool
[libogc.git] / libtinysmb / smb.c
blob37078bd51cb8fdb610a4ccbc6f95fe81d2ef71b6
1 /****************************************************************************
2 * TinySMB
3 * Nintendo Wii/GameCube SMB implementation
5 * Copyright softdev
6 * Modified by Tantric to utilize NTLM authentication
7 * PathInfo added by rodries
8 * SMB devoptab by scip, rodries
10 * You will find WireShark (http://www.wireshark.org/)
11 * invaluable for debugging SAMBA implementations.
13 * Recommended Reading
14 * Implementing CIFS - Christopher R Hertel
15 * http://www.ubiqx.org/cifs/SMB.html
17 * License:
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2.1 of the License, or (at your option) any later version.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 ****************************************************************************/
34 #include <asm.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <malloc.h>
40 #include <ctype.h>
41 #include <wchar.h>
42 #include <gccore.h>
43 #include <network.h>
44 #include <processor.h>
45 #include <lwp_threads.h>
46 #include <lwp_objmgr.h>
47 #include <ogc/lwp_watchdog.h>
48 #include <sys/statvfs.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <smb.h>
53 #define IOS_O_NONBLOCK 0x04
54 #define RECV_TIMEOUT 3000 // in ms
55 #define CONN_TIMEOUT 6000
57 /**
58 * Field offsets.
60 #define SMB_OFFSET_PROTO 0
61 #define SMB_OFFSET_CMD 4
62 #define SMB_OFFSET_NTSTATUS 5
63 #define SMB_OFFSET_ECLASS 5
64 #define SMB_OFFSET_ECODE 7
65 #define SMB_OFFSET_FLAGS 9
66 #define SMB_OFFSET_FLAGS2 10
67 #define SMB_OFFSET_EXTRA 12
68 #define SMB_OFFSET_TID 24
69 #define SMB_OFFSET_PID 26
70 #define SMB_OFFSET_UID 28
71 #define SMB_OFFSET_MID 30
72 #define SMB_HEADER_SIZE 32 /*** SMB Headers are always 32 bytes long ***/
74 /**
75 * Message / Commands
77 #define NBT_SESSISON_MSG 0x00
79 #define SMB_NEG_PROTOCOL 0x72
80 #define SMB_SETUP_ANDX 0x73
81 #define SMB_TREEC_ANDX 0x75
84 #define NBT_KEEPALIVE_MSG 0x85
85 #define KEEPALIVE_SIZE 4
87 /**
88 * SMBTrans2
90 #define SMB_TRANS2 0x32
92 #define SMB_OPEN2 0
93 #define SMB_FIND_FIRST2 1
94 #define SMB_FIND_NEXT2 2
95 #define SMB_QUERY_FS_INFO 3
96 #define SMB_QUERY_PATH_INFO 5
97 #define SMB_SET_PATH_INFO 6
98 #define SMB_QUERY_FILE_INFO 7
99 #define SMB_SET_FILE_INFO 8
100 #define SMB_CREATE_DIR 13
101 #define SMB_FIND_CLOSE2 0x34
102 #define SMB_QUERY_FILE_ALL_INFO 0x107
105 * File I/O
107 #define SMB_OPEN_ANDX 0x2d
108 #define SMB_WRITE_ANDX 0x2f
109 #define SMB_READ_ANDX 0x2e
110 #define SMB_CLOSE 0x04
113 * SMB_COM
115 #define SMB_COM_CREATE_DIRECTORY 0x00
116 #define SMB_COM_DELETE_DIRECTORY 0x01
117 #define SMB_COM_DELETE 0x06
118 #define SMB_COM_RENAME 0x07
119 #define SMB_COM_QUERY_INFORMATION_DISK 0x80
122 * TRANS2 Offsets
124 #define T2_WORD_CNT (SMB_HEADER_SIZE)
125 #define T2_PRM_CNT (T2_WORD_CNT+1)
126 #define T2_DATA_CNT (T2_PRM_CNT+2)
127 #define T2_MAXPRM_CNT (T2_DATA_CNT+2)
128 #define T2_MAXBUFFER (T2_MAXPRM_CNT+2)
129 #define T2_SETUP_CNT (T2_MAXBUFFER+2)
130 #define T2_SPRM_CNT (T2_SETUP_CNT+10)
131 #define T2_SPRM_OFS (T2_SPRM_CNT+2)
132 #define T2_SDATA_CNT (T2_SPRM_OFS+2)
133 #define T2_SDATA_OFS (T2_SDATA_CNT+2)
134 #define T2_SSETUP_CNT (T2_SDATA_OFS+2)
135 #define T2_SUB_CMD (T2_SSETUP_CNT+2)
136 #define T2_BYTE_CNT (T2_SUB_CMD+2)
139 #define SMB_PROTO 0x424d53ff
140 #define SMB_HANDLE_NULL 0xffffffff
141 #define SMB_MAX_NET_READ_SIZE (16*1024) // see smb_recv
142 #define SMB_MAX_NET_WRITE_SIZE 4096 // see smb_sendv
143 #define SMB_MAX_TRANSMIT_SIZE 65472
145 #define CAP_LARGE_FILES 0x00000008 // 64-bit file sizes and offsets supported
146 #define CAP_UNICODE 0x00000004 // Unicode supported
147 #define CIFS_FLAGS1 0x08 // Paths are caseless
148 #define CIFS_FLAGS2_UNICODE 0x8001 // Server may return long components in paths in the response - use 0x8001 for Unicode support
149 #define CIFS_FLAGS2 0x0001 // Server may return long components in paths in the response - use 0x0001 for ASCII support
151 #define SMB_CONNHANDLES_MAX 8
152 #define SMB_FILEHANDLES_MAX (32*SMB_CONNHANDLES_MAX)
154 #define SMB_OBJTYPE_HANDLE 7
155 #define SMB_CHECK_HANDLE(hndl) \
157 if(((hndl)==SMB_HANDLE_NULL) || (LWP_OBJTYPE(hndl)!=SMB_OBJTYPE_HANDLE)) \
158 return NULL; \
161 /* NBT Session Service Packet Type Codes
164 #define SESS_MSG 0x00
165 #define SESS_REQ 0x81
166 #define SESS_POS_RESP 0x82
167 #define SESS_NEG_RESP 0x83
168 #define SESS_RETARGET 0x84
169 #define SESS_KEEPALIVE 0x85
171 struct _smbfile
173 lwp_node node;
174 u16 sfid;
175 SMBCONN conn;
179 * NBT/SMB Wrapper
181 typedef struct _nbtsmb
183 u8 msg; /*** NBT Message ***/
184 u8 length_high;
185 u16 length; /*** Length, excluding NBT ***/
186 u8 smb[SMB_MAX_TRANSMIT_SIZE+128];
187 } NBTSMB;
190 * Session Information
192 typedef struct _smbsession
194 u16 TID;
195 u16 PID;
196 u16 UID;
197 u16 MID;
198 u32 sKey;
199 u32 capabilities;
200 u32 MaxBuffer;
201 u16 MaxMpx;
202 u16 MaxVCS;
203 u8 challenge[10];
204 u8 p_domain[64];
205 s64 timeOffset;
206 u16 count;
207 u16 eos;
208 bool challengeUsed;
209 u8 securityLevel;
210 } SMBSESSION;
212 typedef struct _smbhandle
214 lwp_obj object;
215 char *user;
216 char *pwd;
217 char *share_name;
218 char *server_name;
219 s32 sck_server;
220 struct sockaddr_in server_addr;
221 bool conn_valid;
222 SMBSESSION session;
223 NBTSMB message;
224 bool unicode;
225 } SMBHANDLE;
227 static u32 smb_dialectcnt = 1;
228 static bool smb_inited = false;
229 static lwp_objinfo smb_handle_objects;
230 static lwp_queue smb_filehandle_queue;
231 static struct _smbfile smb_filehandles[SMB_FILEHANDLES_MAX];
232 static const char *smb_dialects[] = {"NT LM 0.12",NULL};
234 extern void ntlm_smb_nt_encrypt(const char *passwd, const u8 * challenge, u8 * answer);
236 // UTF conversion functions
237 size_t utf16_to_utf8(char* dst, char* src, size_t len)
239 mbstate_t ps;
240 size_t count = 0;
241 int bytes;
242 char buff[MB_CUR_MAX];
243 int i;
244 unsigned short c;
245 memset(&ps, 0, sizeof(mbstate_t));
247 while (count < len && *src != '\0')
249 c = *(src + 1) << 8 | *src; // little endian
250 if (c == 0)
251 break;
252 bytes = wcrtomb(buff, c, &ps);
253 if (bytes < 0)
255 *dst = '\0';
256 return -1;
258 if (bytes > 0)
260 for (i = 0; i < bytes; i++)
262 *dst++ = buff[i];
264 src += 2;
265 count += 2;
267 else
269 break;
272 *dst = '\0';
273 return count;
276 size_t utf8_to_utf16(char* dst, char* src, size_t len)
278 mbstate_t ps;
279 wchar_t tempWChar;
280 char *tempChar;
281 int bytes;
282 size_t count = 0;
283 tempChar = (char*) &tempWChar;
284 memset(&ps, 0, sizeof(mbstate_t));
286 while (count < len - 1 && src != '\0')
288 bytes = mbrtowc(&tempWChar, src, MB_CUR_MAX, &ps);
289 if (bytes > 0)
291 *dst = tempChar[3];
292 dst++;
293 *dst = tempChar[2];
294 dst++;
295 src += bytes;
296 count += 2;
298 else if (bytes == 0)
300 break;
302 else
304 *dst = '\0';
305 dst++;
306 *dst = '\0';
307 return -1;
310 *dst = '\0';
311 dst++;
312 *dst = '\0';
313 return count;
317 * SMB Endian aware supporting functions
319 * SMB always uses Intel Little-Endian values, so htons etc are
320 * of little or no use :) ... Thanks M$
323 /*** get unsigned char ***/
324 static __inline__ u8 getUChar(u8 *buffer,u32 offset)
326 return (u8)buffer[offset];
329 /*** set unsigned char ***/
330 static __inline__ void setUChar(u8 *buffer,u32 offset,u8 value)
332 buffer[offset] = value;
335 /*** get signed short ***/
336 static __inline__ s16 getShort(u8 *buffer,u32 offset)
338 return (s16)((buffer[offset+1]<<8)|(buffer[offset]));
341 /*** get unsigned short ***/
342 static __inline__ u16 getUShort(u8 *buffer,u32 offset)
344 return (u16)((buffer[offset+1]<<8)|(buffer[offset]));
347 /*** set unsigned short ***/
348 static __inline__ void setUShort(u8 *buffer,u32 offset,u16 value)
350 buffer[offset] = (value&0xff);
351 buffer[offset+1] = ((value&0xff00)>>8);
354 /*** get unsigned int ***/
355 static __inline__ u32 getUInt(u8 *buffer,u32 offset)
357 return (u32)((buffer[offset+3]<<24)|(buffer[offset+2]<<16)|(buffer[offset+1]<<8)|buffer[offset]);
360 /*** set unsigned int ***/
361 static __inline__ void setUInt(u8 *buffer,u32 offset,u32 value)
363 buffer[offset] = (value&0xff);
364 buffer[offset+1] = ((value&0xff00)>>8);
365 buffer[offset+2] = ((value&0xff0000)>>16);
366 buffer[offset+3] = ((value&0xff000000)>>24);
369 /*** get unsigned long long ***/
370 static __inline__ u64 getULongLong(u8 *buffer,u32 offset)
372 return (u64)(getUInt(buffer, offset) | (u64)getUInt(buffer, offset+4) << 32);
375 static __inline__ SMBHANDLE* __smb_handle_open(SMBCONN smbhndl)
377 u32 level;
378 SMBHANDLE *handle;
380 SMB_CHECK_HANDLE(smbhndl);
382 _CPU_ISR_Disable(level);
383 handle = (SMBHANDLE*)__lwp_objmgr_getnoprotection(&smb_handle_objects,LWP_OBJMASKID(smbhndl));
384 _CPU_ISR_Restore(level);
385 return handle;
389 static __inline__ void __smb_handle_free(SMBHANDLE *handle)
391 u32 level;
393 _CPU_ISR_Disable(level);
394 __lwp_objmgr_close(&smb_handle_objects,&handle->object);
395 __lwp_objmgr_free(&smb_handle_objects,&handle->object);
396 _CPU_ISR_Restore(level);
399 static void __smb_init()
401 smb_inited = true;
402 __lwp_objmgr_initinfo(&smb_handle_objects,SMB_CONNHANDLES_MAX,sizeof(SMBHANDLE));
403 __lwp_queue_initialize(&smb_filehandle_queue,smb_filehandles,SMB_FILEHANDLES_MAX,sizeof(struct _smbfile));
406 static SMBHANDLE* __smb_allocate_handle()
408 u32 level;
409 SMBHANDLE *handle;
411 _CPU_ISR_Disable(level);
412 handle = (SMBHANDLE*)__lwp_objmgr_allocate(&smb_handle_objects);
413 if(handle) {
414 handle->user = NULL;
415 handle->pwd = NULL;
416 handle->server_name = NULL;
417 handle->share_name = NULL;
418 handle->sck_server = INVALID_SOCKET;
419 handle->conn_valid = false;
420 __lwp_objmgr_open(&smb_handle_objects,&handle->object);
422 _CPU_ISR_Restore(level);
423 return handle;
426 static void __smb_free_handle(SMBHANDLE *handle)
428 if(handle->user) free(handle->user);
429 if(handle->pwd) free(handle->pwd);
430 if(handle->server_name) free(handle->server_name);
431 if(handle->share_name) free(handle->share_name);
433 handle->user = NULL;
434 handle->pwd = NULL;
435 handle->server_name = NULL;
436 handle->share_name = NULL;
437 handle->sck_server = INVALID_SOCKET;
439 __smb_handle_free(handle);
442 static void MakeSMBHeader(u8 command,u8 flags,u16 flags2,SMBHANDLE *handle)
444 u8 *ptr = handle->message.smb;
445 NBTSMB *nbt = &handle->message;
446 SMBSESSION *sess = &handle->session;
448 memset(nbt,0,sizeof(NBTSMB));
450 setUInt(ptr,SMB_OFFSET_PROTO,SMB_PROTO);
451 setUChar(ptr,SMB_OFFSET_CMD,command);
452 setUChar(ptr,SMB_OFFSET_FLAGS,flags);
453 setUShort(ptr,SMB_OFFSET_FLAGS2,flags2);
454 setUShort(ptr,SMB_OFFSET_TID,sess->TID);
455 setUShort(ptr,SMB_OFFSET_PID,sess->PID);
456 setUShort(ptr,SMB_OFFSET_UID,sess->UID);
457 setUShort(ptr,SMB_OFFSET_MID,sess->MID);
459 ptr[SMB_HEADER_SIZE] = 0;
463 * MakeTRANS2Hdr
465 static void MakeTRANS2Header(u8 subcommand,SMBHANDLE *handle)
467 u8 *ptr = handle->message.smb;
469 setUChar(ptr, T2_WORD_CNT, 15);
470 setUShort(ptr, T2_MAXPRM_CNT, 10);
471 setUShort(ptr, T2_MAXBUFFER, 16384);
472 setUChar(ptr, T2_SSETUP_CNT, 1);
473 setUShort(ptr, T2_SUB_CMD, subcommand);
477 * smb_send
479 * blocking call with timeout
480 * will return when ALL data has been sent. Number of bytes sent is returned.
481 * OR timeout. Timeout will return -1
482 * OR network error. -ve value will be returned
484 static inline s32 smb_send(s32 s,const void *data,s32 size)
486 u64 t1,t2;
487 s32 ret, len = size, nextsend;
489 t1=ticks_to_millisecs(gettime());
490 while(len>0)
492 nextsend=len;
494 if(nextsend>SMB_MAX_NET_WRITE_SIZE)
495 nextsend=SMB_MAX_NET_WRITE_SIZE; //optimized value
497 ret=net_send(s,data,nextsend,0);
498 if(ret==-EAGAIN)
500 t2=ticks_to_millisecs(gettime());
501 if( (t2 - t1) > RECV_TIMEOUT)
503 return -1; // timeout
505 usleep(100); // allow system to perform work. Stabilizes system
506 continue;
508 else if(ret<0)
510 return ret; // an error occurred
512 else
514 data+=ret;
515 len-=ret;
516 if(len==0) return size;
517 t1=ticks_to_millisecs(gettime());
519 usleep(100);
521 return size;
525 * smb_recv
527 * blocking call with timeout
528 * will return when ANY data has been read from socket. Number of bytes read is returned.
529 * OR timeout. Timeout will return -1
530 * OR network error. -ve value will be returned
532 static s32 smb_recv(s32 s,void *mem,s32 len)
534 s32 ret,read,readtotal=0;
535 u64 t1,t2;
537 t1=ticks_to_millisecs(gettime());
538 while(len > 0)
540 read=len;
541 if(read>SMB_MAX_NET_READ_SIZE)
542 read=SMB_MAX_NET_READ_SIZE; // optimized value
544 ret=net_recv(s,mem+readtotal,read,0);
545 if(ret>0)
547 readtotal+=ret;
548 len-=ret;
549 if(len==0) return readtotal;
551 else
553 if(ret!=-EAGAIN) return ret;
554 t2=ticks_to_millisecs(gettime());
555 if( (t2 - t1) > RECV_TIMEOUT) return -1;
557 usleep(1000);
559 return readtotal;
563 * SMBCheck
565 * Do very basic checking on the return SMB
566 * Read <readlen> bytes
567 * if <readlen>==0 then read a single SMB packet
568 * discard any non NBT_SESSISON_MSG packets along the way.
570 static s32 SMBCheck(u8 command,SMBHANDLE *handle)
572 s32 ret;
573 u8 *ptr = handle->message.smb;
574 NBTSMB *nbt = &handle->message;
575 u32 readlen;
576 u64 t1,t2;
578 if(handle->sck_server == INVALID_SOCKET) return SMB_ERROR;
580 memset(nbt,0xFF,sizeof(NBTSMB)); //NBT_SESSISON_MSG is 0x00 so fill mem with 0xFF
582 t1=ticks_to_millisecs(gettime());
584 /*keep going till we get a NBT session message*/
586 ret=smb_recv(handle->sck_server, (u8*)nbt, 4);
587 if(ret!=4) return SMB_ERROR;
589 if(nbt->msg!=NBT_SESSISON_MSG)
591 readlen=(u32)((nbt->length_high<<16)|nbt->length);
592 if(readlen>0)
594 t1=ticks_to_millisecs(gettime());
595 smb_recv(handle->sck_server, ptr, readlen); //clear unexpected NBT message
598 t2=ticks_to_millisecs(gettime());
599 if( (t2 - t1) > RECV_TIMEOUT * 2) return SMB_ERROR;
601 } while(nbt->msg!=NBT_SESSISON_MSG);
603 /* obtain required length from NBT header if readlen==0*/
604 readlen=(u32)((nbt->length_high<<16)|nbt->length);
606 // Get server message block
607 ret=smb_recv(handle->sck_server, ptr, readlen);
608 if(readlen!=ret) return SMB_ERROR;
610 /*** Do basic SMB Header checks ***/
611 ret = getUInt(ptr,SMB_OFFSET_PROTO);
612 if(ret!=SMB_PROTO) return SMB_BAD_PROTOCOL;
614 ret = getUChar(ptr, SMB_OFFSET_CMD);
615 if(ret!=command) return SMB_BAD_COMMAND;
617 ret = getUInt(ptr,SMB_OFFSET_NTSTATUS);
618 if(ret) return SMB_ERROR;
620 return SMB_SUCCESS;
624 * SMB_SetupAndX
626 * Setup the SMB session, including authentication with the
627 * magic 'NTLM Response'
629 static s32 SMB_SetupAndX(SMBHANDLE *handle)
631 s32 pos;
632 s32 bcpos;
633 s32 i, ret;
634 u8 *ptr = handle->message.smb;
635 SMBSESSION *sess = &handle->session;
636 char pwd[30], ntRespData[24];
638 if(handle->sck_server == INVALID_SOCKET) return SMB_ERROR;
640 MakeSMBHeader(SMB_SETUP_ANDX,CIFS_FLAGS1,handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
641 pos = SMB_HEADER_SIZE;
643 setUChar(ptr,pos,13);
644 pos++; /*** Word Count ***/
645 setUChar(ptr,pos,0xff);
646 pos++; /*** Next AndX ***/
647 setUChar(ptr,pos,0);
648 pos++; /*** Reserved ***/
649 pos += 2; /*** Next AndX Offset ***/
650 setUShort(ptr,pos,sess->MaxBuffer);
651 pos += 2;
652 setUShort(ptr,pos,sess->MaxMpx);
653 pos += 2;
654 setUShort(ptr,pos,sess->MaxVCS);
655 pos += 2;
656 setUInt(ptr,pos,sess->sKey);
657 pos += 4;
658 setUShort(ptr,pos,24); /*** Password length (case-insensitive) ***/
659 pos += 2;
660 setUShort(ptr,pos,24); /*** Password length (case-sensitive) ***/
661 pos += 2;
662 setUInt(ptr,pos,0);
663 pos += 4; /*** Reserved ***/
664 setUInt(ptr,pos,sess->capabilities);
665 pos += 4; /*** Capabilities ***/
666 bcpos = pos;
667 pos += 2; /*** Byte count ***/
669 /*** The magic 'NTLM Response' ***/
670 strcpy(pwd, handle->pwd);
671 if (sess->challengeUsed)
672 ntlm_smb_nt_encrypt((const char *) pwd, (const u8 *) sess->challenge, (u8*) ntRespData);
674 /*** Build information ***/
675 memset(&ptr[pos],0,24);
676 pos += 24;
677 memcpy(&ptr[pos],ntRespData,24);
678 pos += 24;
679 pos++;
680 /*** Account ***/
681 strcpy(pwd, handle->user);
682 for(i=0;i<strlen(pwd);i++)
683 pwd[i] = toupper((int)pwd[i]);
684 if(handle->unicode)
686 pos += utf8_to_utf16((char*)&ptr[pos],pwd,SMB_MAXPATH-2);
687 pos += 2;
689 else
691 memcpy(&ptr[pos],pwd,strlen(pwd));
692 pos += strlen(pwd)+1;
695 /*** Primary Domain ***/
696 if(handle->user[0]=='\0') sess->p_domain[0] = '\0';
697 if(handle->unicode)
699 pos += utf8_to_utf16((char*)&ptr[pos],(char*)sess->p_domain,SMB_MAXPATH-2);
700 pos += 2;
702 else
704 memcpy(&ptr[pos],sess->p_domain,strlen((const char*)sess->p_domain));
705 pos += strlen((const char*)sess->p_domain)+1;
708 /*** Native OS ***/
709 strcpy(pwd,"Unix (libOGC)");
710 if(handle->unicode)
712 pos += utf8_to_utf16((char*)&ptr[pos],pwd,SMB_MAXPATH-2);
713 pos += 2;
715 else
717 memcpy(&ptr[pos],pwd,strlen(pwd));
718 pos += strlen(pwd)+1;
721 /*** Native LAN Manager ***/
722 strcpy(pwd,"Nintendo Wii");
723 if(handle->unicode)
725 pos += utf8_to_utf16((char*)&ptr[pos],pwd,SMB_MAXPATH-2);
726 pos += 2;
728 else
730 memcpy(&ptr[pos],pwd,strlen(pwd));
731 pos += strlen (pwd)+1;
734 /*** Update byte count ***/
735 setUShort(ptr,bcpos,((pos-bcpos)-2));
737 handle->message.msg = NBT_SESSISON_MSG;
738 handle->message.length = htons (pos);
739 pos += 4;
741 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
742 if(ret<=0) return SMB_ERROR;
744 if((ret=SMBCheck(SMB_SETUP_ANDX,handle))==SMB_SUCCESS) {
745 /*** Collect UID ***/
746 sess->UID = getUShort(handle->message.smb,SMB_OFFSET_UID);
747 return SMB_SUCCESS;
749 return ret;
753 * SMB_TreeAndX
755 * Finally, net_connect to the remote share
757 static s32 SMB_TreeAndX(SMBHANDLE *handle)
759 s32 pos, bcpos, ret;
760 char path[512];
761 u8 *ptr = handle->message.smb;
762 SMBSESSION *sess = &handle->session;
764 if(handle->sck_server == INVALID_SOCKET) return SMB_ERROR;
766 MakeSMBHeader(SMB_TREEC_ANDX,CIFS_FLAGS1,handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
767 pos = SMB_HEADER_SIZE;
769 setUChar(ptr,pos,4);
770 pos++; /*** Word Count ***/
771 setUChar(ptr,pos,0xff);
772 pos++; /*** Next AndX ***/
773 pos++; /*** Reserved ***/
774 pos += 2; /*** Next AndX Offset ***/
775 pos += 2; /*** Flags ***/
776 setUShort(ptr,pos,1);
777 pos += 2; /*** Password Length ***/
778 bcpos = pos;
779 pos += 2;
780 pos++; /*** NULL Password ***/
782 /*** Build server share path ***/
783 strcpy ((char*)path, "\\\\");
784 strcat ((char*)path, handle->server_name);
785 strcat ((char*)path, "\\");
786 strcat ((char*)path, handle->share_name);
788 for(ret=0;ret<strlen((const char*)path);ret++)
789 path[ret] = (char)toupper((int)path[ret]);
791 if(handle->unicode)
793 pos += utf8_to_utf16((char*)&ptr[pos],path,SMB_MAXPATH-2);
794 pos += 2;
796 else
798 memcpy(&ptr[pos],path,strlen((const char*)path));
799 pos += strlen((const char*)path)+1;
802 /*** Service ***/
803 strcpy((char*)path,"?????");
804 memcpy(&ptr[pos],path,strlen((const char*)path));
805 pos += strlen((const char*)path)+1;
808 /*** Update byte count ***/
809 setUShort(ptr,bcpos,(pos-bcpos)-2);
811 handle->message.msg = NBT_SESSISON_MSG;
812 handle->message.length = htons (pos);
813 pos += 4;
815 ret = smb_send(handle->sck_server,(char *)&handle->message,pos);
816 if(ret<=0) return SMB_ERROR;
818 if((ret=SMBCheck(SMB_TREEC_ANDX,handle))==SMB_SUCCESS) {
819 /*** Collect Tree ID ***/
820 sess->TID = getUShort(handle->message.smb,SMB_OFFSET_TID);
821 return SMB_SUCCESS;
823 return ret;
827 * SMB_NegotiateProtocol
829 * The only protocol we admit to is 'NT LM 0.12'
831 static s32 SMB_NegotiateProtocol(const char *dialects[],int dialectc,SMBHANDLE *handle)
833 u8 *ptr;
834 s32 pos;
835 s32 bcnt,i,j;
836 s32 ret,len;
837 u16 bytecount;
838 u32 serverMaxBuffer;
839 SMBSESSION *sess;
841 if(!handle || !dialects || dialectc<=0)
842 return SMB_ERROR;
844 if(handle->sck_server == INVALID_SOCKET) return SMB_ERROR;
846 /*** Clear session variables ***/
847 sess = &handle->session;
848 memset(sess,0,sizeof(SMBSESSION));
849 sess->PID = 0xdead;
850 sess->MID = 1;
851 sess->capabilities = 0;
853 MakeSMBHeader(SMB_NEG_PROTOCOL,CIFS_FLAGS1,handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
855 pos = SMB_HEADER_SIZE+3;
856 ptr = handle->message.smb;
857 for(i=0,bcnt=0;i<dialectc;i++) {
858 len = strlen(dialects[i])+1;
859 ptr[pos++] = '\x02';
860 memcpy(&ptr[pos],dialects[i],len);
861 pos += len;
862 bcnt += len+1;
864 /*** Update byte count ***/
865 setUShort(ptr,(SMB_HEADER_SIZE+1),bcnt);
867 /*** Set NBT information ***/
868 handle->message.msg = NBT_SESSISON_MSG;
869 handle->message.length = htons(pos);
870 pos += 4;
872 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
873 if(ret<=0) return SMB_ERROR;
875 /*** Check response ***/
876 if((ret=SMBCheck(SMB_NEG_PROTOCOL,handle))==SMB_SUCCESS)
878 pos = SMB_HEADER_SIZE;
879 ptr = handle->message.smb;
881 /*** Collect information ***/
882 if(getUChar(ptr,pos)!=17) return SMB_PROTO_FAIL; // UCHAR WordCount; Count of parameter words = 17
884 pos++;
885 if(getUShort(ptr,pos)!=0) return SMB_PROTO_FAIL; // USHORT DialectIndex; Index of selected dialect - should always be 0 since we only supplied 1!
887 pos += 2;
888 if(getUChar(ptr,pos) & 1)
890 // user level security
891 sess->securityLevel = 1;
893 else
895 // share level security - can we skip SetupAndX? If so, we would need to specify the password in TreeAndX
896 sess->securityLevel = 0;
899 pos++;
900 sess->MaxMpx = getUShort(ptr, pos); //USHORT MaxMpxCount; Max pending outstanding requests
902 pos += 2;
903 sess->MaxVCS = getUShort(ptr, pos); //USHORT MaxNumberVcs; Max VCs between client and server
905 pos += 2;
906 serverMaxBuffer = getUInt(ptr, pos); //ULONG MaxBufferSize; Max transmit buffer size
909 if(serverMaxBuffer>SMB_MAX_TRANSMIT_SIZE)
910 sess->MaxBuffer = SMB_MAX_TRANSMIT_SIZE;
911 else
912 sess->MaxBuffer = serverMaxBuffer;
913 pos += 4;
914 pos += 4; //ULONG MaxRawSize; Maximum raw buffer size
915 sess->sKey = getUInt(ptr,pos); pos += 4;
916 u32 servcap = getUInt(ptr,pos); pos += 4; //ULONG Capabilities; Server capabilities
917 pos += 4; //ULONG SystemTimeLow; System (UTC) time of the server (low).
918 pos += 4; //ULONG SystemTimeHigh; System (UTC) time of the server (high).
919 sess->timeOffset = getShort(ptr,pos) * 600000000LL; pos += 2; //SHORT ServerTimeZone; Time zone of server (minutes from UTC)
921 //UCHAR EncryptionKeyLength - 0 or 8
922 if(getUChar(ptr,pos)!=8)
924 if (getUChar(ptr,pos)!=0)
926 return SMB_BAD_KEYLEN;
928 else
930 // Challenge key not used
931 sess->challengeUsed = false;
934 else
936 sess->challengeUsed = true;
939 pos++;
940 bytecount = getUShort(ptr,pos);
942 if (sess->challengeUsed)
944 /*** Copy challenge key ***/
945 pos += 2;
946 memcpy(&sess->challenge,&ptr[pos],8);
949 /*** Primary domain ***/
950 pos += 8;
951 i = j = 0;
952 while(ptr[pos+j]!=0) {
953 sess->p_domain[i] = ptr[pos+j];
954 j += 2;
955 i++;
957 sess->p_domain[i] = '\0';
959 // setup capabilities
960 //if(servcap & CAP_LARGE_FILES)
961 // sess->capabilities |= CAP_LARGE_FILES;
963 if(servcap & CAP_UNICODE)
965 sess->capabilities |= CAP_UNICODE;
966 handle->unicode = true;
969 return SMB_SUCCESS;
971 return ret;
974 static s32 do_netconnect(SMBHANDLE *handle)
976 u32 nodelay;
977 s32 ret;
978 s32 sock;
979 u64 t1,t2;
981 handle->sck_server = INVALID_SOCKET;
982 /*** Create the global net_socket ***/
983 sock = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
984 if(sock==INVALID_SOCKET) return -1;
986 // Switch off Nagle with TCP_NODELAY
987 nodelay = 1;
988 net_setsockopt(sock,IPPROTO_TCP,TCP_NODELAY,&nodelay,sizeof(nodelay));
990 // create non blocking socket
991 ret = net_fcntl(sock, F_GETFL, 0);
992 if (ret < 0)
994 net_close(sock);
995 return ret;
998 ret = net_fcntl(sock, F_SETFL, ret | IOS_O_NONBLOCK);
999 if (ret < 0)
1001 net_close(sock);
1002 return ret;
1005 t1=ticks_to_millisecs(gettime());
1006 while(1)
1008 ret = net_connect(sock,(struct sockaddr*)&handle->server_addr,sizeof(handle->server_addr));
1009 if(ret==-EISCONN) break;
1010 t2=ticks_to_millisecs(gettime());
1011 usleep(1000);
1012 if((t2-t1) > CONN_TIMEOUT) break; // usually not more than 90ms
1015 if(ret!=-EISCONN)
1017 net_close(sock);
1018 return -1;
1021 handle->sck_server = sock;
1022 return 0;
1025 static s32 do_smbconnect(SMBHANDLE *handle)
1027 s32 ret;
1029 if(handle->sck_server == INVALID_SOCKET) return -1;
1031 ret = SMB_NegotiateProtocol(smb_dialects,smb_dialectcnt,handle);
1032 if(ret!=SMB_SUCCESS)
1034 net_close(handle->sck_server);
1035 handle->sck_server = INVALID_SOCKET;
1036 return -1;
1039 ret = SMB_SetupAndX(handle);
1040 if(ret!=SMB_SUCCESS)
1042 net_close(handle->sck_server);
1043 handle->sck_server = INVALID_SOCKET;
1044 return -1;
1047 ret = SMB_TreeAndX(handle);
1048 if(ret!=SMB_SUCCESS)
1050 net_close(handle->sck_server);
1051 handle->sck_server = INVALID_SOCKET;
1052 return -1;
1055 handle->conn_valid = true;
1056 return 0;
1059 /****************************************************************************
1060 * Create an NBT SESSION REQUEST message.
1061 ****************************************************************************/
1062 static int MakeSessReq(unsigned char *bufr, unsigned char *Called, unsigned char *Calling)
1064 // Write the header.
1065 bufr[0] = SESS_REQ;
1066 bufr[1] = 0;
1067 bufr[2] = 0;
1068 bufr[3] = 68; // 2x34 bytes in length.
1070 // Copy the Called and Calling names into the buffer.
1071 (void) memcpy(&bufr[4], Called, 34);
1072 (void) memcpy(&bufr[38], Calling, 34);
1074 // Return the total message length.
1075 return 72;
1078 static unsigned char *L1_Encode(unsigned char *dst, const unsigned char *name,
1079 const unsigned char pad, const unsigned char sfx)
1081 int i = 0;
1082 int j = 0;
1083 int k = 0;
1085 while (('\0' != name[i]) && (i < 15))
1087 k = toupper(name[i++]);
1088 dst[j++] = 'A' + ((k & 0xF0) >> 4);
1089 dst[j++] = 'A' + (k & 0x0F);
1092 i = 'A' + ((pad & 0xF0) >> 4);
1093 k = 'A' + (pad & 0x0F);
1094 while (j < 30)
1096 dst[j++] = i;
1097 dst[j++] = k;
1100 dst[30] = 'A' + ((sfx & 0xF0) >> 4);
1101 dst[31] = 'A' + (sfx & 0x0F);
1102 dst[32] = '\0';
1104 return (dst);
1107 static int L2_Encode(unsigned char *dst, const unsigned char *name,
1108 const unsigned char pad, const unsigned char sfx,
1109 const unsigned char *scope)
1111 int lenpos;
1112 int i;
1113 int j;
1115 if (NULL == L1_Encode(&dst[1], name, pad, sfx))
1116 return (-1);
1118 dst[0] = 0x20;
1119 lenpos = 33;
1121 if ('\0' != *scope)
1125 for (i = 0, j = (lenpos + 1); ('.' != scope[i]) && ('\0'
1126 != scope[i]); i++, j++)
1127 dst[j] = toupper(scope[i]);
1129 dst[lenpos] = (unsigned char) i;
1130 lenpos += i + 1;
1131 scope += i;
1132 } while ('.' == *(scope++));
1134 dst[lenpos] = '\0';
1137 return (lenpos + 1);
1140 /****************************************************************************
1141 * Send an NBT SESSION REQUEST over the TCP connection, then wait for a reply.
1142 ****************************************************************************/
1143 static s32 SMB_RequestNBTSession(SMBHANDLE *handle)
1145 unsigned char Called[34];
1146 unsigned char Calling[34];
1147 unsigned char bufr[128];
1148 int result;
1150 if(handle->sck_server == INVALID_SOCKET) return -1;
1152 L2_Encode(Called, (const unsigned char*) "*SMBSERVER", 0x20, 0x20,
1153 (const unsigned char*) "");
1154 L2_Encode(Calling, (const unsigned char*) "SMBCLIENT", 0x20, 0x00,
1155 (const unsigned char*) "");
1157 // Create the NBT Session Request message.
1158 result = MakeSessReq(bufr, Called, Calling);
1160 //Send the NBT Session Request message.
1161 result = smb_send(handle->sck_server, bufr, result);
1162 if (result < 0)
1164 // Error sending Session Request message
1165 return -1;
1168 // Now wait for and handle the reply (2 seconds).
1169 result = smb_recv(handle->sck_server, bufr, 128);
1170 if (result <= 0)
1172 // Timeout waiting for NBT Session Response
1173 return -1;
1176 switch (*bufr)
1178 case SESS_POS_RESP:
1179 // Positive Session Response
1180 return 0;
1182 case SESS_NEG_RESP:
1183 // Negative Session Response
1184 return -1;
1186 case SESS_RETARGET:
1187 // Retarget Session Response
1188 return -1;
1190 default:
1191 // Unexpected Session Response
1192 return -1;
1196 /****************************************************************************
1197 * Primary setup, logon and connection all in one :)
1198 ****************************************************************************/
1199 s32 SMB_Connect(SMBCONN *smbhndl, const char *user, const char *password, const char *share, const char *server)
1201 s32 ret = 0;
1202 SMBHANDLE *handle;
1203 struct in_addr val;
1205 *smbhndl = SMB_HANDLE_NULL;
1207 if(!user || !password || !share || !server ||
1208 strlen(user) > 20 || strlen(password) > 14 ||
1209 strlen(share) > 80 || strlen(server) > 80)
1211 return SMB_BAD_LOGINDATA;
1214 if(!smb_inited)
1216 u32 level;
1217 _CPU_ISR_Disable(level);
1218 __smb_init();
1219 _CPU_ISR_Restore(level);
1222 handle = __smb_allocate_handle();
1223 if(!handle) return SMB_ERROR;
1225 handle->user = strdup(user);
1226 handle->pwd = strdup(password);
1227 handle->server_name = strdup(server);
1228 handle->share_name = strdup(share);
1229 handle->server_addr.sin_family = AF_INET;
1230 handle->server_addr.sin_port = htons(445);
1231 handle->unicode = false;
1233 if(strlen(server) < 16 && inet_aton(server, &val))
1235 handle->server_addr.sin_addr.s_addr = val.s_addr;
1237 else // might be a hostname
1239 #ifdef HW_RVL
1240 struct hostent *hp = net_gethostbyname(server);
1241 if (!hp || !(hp->h_addrtype == PF_INET))
1242 ret = SMB_BAD_LOGINDATA;
1243 else
1244 memcpy((char *)&handle->server_addr.sin_addr.s_addr, hp->h_addr_list[0], hp->h_length);
1245 #else
1246 __smb_free_handle(handle);
1247 return SMB_ERROR;
1248 #endif
1251 *smbhndl =(SMBCONN)(LWP_OBJMASKTYPE(SMB_OBJTYPE_HANDLE)|LWP_OBJMASKID(handle->object.id));
1253 if(ret==0)
1255 ret = do_netconnect(handle);
1256 if(ret==0) ret = do_smbconnect(handle);
1258 if(ret!=0)
1260 // try port 139
1261 handle->server_addr.sin_port = htons(139);
1262 ret = do_netconnect(handle);
1263 if(ret==0) ret = SMB_RequestNBTSession(handle);
1264 if(ret==0) ret = do_smbconnect(handle);
1267 if(ret!=0)
1269 __smb_free_handle(handle);
1270 return SMB_ERROR;
1273 return SMB_SUCCESS;
1276 /****************************************************************************
1277 * SMB_Destroy
1278 ****************************************************************************/
1279 void SMB_Close(SMBCONN smbhndl)
1281 SMBHANDLE *handle = __smb_handle_open(smbhndl);
1282 if(!handle) return;
1284 if(handle->sck_server!=INVALID_SOCKET)
1285 net_close(handle->sck_server);
1287 __smb_free_handle(handle);
1290 s32 SMB_Reconnect(SMBCONN *_smbhndl, bool test_conn)
1292 s32 ret = SMB_SUCCESS;
1293 SMBCONN smbhndl = *_smbhndl;
1294 SMBHANDLE *handle = __smb_handle_open(smbhndl);
1295 if(!handle)
1296 return SMB_ERROR; // we have no handle, so we can't reconnect
1298 if(handle->conn_valid && test_conn)
1300 SMBDIRENTRY dentry;
1301 if(SMB_PathInfo("\\", &dentry, smbhndl)==SMB_SUCCESS) return SMB_SUCCESS; // no need to reconnect
1302 handle->conn_valid = false; // else connection is invalid
1304 if(!handle->conn_valid)
1306 // shut down connection
1307 if(handle->sck_server!=INVALID_SOCKET)
1309 net_close(handle->sck_server);
1310 handle->sck_server = INVALID_SOCKET;
1313 // reconnect
1314 if(handle->server_addr.sin_port > 0)
1316 ret = do_netconnect(handle);
1317 if(ret==0 && handle->server_addr.sin_port == htons(139))
1318 ret = SMB_RequestNBTSession(handle);
1319 if(ret==0)
1320 ret = do_smbconnect(handle);
1322 else // initial connection
1324 handle->server_addr.sin_port = htons(445);
1325 ret = do_netconnect(handle);
1326 if(ret==0) ret = do_smbconnect(handle);
1328 if(ret != 0)
1330 // try port 139
1331 handle->server_addr.sin_port = htons(139);
1332 ret = do_netconnect(handle);
1333 if(ret==0) ret = SMB_RequestNBTSession(handle);
1334 if(ret==0) ret = do_smbconnect(handle);
1337 if(ret != 0)
1338 handle->server_addr.sin_port = 0;
1341 return ret;
1344 SMBFILE SMB_OpenFile(const char *filename, u16 access, u16 creation,SMBCONN smbhndl)
1346 s32 pos;
1347 s32 bpos,ret;
1348 u8 *ptr;
1349 struct _smbfile *fid = NULL;
1350 SMBHANDLE *handle;
1351 char realfile[512];
1353 if(filename == NULL)
1354 return NULL;
1356 if(SMB_Reconnect(&smbhndl,true)!=SMB_SUCCESS) return NULL;
1358 handle = __smb_handle_open(smbhndl);
1359 if(!handle) return NULL;
1361 MakeSMBHeader(SMB_OPEN_ANDX,CIFS_FLAGS1,handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
1363 pos = SMB_HEADER_SIZE;
1364 ptr = handle->message.smb;
1365 setUChar(ptr, pos, 15);
1366 pos++; /*** Word Count ***/
1367 setUChar(ptr, pos, 0xff);
1368 pos++; /*** AndXCommand 0xFF = None ***/
1369 setUChar(ptr, pos, 0);
1370 pos++; /*** AndX Reserved must be 0 ***/
1371 pos += 2; /*** Next AndX Offset to next Command ***/
1372 pos += 2; /*** Flags ***/
1373 setUShort(ptr, pos, access);
1374 pos += 2; /*** Access mode ***/
1375 setUShort(ptr, pos, 0x6);
1376 pos += 2; /*** Type of file ***/
1377 pos += 2; /*** File Attributes ***/
1378 pos += 4; /*** File time - don't care - let server decide ***/
1379 setUShort(ptr, pos, creation);
1380 pos += 2; /*** Creation flags ***/
1381 pos += 4; /*** Allocation size ***/
1382 setUInt(ptr, pos, 0);
1383 pos += 4; /*** Reserved[0] must be 0 ***/
1384 setUInt(ptr, pos, 0);
1385 pos += 4; /*** Reserved[1] must be 0 ***/
1386 pos += 2; /*** Byte Count ***/
1387 bpos = pos;
1388 setUChar(ptr, pos, 0x04); /** Bufferformat **/
1389 pos++;
1391 realfile[0]='\0';
1392 if (filename[0] != '\\')
1393 strcpy(realfile,"\\");
1394 strcat(realfile,filename);
1396 if(handle->unicode)
1398 pos += utf8_to_utf16((char*)&ptr[pos],realfile,SMB_MAXPATH-2);
1399 pos += 2;
1401 else
1403 memcpy(&ptr[pos],realfile,strlen(realfile));
1404 pos += strlen(realfile)+1;
1407 setUShort(ptr,(bpos-2),(pos-bpos));
1409 handle->message.msg = NBT_SESSISON_MSG;
1410 handle->message.length = htons(pos);
1412 pos += 4;
1413 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
1414 if(ret<0) goto failed;
1416 if(SMBCheck(SMB_OPEN_ANDX,handle)==SMB_SUCCESS) {
1417 /*** Check file handle ***/
1418 fid = (struct _smbfile*)__lwp_queue_get(&smb_filehandle_queue);
1419 if(fid) {
1420 fid->conn = smbhndl;
1421 fid->sfid = getUShort(handle->message.smb,(SMB_HEADER_SIZE+5));
1424 return (SMBFILE)fid;
1426 failed:
1427 handle->conn_valid = false;
1428 return NULL;
1432 * SMB_CloseFile
1434 void SMB_CloseFile(SMBFILE sfid)
1436 u8 *ptr;
1437 s32 pos, ret;
1438 SMBHANDLE *handle;
1439 struct _smbfile *fid = (struct _smbfile*)sfid;
1441 if(!fid) return;
1443 handle = __smb_handle_open(fid->conn);
1444 if(!handle) return;
1446 MakeSMBHeader(SMB_CLOSE,CIFS_FLAGS1,handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
1448 pos = SMB_HEADER_SIZE;
1449 ptr = handle->message.smb;
1450 setUChar(ptr, pos, 3);
1451 pos++; /** Word Count **/
1452 setUShort(ptr, pos, fid->sfid);
1453 pos += 2;
1454 setUInt(ptr, pos, 0xffffffff);
1455 pos += 4; /*** Last Write ***/
1456 pos += 2; /*** Byte Count ***/
1458 handle->message.msg = NBT_SESSISON_MSG;
1459 handle->message.length = htons(pos);
1461 pos += 4;
1462 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
1463 if(ret<0) handle->conn_valid = false;
1464 else SMBCheck(SMB_CLOSE,handle);
1465 __lwp_queue_append(&smb_filehandle_queue,&fid->node);
1469 * SMB_CreateDirectory
1471 s32 SMB_CreateDirectory(const char *dirname, SMBCONN smbhndl)
1473 s32 pos;
1474 s32 bpos,ret;
1475 u8 *ptr;
1476 SMBHANDLE *handle;
1477 char realfile[512];
1479 if(dirname == NULL)
1480 return -1;
1482 if(SMB_Reconnect(&smbhndl,true)!=SMB_SUCCESS) return -1;
1484 handle = __smb_handle_open(smbhndl);
1485 if(!handle) return -1;
1487 MakeSMBHeader(SMB_COM_CREATE_DIRECTORY,CIFS_FLAGS1, handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
1489 pos = SMB_HEADER_SIZE;
1490 ptr = handle->message.smb;
1491 setUChar(ptr, pos, 0);
1492 pos++; /*** Word Count ***/
1493 pos += 2; /*** Byte Count ***/
1494 bpos = pos;
1495 setUChar(ptr, pos, 0x04); /*** Buffer format ***/
1496 pos++;
1498 realfile[0]='\0';
1499 if (dirname[0] != '\\')
1500 strcpy(realfile,"\\");
1501 strcat(realfile,dirname);
1503 if(handle->unicode)
1505 pos += utf8_to_utf16((char*)&ptr[pos],realfile,SMB_MAXPATH-2);
1506 pos += 2;
1508 else
1510 memcpy(&ptr[pos],realfile,strlen(realfile));
1511 pos += strlen(realfile)+1;
1514 setUShort(ptr,(bpos-2),(pos-bpos));
1516 handle->message.msg = NBT_SESSISON_MSG;
1517 handle->message.length = htons(pos);
1519 pos += 4;
1520 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
1521 if(ret < 0) goto failed;
1523 ret = SMBCheck(SMB_COM_CREATE_DIRECTORY,handle);
1525 return ret;
1527 failed:
1528 return ret;
1532 * SMB_DeleteDirectory
1534 s32 SMB_DeleteDirectory(const char *dirname, SMBCONN smbhndl)
1536 s32 pos;
1537 s32 bpos,ret;
1538 u8 *ptr;
1539 SMBHANDLE *handle;
1540 char realfile[512];
1542 if(dirname == NULL)
1543 return -1;
1545 if(SMB_Reconnect(&smbhndl,true)!=SMB_SUCCESS) return -1;
1547 handle = __smb_handle_open(smbhndl);
1548 if(!handle) return -1;
1550 MakeSMBHeader(SMB_COM_DELETE_DIRECTORY,CIFS_FLAGS1, handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
1552 pos = SMB_HEADER_SIZE;
1553 ptr = handle->message.smb;
1554 setUChar(ptr, pos, 0);
1555 pos++; /*** Word Count ***/
1556 pos += 2; /*** Byte Count ***/
1557 bpos = pos;
1558 setUChar(ptr, pos, 0x04); /** Bufferformat **/
1559 pos++;
1561 realfile[0]='\0';
1562 if (dirname[0] != '\\')
1563 strcpy(realfile,"\\");
1564 strcat(realfile,dirname);
1566 if(handle->unicode)
1568 pos += utf8_to_utf16((char*)&ptr[pos],realfile,SMB_MAXPATH-2);
1569 pos += 2;
1571 else
1573 memcpy(&ptr[pos],realfile,strlen(realfile));
1574 pos += strlen(realfile)+1;
1577 setUShort(ptr,(bpos-2),(pos-bpos));
1579 handle->message.msg = NBT_SESSISON_MSG;
1580 handle->message.length = htons(pos);
1582 pos += 4;
1583 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
1584 if(ret < 0) goto failed;
1586 ret = SMBCheck(SMB_COM_DELETE_DIRECTORY,handle);
1588 return ret;
1590 failed:
1591 return ret;
1595 * SMB_DeleteFile
1597 s32 SMB_DeleteFile(const char *filename, SMBCONN smbhndl)
1599 s32 pos;
1600 s32 bpos,ret;
1601 u8 *ptr;
1602 SMBHANDLE *handle;
1603 char realfile[512];
1605 if(filename == NULL)
1606 return -1;
1608 if(SMB_Reconnect(&smbhndl,true)!=SMB_SUCCESS) return -1;
1610 handle = __smb_handle_open(smbhndl);
1611 if(!handle) return -1;
1613 MakeSMBHeader(SMB_COM_DELETE,CIFS_FLAGS1, handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
1615 pos = SMB_HEADER_SIZE;
1616 ptr = handle->message.smb;
1617 setUChar(ptr, pos, 1);
1618 pos++; /*** Word Count ***/
1619 setUShort(ptr, pos, SMB_SRCH_HIDDEN);
1620 pos += 2; /*** SearchAttributes ***/
1621 pos += 2; /*** Byte Count ***/
1622 bpos = pos;
1623 setUChar(ptr, pos, 0x04); /** Bufferformat **/
1624 pos++;
1626 realfile[0]='\0';
1627 if (filename[0] != '\\')
1628 strcpy(realfile,"\\");
1629 strcat(realfile,filename);
1631 if(handle->unicode)
1633 pos += utf8_to_utf16((char*)&ptr[pos],realfile,SMB_MAXPATH-2);
1634 pos += 2;
1636 else
1638 memcpy(&ptr[pos],realfile,strlen(realfile));
1639 pos += strlen(realfile)+1;
1642 setUShort(ptr,(bpos-2),(pos-bpos));
1644 handle->message.msg = NBT_SESSISON_MSG;
1645 handle->message.length = htons(pos);
1647 pos += 4;
1648 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
1649 if(ret < 0) goto failed;
1651 ret = SMBCheck(SMB_COM_DELETE,handle);
1653 return ret;
1655 failed:
1656 return ret;
1660 * SMB_Rename
1662 s32 SMB_Rename(const char *filename, const char * newfilename, SMBCONN smbhndl)
1664 s32 pos;
1665 s32 bpos,ret;
1666 u8 *ptr;
1667 SMBHANDLE *handle;
1668 char realfile[512];
1669 char newrealfile[512];
1671 if(filename == NULL || newfilename == NULL)
1672 return -1;
1674 if(SMB_Reconnect(&smbhndl,true)!=SMB_SUCCESS)
1675 return -1;
1677 handle = __smb_handle_open(smbhndl);
1678 if(!handle) return -1;
1680 MakeSMBHeader(SMB_COM_RENAME,CIFS_FLAGS1, handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
1682 pos = SMB_HEADER_SIZE;
1683 ptr = handle->message.smb;
1684 setUChar(ptr, pos, 1);
1685 pos++; /*** Word Count ***/
1686 setUShort(ptr, pos, SMB_SRCH_HIDDEN);
1687 pos += 2; /*** SearchAttributes ***/
1688 pos += 2; /*** Byte Count ***/
1689 bpos = pos;
1690 setUChar(ptr, pos, 0x04); /** Bufferformat **/
1691 pos++;
1693 realfile[0]='\0';
1694 if (filename[0] != '\\')
1695 strcpy(realfile,"\\");
1696 strcat(realfile,filename);
1698 if(handle->unicode)
1700 pos += utf8_to_utf16((char*)&ptr[pos],realfile,SMB_MAXPATH-2);
1701 pos += 2;
1703 else
1705 memcpy(&ptr[pos],realfile,strlen(realfile));
1706 pos += strlen(realfile)+1;
1709 pos++;
1710 setUChar(ptr, pos, 0x04); /** Bufferformat **/
1711 pos++;
1713 newrealfile[0]='\0';
1714 if (newfilename[0] != '\\')
1715 strcpy(newrealfile,"\\");
1716 strcat(newrealfile,newfilename);
1718 if(handle->unicode)
1720 pos += utf8_to_utf16((char*)&ptr[pos],newrealfile,SMB_MAXPATH-2);
1721 pos += 2;
1723 else
1725 memcpy(&ptr[pos],newrealfile,strlen(newrealfile));
1726 pos += strlen(newrealfile)+1;
1729 setUShort(ptr,(bpos-2),(pos-bpos));
1731 handle->message.msg = NBT_SESSISON_MSG;
1732 handle->message.length = htons(pos);
1734 pos += 4;
1735 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
1736 if(ret < 0) goto failed;
1738 ret = SMBCheck(SMB_COM_RENAME,handle);
1740 return ret;
1742 failed:
1743 return ret;
1747 * SMB_DiskInformation
1749 s32 SMB_DiskInformation(struct statvfs *buf, SMBCONN smbhndl)
1751 s32 pos;
1752 s32 bpos,ret;
1753 u16 TotalUnits, BlocksPerUnit, BlockSize, FreeUnits;
1754 u8 *ptr;
1755 SMBHANDLE *handle;
1757 if(SMB_Reconnect(&smbhndl,true)!=SMB_SUCCESS) return -1;
1759 handle = __smb_handle_open(smbhndl);
1760 if(!handle) return -1;
1762 MakeSMBHeader(SMB_COM_QUERY_INFORMATION_DISK,CIFS_FLAGS1, handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
1764 pos = SMB_HEADER_SIZE;
1765 ptr = handle->message.smb;
1766 setUChar(ptr, pos, 0);
1767 pos++; /*** Word Count ***/
1768 setUShort(ptr, pos, 0);
1769 pos += 2; /*** Byte Count ***/
1770 bpos = pos;
1772 handle->message.msg = NBT_SESSISON_MSG;
1773 handle->message.length = htons(pos);
1775 pos += 4;
1776 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
1777 if(ret < 0) goto failed;
1779 if((ret=SMBCheck(SMB_COM_QUERY_INFORMATION_DISK, handle))==SMB_SUCCESS)
1781 ptr = handle->message.smb;
1782 /** Read the received data ***/
1783 /** WordCount **/
1784 s32 recv_pos = 1;
1785 /** TotalUnits **/
1786 TotalUnits = getUShort(ptr,(SMB_HEADER_SIZE+recv_pos));
1787 recv_pos += 2;
1788 /** BlocksPerUnit **/
1789 BlocksPerUnit = getUShort(ptr,(SMB_HEADER_SIZE+recv_pos));
1790 recv_pos += 2;
1791 /** BlockSize **/
1792 BlockSize = getUShort(ptr,(SMB_HEADER_SIZE+recv_pos));
1793 recv_pos += 2;
1794 /** FreeUnits **/
1795 FreeUnits = getUShort(ptr,(SMB_HEADER_SIZE+recv_pos));
1796 recv_pos += 2;
1798 buf->f_bsize = (unsigned long) BlockSize; // File system block size.
1799 buf->f_frsize = (unsigned long) BlockSize; // Fundamental file system block size.
1800 buf->f_blocks = (fsblkcnt_t) (TotalUnits*BlocksPerUnit); // Total number of blocks on file system in units of f_frsize.
1801 buf->f_bfree = (fsblkcnt_t) (FreeUnits*BlocksPerUnit); // Total number of free blocks.
1802 buf->f_bavail = 0; // Number of free blocks available to non-privileged process.
1803 buf->f_files = 0; // Total number of file serial numbers.
1804 buf->f_ffree = 0; // Total number of free file serial numbers.
1805 buf->f_favail = 0; // Number of file serial numbers available to non-privileged process.
1806 buf->f_fsid = 0; // File system ID. 32bit ioType value
1807 buf->f_flag = 0; // Bit mask of f_flag values.
1808 buf->f_namemax = SMB_MAXPATH; // Maximum filename length.
1810 return SMB_SUCCESS;
1813 failed:
1814 handle->conn_valid = false;
1815 return ret;
1819 * SMB_Read
1821 s32 SMB_ReadFile(char *buffer, size_t size, off_t offset, SMBFILE sfid)
1823 u8 *ptr;
1824 u32 pos, ret, ofs;
1825 u16 length = 0;
1826 SMBHANDLE *handle;
1827 size_t readed=0,read;
1828 struct _smbfile *fid = (struct _smbfile*)sfid;
1830 if(!fid) return -1;
1832 // Check for invalid size
1833 if(size == 0) return -1;
1835 while(readed<size)
1837 if((size-readed)>SMB_MAX_TRANSMIT_SIZE) read=SMB_MAX_TRANSMIT_SIZE;
1838 else read=size-readed;
1840 handle = __smb_handle_open(fid->conn);
1841 if(!handle) return -1;
1843 MakeSMBHeader(SMB_READ_ANDX,CIFS_FLAGS1,handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
1845 pos = SMB_HEADER_SIZE;
1846 ptr = handle->message.smb;
1847 setUChar(ptr, pos, 12);
1848 pos++; /*** Word count ***/
1849 setUChar(ptr, pos, 0xff);
1850 pos++;
1851 setUChar(ptr, pos, 0);
1852 pos++; /*** Reserved must be 0 ***/
1853 pos += 2; /*** Next AndX Offset ***/
1854 setUShort(ptr, pos, fid->sfid);
1855 pos += 2; /*** FID ***/
1856 setUInt(ptr, pos, offset+readed);
1857 pos += 4; /*** Offset ***/
1859 setUShort(ptr, pos, read & 0xffff);
1860 pos += 2;
1861 setUShort(ptr, pos, read & 0xffff);
1862 pos += 2;
1863 setUInt(ptr, pos, 0);
1864 pos += 4; /*** Reserved must be 0 ***/
1865 setUShort(ptr, pos, read & 0xffff);
1866 pos += 2; /*** Remaining ***/
1867 setUInt(ptr, pos, (offset+readed) >> 32); // offset high
1868 pos += 4; /*** OffsetHIGH ***/
1869 pos += 2; /*** Byte count ***/
1871 handle->message.msg = NBT_SESSISON_MSG;
1872 handle->message.length = htons(pos);
1874 pos += 4;
1876 ret = smb_send(handle->sck_server,(char*)&handle->message, pos);
1877 if(ret<0) goto failed;
1879 /*** SMBCheck ***/
1880 if(SMBCheck(SMB_READ_ANDX,handle)!=SMB_SUCCESS) goto failed;
1882 ptr = handle->message.smb;
1883 // Retrieve data length for this packet
1884 length = getUShort(ptr,(SMB_HEADER_SIZE+11));
1885 // Retrieve offset to data
1886 ofs = getUShort(ptr,(SMB_HEADER_SIZE+13));
1888 memcpy(&buffer[readed],&ptr[ofs],length);
1889 readed+=length;
1891 if(length==0) break;
1893 return size;
1895 failed:
1896 handle->conn_valid = false;
1897 return ret;
1901 * SMB_Write
1903 s32 SMB_WriteFile(const char *buffer, size_t size, off_t offset, SMBFILE sfid)
1905 u8 *ptr,*src;
1906 s32 pos, ret;
1907 s32 blocks64;
1908 u32 copy_len;
1909 SMBHANDLE *handle;
1910 struct _smbfile *fid = (struct _smbfile*)sfid;
1912 if(!fid) return -1;
1914 handle = __smb_handle_open(fid->conn);
1915 if(!handle) return -1;
1917 MakeSMBHeader(SMB_WRITE_ANDX,CIFS_FLAGS1,handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
1920 pos = SMB_HEADER_SIZE;
1921 ptr = handle->message.smb;
1922 setUChar(ptr, pos, 14);
1923 pos++; /*** Word Count ***/
1924 setUChar(ptr, pos, 0xff);
1925 pos += 2; /*** Next AndX ***/
1926 pos += 2; /*** Next AndX Offset ***/
1928 setUShort(ptr, pos, fid->sfid);
1929 pos += 2;
1930 setUInt(ptr, pos, offset & 0xffffffff);
1931 pos += 4;
1932 setUInt(ptr, pos, 0); /*** Reserved, must be 0 ***/
1933 pos += 4;
1934 setUShort(ptr, pos, 0); /*** Write Mode ***/
1935 pos += 2;
1936 pos += 2; /*** Remaining ***/
1938 blocks64 = size >> 16;
1940 setUShort(ptr, pos, blocks64);
1941 pos += 2; /*** Length High ***/
1942 setUShort(ptr, pos, size & 0xffff);
1943 pos += 2; /*** Length Low ***/
1944 setUShort(ptr, pos, 63);
1945 pos += 2; /*** Data Offset ***/
1946 setUInt(ptr, pos, offset >> 32); /*** OffsetHigh ***/
1947 pos += 4;
1948 setUShort(ptr, pos, size & 0xffff);
1949 pos += 2; /*** Data Byte Count ***/
1951 handle->message.msg = NBT_SESSISON_MSG;
1952 handle->message.length = htons(pos+size);
1954 src = (u8*)buffer;
1955 copy_len = size;
1956 if((copy_len+pos)>SMB_MAX_TRANSMIT_SIZE) copy_len = (SMB_MAX_TRANSMIT_SIZE-pos);
1958 memcpy(&ptr[pos],src,copy_len);
1959 size -= copy_len;
1960 src += copy_len;
1961 pos += copy_len;
1963 pos += 4;
1965 /*** Send Header Information ***/
1966 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
1967 if(ret<0) goto failed;
1969 if(size>0) {
1970 /*** Send the data ***/
1971 ret = smb_send(handle->sck_server,src,size);
1972 if(ret<0) goto failed;
1975 ret = 0;
1976 if(SMBCheck(SMB_WRITE_ANDX,handle)==SMB_SUCCESS) {
1977 ptr = handle->message.smb;
1978 ret = getUShort(ptr,(SMB_HEADER_SIZE+5));
1981 return ret;
1983 failed:
1984 handle->conn_valid = false;
1985 return ret;
1989 * SMB_PathInfo
1991 s32 SMB_PathInfo(const char *filename, SMBDIRENTRY *sdir, SMBCONN smbhndl)
1993 u8 *ptr;
1994 s32 pos;
1995 s32 ret;
1996 s32 bpos;
1997 int len;
1998 SMBHANDLE *handle;
1999 char realfile[512];
2001 if(filename == NULL)
2002 return SMB_ERROR;
2004 handle = __smb_handle_open(smbhndl);
2005 if (!handle) return SMB_ERROR;
2007 MakeSMBHeader(SMB_TRANS2, CIFS_FLAGS1, handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2, handle);
2008 MakeTRANS2Header(SMB_QUERY_PATH_INFO, handle);
2010 bpos = pos = (T2_BYTE_CNT + 2);
2011 pos += 3; /*** Padding ***/
2012 ptr = handle->message.smb;
2013 setUShort(ptr, pos, SMB_QUERY_FILE_ALL_INFO);
2014 pos += 2; /*** Level of information requested ***/
2015 setUInt(ptr, pos, 0);
2016 pos += 4; /*** reserved ***/
2018 realfile[0] = '\0';
2019 if (filename[0] != '\\')
2020 strcpy(realfile,"\\");
2021 strcat(realfile,filename);
2023 if(handle->unicode)
2025 len = utf8_to_utf16((char*)&ptr[pos],realfile,SMB_MAXPATH-2);
2026 pos += len+2;
2027 len+=1;
2029 else
2031 len = strlen(realfile);
2032 memcpy(&ptr[pos],realfile,len);
2033 pos += len+1;
2036 /*** Update counts ***/
2037 setUShort(ptr, T2_PRM_CNT, (7 + len));
2038 setUShort(ptr, T2_SPRM_CNT, (7 + len));
2039 setUShort(ptr, T2_SPRM_OFS, 68);
2040 setUShort(ptr, T2_BYTE_CNT, (pos - bpos));
2042 handle->message.msg = NBT_SESSISON_MSG;
2043 handle->message.length = htons(pos);
2045 pos += 4;
2046 ret = smb_send(handle->sck_server, (char*) &handle->message, pos);
2047 if(ret<0) goto failed;
2049 ret = SMB_ERROR;
2050 if (SMBCheck(SMB_TRANS2, handle) == SMB_SUCCESS) {
2052 ptr = handle->message.smb;
2053 /*** Get parameter offset ***/
2054 pos = getUShort(ptr, (SMB_HEADER_SIZE + 9));
2055 pos += 4; // padding
2056 sdir->ctime = getULongLong(ptr, pos) - handle->session.timeOffset; pos += 8; // ULONGLONG - creation time
2057 sdir->atime = getULongLong(ptr, pos) - handle->session.timeOffset; pos += 8; // ULONGLONG - access time
2058 sdir->mtime = getULongLong(ptr, pos) - handle->session.timeOffset; pos += 8; // ULONGLONG - write time
2059 pos += 8; // ULONGLONG - change time
2060 sdir->attributes = getUInt(ptr,pos); pos += 4; // ULONG - file attributes
2061 pos += 4; // padding
2062 pos += 8; // ULONGLONG - allocated file size
2063 sdir->size = getULongLong(ptr, pos); pos += 8; // ULONGLONG - file size
2064 pos += 4; // ULONG NumberOfLinks;
2065 pos += 2; // UCHAR DeletePending;
2066 pos += 2; // UCHAR Directory;
2067 pos += 2; // USHORT Pad2; // for alignment only
2068 pos += 4; // ULONG EaSize;
2069 pos += 4; // ULONG FileNameLength;
2071 strcpy(sdir->name,realfile);
2073 ret = SMB_SUCCESS;
2075 return ret;
2077 failed:
2078 handle->conn_valid = false;
2079 return ret;
2083 * SMB_FindFirst
2085 * Uses TRANS2 to support long filenames
2087 s32 SMB_FindFirst(const char *filename, unsigned short flags, SMBDIRENTRY *sdir, SMBCONN smbhndl)
2089 u8 *ptr;
2090 s32 pos;
2091 s32 ret;
2092 s32 bpos;
2093 unsigned int len;
2094 SMBHANDLE *handle;
2095 SMBSESSION *sess;
2097 if(filename == NULL)
2098 return SMB_ERROR;
2100 if(SMB_Reconnect(&smbhndl,true)!=SMB_SUCCESS) return SMB_ERROR;
2102 handle = __smb_handle_open(smbhndl);
2103 if(!handle) return SMB_ERROR;
2105 sess = &handle->session;
2106 MakeSMBHeader(SMB_TRANS2,CIFS_FLAGS1,handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
2107 MakeTRANS2Header(SMB_FIND_FIRST2,handle);
2109 ptr = handle->message.smb;
2111 bpos = pos = (T2_BYTE_CNT+2);
2112 pos += 3; /*** Padding ***/
2113 setUShort(ptr, pos, flags);
2114 pos += 2; /*** Flags ***/
2115 setUShort(ptr, pos, 1);
2116 pos += 2; /*** Count ***/
2117 setUShort(ptr, pos, 6);
2118 pos += 2; /*** Internal Flags ***/
2119 setUShort(ptr, pos, 260); // SMB_FIND_FILE_BOTH_DIRECTORY_INFO
2120 pos += 2; /*** Level of Interest ***/
2121 pos += 4; /*** Storage Type == 0 ***/
2123 if(handle->unicode)
2125 len = utf8_to_utf16((char*)&ptr[pos], (char*)filename,SMB_MAXPATH-2);
2126 pos += len+2;
2127 len++;
2129 else
2131 len = strlen(filename);
2132 memcpy(&ptr[pos],filename,len);
2133 pos += len+1;
2136 /*** Update counts ***/
2137 setUShort(ptr, T2_PRM_CNT, (13+len));
2138 setUShort(ptr, T2_SPRM_CNT, (13+len));
2139 setUShort(ptr, T2_SPRM_OFS, 68);
2140 setUShort(ptr, T2_BYTE_CNT,(pos-bpos));
2142 handle->message.msg = NBT_SESSISON_MSG;
2143 handle->message.length = htons(pos);
2145 pos += 4;
2146 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
2147 if(ret<0) goto failed;
2149 sess->eos = 1;
2150 sess->count = 0;
2151 ret = SMB_ERROR;
2152 if(SMBCheck(SMB_TRANS2,handle)==SMB_SUCCESS) {
2153 ptr = handle->message.smb;
2154 /*** Get parameter offset ***/
2155 pos = getUShort(ptr,(SMB_HEADER_SIZE+9));
2156 sdir->sid = getUShort(ptr, pos); pos += 2;
2157 sess->count = getUShort(ptr, pos); pos += 2;
2158 sess->eos = getUShort(ptr, pos); pos += 2;
2159 pos += 2; // USHORT EaErrorOffset;
2160 pos += 2; // USHORT LastNameOffset;
2161 pos += 2; // padding?
2163 if (sess->count)
2165 pos += 4; // ULONG NextEntryOffset;
2166 pos += 4; // ULONG FileIndex;
2167 sdir->ctime = getULongLong(ptr, pos) - handle->session.timeOffset; pos += 8; // ULONGLONG - creation time
2168 sdir->atime = getULongLong(ptr, pos) - handle->session.timeOffset; pos += 8; // ULONGLONG - access time
2169 sdir->mtime = getULongLong(ptr, pos) - handle->session.timeOffset; pos += 8; // ULONGLONG - write time
2170 pos += 8; // ULONGLONG - change time low
2171 sdir->size = getULongLong(ptr, pos); pos += 8;
2172 pos += 8;
2173 sdir->attributes = getUInt(ptr, pos); pos += 4;
2174 len=getUInt(ptr, pos);
2175 pos += 34;
2176 if(handle->unicode)
2177 utf16_to_utf8(sdir->name,(char*)&ptr[pos],len);
2178 else
2179 strcpy(sdir->name,(const char*)&ptr[pos]);
2181 ret = SMB_SUCCESS;
2184 return ret;
2186 failed:
2187 handle->conn_valid = false;
2188 return ret;
2192 * SMB_FindNext
2194 s32 SMB_FindNext(SMBDIRENTRY *sdir,SMBCONN smbhndl)
2196 u8 *ptr;
2197 s32 pos;
2198 s32 ret;
2199 s32 bpos;
2200 SMBHANDLE *handle;
2201 SMBSESSION *sess;
2203 handle = __smb_handle_open(smbhndl);
2204 if(!handle) return SMB_ERROR;
2206 sess = &handle->session;
2207 if(sess->eos || sdir->sid==0) return SMB_ERROR;
2209 MakeSMBHeader(SMB_TRANS2,CIFS_FLAGS1,handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
2210 MakeTRANS2Header(SMB_FIND_NEXT2,handle);
2212 bpos = pos = (T2_BYTE_CNT+2);
2213 pos += 3; /*** Padding ***/
2214 ptr = handle->message.smb;
2215 setUShort(ptr, pos, sdir->sid);
2216 pos += 2; /*** Search ID ***/
2217 setUShort(ptr, pos, 1);
2218 pos += 2; /*** Count ***/
2219 setUShort(ptr, pos, 260); // SMB_FIND_FILE_BOTH_DIRECTORY_INFO
2220 pos += 2; /*** Level of Interest ***/
2221 pos += 4; /*** Storage Type == 0 ***/
2222 setUShort(ptr, pos, 12);
2223 pos+=2; /*** Search flags ***/
2224 pos++;
2226 /*** Update counts ***/
2227 setUShort(ptr, T2_PRM_CNT, 13);
2228 setUShort(ptr, T2_SPRM_CNT, 13);
2229 setUShort(ptr, T2_SPRM_OFS, 68);
2230 setUShort(ptr, T2_BYTE_CNT, (pos-bpos));
2232 handle->message.msg = NBT_SESSISON_MSG;
2233 handle->message.length = htons(pos);
2235 pos += 4;
2236 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
2237 if(ret<0) goto failed;
2239 ret = SMB_ERROR;
2240 if (SMBCheck(SMB_TRANS2,handle)==SMB_SUCCESS) {
2241 ptr = handle->message.smb;
2242 /*** Get parameter offset ***/
2243 pos = getUShort(ptr,(SMB_HEADER_SIZE+9));
2244 sess->count = getUShort(ptr, pos); pos += 2;
2245 sess->eos = getUShort(ptr, pos); pos += 2;
2246 pos += 2; // USHORT EaErrorOffset;
2247 pos += 2; // USHORT LastNameOffset;
2249 if (sess->count)
2251 int len;
2252 pos += 4; // ULONG NextEntryOffset;
2253 pos += 4; // ULONG FileIndex;
2254 sdir->ctime = getULongLong(ptr, pos) - handle->session.timeOffset; pos += 8; // ULONGLONG - creation time
2255 sdir->atime = getULongLong(ptr, pos) - handle->session.timeOffset; pos += 8; // ULONGLONG - access time
2256 sdir->mtime = getULongLong(ptr, pos) - handle->session.timeOffset; pos += 8; // ULONGLONG - write time
2257 pos += 8; // ULONGLONG - change time low
2258 sdir->size = getULongLong(ptr, pos); pos += 8;
2259 pos += 8;
2260 sdir->attributes = getUInt(ptr, pos); pos += 4;
2261 len=getUInt(ptr, pos);
2262 pos += 34;
2263 if(handle->unicode)
2264 utf16_to_utf8(sdir->name, (char*)&ptr[pos],len);
2265 else
2266 strcpy (sdir->name, (const char*)&ptr[pos]);
2268 ret = SMB_SUCCESS;
2271 return ret;
2273 failed:
2274 handle->conn_valid = false;
2275 return ret;
2279 * SMB_FindClose
2281 s32 SMB_FindClose(SMBDIRENTRY *sdir,SMBCONN smbhndl)
2283 u8 *ptr;
2284 s32 pos;
2285 s32 ret;
2286 SMBHANDLE *handle;
2287 SMBSESSION *sess;
2289 handle = __smb_handle_open(smbhndl);
2290 if(!handle) return SMB_ERROR;
2292 sess = &handle->session;
2293 if(sdir->sid==0) return SMB_ERROR;
2295 MakeSMBHeader(SMB_FIND_CLOSE2,CIFS_FLAGS1,handle->unicode?CIFS_FLAGS2_UNICODE:CIFS_FLAGS2,handle);
2297 pos = SMB_HEADER_SIZE;
2298 ptr = handle->message.smb;
2299 setUChar(ptr, pos, 1);
2300 pos++; /*** Word Count ***/
2301 setUShort(ptr, pos, sdir->sid);
2302 pos += 2;
2303 pos += 2; /*** Byte Count ***/
2305 handle->message.msg = NBT_SESSISON_MSG;
2306 handle->message.length = htons(pos);
2308 pos += 4;
2309 ret = smb_send(handle->sck_server,(char*)&handle->message,pos);
2310 if(ret<0) goto failed;
2312 ret = SMBCheck(SMB_FIND_CLOSE2,handle);
2313 return ret;
2315 failed:
2316 handle->conn_valid = false;
2317 return ret;