-
February 3rd, 2004, 11:11 AM
#1
Simple socket proxy for Unix
It's compiling normally. This proxy can accept the incoming connection (int server_side()) and connect to 192.168.0.2 (int client_side()), but it stops right there -> in the second read(newsockfd_bnetd, buffer, MAXMSG);
What's wrong with my code? I just want to pass the packets through my proxy. Please, help me. It's very important to me.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#define MAXMSG 512
int server_side(void);
int client_side(void);
int main(void)
{
int newsockfd, newsockfd_bnetd;
char buffer[MAXMSG];
newsockfd_bnetd = client_side();
newsockfd = server_side();
for (;;)
{
bzero(buffer, MAXMSG);
read(newsockfd, buffer, MAXMSG);
write(newsockfd_bnetd, buffer, strlen(buffer));
bzero(buffer, MAXMSG);
read(newsockfd_bnetd, buffer, MAXMSG);
write(newsockfd, buffer, strlen(buffer));
}
close(newsockfd_bnetd);
close(newsockfd);
return 0;
}
int server_side(void)
{
int sockfd, newsockfd, portno, n;
struct sockaddr_in serv_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
printf ("ERROR opening socket");
fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK);
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(6112);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
printf ("ERROR on binding");
n = listen(sockfd,5);
if (n < 0)
printf ("ERROR on listening");
newsockfd = accept(sockfd, (struct sockaddr *) NULL, NULL);
while ((newsockfd = accept(sockfd, (struct sockaddr *) NULL, NULL)) < 0);
if (newsockfd < 0)
printf ("accept error\n");
return (newsockfd);
}
int client_side(void)
{
int sockfd_bnetd, newsockfd_bnetd, n;
struct sockaddr_in servaddr_bnetd;
if ( (sockfd_bnetd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
printf ("socket bnetd error\n");
fcntl(sockfd_bnetd, F_SETFL, fcntl(sockfd_bnetd, F_GETFL, 0) | O_NONBLOCK);
bzero(&servaddr_bnetd, sizeof(servaddr_bnetd));
servaddr_bnetd.sin_family = AF_INET;
servaddr_bnetd.sin_port = htons(6003);
if (inet_pton(AF_INET, "192.168.0.2", &servaddr_bnetd.sin_addr) <= 0)
printf ("inet_pton error for 192.168.0.2\n");
while ((newsockfd_bnetd = connect(sockfd_bnetd, (struct sockaddr *) &servaddr_bnetd,
sizeof(servaddr_bnetd))) < 0);
if (n < 0)
printf ("connect error\n");
return (newsockfd_bnetd);
}
-
February 3rd, 2004, 01:24 PM
#2
I made some changes, however, the proxy stops in the second write() (it seems he can't read. May it be a problem in the second socket?) The screen shows:
Code:
Ponto 1
Ponto 2
Ponto 3!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#define MAXMSG 512
int server_side(void);
int client_side(void);
int main(void)
{
int newsockfd, newsockfd_bnetd, size;
char buffer[MAXMSG];
newsockfd_bnetd = client_side();
printf ("Ponto 1\n");
newsockfd = server_side();
printf ("Ponto 2\n");
for (;;)
{
bzero(buffer, MAXMSG);
size = read(newsockfd, buffer, MAXMSG);
if (size > 0)
write(newsockfd_bnetd, buffer, size);
else
printf ("Size: %d\n", size);
printf ("Ponto 3!\n");
bzero(buffer, MAXMSG);
size = read(newsockfd_bnetd, buffer, MAXMSG);
printf ("Ponto 4!\n");
if (size > 0)
write(newsockfd, buffer, size);
else
printf ("Size: %d\n", size);
}// Fim for()
close(newsockfd_bnetd);
close(newsockfd);
return 0;
}
int server_side(void)
{
int sockfd, newsockfd, portno, n;
struct sockaddr_in serv_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
printf ("ERROR opening socket");
fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK);
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(6112);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
printf ("ERROR on binding");
n = listen(sockfd,5);
if (n < 0)
printf ("ERROR on listening");
while ((newsockfd = accept(sockfd, (struct sockaddr *) NULL, NULL)) < 0);
if (newsockfd < 0)
printf ("accept error\n");
return (newsockfd);
}
int client_side(void)
{
int sockfd_bnetd, newsockfd_bnetd, n;
struct sockaddr_in servaddr_bnetd;
if ( (sockfd_bnetd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
printf ("socket bnetd error\n");
fcntl(sockfd_bnetd, F_SETFL, fcntl(sockfd_bnetd, F_GETFL, 0) | O_NONBLOCK);
bzero(&servaddr_bnetd, sizeof(servaddr_bnetd));
servaddr_bnetd.sin_family = AF_INET;
servaddr_bnetd.sin_port = htons(6003); // Ele ira tentar conectar-se a porta 6003
if (inet_pton(AF_INET, "192.168.0.2", &servaddr_bnetd.sin_addr) <= 0) // no endereco 192.168.0.2
printf ("inet_pton error for 192.168.0.2\n");
while ((newsockfd_bnetd = connect(sockfd_bnetd, (struct sockaddr *) &servaddr_bnetd, sizeof(servaddr_bnetd))) < 0);
if (n < 0)
printf ("connect error\n");
return (newsockfd_bnetd);
}
-
February 3rd, 2004, 05:37 PM
#3
Let's see, you do (in a loop):
1. read from server
2. write to client
3. read from client
4. write to server
I see two problems with your code:
a) Read may (if it works the same as recv()) return anything from 0 to MAXMSG. So how do you know you have read all data from the other end?
b). Your code assume knowledge of the application protocol being proxy'd and I don't see any protocol parsing in your code
The reason why the second read() (step 3) blocks is obvious to me: the client isn't ready for sending data yet!
The client app probably wants to read more data from the server-end--the data that is lost due to problem a) above.
There are two possible solutions:
1. Obey the application protocol being proxy'd (can be very challenging)
2. Implement a generic TCP proxy (which is protocol independant)
I don't know what your proxy is supposed to do (you aren't trying to cheat in Starcraft/Warcraft 3/Diablo I hope?), but I recommend my 2:nd solution...
/Jonas
-
April 2nd, 2006, 10:37 AM
#4
Re: Simple socket proxy for Unix
2. Implement a generic TCP proxy (which is protocol independant)
How would you implement a generic TCP proxy ?
cyph
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|