Exemplo n.º 1
0
int main(int argc,char *argv[])
{
    if (argc < 2)
        return -1;
    std::string str;
    std::string str2;
    Base64 b;
    if (!strcmp(argv[1],"-file"))
    {
        if (argc < 3)
            return -2;
        FILE *fil;
        if ((fil = fopen(argv[2],"rt")) != NULL)
        {
            b.encode(fil, str);
            fclose(fil);
        }
        printf("File:\n%s\n--End of File\n",str.c_str());
        b.decode(str, str2);
        printf("Content:\n%s\n--End of Content\n",str2.c_str());
    }
    else
    {
        b.encode(argv[1], strlen(argv[1]), str);
        printf("'%s' ==> '%s'",argv[1], str.c_str());
        b.decode(str, str2);
        printf(" ==> '%s'\n",str2.c_str());
    }
}
Exemplo n.º 2
0
void ResolvSocket::OnDetached()
{
#ifndef _WIN32
	if (m_query == "getaddrinfo")
	{
		struct addrinfo hints;
		struct addrinfo *res;
		memset(&hints, 0, sizeof(hints));
		hints.ai_flags |= AI_CANONNAME;
		int n = getaddrinfo(m_data.c_str(), NULL, &hints, &res);
		if (!n)
		{
/*
       struct addrinfo {
           int     ai_flags;
           int     ai_family; // PF_INET, PF_INET6
           int     ai_socktype;
           int     ai_protocol;
           size_t  ai_addrlen;
           struct sockaddr *ai_addr;
           char   *ai_canonname;
           struct addrinfo *ai_next;
       };


*/
			while (res)
			{
				Send("Flags: " + Utility::l2string(res -> ai_flags) + "\n");
				Send("Family: " + Utility::l2string(res -> ai_family) + "\n");
				Send("Socktype: " + Utility::l2string(res -> ai_socktype) + "\n");
				Send("Protocol: " + Utility::l2string(res -> ai_protocol) + "\n");
				Send("Addrlen: " + Utility::l2string(res -> ai_addrlen) + "\n");
				std::string tmp;
				Base64 bb;
				bb.encode( (unsigned char *)res -> ai_addr, res -> ai_addrlen, tmp, false);
				Send("Address: " + tmp + "\n");
				// base64-encoded sockaddr
				Send("Canonname: ");
				Send( res -> ai_canonname );
				Send("\n");
				Send("\n");
				//
				res = res -> ai_next;
			}
			freeaddrinfo(res);
		}
		else
		{
			std::string error = "Error: ";
			error += gai_strerror(n);
			Send( error + "\n" );
			Send("\n");
		}
	}
	else
#endif // _WIN32
	if (m_query == "gethostbyname")
	{
		struct hostent *h = gethostbyname(m_data.c_str());
		if (h)
		{
			char slask[1000];
			sprintf(slask, "Name: %s\n", h -> h_name);
			Send( slask );
			size_t i = 0;
			while (h -> h_aliases[i])
			{
				sprintf(slask, "Alias: %s\n", h -> h_aliases[i]);
				Send( slask );
				i++;
			}
			sprintf(slask, "AddrType: %d\n", h -> h_addrtype);
			Send( slask );
			sprintf(slask, "Length: %d\n", h -> h_length);
			Send( slask );
			i = 0;
			while (h -> h_addr_list[i])
			{
				// let's assume 4 byte IPv4 addresses
				char ip[40];
				*ip = 0;
				for (int j = 0; j < 4; j++)
				{
					if (*ip)
						strcat(ip,".");
					sprintf(ip + strlen(ip),"%u",(unsigned char)h -> h_addr_list[i][j]);
				}
				sprintf(slask, "A: %s\n", ip);
				Send( slask );
				i++;
			}
		}
		else
		{
			Send("Failed\n");
		}
		Send( "\n" );
	}
	else
	if (m_query == "gethostbyaddr")
	{
		Send("Not Implemented\n\n");
	}
	else
	{
		std::string msg = "Unknown query type: " + m_query;
		Handler().LogError(this, "OnDetached", 0, msg);
		Send("Unknown\n\n");
	}
	SetCloseAndDelete();
}