Interesting Problem About Incrementing ++


 
Thread Tools Search this Thread
Top Forums Programming Interesting Problem About Incrementing ++
# 1  
Old 10-18-2008
Interesting Problem About Incrementing ++

Here is my code:

Code:
int startingPort = 200;
string tempPort;
stringstream out;
out<<startingPort;
tempPort = out.str();    //tempPort carries startingPort in string format

//convert tempPort to *char - currentPort going to be passed into getaddrinfo()
char currentPort[10];
strcpy(currentPort, tempPort.c_str());

while(startingPort <= 400)
{
                          //shows the first old value, does not show the incremented value
                          cout<<"******currentPort "<<currentPort<<endl;
       //move to the next port in the specified range
		startingPort++;

		//convert the incremented port to a string
		string tempPort;
		stringstream out;
		out<<startingPort;
		tempPort = out.str();
		char currentPort[10];
		strcpy(currentPort, tempPort.c_str());
                     
                          //works fine - incremented
		cout<<"TEST* currentPort "<<currentPort<<endl;
}

My program compiles, the problem is currentPort reset itself to 200 everytime a new loop starts...

This code is just example. Do you know why in every loop, the value reset itself?
# 2  
Old 10-19-2008
You have currentPort defined in two scopes; don't do this.
MrC
# 3  
Old 10-19-2008
Like Mrc said:

Quote:
Originally Posted by f.ben.isaac
Here is my code:

Code:
int startingPort = 200;
string tempPort;
stringstream out;
out<<startingPort;
tempPort = out.str();    //tempPort carries startingPort in string format

//convert tempPort to *char - currentPort going to be passed into getaddrinfo()
 char currentPort[10];
strcpy(currentPort, tempPort.c_str());

while(startingPort <= 400)
{
                          //shows the first old value, does not show the incremented value
                          cout<<"******currentPort "<<currentPort<<endl;
       //move to the next port in the specified range
        startingPort++;

        //convert the incremented port to a string
        string tempPort;
        stringstream out;
        out<<startingPort;
        tempPort = out.str();
        char currentPort[10];
        strcpy(currentPort, tempPort.c_str());
                     
                          //works fine - incremented
        cout<<"TEST* currentPort "<<currentPort<<endl;
}

My program compiles, the problem is currentPort reset itself to 200 everytime a new loop starts...

This code is just example. Do you know why in every loop, the value reset itself?
# 4  
Old 10-19-2008
The thing is char currentPort[10] does not reset it self to empty, so it can take another value of 10 chars. I do not how to make currentPort[10] empty at the end of every loop. In other words, relating to my trial-and-error tries, i noticed the new value in every loop append beside the old value, untill hits array size limit. I treid what you have said, but i get completely unrelated error! I can't illustrate until you see my code.

The following program scans range of port of the local machine that program is running on. Hence, this is my first program so i do not really know if the algorithm is right or now...I only care how to solve this loop thing for now. First of all, prints the name of the host name.


Code:
#include<iostream>
#include<string>
#include<sstream>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.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 *pcName = new char[40];    //holds your pc name
string tempPort;                //holds ports temporarly
int startingPort;               //stores the starting range of port number
int status;                     //receives the status of your pc address
char currentPort[10];           //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;

}

//call host (local machine) name
void callHostName()
{
	/**get your computer's name**/
	//char *pcName = new char[40];       		  //holds your pc name - i made it static
	size_t pcNameSize;     				  //holds size of the name
	int hostNameStatus; 				  //holds status of the function whether it succeeded or failed
	hostNameStatus = gethostname(pcName, pcNameSize); //gethostname grabs your pc name & pass it to pcName
	
	//make sure function host name returned successfully
	if(hostNameStatus == -1 )
	{
		cout<<"Error: could not locate your host name\n";
		exit(1);
	}

	//print machine name
	cout<<"Your Machine Name: ";
	cout<<pcName<<endl;
	cout<<endl;

	//deallocate memory
	//delete[] pcName; - i put it inside main()

}

//getaddrinfe locates your machine, more specific
//details of your host address is returned to results. 
void getAddrIn()
{

	/***first parameter of getaddrinfo() is the host name to connect to, or an IP address to 
	connect to. If you put a host name in the first parameter, you can get its ip address from 
	results, but if you put an IP address as the first parameter, getaddrinfo() will not be able 
	to obtain the hostname, so getaddrinfo can lookup an IP address for a hostname, but can not 
	give you a host name for a given IP address as far as i know. To get a host name for an 
	IP Address, you need to use gethostname() or getnameinfo(). You could simply put NULL in the 
	first parameter if you want to listen on your host's IP address, port "currentPort". Note that 
	this doesn't actually do any listening or network setup; it merely sets up structures we'll
	use later . See "hints.ai_flags = AI_PASSIVE;" at the top; this tells getaddrinfo() to assign 
	the address of my local host to the socket structures. - you can put a specific address in as 
	the first parameter to getaddrinfo() where I currently have NULL. If you put NULL in the first 
	parameter, this will assign the address of your local host to the socket structure. Putting NULL 
	as the first parameter will not print your local IP address to the screen when you request it 
	from results - you will just see an artibetry result like - IPv6 ::. Go ahead & change pcName to 
	NULL to see this in action! pcName here carries the name of the local machine....***/

	status = getaddrinfo(pcName, currentPort, &hints, &results);

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

//get local IP which is usually version 4
void grabLocalIP()
{
	//carries your local IP
	char ipString[INET6_ADDRSTRLEN];
	struct addrinfo *p;
	
	cout<<"IP Address for "<<pcName<<" is of type ";

	//print all your local IP Addresses
	for(p = results; p != NULL; p = p->ai_next)
	{
		void *addr;
		string ipVer;
		if(p->ai_family == AF_INET)
		{
			struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
			addr = &(ipv4->sin_addr);
			ipVer = "IPv4";

		}else{
			struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
			addr = &(ipv6->sin6_addr);
			ipVer = "IPv6";
		}

		inet_ntop(p->ai_family, addr, ipString, sizeof ipString);
		cout<<""<<ipVer<<": ";
		printf("%s\n", ipString);
		cout<<endl;
	}	
}

//convert a port from int to *char, so it can be passed into getaddrinfo()
//call this function before the loop starts
void convertPortsToString()
{
	//convert startingPort to string. After that, convert the string into *char...
	//string tempPort;  make it static 
	stringstream out;
	out<<startingPort;
	tempPort = out.str();    //tempPort carries startingPort in string format

	//convert tempPort to *char - currentPort going to be passed into getaddrinfo()
	//char currentPort[10];  - i made it static
	strcpy(currentPort, tempPort.c_str());

}

//convert a port from int to *char, so it can be passed into getaddrinfo()
//call this function at the bottom of the loop
void convertIncrementedPortsToString()
{
	//you have to declare out in every loop, otherwise the 
	//incremented ports will concatenated with the previous ports.
	stringstream out;
	out<<startingPort;
	tempPort = out.str();    		//tempPort carries startingPort in string format
	strcpy(currentPort, tempPort.c_str());  //convert the string to *char

}

//Run the program
int main()
{
	
	//struct addrinfo hints;      	//fill your host info - hints is input    - i made it static
	//struct addrinfo *results;	//gets your host info - results is output - i made it static
	//int status;                   //receives the status of your pc address - i made it static
	//int startingPort;             //stores the starting range of port number - i made it static
	int endingPort;                 //stores the ending rage of port number

	system("clear");

	//tell user what program does
	progDesc();

	//grab the name of the local machine (host)
	callHostName();
	
	//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;

	//call getaddrinfo() to output to "results"
	//so you can grab the local IP from results
	getAddrIn();

	//grab your machine local IP
	grabLocalIP();

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

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

	cout<<endl;

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

	//convert a port from int to *char, so it can be passed into getaddrinfo()
	convertPortsToString();

	//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;
		}

		//connect to your own IP address in every loop with 
		//a new port, connect() will associate your socket
		//with the "current port number & your local IP address".
		int connectStatus;
		connectStatus = connect(socketfd, results->ai_addr, results->ai_addrlen);
	
		if(connectStatus == -1 )
		{
			cout<<"Port "<<currentPort<<" is Closed or Blocked.\n";
		}else{
			cout<<"Port "<<currentPort<<" is OPEN.\n";
		}

		//close the created socket, because
		//a new one should be created in every
		//loop because connect will fail if the
		//socket is already connected. You'd 
		//need to disconnect or close the socket
		//at the end of the loop. I choose to
		//close & create one in the next loop.
		close(socketfd);
	
		//move to the next port in the specified range
		startingPort++;

		/***convert the incremented port to *char***/
		convertIncrementedPortsToString();
	}

	//deallocate memory
	delete[] pcName;

	//free linkedlist of struct addrinfo *results 
	freeaddrinfo(results);

	return 0;
}

If i take this out, my program return hostNameStatus as failed! Completely unrelated. Same problem happens if i take off "string tempPort & stringstream out", Weird! Isn't it!

Last edited by f.ben.isaac; 10-21-2008 at 08:32 PM..
# 5  
Old 10-20-2008
A character array is never empty. It is a contiguous array of memory cells, each of which contains some value. You are using a character array to represent strings; as such, you must manage the terminating NULL byte '\0'.

[ xxx deleted backwards description xxx ]

It is not useful to attempt to diagnose other errors you see when there are likely out of bounds array copies occurring, as the stack becomes trashed.

Never use any function that copies memory from one location to another which is of unknown length, or copy a know length to an offset that would make the copy exceed the bounds of the variable or data structure. Use strncat() or strncpy() to ensure the copy never exceeds the variable's bounds.

Last edited by MrC; 10-21-2008 at 09:13 PM..
MrC
# 6  
Old 10-20-2008
Quote:
Originally Posted by MrC
A character array is never empty. It is a contiguous array of memory cells, each of which contains some value. You are using a character array to represent strings; as such, you must manage the terminating NULL byte '\0'.

