66#define SMB_NAME_MAX 256
75 char name[SMB_NAME_MAX];
78#define MAX_FDHANDLES 32
79FHANDLE smbman_fdhandles[MAX_FDHANDLES];
81#define SMB_SEARCH_BUF_MAX 4096
82#define SMB_PATH_MAX 1024
85static u8 SearchBuf[SMB_SEARCH_BUF_MAX];
86static char smb_curdir[SMB_PATH_MAX];
87static char smb_curpath[SMB_PATH_MAX];
88static char smb_secpath[SMB_PATH_MAX];
90static int keepalive_mutex = -1;
91static int keepalive_inited = 0;
92static int keepalive_locked = 1;
93static int keepalive_tid;
109static unsigned int timer_intr_handler(
void *args)
111 iSignalSema(keepalive_mutex);
112 iSetAlarm(&keepalive_sysclock, timer_intr_handler, NULL);
114 return (
unsigned int)args;
118static void keepalive_deinit(
void)
123 if (keepalive_inited) {
125 CancelAlarm(timer_intr_handler, NULL);
127 keepalive_inited = 0;
132static void keepalive_init(
void)
135 if ((!keepalive_inited) && (!keepalive_locked)) {
137 SetAlarm(&keepalive_sysclock, timer_intr_handler, NULL);
138 keepalive_inited = 1;
143static void keepalive_lock(
void)
145 keepalive_locked = 1;
149static void keepalive_unlock(
void)
151 keepalive_locked = 0;
155static void smb_io_lock(
void)
159 WaitSema(smbman_io_sema);
163static void smb_io_unlock(
void)
165 SignalSema(smbman_io_sema);
171static void keepalive_thread(
void *args)
173 int opened_share = 0;
181 WaitSema(keepalive_mutex);
184 WaitSema(smbman_io_sema);
187 r = smb_Echo(
"PS2 KEEPALIVE ECHO", 18);
205 SignalSema(smbman_io_sema);
223 smbman_io_sema = CreateMutex(IOP_MUTEX_UNLOCKED);
226 keepalive_mutex = CreateMutex(IOP_MUTEX_LOCKED);
229 USec2SysClock(3000 * 1000, &keepalive_sysclock);
234 thread.thread = (
void *)keepalive_thread;
238 keepalive_tid = CreateThread(&
thread);
239 StartThread(keepalive_tid, NULL);
253 for (i = 0; i < MAX_FDHANDLES; i++) {
256 fh = (
FHANDLE *)&smbman_fdhandles[i];
273 DeleteThread(keepalive_tid);
275 DeleteSema(smbman_io_sema);
277 DeleteSema(keepalive_mutex);
278 keepalive_mutex = -1;
284static FHANDLE *smbman_getfilefreeslot(
void)
288 for (i = 0; i < MAX_FDHANDLES; i++) {
291 fh = (
FHANDLE *)&smbman_fdhandles[i];
300static char *prepare_path(
const char *path,
char *full_path,
int max_path)
306 strncpy(full_path, smb_curdir, max_path - 3);
307 strcat(full_path,
"\\");
311 while ((*p ==
'\\') || (*p ==
'/'))
315 p2 = &path[strlen(path)];
316 while ((*p2 ==
'\\') || (*p2 ==
'/'))
320 for (i = strlen(full_path); (p <= p2) && (max_path - i - 2 > 0); p++, i++) {
321 full_path[i] = (*p ==
'/') ?
'\\' : *p;
326 full_path[i + 1] =
'\0';
332int smb_open(
iop_file_t *f,
const char *filename,
int flags,
int mode)
343 if ((UID == -1) || (TID == -1))
346 char *path = prepare_path(filename, smb_curpath, SMB_PATH_MAX);
350 fh = smbman_getfilefreeslot();
352 r = smb_OpenAndX(UID, TID, path, &filesize, flags);
358 fh->filesize = filesize;
360 if (fh->mode & O_TRUNC)
362 else if (fh->mode & O_APPEND)
363 fh->position = filesize;
364 strncpy(fh->name, path, SMB_NAME_MAX);
381 if ((UID == -1) || (TID == -1) || (fh->smb_fid == -1))
387 if (fh->mode != O_DIROPEN) {
388 r = smb_Close(UID, TID, fh->smb_fid);
393 memset(fh, 0,
sizeof(
FHANDLE));
405void smb_closeAll(
void)
409 for (i = 0; i < MAX_FDHANDLES; i++) {
412 fh = (
FHANDLE *)&smbman_fdhandles[i];
413 if (fh->smb_fid != -1)
414 smb_Close(UID, TID, fh->smb_fid);
419int smb_lseek(
iop_file_t *f,
int pos,
int where)
421 return (
int)smb_lseek64(f, pos, where);
425int smb_read(
iop_file_t *f,
void *buf,
int size)
430 if ((UID == -1) || (TID == -1) || (fh->smb_fid == -1))
433 if ((fh->position + size) > fh->filesize)
434 size = fh->filesize - fh->position;
438 r = smb_ReadFile(UID, TID, fh->smb_fid, fh->position, buf, size);
449int smb_write(
iop_file_t *f,
void *buf,
int size)
454 if ((UID == -1) || (TID == -1) || (fh->smb_fid == -1))
457 if ((!(fh->mode & O_RDWR)) && (!(fh->mode & O_WRONLY)))
462 r = smb_WriteFile(UID, TID, fh->smb_fid, fh->position, buf, size);
465 if (fh->position > fh->filesize)
466 fh->filesize += fh->position - fh->filesize;
475int smb_remove(
iop_file_t *f,
const char *filename)
484 if ((UID == -1) || (TID == -1))
487 char *path = prepare_path(filename, smb_curpath, SMB_PATH_MAX);
491 DPRINTF(
"smb_remove: filename=%s\n", filename);
493 r = smb_Delete(UID, TID, path);
501int smb_mkdir(
iop_file_t *f,
const char *dirname,
int mode)
511 if ((UID == -1) || (TID == -1))
514 char *path = prepare_path(dirname, smb_curpath, SMB_PATH_MAX);
518 r = smb_ManageDirectory(UID, TID, path, SMB_COM_CREATE_DIRECTORY);
526int smb_rmdir(
iop_file_t *f,
const char *dirname)
536 if ((UID == -1) || (TID == -1))
539 char *path = prepare_path(dirname, smb_curpath, SMB_PATH_MAX);
543 r = smb_QueryPathInformation(UID, TID, &
info, path);
547 if (!(
info.FileAttributes & EXT_ATTR_DIRECTORY)) {
552 r = smb_ManageDirectory(UID, TID, path, SMB_COM_DELETE_DIRECTORY);
561static void FileTimeToDate(u64 FileTime, u8 *datetime)
563 u8 daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
567 u8 leapdays, months, hours, minutes, seconds;
569 time = FileTime / 10000000;
571 years = (u16)(time / ((u64)60 * 60 * 24 * 365));
572 time -= years * ((u64)60 * 60 * 24 * 365);
574 leapdays = (years / 4) - (years / 100) + (years / 400);
577 days = (u16)(time / (60 * 60 * 24));
578 time -= (
unsigned int)days * (60 * 60 * 24);
581 if ((years % 4) == 0 && ((years % 100) != 0 || (years % 400) == 0))
585 for (i = 0; i < 12; i++) {
586 if (days > daysPerMonth[i]) {
587 days -= daysPerMonth[i];
597 hours = (u8)(time / (60 * 60));
598 time -= (u16)hours * (60 * 60);
600 minutes = (u8)(time / 60);
601 time -= minutes * 60;
603 seconds = (u8)(time);
606 datetime[1] = seconds;
607 datetime[2] = minutes;
609 datetime[4] = days + 1;
610 datetime[5] = months + 1;
611 datetime[6] = (u8)(years & 0xFF);
612 datetime[7] = (u8)((years >> 8) & 0xFF);
617 FileTimeToDate(
info->Created, stat->ctime);
618 FileTimeToDate(
info->LastAccess, stat->atime);
619 FileTimeToDate(
info->Change, stat->mtime);
621 stat->size = (int)(
info->EndOfFile & 0xffffffff);
622 stat->hisize = (int)((
info->EndOfFile >> 32) & 0xffffffff);
628int smb_dopen(
iop_file_t *f,
const char *dirname)
637 if ((UID == -1) || (TID == -1))
640 char *path = prepare_path(dirname, smb_curpath, SMB_PATH_MAX);
645 r = smb_QueryPathInformation(UID, TID, &
info, path);
650 if (!(
info.FileAttributes & EXT_ATTR_DIRECTORY)) {
655 fh = smbman_getfilefreeslot();
659 fh->mode = O_DIROPEN;
663 strncpy(fh->name, path, 255);
664 if (fh->name[strlen(fh->name) - 1] !=
'\\')
665 strcat(fh->name,
"\\");
666 strcat(fh->name,
"*");
690 if ((UID == -1) || (TID == -1))
699 if (fh->smb_fid == -1) {
700 r = smb_FindFirstNext2(UID, TID, fh->name, TRANS2_FIND_FIRST2,
info);
704 fh->smb_fid =
info->SID;
707 info->SID = fh->smb_fid;
708 r = smb_FindFirstNext2(UID, TID, NULL, TRANS2_FIND_NEXT2,
info);
716 smb_statFiller(&
info->fileInfo, &dirent->stat);
717 strncpy(dirent->name,
info->FileName, SMB_NAME_MAX);
737 if ((UID == -1) || (TID == -1))
740 char *path = prepare_path(filename, smb_curpath, SMB_PATH_MAX);
746 r = smb_QueryPathInformation(UID, TID, &
info, path);
751 smb_statFiller(&
info, stat);
760int smb_rename(
iop_file_t *f,
const char *oldname,
const char *newname)
766 if ((!oldname) || (!newname))
769 if ((UID == -1) || (TID == -1))
772 char *oldpath = prepare_path(oldname, smb_curpath, SMB_PATH_MAX);
773 char *newpath = prepare_path(newname, smb_secpath, SMB_PATH_MAX);
777 DPRINTF(
"smb_rename: oldname=%s newname=%s\n", oldname, newname);
779 r = smb_Rename(UID, TID, oldpath, newpath);
787int smb_chdir(
iop_file_t *f,
const char *dirname)
797 if ((UID == -1) || (TID == -1))
800 char *path = prepare_path(dirname, smb_curpath, SMB_PATH_MAX);
804 if ((path[strlen(path) - 2] ==
'.') && (path[strlen(path) - 1] ==
'.')) {
805 char *p = (
char *)smb_curdir;
806 for (i = strlen(p) - 1; i >= 0; i--) {
812 }
else if (path[strlen(path) - 1] ==
'.') {
815 r = smb_QueryPathInformation(UID, TID, &
info, path);
820 if (!(
info.FileAttributes & EXT_ATTR_DIRECTORY)) {
825 strncpy(smb_curdir, path,
sizeof(smb_curdir) - 1);
835s64 smb_lseek64(
iop_file_t *f, s64 pos,
int where)
844 r = fh->position + pos;
845 if (r > fh->filesize) {
852 if (fh->filesize < pos) {
874void DMA_sendEE(
void *buf,
int size,
void *EE_addr)
879 dmat.dest = (
void *)EE_addr;
881 dmat.src = (
void *)buf;
887 id = sceSifSetDma(&dmat, 1);
890 while (sceSifDmaStat(
id) >= 0)
897 LM_Password_Hash((
const unsigned char *)in->password, (
unsigned char *)out->LMhash);
898 NTLM_Password_Hash((
const unsigned char *)in->password, (
unsigned char *)out->NTLMhash);
902static int smb_LogOff(
void);
913 r = smb_Connect(logon->serverIP, logon->serverPort);
915 return -SMB_DEVCTL_LOGON_ERR_CONN;
917 r = smb_NegotiateProtocol(&capabilities);
919 return -SMB_DEVCTL_LOGON_ERR_PROT;
921 r = smb_SessionSetupAndX(logon->User, logon->Password, logon->PasswordType, capabilities);
923 return -SMB_DEVCTL_LOGON_ERR_LOGON;
927 memcpy((
void *)&glogon_info, (
void *)logon,
sizeof(
smbLogOn_in_t));
935static int smb_LogOff(
void)
944 smb_TreeDisconnect(UID, TID);
948 r = smb_LogOffAndX(UID);
964 int i, r, sharecount, shareindex;
971 smb_TreeDisconnect(UID, TID);
976 sprintf(tree_str,
"\\\\%s\\IPC$", specs->ServerIP);
977 r = smb_TreeConnectAndX(UID, tree_str, NULL, 0);
987 r = smb_NetShareEnum(UID, TID, (
ShareEntry_t *)&ShareList, 0, 0);
995 for (i = 0; i < sharecount; i++) {
997 r = smb_NetShareEnum(UID, TID, (
ShareEntry_t *)&ShareList, i, 1);
1002 if ((strcmp(ShareList.ShareName,
"IPC$")) && (shareindex < getsharelist->maxent)) {
1003 DMA_sendEE((
void *)&ShareList,
sizeof(ShareList), (
void *)(getsharelist->EE_addr + (shareindex *
sizeof(
ShareEntry_t))));
1009 r = smb_TreeDisconnect(UID, TID);
1029 smb_TreeDisconnect(UID, TID);
1033 sprintf(tree_str,
"\\\\%s\\%s", specs->ServerIP, openshare->ShareName);
1034 r = smb_TreeConnectAndX(UID, tree_str, openshare->Password, openshare->PasswordType);
1046static int smb_CloseShare(
void)
1055 r = smb_TreeDisconnect(UID, TID);
1067 return smb_Echo(echo->echo, echo->len);
1073 if ((UID == -1) || (TID == -1))
1076 return smb_QueryInformationDisk(UID, TID, querydiskinfo);
1080int smb_devctl(
iop_file_t *f,
const char *devname,
int cmd,
void *arg,
unsigned int arglen,
void *bufp,
unsigned int buflen)
1093 case SMB_DEVCTL_GETPASSWORDHASHES:
1098 case SMB_DEVCTL_LOGON:
1102 case SMB_DEVCTL_LOGOFF:
1106 case SMB_DEVCTL_GETSHARELIST:
1110 case SMB_DEVCTL_OPENSHARE:
1114 case SMB_DEVCTL_CLOSESHARE:
1115 r = smb_CloseShare();
1118 case SMB_DEVCTL_ECHO:
1122 case SMB_DEVCTL_QUERYDISKINFO:
int CpuResumeIntr(int state)
int CpuSuspendIntr(int *state)