LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2012, 06:23 AM   #1
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Rep: Reputation: 16
get the remote port number


I have a c++ cgi script that gets called and I get the IP address of the client like so:

Code:
char* remote_host = getenv("REMOTE_ADDR");
But I need to get the remote client port numer as well. I believe that APACHE doesn't set the REMOTE_PORT so can I get it using sin_port or anyting??

How can I do this???

Thanks
 
Old 06-06-2012, 09:07 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Try getpeername(fd=0/1).
 
Old 06-06-2012, 09:29 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
The CGI program is typically not directly connected to the client socket in any way, so getpeername() is unlikely to work. The correct solution is to tell Apache to provide the REMOTE_PORT environment variable for you.

The simplest method I can think of is to enable URL rewriting via the mod_rewrite module: add
Code:
RewriteEngine  On
Options        +FollowSymlinks
RewriteCond    (%{REMOTE_PORT}) ^
RewriteRule    ^ - [E=REMOTE_PORT:%1]
to the Directory section that governs your CGI scripts in your Apache configuration.

The way this works is that the URL rewriting engine knows about the remote port; it refers to it as %{REMOTE_PORT}. Since it is in parentheses, it will be accessible in any applied rule using %1. Since every string has a start, the condition always matches. The rule always matches also. The - tells the rule to do no replacements. The [E=REMOTE_PORT:%1] sets the REMOTE_PORT environment variable as per the remote port. For the rewrite engine to work, both RewriteEngine and symlinks (FollowSymlinks or SymlinksIfOwnerMatch) must be enabled.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Port number used by server when using dynamic port forwarding in SSH? kreeder Linux - Networking 4 11-21-2011 02:07 PM
Warning: remote port forwarding failed for listen port 7869 windstory Linux - Newbie 1 08-02-2010 10:07 AM
USB to serail port adaptor COM port number areftaidi Linux - Software 2 09-25-2007 01:05 AM
Number NICs on remote PC timonbg Linux - Networking 5 10-18-2006 05:31 AM
right port number gogo Linux - General 1 05-07-2001 03:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration