Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

c++ - Telnet server wont execute my command sent from a socket

Hi im currently working on a program that sends a command to the telnet server.

Here is my code:

int _tmain(int argc, _TCHAR* argv[])
{
    WSAData wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);
    addrinfo hints;
    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    addrinfo* result;
    int res = getaddrinfo("192.168.56.101", "23", &hints, &result);

    if (res)
    {
        std::cout << "failed to getaddrinfo" << std::endl;
    }

    SOCKET sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);

    if (connect(sock, result->ai_addr, (int)result->ai_addrlen) != SOCKET_ERROR && sock != INVALID_SOCKET)
    {
        std::cout << "connected";
        char* buf = "md c:\testfolder
";
        std::cout << send(sock, buf, sizeof("md c:\testfolder
"), 0);
        const int size = 255;
        char out[size];
        memset(out, 0, size);
        while (true)
        {
            res = recv(sock, out, size, 0);
            std::cout << res << std::endl; // this outputs 21
        }
    }
    else
    {

        std::cout << res;
    }



    std::cin.get();
    return 0;
}

After I send the command, the telnet server should create a folder in C:\ directory named testfolder, but its not doing it. recv() is sending me garbage values.

image

I have read RFC 854, but its really hard to read. So please, explain to me what I am doing wrong with my code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several problems with your code. Most importantly, if getaddrinfo() fails, you are progressing to the rest of the code, which will crash since result is invalid. And also, you are ignoring the return values of send() and recv(). And you are not actually outputting the data you receive.

Try something more like this:

int _tmain(int argc, _TCHAR* argv[])
{
    WSAData wsaData;
    int res = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (res != 0)
    {
        std::cerr << "failed to init Winsock, error " << res << std::endl;
        return 1;
    }

    addrinfo hints;
    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    addrinfo* result;

    res = getaddrinfo("192.168.56.101", "23", &hints, &result);
    if (res != 0)
    {
        std::cerr << "failed to getaddrinfo, error " << res << std::endl;
        WSACleanup();
        return 1;
    }

    SOCKET sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (sock == INVALID_SOCKET)
    {
        res = WSAGetLastError();
        std::cerr << "failed to create socket, error " << res << std::endl;
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }

    if (connect(sock, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR)
    {
        res = WSAGetLastError();
        std::cerr << "failed to connect, error " << res << std::endl;
        freeaddrinfo(result);
        closesocket(sock);
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);
    std::cout << "connected";

    std::string cmd = "md c:\testfolder
";

    const char *buf = cmd.c_str();
    int size = cmd.length();
    do
    {
        res = send(sock, buf, size, 0);
        if (res == SOCKET_ERROR)
        {
            res = WSAGetLastError();
            std::cerr << "failed to send, error " << res << std::endl;
            closesocket(sock);
            WSACleanup();
            return 1;
        }
        buf += res;
        size -= res;
    }
    while (size > 0);

    char out[255];
    while (true)
    {
        res = recv(sock, out, sizeof(out), 0);
        if (res == SOCKET_ERROR)
        {
            res = WSAGetLastError();
            std::cerr << "failed to recv, error " << res << std::endl;
            closesocket(sock);
            WSACleanup();
            return 1;
        }

        if (res == 0)
        {
            std::cout << "disconnected" << std::endl;
            break;
        }

        std::cout.write(out, res);
    }

    closesocket(sock);
    WSACleanup();

    return 0;
}

Now, if you are connecting to an actual Telnet server, not just an arbitrary TCP server running on the standard Telnet port, then you need to implement the actual Telnet protocol. That means handling and responding to Telnet commands (WILL, WONT, etc), negotiating Telnet options (message buffer size, charset support, etc), and so on. Everything that is described in RFC 854, and other related RFCs. It may be hard to read (not really), but you need to read and understand it. The "garbage" you receive from the server is likely not garbage at all. It is Telnet commands you are not interpreting yet.

In a nutshell, Telnet commands are escaped, so you have to process the received data byte-by-byte, and if you encounter the escape byte 0xFF then the next byte is the command type, and depending on what that command is, there may be further bytes to complete the command. Everything else that is not a command is just raw data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...