If you want to use strcpy(), you must ensure that there is a NULL byte at the location where you want the copy to begin. If you are trying to copy into an array for the first time, use strcat(). Use strcpy() to append to an existing array (or again, set the first byte to '\0').

It is not useful to attempt to diagnose other errors you see when there are likely out of bounds array copies occurring, as the stack becomes trashed.

Never use any function that copies memory from one location to another which is of unknown length, or copy a know length to an offset that would make the copy exceed the bounds of the variable or data structure. Use strncat() or strncpy() to ensure the copy never exceeds the variable's bounds.
Thanks!!!! This advice will help me now and later on .... I will play around with it....Anyhow, i was trying to write a simple very basic local port scanner, do you think the algorithm of my code right?
# 7  
Old 10-21-2008
I solved it!

I solved it!

I had to make pcName declared as char *pcName = new char[40];
not char pcName[40]; Things after that went fine!

Its odd, pcName was the cause of the problem even though it got to do
nothing with currentPort!

The appending cause by stringstream out; it should be declared in every loop. Otherwise, new results will appended to old results
Thanks anyway. Done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Interesting awk problem

Hello; I wish to manipulate the output from the following: ps axo %mem,pid,euser,cmd|sort -nr 14.4 3170 root /usr/sbin/snmpd -Lsd -Lf /dev/null -p /var/run/snmpd.pid -a 3.6 3635 root splunkd -p 8089 start 2.6 3383 root /usr/sbin/httpd 2.1 15496 apache /usr/sbin/httpd ... (5 Replies)
Discussion started by: delphys
5 Replies

2. Solaris

Interesting Disk Error Problem

Hi Folks, Have an interesting problem here, have just upgraded some machines (Test and Development) to the latest and greatest as in. SunOS ss063a 5.10 Generic_147440-13 sun4u sparc SUNW,Sun-Fire-V440 However on all the systems we are seeing the same problems, during the boot there is a... (3 Replies)
Discussion started by: gull04
3 Replies

3. Shell Programming and Scripting

AWK SCRIPT HELP : INCREMENTING PROBLEM

Hi Guys , I am having one command file like this FILE1 ################################ awk '/output/ {a=$2} {for(i=1;i<=NF;i++) { gsub("i1", i) ; gsub("i2",++i) ; gsub("P1", p) }}1' output >> out9 awk '/output/ {a=$2} {for(i=1;i<=NF;i++) { gsub("i1", i) ;... (2 Replies)
Discussion started by: jaita
2 Replies

4. Shell Programming and Scripting

Facing problem in incrementing the variable

When I did, echo $SHELL in cmd prompt of putty, its displaying /bin/sh And in my shell script., I hav started with., #!/bin/sh and i=1; while ; do . . . i=$; (9 Replies)
Discussion started by: av_vinay
9 Replies

5. Solaris

Very Interesting Problem

Ok Lets say i have a webpage on a solaris box. and i am trying to make a hyperlink. This hyperlink references a Microsoft Access file on a Windows box. So, How do i make a file reference from The unix box to a windows box. Things to keep in mind -- 1.) Solaris doesnt need to... (6 Replies)
Discussion started by: Demon002
6 Replies

6. Shell Programming and Scripting

interesting problem

Hi, I am new in this forum and I am glad to be a part of it. I have a problem that has two parts: 1/ extract just the timestamp from a filename: for example, I have a file called 'sales20080226144525.txt' and I want to extract just the '20080226144525' part out of it. 2/ Now, take that... (5 Replies)
Discussion started by: ChicagoBlues
5 Replies

7. Shell Programming and Scripting

Interesting problem

Hello, So I'm utilizing the bash brace expansion feature to checkout multiple folders from cvs with ease, while excluding certain subfolders within. So I do a command like this: cvs co trunk/{mod_a,mod_b,mod_c} \!trunk/{mod_a,mod_b,mod_c}/web to checkout modules trunk/mod_a , trunk/mod_b ,... (1 Reply)
Discussion started by: neked
1 Replies

8. SCO

I have an interesting problem and need advice

We have an inhouse built application which handles our AR function. We'd like to email statements out of the application directly. My question is this is there a good, solid, command line driven, scriptable Mail Transport Agent that would allow us to send a semi unique email (with a statement and... (1 Reply)
Discussion started by: Maldain
1 Replies

9. UNIX for Advanced & Expert Users

interesting problem

After a server reboot for regular mainatanance we face the following situation: 1/ user accounts with ksh can't start the korn shell 2/ korn shell scripts do work however, if executed from bash 3/ normal users with bash can't run ksh 4/ root can run ksh 5/ if I switch the default shell to... (3 Replies)
Discussion started by: rein
3 Replies

10. UNIX for Advanced & Expert Users

An interesting problem

hi!, here is an interesting problem ..... I ftp a file named " my first file.doc" from windows to my HP workstation. Ftp is succesful. The file appears with the same name in my home directory.. But now how do I open this file?? while using Vi or More or other editor, they take only the... (5 Replies)
Discussion started by: jyotipg
5 Replies
Login or Register to Ask a Question