gpt4 book ai didi

c - 使用C套接字为Unix制作代理

转载 作者:行者123 更新时间:2023-12-03 11:52:57 25 4
gpt4 key购买 nike

我正在尝试创建将两个不同的虚拟机连接在一起的两个代理。他们应该这样连接

vm1(telnet) <--> proxy1 <--> proxy2 <--> vm2(telnet daemon)



用户将打开vm1和vm2,然后打开vm1的另一个实例。在vm1上,您将运行proxy1,在vm2上,运行proxy2,然后在第二个vm1上,通过telnet连接到proxy1。

一旦用户通过vm1连接到proxy1,proxy1就应该连接到proxy2。 Proxy2将发送vm2显示的内容,提示登录。它将发送到proxy1,然后发送到vm1。用户应该能够键入vm1并将每一行发送到proxy1然后proxy2然后vm2,就像它们一直在vm2一样。取而代之的是,我从vm2的登录页面中得到了一些提示,但它主要是随机字符和一些电视静态字符。它一直向我发送大量随机字符,直到我关闭所有内容。

这是我的代码(cproxy.c是proxy1,sproxy.c是proxy2)

cproxy.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define MAX_PENDING 5
#define MAX_LINE 1024

struct sockaddr_in LocalTelnetAddress;
struct sockaddr_in SproxyAddress;
struct hostent *hp;
struct timeval tv;

int setUpTelnetConnection(int port){
bzero((char *)&LocalTelnetAddress, sizeof(LocalTelnetAddress));
LocalTelnetAddress.sin_family = AF_INET;
LocalTelnetAddress.sin_addr.s_addr = INADDR_ANY;
LocalTelnetAddress.sin_port = htons(port); // Change 5200

// Create socket.
int localTelnetSocket = socket(PF_INET, SOCK_STREAM, 0);
if (localTelnetSocket < 0) {
fprintf(stderr, "ERROR: c telnet socket.\n");
exit(1);
}

// Bind socket.
if (bind(localTelnetSocket, (struct sockaddr *) &LocalTelnetAddress, sizeof(LocalTelnetAddress)) < 0) {
fprintf(stderr, "ERROR: c telnet bind.\n");
exit(1);
}

return localTelnetSocket;
}

int setUpSproxyConnection(char *host, int port)
{
hp = gethostbyname(host);
bzero((char *)&SproxyAddress, sizeof(SproxyAddress));
SproxyAddress.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&SproxyAddress.sin_addr, hp->h_length);
SproxyAddress.sin_port = htons(port); //change 6200


// Create the socket.
int sproxySocket = socket(PF_INET, SOCK_STREAM, 0);
if (sproxySocket < 0) {
fprintf(stderr, "ERROR: sproxy socket.\n");
exit(1);
}

return sproxySocket;
}

int main(int argc, char * argv[]){

fd_set readfds;
int rv;
int n, len = 0;
struct timeval tv;

char buf1[MAX_LINE];
socklen_t len1;
char buf2[MAX_LINE];

int port1;
int port2;
int localTelnetSocket;
int sproxySocket;
if(argc == 4){
port1 = atoi(argv[1]);
port2 = atoi(argv[3]);
localTelnetSocket = setUpTelnetConnection(port1);
sproxySocket = setUpSproxyConnection(argv[2],port2);
}
else{
fprintf(stderr, "illegal argc\n");
exit(1);
}

listen(localTelnetSocket, MAX_PENDING);
// int len = sizeof(LocalTelnetAddress);
// Connect local telnet
int localTelnet_s = accept(localTelnetSocket, (struct sockaddr *)&LocalTelnetAddress, &len1);
if (localTelnet_s < 0) {
perror("simplex-talk: accept");
exit(1);
}

// Connect sproxy
int sproxy_s = connect(sproxySocket, (struct sockaddr *) &SproxyAddress, sizeof(SproxyAddress));
if (sproxy_s < 0) {
fprintf(stderr, "ERROR connecting to sproxy.\n");
exit(EXIT_FAILURE);
}

while(1){
// clear the set ahead of time
FD_ZERO(&readfds);

// add our descriptors to the set
FD_SET(localTelnet_s, &readfds);
FD_SET(sproxySocket, &readfds);

// find the largest descriptor, and plus one.

if (localTelnet_s > sproxySocket) n = localTelnet_s + 1;
else n = sproxySocket +1;


// wait until either socket has data ready to be recv()d (timeout 10.5 secs)
tv.tv_sec = 10;
tv.tv_usec = 500000;

rv = select(n, &readfds, NULL, NULL, &tv);


if (rv == -1) {
perror("select"); // error occurred in select()
} else if (rv == 0) {
printf("Timeout occurred! No data after 10.5 seconds.\n");
} else {
// one or both of the descriptors have data
if (FD_ISSET(localTelnet_s, &readfds)) {
memset(buf1,0,strlen(buf1));
len = recv(localTelnet_s, buf1, sizeof(buf1), 0);
send(sproxySocket, buf1, sizeof(buf1), 0);
// printf("%s", buf1);
}
if (FD_ISSET(sproxySocket, &readfds)) {
memset(buf2,0,strlen(buf2));
len = recv(sproxySocket, buf2, sizeof(buf2), 0);
send(localTelnet_s, buf2, sizeof(buf2), 0);
}
}
}
return 0;
}

吡咯
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define MAX_PENDING 5
#define MAX_LINE 1024

struct sockaddr_in TelnetDaemonAddress;
struct sockaddr_in CproxyAddress;
struct hostent *hp;
char *ip = "127.0.0.1";
//struct hostent *hp;
struct timeval tv;

int setUpCproxyConnection(int port)
{
bzero((char *)&CproxyAddress, sizeof(CproxyAddress));
CproxyAddress.sin_family = AF_INET;
CproxyAddress.sin_addr.s_addr = INADDR_ANY;
CproxyAddress.sin_port = htons(port);

// Create socket.
int cproxySocket = socket(PF_INET, SOCK_STREAM, 0);
if (cproxySocket < 0) {
fprintf(stderr, "ERROR: cproxy socket.\n");
exit(EXIT_FAILURE);
}

// Bind socket
if (bind(cproxySocket, (struct sockaddr *) &CproxyAddress, sizeof(CproxyAddress)) < 0) {
fprintf(stderr, "ERROR: cproxy bind.\n");
exit(EXIT_FAILURE);
}

return cproxySocket;
}

int setUpTelnetDaemonConnection()
{
hp = gethostbyname("127.0.0.1");
bzero((char *)&TelnetDaemonAddress, sizeof(TelnetDaemonAddress));
TelnetDaemonAddress.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&TelnetDaemonAddress.sin_addr, hp->h_length);
TelnetDaemonAddress.sin_port = htons(23); //change 6200


// Create the socket.
int telnetDaemonSocket = socket(PF_INET, SOCK_STREAM, 0);
if (telnetDaemonSocket < 0) {
fprintf(stderr, "ERROR: telnetDaemonSocket.\n");
exit(1);
}

return telnetDaemonSocket;
}

int main(int argc, char * argv[]){

fd_set readfds;
int rv;
int n, len = 0;
struct timeval tv;

char buf1[MAX_LINE];
socklen_t len1;
char buf2[MAX_LINE];


int port;
int cproxySocket;
int telnetDaemonSocket;


if(argc == 2){
port = atoi(argv[1]);
cproxySocket = setUpCproxyConnection(port);
telnetDaemonSocket = setUpTelnetDaemonConnection();
}
else{
fprintf(stderr, "illegal argc\n");
exit(1);
}

// Connect cproxy
listen(cproxySocket, MAX_PENDING);
// int len = sizeof(CproxyAddress);
int cproxy_s = accept(cproxySocket, (struct sockaddr *) &CproxyAddress, &len1);


if (cproxy_s < 0) {
fprintf(stderr, "ERROR: cproxy accept.\n");
exit(1);
}

// Connect telnet daemon
if (connect(telnetDaemonSocket, (struct sockaddr *) &TelnetDaemonAddress, sizeof(TelnetDaemonAddress)) < 0) {
fprintf(stderr, "ERROR: telnet daemon connect.\n");
exit(1);
}

while(1){
// clear the set ahead of time
FD_ZERO(&readfds);

// add our descriptors to the set
FD_SET(cproxy_s, &readfds);
FD_SET(telnetDaemonSocket, &readfds);


// find the largest descriptor, and plus one.

if (cproxy_s > telnetDaemonSocket) n = cproxy_s + 1;
else n = telnetDaemonSocket +1;

// wait until either socket has data ready to be recv()d (timeout 10.5 secs)
tv.tv_sec = 10;
tv.tv_usec = 500000;
rv = select(n, &readfds, NULL, NULL, &tv);

if (rv == -1) {
perror("select"); // error occurred in select()
} else if (rv == 0) {
printf("Timeout occurred! No data after 10.5 seconds.\n");
} else {
// one or both of the descriptors have data
if (FD_ISSET(cproxy_s, &readfds)) {
memset(buf1,0,strlen(buf1));
len = recv(cproxy_s, buf1, sizeof(buf1), 0);
send(telnetDaemonSocket, buf1, sizeof(buf1), 0);
}
if (FD_ISSET(telnetDaemonSocket, &readfds)) {
memset(buf2,0,strlen(buf2));
len = recv(telnetDaemonSocket, buf2, sizeof(buf2), 0);
send(cproxy_s, buf2, sizeof(buf2), 0);
//printf("%s", buf2);
}
}
}
return 0;
}

最佳答案

除了您确实要添加的完全没有错误检查的功能外,您只想发送与接收的一样多的字节。

因此,除了对所有相关的系统调用添加错误检查之外,还要删除这些行memset( ....并将send(.., ..., sizeof ..., 0);替换为send(.., ..., len, 0);

关于c - 使用C套接字为Unix制作代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46442846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com