Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to add a second instance to this if statement and or doesn't seem to do it.
C++
if (ip_header->source_ip == *addressValue or ip_header->source_ip == addr)
		  {
		  printf("\n   Source      IP: %s", "0.0.0.0");
		  printf("\n   Destination IP: %s", ipDest);
		  }
		  else
		  {
                  printf("\n   Source      IP: %s", ipSrc);
		  printf("\n   Destination IP: %s", ipDest);
Posted
Updated 15-Sep-11 12:29pm
v2

You have a serious problem with the logic here. Given the types and names of the variables, I assume that you have the incoming IP address *AS A DWORD* stored in ip_header->source_ip. The original code compared it to some saved address *ALSO A DWORD*. Now you want to add a compare to another address (addr) which appears to be stored *AS A STRING OF CHARACTERS*.

You cannot simply compare a binary number to a string of characters and expect the comparison to convert the string into a number. All that compare is going to do is compare a binary number to the numeric value of the 1st character of the string (e.g., an 'A' would be 65).

You need to convert the string (addr) to a number, like the original program did with (*addressValue), and compare against that.
 
Share this answer
 
Comments
Member 7766180 15-Sep-11 22:01pm    
Here is the code I have to get the "addr" it is a char....
I tried doing int addr but that blew up on me. How can I do this????? Thak you.
HOSTENT *pHostEnt;
int **ppaddr;
SOCKADDR_IN sockAddr;
char* addr;
pHostEnt = gethostbyname( "www.codeproject.com");
ppaddr = (int**)pHostEnt->h_addr_list;
sockAddr.sin_addr.s_addr = **ppaddr;
addr = inet_ntoa(sockAddr.sin_addr);
Member 7766180 15-Sep-11 22:06pm    
Perhaps?
addr = inet_ntoa(sockAddr.sin_addr); //this is your ip address
int GD;
GD = atoi(addr);

if ((ip_header->source_ip == *addressValue) || (ip_header->source_ip == GD))
Chuck O'Toole 15-Sep-11 22:10pm    
if addr is "192.168.1.1", then atoi(addr) will return the number 192. It will convert the string of numbers into a number and stop with it hits something that isn't a number, the ".". "sockAddr.sin_addr_s.addr" looks like the right thing.
Member 7766180 15-Sep-11 22:35pm    
Thank You. I tried this but it wont compile...inet_atoi is undefined.
int GD;
GD = inet_atoi(sockAddr.sin_addr);
Chuck O'Toole 15-Sep-11 22:41pm    
Where did I say inet_atoi() anywhere. I simply told you that atoi() will not give you the IP address as an number, it will only give you the first part of the address. You also showed some code above where you have the IP address as a number just like you want and it's in "sockAddr.sin_addr.s_addr" in your code. I suggested that is the value you want to capture instead of the address as a string.
I changed it to this but the 97.74.31.42 is giing problems.
C++
if (ip_header->source_ip == *addressValue || addr)
 
Share this answer
 
v2
Comments
Member 7766180 15-Sep-11 17:39pm    
Actually this doesn't work, it's always returning 0.0.0.0 now, any suggestions?
Chuck O'Toole 15-Sep-11 19:03pm    
what are the types of the variable involved, CString, char [], char *, what?
Member 7766180 15-Sep-11 19:37pm    
ip_header DWORD
*addrssValue int
*addr char
Chuck O'Toole 15-Sep-11 19:05pm    
Also, that's not the correct syntax for logical or (||) inside an "if" statement.
((ip_header->source_ip == *addressValue) || (ip_header->source_ip == addr))
Member 7766180 15-Sep-11 19:37pm    
This compiles but doesn't work...
if ((ip_header->source_ip == *addressValue) || (ip_header->source_ip == *addr)) it doesn't turn the IP ino a 0 when its suppose to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900