DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Odd Errors When Compiling Winsock Code. (http://www.daniweb.com/forums/thread153546.html)

f.ben.isaac Oct 26th, 2008 1:41 am
Odd Errors When Compiling Winsock Code.
 
Hello, first of all i'm glad to be a member here. Nice to be with you!

I wrote a simple dum port scanner. For practicing purposes. It worked fine under linux, i ported to windows, i linked it to winsock libraries. I keep getting errors, such as

C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|29|error C3861: 'getaddrinfo': identifier not found|

Anyway here is my code if you want to see it:

#include<iostream>
#include<string>
#include<sstream>
#include"WinSock2.h"
using namespace std;

struct addrinfo hints;              //fill your host info - hints is input
struct addrinfo *results;        //gets your host info - results is output
char *remoteIP = new char[40];  //holds inserted remote IP
string tempPort;                //holds ports temporarly
int startingPort;              //stores the starting range of port number
int status;                    //receives the status of your pc address
int currentPort;          //holds current port


//program description
void progDesc()
{
    cout<<"This is a simple port scanner, scan range of\n";
    cout<<"ports on your local machine...\n"<<endl;

}


//getaddrinfe locates your machine, more specific
//details of your host address is returned to results.
void getAddrIn()
{
        status = getaddrinfo(remoteIP, NULL , &hints, &results);

        if(status != 0)
        {
                fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
                exit(1);
        }
}


//Run the program
int main()
{
        int endingPort;                //stores the ending rage of port number

    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

        /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
    wVersionRequested = MAKEWORD(2, 2);

    err = WSAStartup(wVersionRequested, &wsaData);
    if (err != 0) {
        /* Tell the user that we could not find a usable */
        /* Winsock DLL.                                  */
        printf("WSAStartup failed with error: %d\n", err);
        return 1;
    }

        system("clear");

        //tell user what program does
        progDesc();

        //set size of hints to zero
        memset(&hints, 0, sizeof hints);

        //fill some of your host address info
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        //ask for remote IP
        cout<<"Please enter your target IP: ";
        cin>>remoteIP;

        getAddrIn();

        //ask port range from user
        cout<<"Enter Starting Port: ";
        cin>>startingPort;

        cout<<"Enter Ending Port: ";
        cin>>endingPort;

        cout<<endl;

        cout<<"Start Checking: "<<endl;


        //check the status
        while(startingPort <= endingPort)
        {
                //call getaddrinfo()
                getAddrIn();

                //create a socket.
                int socketfd;
                socketfd = socket(results->ai_family, results->ai_socktype, results->ai_protocol);

                if(socketfd == -1 )
                {
                        cout<<"Error: failed to create a socket.\n";
                        return 2;
                }

                struct sockaddr_in *specifyPort = (struct sockaddr_in *)results->ai_addr;

                specifyPort->sin_port = htons(startingPort);

                int connectStatus;
                connectStatus = connect(socketfd, results->ai_addr, results->ai_addrlen);

                if(connectStatus == -1 )
                {
                        if(errno == ECONNREFUSED)
                                cout<<"Port "<<startingPort<<" is Closed or Blocked.\n";
                        //Alternatively, you can do:
                        //cout << "connect failed; reason = " << strerror(errno) <<endl;
                }else{
                        cout<<"Port "<<startingPort<<" is OPEN.\n";
                }


                closesocket(socketfd);

                //move to the next port in the specified range
                startingPort++;

        }

        //deallocate memory
        delete[] remoteIP;

        //free linkedlist of struct addrinfo *results
        freeaddrinfo(results);
    WSACleanup();
        return 0;
}

Errors:

C:\Program Files\Microsoft Visual Studio 9.0\VC\include\xlocale|342|warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|7|error C2079: 'hints' uses undefined struct 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|29|error C3861: 'getaddrinfo': identifier not found|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|33|error C3861: 'gai_strerror': identifier not found|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|53|error C2228: left of '.ai_family' must have class/struct/union|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|54|error C2228: left of '.ai_socktype' must have class/struct/union|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_family' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_socktype' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_protocol' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|90|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|90|error C2227: left of '->ai_addr' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2227: left of '->ai_addr' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2027: use of undefined type 'addrinfo'|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2227: left of '->ai_addrlen' must point to class/struct/union/generic type|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|99|error C2065: 'ECONNREFUSED' : undeclared identifier|
C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|119|error C3861: 'freeaddrinfo': identifier not found|
||=== Build finished: 19 errors, 1 warnings ===|


PLZ HELP.... I ALREADY DID ALL I CAN. I'm about to give up....

Ancient Dragon Oct 26th, 2008 9:39 am
Re: Odd Errors When Compiling Winsock Code.
 
you are missing a header file -- see example program here

William Hemsworth Oct 26th, 2008 1:09 pm
Re: Odd Errors When Compiling Winsock Code.
 
I managed to make the code compile free of errors and warnings, here are the changes I made.
#include<iostream>
#include<string>
#include<sstream>
#include<winsock2.h>
#include<ws2tcpip.h>

#pragma comment(lib, "Ws2_32.lib")

using namespace std;

struct addrinfo hints;              //fill your host info - hints is input
struct addrinfo *results;        //gets your host info - results is output
char *remoteIP = new char[40];  //holds inserted remote IP
string tempPort;                //holds ports temporarly
int startingPort;              //stores the starting range of port number
int status;                    //receives the status of your pc address
int currentPort;          //holds current port


//program description
void progDesc()
{
    cout<<"This is a simple port scanner, scan range of\n";
    cout<<"ports on your local machine...\n"<<endl;

}


//getaddrinfe locates your machine, more specific
//details of your host address is returned to results.
void getAddrIn()
{
        status = getaddrinfo(remoteIP, NULL , &hints, &results);

        if(status != 0)
        {
                fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
                exit(1);
        }
}


//Run the program
int main()
{
        int endingPort;                //stores the ending rage of port number

    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

        /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
    wVersionRequested = MAKEWORD(2, 2);

    err = WSAStartup(wVersionRequested, &wsaData);
    if (err != 0) {
        /* Tell the user that we could not find a usable */
        /* Winsock DLL.                                  */
        printf("WSAStartup failed with error: %d\n", err);
        return 1;
    }

        system("cls");

        //tell user what program does
        progDesc();

        //set size of hints to zero
        memset(&hints, 0, sizeof hints);

        //fill some of your host address info
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        //ask for remote IP
        cout<<"Please enter your target IP: ";
        cin>>remoteIP;

        getAddrIn();

        //ask port range from user
        cout<<"Enter Starting Port: ";
        cin>>startingPort;

        cout<<"Enter Ending Port: ";
        cin>>endingPort;

        cout<<endl;

        cout<<"Start Checking: "<<endl;


        //check the status
        while(startingPort <= endingPort)
        {
                //call getaddrinfo()
                getAddrIn();

                //create a socket.
                SOCKET socketfd;
                socketfd = (int) socket(results->ai_family, results->ai_socktype, results->ai_protocol);

                if(socketfd == -1 )
                {
                        cout<<"Error: failed to create a socket.\n";
                        return 2;
                }

                struct sockaddr_in *specifyPort = (struct sockaddr_in *)results->ai_addr;

                specifyPort->sin_port = htons(startingPort);

                int connectStatus;
                connectStatus = connect(socketfd, results->ai_addr, (int)results->ai_addrlen);

                if(connectStatus == -1 )
                {
                        if(errno == WSAECONNREFUSED)
                                cout<<"Port "<<startingPort<<" is Closed or Blocked.\n";
                        //Alternatively, you can do:
                        //cout << "connect failed; reason = " << strerror(errno) <<endl;
                }else{
                        cout<<"Port "<<startingPort<<" is OPEN.\n";
                }


                closesocket(socketfd);

                //move to the next port in the specified range
                startingPort++;

        }

        //deallocate memory
        delete[] remoteIP;

        //free linkedlist of struct addrinfo *results
        freeaddrinfo(results);
    WSACleanup();
        return 0;
}
Hope this helps :]

f.ben.isaac Oct 26th, 2008 5:29 pm
Re: Odd Errors When Compiling Winsock Code.
 
Hey, great!!!!! worked

f.ben.isaac Oct 26th, 2008 5:31 pm
Re: Odd Errors When Compiling Winsock Code.
 
I did not do this #pragma comment(lib, "Ws2_32.lib")

I went to the linker & put the path that leads to Ws2_32.lib....

THANKS


All times are GMT -4. The time now is 11:44 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC