⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cwinsock.cpp

📁 能够通过输入IP地址扫描端口
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			- gestiti: CONNECT (interno), HELO, MAIL FROM, RCPT TO, DATA, END (interno), QUIT
		*/
		case SMTP_PORT:
		{
			/*
			CONNECT
			*/
			if(memcmp(pHostLastSend,"CONNECT",7)==0)
			{
				date.SetDateFormat(GMT);

				// comando
				sprintf(	pHostLastRecv,
						"220-%s ready at %s\r\n220 ESMTP spoken here\r\n",
						pHostName,
						date.GetFormattedDate()
						);
			}
			/*
			HELO
			*/
			else if(memcmp(pHostLastSend,"HELO",4)==0)
			{
				// comando
				sprintf(	pHostLastRecv,
						"250 %s Hello [%s], pleased to meet you\r\n",
						pHostName,
						pHostIp
						);
			}
			/*
			MAIL FROM
			*/
			else if(memcmp(pHostLastSend,"MAIL FROM",9)==0)
			{
				char* p;
				char buffer[MAX_EMAIL_LEN + 1] = {0};

				// ricava l'email del mittente
				strcpyn(buffer,pHostLastSend+10,sizeof(buffer));
				if((p = strchr(buffer,'\r'))!=NULL)
					*p = '\0';

				// comando
				sprintf(	pHostLastRecv,
						"250 %s Sender ok\r\n",
						buffer
						);
			}
			/*
			RCPT TO
			*/
			else if(memcmp(pHostLastSend,"RCPT TO",7)==0)
			{
				char* p;
				char buffer[MAX_EMAIL_LEN + 1] = {0};

				// imposta il nome della mailbox (ossia della directory) con l'email del mittente
				strcpyn(pMailBox,pHostLastSend+9,MAX_EMAIL_LEN + 1);
				if((p = strchr(pMailBox,'>'))!=NULL)
					*p = '\0';

				// comando
				sprintf(	pHostLastRecv,
						"250 <%s> Recipient ok\r\n",
						pMailBox
						);
			}
			/*
			DATA
			*/
			else if(memcmp(pHostLastSend,"DATA",4)==0)
			{
				int h,tot;
				char	buffer[_MAX_PATH + 1] = {0};

				// ricava il nome del file relativo ai totali
				sprintf(buffer,"%s%s\\%s",MAIL_DIRECTORY,pMailBox,COUNT_FILE);
					
				// apre (o crea) il file dei totali e ricava il numero di email presenti
				if((h = _lopen(buffer,OF_READWRITE))!=HFILE_ERROR)
				{
					char buffer[6] = {0};
						
					if(_lread(h,buffer,sizeof(buffer)-1) <= 0)
						tot = 1;
					else
						tot = atoi(buffer)+1;
				}
				else
				{
					h = _lcreat(buffer,0);
					tot = 1;
				}
				
				// apertura/creazione fallita
				if(h==HFILE_ERROR)
				{
					WSASetLastError(WSAEBADF);
					return(SOCKET_ERROR);
				}
					
				// imposta il nome del file per la registrazione del messaggio email
				sprintf(buffer,"%s%s\\%d.txt",MAIL_DIRECTORY,pMailBox,tot);
					
				// crea il file per il messaggio email e registra sul file dei totali il numero di email
				// (il file verra' chiuso con il comando END)
				if((hMailBox = _lcreat(buffer,0))!=HFILE_ERROR)
				{
					sprintf(buffer,"%d     ",tot);
					_llseek(h,0,0);
					_lwrite(h,buffer,strlen(buffer));
				}
				else
				{
					WSASetLastError(WSAEBADF);
					return(SOCKET_ERROR);
				}
					
				_lclose(h);

				// comando
				strcpyn(pHostLastRecv,"354 Enter mail, end with \".\" on a line by itself\r\n",WSA_MAX_MTU + 1);
			}
			/*
			END
			*/
			else if(memcmp(pHostLastSend,"END",3)==0)
			{   
				// chiude il file per il messaggio email
				// (aperto con il comando DATA)
				_lclose(hMailBox);
				hMailBox = HFILE_ERROR;
				
				// comando
				strcpy(pHostLastRecv,"250 Message accepted for delivery\r\n");
			}
			/*
			QUIT
			*/
			else if(memcmp(pHostLastSend,"QUIT",4)==0)
			{
				// comando
				sprintf(	pHostLastRecv,
						"221 %s closing connection\r\n",
						pHostName
						);
			}
			else if(pHostLastSend[0]=='\0')
			{
				memset(pHostLastRecv,'\0',WSA_MAX_MTU + 1);
				WSASetLastError(0);
				ret = 0;
			}
			/*
			?
			*/
			else
			{
				// comando
				strcpy(pHostLastRecv,"421");
				WSASetLastError(WSAEOPNOTSUPP);
				ret = SOCKET_ERROR;
			}
			
			memset(pHostLastSend,'\0',WSA_MAX_MTU + 1);

			break;
		}
        
		/*
			POP3
			
			- gestisce solo gli utenti presenti in USERS_FILE
			- il messaggio non puo' eccedere i BUF_SIZE caratteri
			- il comando DELE puo' gestire fino a MAX_DELE_MARK mark
			- il comando DELE elimina solo i messaggi marcati, pero', dato che il file per i totali viene
			  azzerato comunque ed i messaggi non vengono rinumerati, la chiamata seguente restituisce 0
			  messaggi presenti
			- il comando USER non controlla l'esistenza dell'email (directory su disco)
			- il comando PASS non controlla l'esistenza della password (USERS_FILE)
			- il comando STAT restituisce solo il numero di email presenti, senza la dimensione del messaggio
			- ogni record del file USERS_FILE deve essere di USR_ENTRY_LEN caratteri (compresi i CRLF)
			- non gestisce la possibilita' che durante la lettura dal buffer interno su quello per la
			  ricezione dall'host la sequenza \r\n.\r\n possa trovarsi a cavallo di due cicli
			- gestiti: CONNECT (interno), USER, PASS, STAT, RETR, TOP, DELE, QUIT, NOP (interno)
		*/
		case POP3_PORT:
		{
			/*
			CONNECT
			*/
			if(memcmp(pHostLastSend,"CONNECT",7)==0)
			{
				int i;
				
				// inizializza l'array per il comando DELE
				for(iDelIndex = -1,i = 0; i < MAX_DELE_MARK; i++)
					iDelArray[i] = -1;
					
				// comando
				sprintf(	pHostLastRecv,
						"+OK %s POP3 Server (Version 1.0) ready.\r\n",
						pHostName
						);
			}
			/*
			USER
			*/
			else if(memcmp(pHostLastSend,"USER",4)==0)
			{
				int h;
				char buffer[_MAX_PATH + 1];
				
				// azzera il nome della mailbox
				memset(pMailBox,'\0',MAX_EMAIL_LEN + 1);
				
				// imposta il nome per il file con l'elenco degli utenti
				sprintf(buffer,"%s%s",MAIL_DIRECTORY,USERS_FILE);
					
				// apre l'elenco degli utenti
				if((h = _lopen(buffer,OF_READWRITE))!=HFILE_ERROR)
				{
					// legge cercando il nome dell'utente
					while(_lread(h,buffer,USR_ENTRY_LEN)==USR_ENTRY_LEN)
					{
						char* p;

						// ricava il nome dell'utente
						if((p = strchr(buffer,' '))!=NULL)
							*p = '\0';
						
						// lo confronta con quanto ricevuto
						if(memcmp(buffer,pHostLastSend+5,strlen(buffer))==0)
						{
							// ricava l'email (ossia il nome della directory)
							if((p = strchr(buffer+MAX_USR_LEN,' '))!=NULL)
								*p = '\0';
							strcpyn(pMailBox,buffer+MAX_USR_LEN,MAX_EMAIL_LEN + 1);
							break;
						}
					}
						
					_lclose(h);
				}

				// comando
				if(pMailBox[0]!='\0')
					strcpy(pHostLastRecv,"+OK please send PASS command\r\n");
				else
					strcpy(pHostLastRecv,"-ERR unknow user\r\n");
			}
			/*
			PASS
			*/
			else if(memcmp(pHostLastSend,"PASS",4)==0)
			{
				int h;
				char buffer[_MAX_PATH + 1];
					
				// ricava il nome del file relativo ai totali
				sprintf(buffer,"%s%s\\%s",MAIL_DIRECTORY,pMailBox,COUNT_FILE);
					
				// legge il numero totale di email presenti
				if((h = _lopen(buffer,OF_READWRITE))==HFILE_ERROR)
				{
					if((h = _lcreat(buffer,0))!=HFILE_ERROR)
					{
						_lwrite(h,"0",1);
						iTotMail = 0;
					}
					else
					{
						WSASetLastError(WSAECONNREFUSED);
						return(SOCKET_ERROR);
					}
				}
				else
				{
					_lread(h,buffer,4);
					iTotMail = atoi(buffer);
				}
				
				if(h!=HFILE_ERROR)
					_lclose(h);
				
				// comando
				sprintf(	pHostLastRecv,
						"+OK %d message ready for %s in %s\\%s\r\n",
						iTotMail,
						pMailBox,
						MAIL_DIRECTORY,
						pMailBox
						);
			}
			/*
			STAT
			*/
			else if(memcmp(pHostLastSend,"STAT",4)==0)
			{
				// comando
				sprintf(	pHostLastRecv,
						"+OK %d 0\r\n",
						iTotMail
						);
			}
			/*
			RETR
			*/
			else if(memcmp(pHostLastSend,"RETR",4)==0)
			{
				int h,num;
				char buffer[_MAX_PATH + 1];
				
				// numero dell'email da ricevere
				num = atoi(pHostLastSend+5);
				
				// imposta il nome del file relativo al messaggio email richiesto
				sprintf(buffer,"%s%s\\%d.txt",MAIL_DIRECTORY,pMailBox,num);

				memset(pPop3Buffer,'\0',BUF_SIZE+1);

				// apre (e legge) il file contenente il messaggio email
				if((h = _lopen(buffer,OF_READWRITE))!=HFILE_ERROR)
				{
					int n = _lread(h,pPop3Buffer,BUF_SIZE);
					_lclose(h);
					
					// controlla che sia presente la sequenza di fine messaggio
					if(strstr(pPop3Buffer,"\r\n.\r\n")==NULL)
					{
						if((n+6) >= (BUF_SIZE))
							n = BUF_SIZE-6;
							
						pPop3Buffer[n+1] = '\r';
						pPop3Buffer[n+2] = '\n';
						pPop3Buffer[n+3] = '.';
						pPop3Buffer[n+4] = '\r';
						pPop3Buffer[n+5] = '\n';
						pPop3Buffer[n+6] = '\0';
					}
				}
				else
				{
					WSASetLastError(WSAEBADF);
					return(SOCKET_ERROR);
				}
                
				// imposta il flag per la lettura del file dal buffer
				bRetr = TRUE;

				// comando
				strcpy(pHostLastSend,"NOP");
				sprintf(	pHostLastRecv,
						"+OK message %d (0 octets):\r\n",
						num
						);
			}
			/*
			TOP
			*/
			else if(memcmp(pHostLastSend,"TOP",3)==0)
			{
				int h,num;
				char buffer[_MAX_PATH + 1];
				
				// numero dell'email da ricevere
				num = atoi(pHostLastSend+4);
				
				// imposta il nome del file relativo al messaggio email richiesto
				sprintf(buffer,"%s%s\\%d.txt",MAIL_DIRECTORY,pMailBox,num);

				memset(pPop3Buffer,'\0',BUF_SIZE+1);

				// apre (e legge) il file contenente il messaggio email
				if((h = _lopen(buffer,OF_READWRITE))!=HFILE_ERROR)
				{
					int n = _lread(h,pPop3Buffer,BUF_SIZE-1);
					_lclose(h);
					
					// lascia nel buffer solo l'header del messaggio
					*strstr(pPop3Buffer,"\r\n\r\n") = '\0';
					strcat(pPop3Buffer,"\r\n.\r\n");
				}
				else
				{
					WSASetLastError(WSAEBADF);
					return(SOCKET_ERROR);
				}
                
				// imposta il flag per la lettura del file dal buffer
				bRetr = TRUE;

				// comando
				strcpy(pHostLastSend,"NOP");
				sprintf(	pHostLastRecv,
						"+OK message %d (0 octets):\r\n",
						num
						);
			}
			/*
			DELE
			*/
			else if(memcmp(pHostLastSend,"DELE",4)==0)
			{
				int num;
				
				// numero dell'email da eliminare
				num = atoi(pHostLastSend+5);
				
				// inserisce il progressivo dell'email da eliminare nell'array
				if(++iDelIndex < MAX_DELE_MARK)
					iDelArray[iDelIndex] = num;
				
				// comando
				sprintf(	pHostLastRecv,
						"+OK message %d marked for deletion\r\n",
						num
						);
			}
			/*
			QUIT
			*/
			else if(memcmp(pHostLastSend,"QUIT",4)==0)
			{
				int i,h;
				char buffer[_MAX_PATH + 1];
				
				// elimina le email marcate con DELE
				for(i = 0; i < MAX_DELE_MARK; i++)
				{
					if(iDelArray[i]!=-1)
					{
						_snprintf(buffer,sizeof(buffer)-1,"%s%s\\%d.txt",MAIL_DIRECTORY,pMailBox,iDelArray[i]);
						remove(buffer);
					}
				}

				//$
				_snprintf(buffer,sizeof(buffer)-1,"%s%s\\%s",MAIL_DIRECTORY,pMailBox,COUNT_FILE);
				h = _lcreat(buffer,0);
				_lclose(h);

				for(iDelIndex = -1,i = 0; i < MAX_DELE_MARK; i++)
					iDelArray[i] = -1;
				
				// comando
				sprintf(	pHostLastRecv,
						"+OK %s POP3 Server (Version 1.0) shutdown.\r\n",
						pHostName
						);
			}
			/*
			NOP
			*/
			else if(memcmp(pHostLastSend,"NOP",3)==0)
			{
				; // per i cicli di lettura dal buffer interno su quello di ricezione con RETR e TOP
			}
			else if(pHostLastSend[0]=='\0')
			{
				memset(pHostLastRecv,'\0',WSA_MAX_MTU + 1);
				WSASetLastError(0);
				ret = 0;
			}
			/*
			?
			*/
			else
			{
				// comando
				strcpy(pHostLastRecv,"-ERR unknow command\r\n");
				WSASetLastError(WSAEOPNOTSUPP);
				ret = SOCKET_ERROR;
			}

			memset(pHostLastSend,'\0',sizeof(pHostLastSend));
            
			/*
			sta' leggendo il file dal buffer interno su quello per la ricezione dall'host
			*/
			if(bRetr)
			{
				int i = 0,n = 0,lenght = len;
				char* b = (pPop3Buffer+tot);
				char* p = pHostLastRecv;

				// per la prima lettura dal buffer (tot=0) posiziona il puntatore dopo +OK...
				if(tot==0)
				{
					while(*p)
						p++,n++;
						
					lenght -= n;
				}
				
				if(*b)
				{
					// copia dal buffer interno sul buffer di ricezione
					while(*b && i < lenght)
					{
						*p++ = *b++;
						i++;
					}
	                
					// aggiorna l'offset per il buffer interno
					tot += i;
					
					// termina il buffer di ricezione 
					*p = '\0';
				}
				else
				{   
					strcpy(pHostLastRecv,"\r\n.\r\n");
				}

				if(strstr(pHostLastRecv,"\r\n.\r\n"))
				{
					tot = 0;
					bRetr = FALSE;
				}
			}

			break;
		}
        
		/*
		?
		*/
		default:
		{
			memset(pHostLastRecv,'\0',WSA_MAX_MTU + 1);
			WSASetLastError(WSAEOPNOTSUPP);
			ret = SOCKET_ERROR;
			break;
		}
	}
	
	// copia dal buffer di ricezione su quello del chiamante
	if((int)strlen(pHostLastRecv) > len)
		pHostLastRecv[len-1] = '\0';
		
	strcpyn(buf,pHostLastRecv,len);
	
	return(ret);
}

#endif // _DEBUGSOCKET

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -