Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 9th, 2005, 4:05 PM   #1
Chriss
Newbie
 
Join Date: Feb 2005
Posts: 1
Rep Power: 0 Chriss is on a distinguished road
BSD Sockets Proxy

Hello everyone,

I've been working on a proxy for a game I play for about a week now. I'm releatively new to BSD Sockets Programming so any help would be appreciated. I've already written a bunch of low end client/server apps but am haveing trouble with the proxy.

My specific problem is getting data from one socket and sending it through another...

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include "show_dump.h"

/* show_dump.h written by Luigi Auriemma,
   please refer to show_dump.h for more
   information                         */
   
//buffer size
#define BUFFSIZE 1024

void dat_handle(int socket1, int socket2, char *outaddr, char *inaddr);

main()
{
      int sd1, sd2, sda;
      int state,
          clilen,
          port;
      char remotehost[12];
      struct sockaddr_in server, localgr, proxy;

      printf("Ghost Recon Proxy 1.0\n\n");
      printf("Please enter the remote address.\n");
      printf("Server IP: ");
      scanf("%s", &remotehost);
      printf("Port to listen on: ");
      scanf("%d", &port);
      
      //create a socket for gr
      sd1 = socket(AF_INET, SOCK_STREAM, 0);
      if(sd1 < 0) {
              printf("A listening socket could not be created for Ghost Recon!\n");
              return 1;
      }
      else
          printf("Local socket created...\n");
          
      //create a socket for the gr server
      sd2 = socket(AF_INET, SOCK_STREAM, 0);
      if(sd2 < 0) {
          printf("A socket could not be created for the Ghost Recon server!\n");
          return 1;
      }
      else
          printf("Server socket created...\n");
      
      //local info
      proxy.sin_family = AF_INET;
      proxy.sin_addr.s_addr = htonl(INADDR_ANY);
      proxy.sin_port = htons(port);
      
      //remote info
      server.sin_family = AF_INET;
      inet_aton(remotehost, &server.sin_addr);
      server.sin_port = htons(port);

      //bind socket to port
      state = bind(sd1, (struct sockaddr *) &proxy, sizeof(proxy));
      if(state < 0) {
          printf("The socket isn't ready for Ghost Recon!\n");
          return 1;
      }
      else
          printf("The socket is now ready to listen for Ghost Recon...\n");
      
      //listen on a new socket
      listen(sd1, 5);
      printf("Listening on port %d...\n", port);
      
      //accept incoming connections
      clilen = sizeof(localgr);

      sda = accept(sd1, (struct sockaddr *) &localgr, (&clilen));
              if(sda < 0) {
                  printf("There was an error connecting to the client.\n");
                  return 1;
              }
              else {
                  printf("Connection with the client has been established!\n");
                  printf("Connecting to the server...\n");
              }
          if((connect(sd2, (struct sockaddr *) &server, sizeof(server))) < 0) {
              printf("Error connecting to the Ghost Recon server.");
              return 1;
          }
          else
              printf("You are now connected to %s:%d\n", remotehost, port);


    while(1) {
        //get data from the local host
        dat_handle(sda, sd2, "Local Host", remotehost);
        //get data from the server
        dat_handle(sd2, sda, remotehost, "Local Host");
    }
}

void dat_handle(int socket1, int socket2, char *inaddr, char *outaddr)
{
    u_char *buff;
    int bufflen;
    
        if((bufflen = recv(socket1, buff, BUFFSIZE, 0)) > 0) {
            if((send(socket2, buff, bufflen, 0)) < 0) {
                printf("=========================================================\n"
                       "From %s\n"
                       "=========================================================\n\n", inaddr);
                       show_dump(buff, bufflen, stdin);
                printf("=========================================================\n\n");
            }
            else
                printf("=========================================================\n"
                       "Error sending data from the proxy to %s\n"
                       "=========================================================\n\n", outaddr);
        }
        else 
            printf("=========================================================\n"
                   "Error parseing data from %s to %s\n"
                   "=========================================================\n\n", inaddr, outaddr);
        
    return;
}

Any help would be apreciated.

Thanks,
Chris

Last edited by Chriss; Feb 9th, 2005 at 4:56 PM.
Chriss is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC