Example #1
0
BOOL GetIPAddress(LPCTSTR lpszHost, LPTSTR lpszIP, int& iIPLen)
{
	BOOL isOK = TRUE;

	if(IsIPAddress(lpszHost))
	{
		int iHostLen = lstrlen(lpszHost);
		
		if(iHostLen > 0)
			++iHostLen;

		if(iHostLen > 0 && iIPLen >= iHostLen)
			lstrcpy(lpszIP, lpszHost);
		else
			isOK = FALSE;

		iIPLen = iHostLen;
	}
	else
	{
		IN_ADDR addr;

		if(GetOptimalIPByHostName(lpszHost, addr))
			isOK = IN_ADDR_2_IP(addr, lpszIP, iIPLen);
		else
			isOK = FALSE;
	}

	return isOK;
}
Example #2
0
    static bool HandleBanInfoIPCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        char* ipStr = strtok((char*)args, "");
        if (!ipStr)
            return false;

        if (!IsIPAddress(ipStr))
            return false;

        std::string IP = ipStr;

        LoginDatabase.EscapeString(IP);
        QueryResult result = LoginDatabase.PQuery("SELECT ip, FROM_UNIXTIME(bandate), FROM_UNIXTIME(unbandate), unbandate-UNIX_TIMESTAMP(), banreason, bannedby, unbandate-bandate FROM ip_banned WHERE ip = '%s'", IP.c_str());
        if (!result)
        {
            handler->PSendSysMessage(LANG_BANINFO_NOIP);
            return true;
        }

        Field* fields = result->Fetch();
        bool permanent = !fields[6].GetUInt64();
        handler->PSendSysMessage(LANG_BANINFO_IPENTRY,
            fields[0].GetCString(), fields[1].GetCString(), permanent ? handler->GetTrinityString(LANG_BANINFO_NEVER) : fields[2].GetCString(),
            permanent ? handler->GetTrinityString(LANG_BANINFO_INFINITE) : secsToTimeString(fields[3].GetUInt64(), true).c_str(), fields[4].GetCString(), fields[5].GetCString());


        return true;
    }
Example #3
0
/// Ban an IP address or a user account
void CliBan(char*command,pPrintf zprintf)
{
    ///- Get the command parameter
    char *banip = strtok(command," ");
    if(!banip)
    {
        zprintf("Syntax is: ban <account|ip>\r\n");
        return;
    }

    // Is this IP address or account name?
    bool is_ip = IsIPAddress(banip);

    if(sWorld.BanAccount(banip))
    {
        if(is_ip)
            zprintf("We banned IP: %s\r\n",banip);
        else
            zprintf("We banned account: %s\r\n",banip);
    }
    else
    {
        zprintf("Account %s not found and not banned.\r\n",banip);
    }
}
Example #4
0
bool CheckValidation(DataBaseAccess *dBA, char * serverIP){
	char macAddress[SIZEOFCHARARRAY];
	int DID = 0;

	if ( GetMacAddress(macAddress) ){
		cout<<"\nMacAddress--->"<<macAddress<<endl;
		CreateUniqueDID(macAddress,DID);
		if ( !dBA->isMacAddressExist(macAddress, serverIP ) ){
			while(1){
				cout<<"\nPlease Enter DeviceIp---->";
				gets(serverIP);
				if ( !IsIPAddress(serverIP) ){
					cout<<"\nInvalid IPAddress Please try again\n";
				}
				else
					break;
			}
			if ( !dBA->InsertOwnerDeviceIp( macAddress,DID,serverIP ) ){
				cout<<"\n Unable to Insert \n"<<endl;
				return false;
			}
			else{
				cout<<"\nnot Exists inserted\n";
				return true;
			}
		}else{
			cout<<"\nServerIP--->"<<serverIP;
			return true;
		}
	}
	else
		return false;
}
char *FindIPAddressInString (char *psz)
   {
   psz = FindNumber(psz);
   while (psz && !IsIPAddress(psz))
      {
      while (psz && isdigit(*psz))
         ++psz;
      psz = FindNumber(psz);
      }

   return psz;
   }
Example #6
0
/// Unban an IP adress or a user account
void CliRemoveBan(char *command,pPrintf zprintf)
{
    ///- Get the command parameter
    char *banip = strtok(command," ");
    if(!banip)
        zprintf("Syntax is: removeban <account|ip>\r\n");

    sWorld.RemoveBanAccount(banip);

    ///- If this is an IP address
    if(IsIPAddress(banip))
        zprintf("We removed banned IP: %s\r\n",banip);
    else
        zprintf("We removed ban from account: %s\r\n",banip);
}
Example #7
0
BOOL GetIPAddress(LPCTSTR lpszHost, CStringA& strIP)
{
	BOOL isOK = TRUE;

	if(IsIPAddress(lpszHost))
		strIP = lpszHost;
	else
	{
		hostent* host = ::gethostbyname(CT2A(lpszHost));

		if(!host)
			return FALSE;

		strIP = inet_ntoa(*(struct in_addr *)*host->h_addr_list); 
	}

	return TRUE;
}
Example #8
0
    static bool HandleUnBanHelper(BanMode mode, char const* args, ChatHandler* handler)
    {
        if (!*args)
            return false;

        char* nameOrIPStr = strtok((char*)args, " ");
        if (!nameOrIPStr)
            return false;

        std::string nameOrIP = nameOrIPStr;

        switch (mode)
        {
            case BAN_ACCOUNT:
                if (!AccountMgr::normalizeString(nameOrIP))
                {
                    handler->PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, nameOrIP.c_str());
                    handler->SetSentErrorMessage(true);
                    return false;
                }
                break;
            case BAN_CHARACTER:
                if (!normalizePlayerName(nameOrIP))
                {
                    handler->SendSysMessage(LANG_PLAYER_NOT_FOUND);
                    handler->SetSentErrorMessage(true);
                    return false;
                }
                break;
            case BAN_IP:
                if (!IsIPAddress(nameOrIP.c_str()))
                    return false;
                break;
        }

        if (sWorld->RemoveBanAccount(mode, nameOrIP))
            handler->PSendSysMessage(LANG_UNBAN_UNBANNED, nameOrIP.c_str());
        else
            handler->PSendSysMessage(LANG_UNBAN_ERROR, nameOrIP.c_str());

        return true;
    }
    // ========================================================================
    // Function:    GetIPAddress
    // Purpose:     To get the IP address of the string as an ipaddress
    //              structure. Throws an exception if the address cannot be
    //              converted.
    // ========================================================================
    ipaddress GetIPAddress( const std::string p_address )
    {
        
        if( IsIPAddress( p_address ) )
        {
            // if the address is just a regular IP address, there's no need
            // to do a DNS lookup, so just convert the string directly into
            // its binary format.
            ipaddress addr = inet_addr( p_address.c_str() );
            
            // if the address is invalid, throw a HOST_NOT_FOUND exception.
            if( addr == INADDR_NONE )
            {
                throw Exception( EDNSNotFound );
            }

            // by this point, the address is valid, so return it.
            return addr;
        }
        else
        {
            // the address isn't an IP address, so we need to look it up using
            // DNS. 
            struct hostent* host = gethostbyname( p_address.c_str() );

            // if there was an error, throw an exception.
            if( host == 0 )
            {
                // get the error from h_errno.
                throw Exception( GetError( false ) );
            }

            // now perform some really wierd casting tricks to get the value.
            // h_addr is a char*, so cast it into an ipaddress*, and 
            // dereference it to get the value.
            return *((ipaddress*)host->h_addr);
        }
    }
Example #10
0
    static bool HandleBanHelper(BanMode mode, char const* args, ChatHandler* handler)
    {
        if (!*args)
            return false;

        char* cnameOrIP = strtok((char*)args, " ");
        if (!cnameOrIP)
            return false;

        std::string nameOrIP = cnameOrIP;

        char* durationStr = strtok(NULL, " ");
        if (!durationStr || !atoi(durationStr))
            return false;

        char* reasonStr = strtok(NULL, "");
        if (!reasonStr)
            return false;

        switch (mode)
        {
            case BAN_ACCOUNT:
                if (!AccountMgr::normalizeString(nameOrIP))
                {
                    handler->PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, nameOrIP.c_str());
                    handler->SetSentErrorMessage(true);
                    return false;
                }
                break;
            case BAN_CHARACTER:
                if (!normalizePlayerName(nameOrIP))
                {
                    handler->SendSysMessage(LANG_PLAYER_NOT_FOUND);
                    handler->SetSentErrorMessage(true);
                    return false;
                }
                break;
            case BAN_IP:
                if (!IsIPAddress(nameOrIP.c_str()))
                    return false;
                break;
        }

        switch (sWorld->BanAccount(mode, nameOrIP, durationStr, reasonStr, handler->GetSession() ? handler->GetSession()->GetPlayerName().c_str() : ""))
        {
            case BAN_SUCCESS:
                if (atoi(durationStr) > 0)
                    handler->PSendSysMessage(LANG_BAN_YOUBANNED, nameOrIP.c_str(), secsToTimeString(TimeStringToSecs(durationStr), true).c_str(), reasonStr);
                else
                    handler->PSendSysMessage(LANG_BAN_YOUPERMBANNED, nameOrIP.c_str(), reasonStr);
                break;
            case BAN_SYNTAX_ERROR:
                return false;
            case BAN_NOTFOUND:
                switch (mode)
                {
                    default:
                        handler->PSendSysMessage(LANG_BAN_NOTFOUND, "account", nameOrIP.c_str());
                        break;
                    case BAN_CHARACTER:
                        handler->PSendSysMessage(LANG_BAN_NOTFOUND, "character", nameOrIP.c_str());
                        break;
                    case BAN_IP:
                        handler->PSendSysMessage(LANG_BAN_NOTFOUND, "ip", nameOrIP.c_str());
                        break;
                }
                handler->SetSentErrorMessage(true);
                return false;
        }

        return true;
    }
Example #11
0
/*********************************************
Finger
    Performs the finger for the specified address
    the information that is returned can be 
    retrieved with the GetReturnLine function.

    If filename parameter was specified all the 
    received data is saved in the file. In this 
    case you can't access data with GetReturnLine 
    function.

Params
    address     - address to perform the finger on. 
                  address format is: @domain.com or 
                  [email protected]

    dest        - data source for saving received data
    [fileType]  -   -1              - don't use
                    UTM_OM_WRITING  - open writing
                    UTM_OM_APPEND   - open for appending 


Return
    UTE_SUCCESS                 - success
    UTE_INVALID_ADDRESS_FORMAT  - invalid address format
    UTE_INVALID_ADDRESS         - invalid address
    UTE_CONNECT_FAILED          - connection failed
    UTE_NO_RESPONSE             - no response
    UTE_ABORTED                 - aborted
    UTE_CONNECT_TIMEOUT         - time out
**********************************************/
int CUT_FingerClient::Finger(LPTSTR address, CUT_DataSource & dest, OpenMsgType fileType) 
{
    
    int     error = UTE_SUCCESS;
    int     len;
    char    buf[MAX_PATH+1];
    char    domain[MAX_PATH+1];

    //delete prev finger information
    m_listReturnLines.ClearList();          

    //split the address apart
	// v4.2 address splitting into name and domain allows AC macro, hence LPTSTR in interface.
    if(SplitAddress(buf,domain,AC(address)) != UTE_SUCCESS)
        return OnError(UTE_INVALID_ADDRESS_FORMAT);

    //check to see if the domain is a name or address
    if(IsIPAddress(domain) != TRUE) {
        //get the name from the address
        if(GetAddressFromName(domain,domain,sizeof(domain)) != UTE_SUCCESS)
            return OnError(UTE_INVALID_ADDRESS);
        }

    //connect using a timeout
    if((error=Connect(m_nPort, domain, m_nConnectTimeout)) != UTE_SUCCESS)
        return OnError(error);

    if(IsAborted())                         // Test abort flag
        error = UTE_ABORTED;                // Aborted

    if(error == UTE_SUCCESS) {

        //send the name to finger
        Send(buf);
        Send("\r\n");

        // Read data into the file
        if(fileType != -1) {
            error = Receive(dest, fileType, m_nReceiveTimeout);
            }

        // Read data line by line & save it in the string list
        else {
            //read in the return lines

			// v4.2 change to eliminate C4127:conditional expression is constant
			for(;;) {
				if(IsAborted()) {                       // Test abort flag
                    error = UTE_ABORTED;                // Aborted
                    break;
                    }

                //wait for a receive
                if(WaitForReceive(m_nReceiveTimeout, 0)!= UTE_SUCCESS)
                    break;
        
                //get the information
                len = ReceiveLine(buf, sizeof(buf)-1);
                if(len <= 0)
                    break;
        
                buf[len]=0;
                CUT_StrMethods::RemoveCRLF(buf);

                //store the information
                m_listReturnLines.AddString(buf);
                }

            if (error == UTE_SUCCESS && m_listReturnLines.GetCount() == 0)
                error = UTE_NO_RESPONSE;                // No response
            }
        }

    // Close connection
    CloseConnection();

    return OnError(error);
}
Example #12
0
int
nsSocket::Open()
{
#ifdef _WINDOWS
    /* funky windows initialization of winsock */

    int err;
    WSADATA wsaData;
    WORD wVersionRequested;
    
    /* We don't care which version we get because we're not
     * doing any specific to a particular winsock version. */
    /* Request for version 2.2 */
    wVersionRequested = MAKEWORD(2, 2);
    err = WSAStartup(wVersionRequested, &wsaData);
    if (err == WSAVERNOTSUPPORTED)
    {
        /* Request for version 1.1 */
        wVersionRequested = MAKEWORD(1, 1);
        err = WSAStartup(wVersionRequested, &wsaData);
        if (err == WSAVERNOTSUPPORTED)
        {
            /* Request for version 1.0 */
            wVersionRequested = MAKEWORD(0, 1);
            err = WSAStartup(wVersionRequested, &wsaData);
            if (err == WSAVERNOTSUPPORTED)
            {
                /* Request for version 0.4 */
                wVersionRequested = MAKEWORD(4, 0);
                err = WSAStartup(wVersionRequested, &wsaData);
            }
        }
    }

    if (err != 0)
    {
        return E_WINSOCK;
    }
#endif

    int rv = OK;
    struct sockaddr_in servaddr;
    struct hostent *hptr = NULL;

    mFd = socket(AF_INET, SOCK_STREAM, 0);
#ifdef _WINDOWS
	if (mFd == INVALID_SOCKET) {
			printf("Last error: %d\n", WSAGetLastError());
    }
    if ( mFd != INVALID_SOCKET ) {
#else
    if ( mFd != -1 ) {
#endif

        int windowSize = 32768;   // we could tune this, but for now...
        socklen_t newTCPWin, len;
       
        len = sizeof( newTCPWin );
 
        setsockopt( mFd, SOL_SOCKET, SO_RCVBUF, (char*) &windowSize, sizeof( windowSize ));
#ifdef DEBUG
        getsockopt( mFd, SOL_SOCKET, SO_RCVBUF, (char*) &newTCPWin, &len );
#endif
 
        setsockopt( mFd, SOL_SOCKET, SO_SNDBUF, (char*) &windowSize, sizeof( windowSize ));
#ifdef DEBUG
        getsockopt( mFd, SOL_SOCKET, SO_RCVBUF, (char*) &newTCPWin, &len );
#endif

        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(mPort);

        if ((hptr = gethostbyname(mHost)) == NULL )
        {
            if (IsIPAddress(mHost) == OK)
            {
                unsigned long netAddr;

                netAddr = inet_addr(mHost);
                if ((hptr = gethostbyaddr((char *)&netAddr, sizeof(unsigned long), AF_INET)) == NULL )
                    return E_INVALID_HOST;
            }
            else
            {
                return E_INVALID_HOST;
            }
        }

        memcpy(&servaddr.sin_addr, (struct in_addr **) hptr->h_addr_list[0], sizeof(struct in_addr));

        rv = connect(mFd, (struct sockaddr *) &servaddr, sizeof(servaddr));
        if (rv < 0)
        {
#if defined(DEBUG) && (defined(__unix) || defined(__unix__))
            printf("ETIMEDOUT: %d\n", ETIMEDOUT);
            printf("ECONNREFUSED: %d\n", ECONNREFUSED);
            printf("EHOSTUNREACH: %d\n", EHOSTUNREACH);
            printf("ENETUNREACH: %d\n", ENETUNREACH);

            printf("connect error: %d\n", errno);
#endif /* DEBUG && (__unix || __unix__) */
            mFd = -1;
            rv = E_SOCK_OPEN;
        }
        else
            rv = OK;
    } else
        rv = E_SOCK_OPEN;

    return rv;
}

int
nsSocket::SrvOpen()
{
    int rv = OK;
    struct sockaddr_in servaddr;

    /* init data socket making it listen */
    mListenFd = socket(AF_INET, SOCK_STREAM, 0);

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* wildcard */
    servaddr.sin_port = 0; /* let kernel bind an ephemeral port */
    
    if ((bind(mListenFd, (struct sockaddr *) &servaddr, sizeof(servaddr))) != 0)
        return E_BIND;

    if ((listen(mListenFd, SOMAXCONN)) != 0)
        return E_LISTEN;
    return rv;
}