Odd Errors When Compiling Winsock Code.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 33
Reputation: f.ben.isaac is an unknown quantity at this point 
Solved Threads: 0
f.ben.isaac f.ben.isaac is offline Offline
Light Poster

Odd Errors When Compiling Winsock Code.

 
1
  #1
Oct 26th, 2008
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:

  1. #include<iostream>
  2. #include<string>
  3. #include<sstream>
  4. #include"WinSock2.h"
  5. using namespace std;
  6.  
  7. struct addrinfo hints; //fill your host info - hints is input
  8. struct addrinfo *results; //gets your host info - results is output
  9. char *remoteIP = new char[40]; //holds inserted remote IP
  10. string tempPort; //holds ports temporarly
  11. int startingPort; //stores the starting range of port number
  12. int status; //receives the status of your pc address
  13. int currentPort; //holds current port
  14.  
  15.  
  16. //program description
  17. void progDesc()
  18. {
  19. cout<<"This is a simple port scanner, scan range of\n";
  20. cout<<"ports on your local machine...\n"<<endl;
  21.  
  22. }
  23.  
  24.  
  25. //getaddrinfe locates your machine, more specific
  26. //details of your host address is returned to results.
  27. void getAddrIn()
  28. {
  29. status = getaddrinfo(remoteIP, NULL , &hints, &results);
  30.  
  31. if(status != 0)
  32. {
  33. fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
  34. exit(1);
  35. }
  36. }
  37.  
  38.  
  39. //Run the program
  40. int main()
  41. {
  42. int endingPort; //stores the ending rage of port number
  43.  
  44. WORD wVersionRequested;
  45. WSADATA wsaData;
  46. int err;
  47.  
  48. /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
  49. wVersionRequested = MAKEWORD(2, 2);
  50.  
  51. err = WSAStartup(wVersionRequested, &wsaData);
  52. if (err != 0) {
  53. /* Tell the user that we could not find a usable */
  54. /* Winsock DLL. */
  55. printf("WSAStartup failed with error: %d\n", err);
  56. return 1;
  57. }
  58.  
  59. system("clear");
  60.  
  61. //tell user what program does
  62. progDesc();
  63.  
  64. //set size of hints to zero
  65. memset(&hints, 0, sizeof hints);
  66.  
  67. //fill some of your host address info
  68. hints.ai_family = AF_UNSPEC;
  69. hints.ai_socktype = SOCK_STREAM;
  70.  
  71. //ask for remote IP
  72. cout<<"Please enter your target IP: ";
  73. cin>>remoteIP;
  74.  
  75. getAddrIn();
  76.  
  77. //ask port range from user
  78. cout<<"Enter Starting Port: ";
  79. cin>>startingPort;
  80.  
  81. cout<<"Enter Ending Port: ";
  82. cin>>endingPort;
  83.  
  84. cout<<endl;
  85.  
  86. cout<<"Start Checking: "<<endl;
  87.  
  88.  
  89. //check the status
  90. while(startingPort <= endingPort)
  91. {
  92. //call getaddrinfo()
  93. getAddrIn();
  94.  
  95. //create a socket.
  96. int socketfd;
  97. socketfd = socket(results->ai_family, results->ai_socktype, results->ai_protocol);
  98.  
  99. if(socketfd == -1 )
  100. {
  101. cout<<"Error: failed to create a socket.\n";
  102. return 2;
  103. }
  104.  
  105. struct sockaddr_in *specifyPort = (struct sockaddr_in *)results->ai_addr;
  106.  
  107. specifyPort->sin_port = htons(startingPort);
  108.  
  109. int connectStatus;
  110. connectStatus = connect(socketfd, results->ai_addr, results->ai_addrlen);
  111.  
  112. if(connectStatus == -1 )
  113. {
  114. if(errno == ECONNREFUSED)
  115. cout<<"Port "<<startingPort<<" is Closed or Blocked.\n";
  116. //Alternatively, you can do:
  117. //cout << "connect failed; reason = " << strerror(errno) <<endl;
  118. }else{
  119. cout<<"Port "<<startingPort<<" is OPEN.\n";
  120. }
  121.  
  122.  
  123. closesocket(socketfd);
  124.  
  125. //move to the next port in the specified range
  126. startingPort++;
  127.  
  128. }
  129.  
  130. //deallocate memory
  131. delete[] remoteIP;
  132.  
  133. //free linkedlist of struct addrinfo *results
  134. freeaddrinfo(results);
  135. WSACleanup();
  136. return 0;
  137. }

Errors:

  1. 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|
  2. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|7|error C2079: 'hints' uses undefined struct 'addrinfo'|
  3. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|29|error C3861: 'getaddrinfo': identifier not found|
  4. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|33|error C3861: 'gai_strerror': identifier not found|
  5. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|53|error C2228: left of '.ai_family' must have class/struct/union|
  6. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|54|error C2228: left of '.ai_socktype' must have class/struct/union|
  7. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'|
  8. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_family' must point to class/struct/union/generic type|
  9. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'|
  10. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_socktype' must point to class/struct/union/generic type|
  11. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2027: use of undefined type 'addrinfo'|
  12. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|82|error C2227: left of '->ai_protocol' must point to class/struct/union/generic type|
  13. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|90|error C2027: use of undefined type 'addrinfo'|
  14. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|90|error C2227: left of '->ai_addr' must point to class/struct/union/generic type|
  15. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2027: use of undefined type 'addrinfo'|
  16. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2227: left of '->ai_addr' must point to class/struct/union/generic type|
  17. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2027: use of undefined type 'addrinfo'|
  18. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|95|error C2227: left of '->ai_addrlen' must point to class/struct/union/generic type|
  19. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|99|error C2065: 'ECONNREFUSED' : undeclared identifier|
  20. C:\Documents and Settings\Fahmi\Desktop\test\main.cpp|119|error C3861: 'freeaddrinfo': identifier not found|
  21. ||=== Build finished: 19 errors, 1 warnings ===|


PLZ HELP.... I ALREADY DID ALL I CAN. I'm about to give up....
Last edited by f.ben.isaac; Oct 26th, 2008 at 1:47 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Odd Errors When Compiling Winsock Code.

 
0
  #2
Oct 26th, 2008
you are missing a header file -- see example program here
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,407
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Odd Errors When Compiling Winsock Code.

 
0
  #3
Oct 26th, 2008
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 :]
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 33
Reputation: f.ben.isaac is an unknown quantity at this point 
Solved Threads: 0
f.ben.isaac f.ben.isaac is offline Offline
Light Poster

Re: Odd Errors When Compiling Winsock Code.

 
0
  #4
Oct 26th, 2008
Hey, great!!!!! worked
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 33
Reputation: f.ben.isaac is an unknown quantity at this point 
Solved Threads: 0
f.ben.isaac f.ben.isaac is offline Offline
Light Poster

Re: Odd Errors When Compiling Winsock Code.

 
0
  #5
Oct 26th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC