C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-05-2009, 09:02 AM   #1
Registered User
 
Join Date: Mar 2008
Location: India
Posts: 72
how to know which ip is binded to the specific port using a c/linux program?

Friends,

I have a program which creates a tcp socket and binds on to the port 5819.

The expected behaviour is it needs to bind to 5819 using the provided ip.

Due to some malfunction ( which i need to trace ) after bind instead of provided ip it is taking some other ip.

when i do a netstat it shows me like below

tcp 0 0 255.255.255.255:5819 0.0.0.0:* LISTEN


Do i have any system call which i can use in my program where i can check like on this port which ip is being bind .

I thought i can use netstat like executing with system command and then output to a textfile . Now parse the text file to know the result . But it is very expensive as lot of file opertaion and then parsing .

So Any system call or any function through which i can get that info ?

I hope iam making some sense if not please let me know to explain with more workds.

regards
vlrk is offline   Reply With Quote
Old 02-05-2009, 10:02 AM   #2
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,230
Quote:
Originally Posted by vlrk View Post
Friends,

I have a program which creates a tcp socket and binds on to the port 5819.

The expected behaviour is it needs to bind to 5819 using the provided ip.

Due to some malfunction ( which i need to trace ) after bind instead of provided ip it is taking some other ip.
This only takes at most half a dozen lines of code, so why don't you post the part that includes socket(), bind(), and the relevant struct declarations? And explain how you believe you are assigning an ip address in this process.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 02-05-2009, 10:10 AM   #3
Registered User
 
Join Date: Mar 2008
Location: India
Posts: 72
Post the code is like below

Code:
        /* setup address info for our server socket */
		bzero((char *)&addrServer, lenSockAddr);
		addrServer.sin_family = AF_INET;
        addrServer.sin_addr.s_addr = inet_addr(UPNP_SERVER_IP);
        addrServer.sin_port = htons(UPNP_SERVER_PORT);
        /* create our socket */
         if ( UPNP_HTTP_NO_SOCKET == serverSocket )
         {
             serverSocket = socket(AF_INET, SOCK_STREAM, 0);
         }

if ( -1 == serverSocket) 
{
syslog(LOG_ERROR, "Could not create server socket");
continue;
}

        if(setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR,(char *)&iAddrReuse, sizeof(iAddrReuse)) == (-1))
        {
          syslog(LOG_ERR, "Setsockopt failed for SO_REUSEADDR \n");
        }

	/* bind address to our socket */
        if ( bindretVal == UPNP_HTTP_BIND_ERROR  )
          {
              bindretVal = bind(serverSocket, (struct sockaddr *)&addrServer, lenSockAddr);
          }
here while running i tried to print the ip address structure using inet_ntoa both before and after bind it shows me as the ip which i provided .

But when i run the netstat command it shows me a other ip as 255.255.255.255

regards
vlrk is offline   Reply With Quote
Old 02-05-2009, 11:28 AM   #4
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,230
I don't see anything wrong, but I can't claim to be much of an authority. This works for me:
Code:
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define IP ((in_addr_t) 0x0a000002)

int main() {
	struct sockaddr_in Addr;
	int sock=socket(PF_INET,SOCK_STREAM,0);
	Addr.sin_family = AF_INET;
	Addr.sin_port = htons(5819);
	Addr.sin_addr.s_addr = htonl(IP);
	if (bind(sock,(struct sockaddr*)&Addr,sizeof(Addr))<0) puts("NOPE");
	getchar();         /* a pause so we can check netstat */
	return 0;
}
However, if I use anything but 10.0.0.2 (my real address) for IP, bind fails.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 02-05-2009, 02:11 PM   #5
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Maybe this is a dumb question, but are you actually sure that UPNP_SERVER_IP isn't 255.255.255.255 ?

It seems... well, really weird, to have an IP address hardcoded like that, unless it happened to be 255.255.255.255.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 02-05-2009, 04:26 PM   #6
Registered User
 
Join Date: Apr 2008
Posts: 282
Code:
addrServer.sin_addr.s_addr = inet_addr(UPNP_SERVER_IP);
inet_addr() returns the address in the host encoding, converting the result with htonl() may help.
root4 is offline   Reply With Quote
Old 02-08-2009, 04:30 PM   #7
Registered User
 
Join Date: Aug 2008
Location: Croatia
Posts: 20
yeah, you need to convert from little endian byte order to a network byte order by using htonl().

Code:
addrServer.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
dotunix is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BOOKKEEPING PROGRAM, need help! yabud C Programming 3 11-16-2006 11:17 PM
Waiting for a specific character through a port shoobsie C Programming 5 08-25-2005 11:45 AM
is there a way of checking what program uses a specific port? Xterria A Brief History of Cprogramming.com 6 05-27-2004 10:54 PM
Basic port scanner code .. pls help ??? intruder C Programming 18 03-13-2003 08:47 AM
DOS, Serial, and Touch Screen jon_nc17 A Brief History of Cprogramming.com 0 01-08-2003 04:59 PM


All times are GMT -6. The time now is 06:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22