Socket, Bind, Connect: Remapping The Server

Posted on 1998-09-16
Last Modified: 2013-12-25
I am beginning to use the Perl commands SOCKET, BIND, and CONNECT to establish a connection to another website. What I do is set SOCKET appropriately, set BIND to my server name, and then set CONNECT to the website I want to connect to.  It all seems to work perfectly.

Now I have a CGI Perl script called Getgraphic.CGI.  My website has an IMG tag which calls this script (<img src="">).  Now I realize that a browser like Netscape must do the same procedure with the Socket, Bind, and Connect so as to make a connection to my CGI script.  Therefore when my CGI script begins to run a SOCKET path has been established to the client so that data can flow to and from.

What I would like to do is instead of my GetGraphic.CGI to output its image graphics I would like to "map" this existing socket connection to another server's CGI script for it to output its image graphics, all from within my GetGraphics.CGI.  Is there any way to do this?  Perhaps altering BIND (the command which took in my server name)?
Question by:ratboy
  • 7
  • 5

Accepted Solution

stormerider earned 200 total points
ID: 1828646
So what you want to do is to basically tunnel the traffic through your cgi? You could make a connection to where it needs to go and then grab the result, store it locally, and then send it to the connection being made to it. Or you could basically make a connection to the location, retrieve and print to the incoming location and not store it. Couple of different options there. Why are you doing this? You might also want to look at some port redirectors as well that do essentially this type of work.


Author Comment

ID: 1828647
But if i make the call to that other server to get the picture then "my" IP address goes with it.  I want the clients ip address to fetch it instead. It's like i remapped the socket connection from me to the other server. Can these port redirectors do such things?

Author Comment

ID: 1828648
I could use the Location:  command output from my CGI script which would do the trick perfectly, but I also want the Referer value to be set to a specific page; the CGI call can be made from multiple different pages but I want the Referer value to be set to a single, unchanging name.  How do I do that?

Sorry for the extra difficulties.  And thanks so far...
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Expert Comment

ID: 1828649
Port redirectors would mark the ip as well. In re: to the referrer, assuming you're using perl with the package,

if ($ENV{'HTTP_REFERER'} ne "") {


Author Comment

ID: 1828650
To Stormerider:
  Unfortunately i don't see how that piece of Perl code can help me with that location and referrer problem. Can you explain to me in detail what you mean.

And for the port redirectors, you mean the IP Address will still be my own as opposed to the clients?

Expert Comment

ID: 1828651
It should show it as originating from the client. If they do intensive logging, it should show that it routed through you, but that's unlikely.

Analyzing the code:
1.) If the page they are coming from ($ENV{'HTTP_REFERER'}) does not match the one you want (replace the one i put with your own, it was an example)
2.) Some subroutine to serve them a blank image, not the image they would get via the CGI
3.) Do whatever ordinary CGI cleanup you do
4.) Exit the CGI
5.) Needed for syntax.

This will block from anyone accessing this cgi who is not coming from that page.


Author Comment

ID: 1828652
Truly I thank you Stormerider for being so patient with me.  Writing of the above message and comments have been a 30 second thing as I run in and out of the house.  Now I have more time to write what I want in more detail.

I have a website with 10 webpages on it (webpage 1 to 10).  What I would like to do is have a CGI script, called COCO.CGI, on webpage 1 which is called with <IMG src="cgi-bin/COCO.CGI">.  It takes in the HTTP_REFERER and REMOTE_ADDR and returns a graphical image if the HTTP_REFERER was set to webpage 1.

Now I would like to make a call to COCO.CGI from webpage 2 to 10 but unfortuantely the HTTP_REFERER will be set to webpage 2 to 10 instead of the required webpage 1.

This kind of problem sounds very trivial, even pointless/stupid but its something I need to figure out considering all those restrictions.

What I was thinking is to have a <IMG src="cgi-bin/MAPPER.CGI"> where MAPPER.CGI is a CGI script which is called from webpage 2 to 10.  Inside the script it will simply "redirect" (print "Location:\n\n";) the incoming call to the webpage 1 in hopes of 'triggering' the COCO.CGI.  Unfortunately the call to MAPPER.CGI, and so webpage1.htm, returns an error since the returned data is not a graphical image but an HTML file (due to the error, the HTM page isn't triggered and nor is COCO.CGI).  Now if I simply redirected to COCO.CGI instead of webpage1.htm, the HTTP_REFERER won't be set to webpage 1.  Tricky eh?

Now I'm probably over thinking the situation (I hope) and there's most likely an easy solution, but it just continues to elude me.  Any help you can provide in successfully "mapping" a call from one CGI script to another "while altering the HTTP_REFERER" to a new value would be greatly appreciated!

Thanks for your patience.  I'm crossing my fingers.   :>

Expert Comment

ID: 1828653
Well, if this is the case, then I would save yourself a hell of a lot of grief and forget MAPPER.CGI. As far as I can tell, you don't need it. Basically what you want to do is along the lines of:

$http1 = "";
$http2 = "";
$http3 = "";
$http4 = "";
$http5 = "";

if ($ENV{'HTTP_REFERER'} eq $http1 {
} else if ($ENV{'HTTP_REFERER'} eq $http2) {
} else if ($ENV{'HTTP_REFERER'} eq $http3) {
} else if ($ENV{'HTTP_REFERER'} eq $http4) {
} else if ($ENV{'HTTP_REFERER'} eq $http5) {
} else <etc...> {

Does that help any?


Author Comment

ID: 1828654
Great message Stormerider.   It does help!  Now just one last thing.  Those graphical images I send back to the client need to come from another server (my site is  and I need to fetch the graphics from  

If I use BIND/SOCKET/CONNECT then I'd be binding to my IP address and not the clients, unless I'm able to route BIND to the client and just send him/her the graphic directly from that server.  When I make this BIND/SOCKET/CONNECT call I can then set the Referer field to whatever I want.

So close to solving this problem. I can almost taste it. Any solutions with this added restricition?

Thanks for everything!

Expert Comment

ID: 1828655
The only thing I can think of in regards to that, if the client IP has to be saved is to either route it through with a port redirector or use SSI. Rather than <IMG SRC=blah.cgi>, try
<!--#exec cgi="blah.cgi"--> and have it spit back <IMG SRC="">.

Make sense?

Author Comment

ID: 1828656
Thanks a bunch Storemrider for everything!  You definitely deserve the points!

One last thing, what if the graphic that is returned is not an actual graphic file ( but is actually an output from a cgi script ( which outputs a graphical image.  The CGI script then checks IP # and Referer!

Thanks and here's your points!

Author Comment

ID: 1828657
That's strange, the point worth was for 200 not 20.  Make sure you get the proper amount!  You deserve it.

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question