c: networking: ip deamons.

Discuss Programming

c: networking: ip deamons.

Postby X11 » Wed Jan 29, 2003 5:11 am

What i need is a program that can sit and wait for a connection to it, then when one is made it connects to another machine and works as a proxy. Im not lazy and i dont want a program to do this for any "particular" purpose. Well i do but I also want to learn network stuff in C and was wondering if the people of this forum would help me with it.

(I find my writeing has been a bit better since i washed my keybored to!)

Anyways just to be a bit more specific...

| Client | >> | forwarding machine | >> | Other Machine |
Basicly i want a program that will forward data on (any given port) to the "Other Machine" and no i dont want to use iptables this is for learning purposes mainly. Keep note that a simple script using nc wont work either as "Other Machine" must talk back to the "Client" machine.

And you dont have to write the whole thing but a couple of examples wont hurt because i find thats the best way i learn. I looked at some socket man pages and soforth but usually Voidmain and everyone else is much better then this documentation.

Anyways thanks in advance.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Wed Jan 29, 2003 1:24 pm

http://www.onlineprogrammer.org/article ... 2-2-2.html
http://www.fortunecity.com/skyscraper/arpanet/6/cc.htm

The above gives you what you need to know. You can also do this easily in Perl in similar ways (perl comes with a couple client/server examples). It's not all that difficult. Search google for "linux TCP/IP socket programming" for more information...
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Thu Jan 30, 2003 1:26 am

Hmmmmm i know how to connect to other services in PHP, can i use PHP to wait for a connection.

And thanks for the Links.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Thu Jan 30, 2003 1:42 am

I don't understand. I thought you wanted it to be in C. Or do you want a server process written in C that listens on a port and then a PHP client process that can grab data from that port?

Not quite sure what you are after. Maybe if you draw me a picture...
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Fri Jan 31, 2003 7:55 am

I will, i was just wondering if PHP could do that.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Fri Jan 31, 2003 8:21 am

User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Fri Jan 31, 2003 8:28 am

After reading the source for that program on OnlineProgrammer, i cant get it to compile.
Code: Select all
[X11@exeleven xd]$ gcc -o xd main.c -lsocket
/usr/bin/ld: cannot find -lsocket
collect2: ld returned 1 exit status
[X11@exeleven xd]$ gcc -o xd main.c
[X11@exeleven xd]$ ./xd
Broken pipe

how would i get this to compile.

Also, despite reading both articles, i think i could write a nice program and i think i understand the source code for the one that sends the thing to the server and gets a reply, but i will need a small example for a thing that waits for a connection and puts the incoming information in a buffer.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Fri Jan 31, 2003 8:38 am

Remove the "-lsocket" from your gcc command line. You'll need the server code though:

http://www.onlineprogrammer.org/article ... 2-3-2.html

Also you'll need to change the port in "client.c" to match the port in "server.c". Then it should work.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Fri Jan 31, 2003 8:47 am

now its
[X11@exeleven xd]$ ./xd
Broken pipe

(note: server not done, thats the client, i made it send to port 80 on this site)
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Fri Jan 31, 2003 8:59 am

Don't send it to port 80 just yet. This is just an example for you to understand how TCP/IP sockets work. It will get the "Broken Pipe" until you have the server program running and you have the client set to contact the server on the port that the server is listening on. Open two terminals, run the server in one and the client in the other.

I modified the two files slightly:

http://voidmain.is-a-geek.net/files/socket/
Last edited by Void Main on Fri Jan 31, 2003 9:09 am, edited 1 time in total.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Fri Jan 31, 2003 9:08 am

Actually before i read that last post i did this, but i wiill go do what you just said.
Code: Select all
 #include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>

#define SERV-PORT 80
#define SERV

main(void)
{
   system("clear");
   printf("Client:\n");
   tcpserv();
}

client-handler(char buf[8096])
{
    struct sockaddr_in pin;
    int sock_descriptor;
    int address_size;
    struct hostent *server_host_name;
    long len;

    remote host is local host
    server_host_name=gethostbyname(RELAY-TO-HOST);
    bzero(&pin,sizeof(pin));
    pin.sin_family=AF_INET;
    pin.sin_addr.s_addr=((struct in_addr *)
    (server_host_name->h_addr))->s_addr;
   
    pin.sin_port=htons(RELAY-TO-PORT); //we are using port 12000
   
    //create socket
    sock_descriptor=socket(AF_INET,SOCK_STREAM,0);
    connect(sock_descriptor,(void *)&pin,sizeof(pin));
    send(sock_descriptor,buf,strlen(buf),0);
    bzero(buf,20);
    recv(sock_descriptor,buf,80,0);
     close(sock_descriptor);
    return buf;
}

tcpserv()
{
   char buf[8096];
   char buf2[8096];
   struct sockaddr_in sin;
   struct sockaddr_in pin;
   int sock_descriptor;
   int temp_sock_descriptor;
   int address_size;
   long len;

   int counter=0;

   sock_descriptor=socket(AF_INET,SOCK_STREAM,0);

   bzero(&sin,sizeof(sin));
   sin.sin_family=AF_INET;
   sin.sin_addr.s_addr=INADDR_ANY;
   sin.sin_port=htons(SERV-PORT); //we are using port 8000

   bind(sock_descriptor,(struct sockaddr *)&sin,sizeof(sin));

   listen(sock_descriptor,20); //queue up to 20 connections

   while(1)
   {
      //get a temporary socket to handle client request
      temp_sock_descriptor= accept
      (sock_descriptor,(struct sockaddr *)&pin,&address_size);
      //receive data from client
      recv(temp_sock_descriptor,buf,8096,0);
      // send data to server
      buf2 = client-handle(buf);
      strcpy(buf,buf2);
      len=strlen(buf);
      printf("\nA request arrived\n");
      // here we can process the client request
      // return data to the client
      send(temp_sock_descriptor,buf,len,0);
      //close the temporary socket as we are done with it
      close(temp_sock_descriptor);
   }
}


This wont compile, but i will try what you just said, also not the above peice of source is a "first-hack" and i have not yet fixed my mistakes.

Off do to what im told...
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Fri Jan 31, 2003 9:10 am

First get these two files and get them working:

http://voidmain.is-a-geek.net/files/socket/
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Fri Jan 31, 2003 9:22 am

YAY IT WORKS!!!

YAY YAY YAY VOIDMAIN SAVED THE DAY!!!

Now as for setting it up as a relay, where do i go from here?
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Fri Jan 31, 2003 9:47 am

You first of all need to explain what you mean by "relay", in depth.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Fri Jan 31, 2003 10:18 am

A simple C program that relays things to another host

eg:

Packet gets sent to Server program from a client (web browser for example)
Server program sends packet to the server its relaying to (http server for example)
Server program sends reply to the client (the web page afeter a GET reqest for example)
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron