#pragma once #pragma comment (lib,"Ws2.lib") #include #include #include #include #define GET_MODE(command) (command & 0x000000ffUL) #define GET_OPT(command) (command & 0xffffff00UL) /* header */ #define IPMSG_VERSION 0x0001 #define IPMSG_DEFAULT_PORT 0x0979 /* command */ #define IPMSG_NOOPERATION 0x00000000UL #define IPMSG_BR_ENTRY 0x00000001UL #define IPMSG_BR_EXIT 0x00000002UL #define IPMSG_ANSENTRY 0x00000003UL #define IPMSG_BR_ABSENCE 0x00000004UL #define IPMSG_BR_ISGETLIST 0x00000010UL #define IPMSG_OKGETLIST 0x00000011UL #define IPMSG_GETLIST 0x00000012UL #define IPMSG_ANSLIST 0x00000013UL #define IPMSG_BR_ISGETLIST2 0x00000018UL #define IPMSG_SENDMSG 0x00000020UL #define IPMSG_RECVMSG 0x00000021UL #define IPMSG_READMSG 0x00000030UL #define IPMSG_DELMSG 0x00000031UL #define IPMSG_ANSREADMSG 0x00000032UL #define IPMSG_GETINFO 0x00000040UL #define IPMSG_SENDINFO 0x00000041UL #define IPMSG_GETABSENCEINFO 0x00000050UL #define IPMSG_SENDABSENCEINFO 0x00000051UL #define IPMSG_GETFILEDATA 0x00000060UL #define IPMSG_RELEASEFILES 0x00000061UL #define IPMSG_GETDIRFILES 0x00000062UL #define IPMSG_GETPUBKEY 0x00000072UL #define IPMSG_ANSPUBKEY 0x00000073UL /* option for all command */ #define IPMSG_ABSENCEOPT 0x00000100UL #define IPMSG_SERVEROPT 0x00000200UL #define IPMSG_DIALUPOPT 0x00010000UL #define IPMSG_FILEATTACHOPT 0x00200000UL #define IPMSG_ENCRYPTOPT 0x00400000UL /* option for m8 */ #define IPMSG_M8 0x01000000UL /* option for send command */ #define IPMSG_SENDCHECKOPT 0x00000100UL #define IPMSG_SECRETOPT 0x00000200UL #define IPMSG_BROADCASTOPT 0x00000400UL #define IPMSG_MULTICASTOPT 0x00000800UL #define IPMSG_NOPOPUPOPT 0x00001000UL #define IPMSG_AUTORETOPT 0x00002000UL #define IPMSG_RETRYOPT 0x00004000UL #define IPMSG_PASSWORDOPT 0x00008000UL #define IPMSG_NOLOGOPT 0x00020000UL #define IPMSG_NEWMUTIOPT 0x00040000UL #define IPMSG_NOADDLISTOPT 0x00080000UL #define IPMSG_READCHECKOPT 0x00100000UL #define IPMSG_SECRETEXOPT (IPMSG_READCHECKOPT|IPMSG_SECRETOPT) /* file types for fileattach command */ #define IPMSG_FILE_REGULAR 0x00000001UL #define IPMSG_FILE_DIR 0x00000002UL #define IPMSG_FILE_RETPARENT 0x00000003UL // return parent directory #define IPMSG_FILE_SYMLINK 0x00000004UL #define IPMSG_FILE_CDEV 0x00000005UL // for UNIX #define IPMSG_FILE_BDEV 0x00000006UL // for UNIX #define IPMSG_FILE_FIFO 0x00000007UL // for UNIX #define IPMSG_FILE_RESFORK 0x00000010UL // for Mac /* file attribute options for fileattach command */ #define IPMSG_FILE_RONLYOPT 0x00000100UL #define IPMSG_FILE_HIDDENOPT 0x00001000UL #define IPMSG_FILE_EXHIDDENOPT 0x00002000UL // for MacOS X #define IPMSG_FILE_ARCHIVEOPT 0x00004000UL #define IPMSG_FILE_SYSTEMOPT 0x00008000UL /* extend attribute types for fileattach command */ #define IPMSG_FILE_UID 0x00000001UL #define IPMSG_FILE_USERNAME 0x00000002UL // uid by string #define IPMSG_FILE_GID 0x00000003UL #define IPMSG_FILE_GROUPNAME 0x00000004UL // gid by string #define IPMSG_FILE_PERM 0x00000010UL // for UNIX #define IPMSG_FILE_MAJORNO 0x00000011UL // for UNIX devfile #define IPMSG_FILE_MINORNO 0x00000012UL // for UNIX devfile #define IPMSG_FILE_CTIME 0x00000013UL // for UNIX #define IPMSG_FILE_MTIME 0x00000014UL #define IPMSG_FILE_ATIME 0x00000015UL #define IPMSG_FILE_CREATETIME 0x00000016UL #define IPMSG_FILE_CREATOR 0x00000020UL // for Mac #define IPMSG_FILE_FILETYPE 0x00000021UL // for Mac #define IPMSG_FILE_FINDERINFO 0x00000022UL // for Mac #define IPMSG_FILE_ACL 0x00000030UL #define IPMSG_FILE_ALIASFNAME 0x00000040UL // alias fname #define IPMSG_FILE_UNICODEFNAME 0x00000041UL // UNICODE fname #define FILELIST_SEPARATOR '\a' #define HOSTLIST_SEPARATOR '\a' #define HOSTLIST_DUMMY "\b" #define MAX_BUF 1024 #define MAX_SOCKBUF 65536 #define MAX_UDPBUF 16384 #define MAX_NAMEBUF 50 /* struct */ struct HostSub { char userName[MAX_NAMEBUF]; char hostName[MAX_NAMEBUF]; ULONG addr; int portNo; int hostType; }; struct MsgBuf { HostSub hostSub; int version; int portNo; ULONG packetNo; ULONG command; int exOffset; // expand message offset in msgBuf int dummy; char msgBuf[MAX_UDPBUF]; void Init(MsgBuf *org) { if (org == 0) { memset(this, 0, (char *)&this->dummy - (char *)this); *msgBuf = 0; return; } memcpy(this, org, (char *)&this->dummy - (char *)this); strcpy_s(this->msgBuf,MAX_UDPBUF, org->msgBuf); strcpy_s(this->msgBuf + exOffset,MAX_UDPBUF - exOffset,org->msgBuf + exOffset); } }; struct RecvBuf { struct sockaddr_in addr; int addrSize; int size; char msgBuf[MAX_UDPBUF]; }; class FileInfo { int id; // char *fname; const char *fname_ext; // for recv dir thread UINT attr; _int64 size; time_t mtime; time_t atime; time_t crtime; BOOL isSelected; // for recvdlg public: FileInfo(int _id=0) { id=_id; fname_ext=fname=NULL; size=0; mtime=0; } FileInfo(const FileInfo& org) { fname=NULL; *this = org; } ~FileInfo() { if (fname) free(fname); } int Id() { return id; } void SetId(int _id) { id = _id; } const char *Fname() { return fname ? fname : fname_ext; } void SetFname(const char *_fname) { if (fname) free(fname); fname = (char *)_strdup(_fname); } void SetFnameExt(const char *_fname) { fname_ext = _fname; } _int64 Size() { return size; } void SetSize(_int64 _size) { size = _size; } time_t Mtime() { return mtime; } void SetMtime(time_t _mtime) { mtime = _mtime; } time_t Atime() { return atime; } void SetAtime(time_t _atime) { atime = _atime; } time_t Crtime() { return crtime; } void SetCrtime(time_t _crtime) { crtime = _crtime; } UINT Attr() { return attr; } void SetAttr(UINT _attr) { attr = _attr; } BOOL IsSelected() { return isSelected; } void SetSelected(BOOL _isSelected) { isSelected = _isSelected; } FileInfo& operator =(const FileInfo& org) { id=org.id; SetFname(org.fname); attr=org.attr; size=org.size; mtime=org.mtime; atime=org.atime; crtime=org.crtime; isSelected=org.isSelected; return *this; } }; struct ShareInfo { int packetNo; // not use recvdlg //Host **host; // allow host list, not use recvdlg int hostCnt; // not use recvdlg char *transStat; // not use recvdlg FileInfo **fileInfo; // allow file list int fileCnt; FILETIME attachTime; public: ShareInfo(int _packetNo=0) { packetNo = _packetNo; //hostSub = NULL; transStat = NULL; fileInfo = NULL; hostCnt = fileCnt = 0; } ~ShareInfo() { for(int i =0;i< fileCnt; i++) { delete fileInfo[i]; } delete fileInfo; } }; /* Global function */ #define BIG_ALLOC 5 //解析协议字符串 char *separate_token(char *buf, char separetor, char **handle); //将十六进制字符串数字转换为实际数字 _int64 hex2ll(char *buf); //将字符串内容解析为ShareInfo ShareInfo *DecodeShareMsg(char *buf); //将字符串内容解析为FileInfo BOOL DecodeDirEntry(char *buf, FileInfo *info); //接收指定长度的TCP数据 int inline RecvTCP(SOCKET& tcp, char* buf, long size) { long received= 0L; while( received < size) { received += ::recv(tcp,buf + received, size - received, 0); if(received <= 0) return 0; } return received; } int inline SendTCP(SOCKET& tcp, char* buf, long size) { long sended = 0L; while(sended < size) { sended += ::send(tcp, buf+sended, size - sended, 0); if(sended <= 0) return 0; } return sended; } inline void A2W(const char* input, std::wstring& output) { int nLen = MultiByteToWideChar(CP_ACP, 0, input , -1, NULL, NULL); LPWSTR lpszW = new WCHAR[nLen]; MultiByteToWideChar(CP_ACP, 0, input, -1, lpszW, nLen); output = lpszW; delete[] lpszW; } inline void A2W(std::string& input, std::wstring& output) { A2W(input.c_str(), output); } inline void W2A(const WCHAR* input, std::string& output) { int len = ::WideCharToMultiByte(CP_ACP, NULL, input, -1, NULL, 0, NULL, NULL); char* asciiout = new char[len]; WideCharToMultiByte( CP_ACP, 0, input, -1, asciiout,len, NULL,NULL ); //asciiout[len] = '\0'; output = asciiout; delete [] asciiout; } inline void W2A(std::wstring& input, std::string& output) { W2A(input.c_str(), output); }