重定向到 c 中的网站 [英] redirect to a website in c

查看:71
本文介绍了重定向到 c 中的网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码重定向客户端请求.但是在执行以下操作时,不会重定向客户端.它在浏览器中显示无法连接".我使用 iptables 将客户端重定向到端口 8080.并运行以下可执行文件进行重定向.如何重定向客户端.请提供解决方案....

I am using the following code to redirect client request. But when doing the following the clients are not redirected. Its showing "Unable to connect" in the browser. I redirect the clients to port 8080 using iptables. And running the following executable to redirect. How to redirect the clients. Please provide solution....

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 

#include<stdlib.h>

int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr; 

char *reply = "HTTP/1.1 301 Moved Permanently\nServer: Apache/2.2.3\nLocation: 
http://www.google.com\nContent-Length: 1000\nConnection: close\nContent-Type:  
text/html; charset=UTF-8";

char sendBuff[1025];
time_t ticks; 

listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff)); 

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(8080); 


bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

listen(listenfd, 10); 

while(1)
{
    connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 

printf("client connected\n");
    send(connfd, reply, strlen(reply), 0);

    close(connfd);
    sleep(1);
 }
 }

推荐答案

我无法重现您看到的错误.您应该提供更多详细信息(例如,什么样的客户端,iptables 规则的确切文本).对于我的测试,我没有设置任何 iptables 规则,而是将 Firefox 12.0 浏览器直接指向 localhost:8080.

I am unable to reproduce the error that you see. You should provide more details (e.g., what kind of client, exact text of iptables rule). For my test, I did not set any iptables rule, and instead pointed the Firefox 12.0 browser directly to localhost:8080.

拆分您的回复以便于阅读节目:

Splitting up your reply so that it is easier to read shows:

char *reply =
"HTTP/1.1 301 Moved Permanently\n"
"Server: Apache/2.2.3\n"
"Location: http://www.google.com\n"
"Content-Length: 1000\n"
"Connection: close\n"
"Content-Type: text/html; charset=UTF-8"
;

虽然 RFC 指定 \r\n行终止符,大多数客户端会接受 \n (你不说你正在使用哪个客户端).但是,另外三个明显的问题是最后一行没有终止,响应本身没有以空行终止,并且你有一个 1000Content-Length 标头,但没有内容.这些问题中的任何一个都可能导致客户端将响应视为无效并忽略它.

Although the RFC specifies \r\n for line terminators, most clients will accept \n (you don't say which client you are using). But, three other glaring issues are that the last line is not terminated, the response itself is not terminated by a blank line, and you have a Content-Length header of 1000, but no content. Any of these issues could be cause for a client to treat the response as invalid and ignore it.

char *reply =
"HTTP/1.1 301 Moved Permanently\r\n"
"Server: Apache/2.2.3\r\n"
"Location: http://www.google.com\r\n"
"Content-Length: 0\r\n"
"Connection: close\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"\r\n"
;

深入阅读您的代码,您在发送回复后立即关闭连接,而无需先阅读请求.这可能会导致(尽管不太可能)在请求完全传送到服务器之前关闭连接的竞争.然后,当请求确实到达时,它将触发对客户端的重置,并且可能会丢弃响应.因此,您应该添加代码以使回复的传递更加可靠:

Reading farther into your code, you close the connection immediately after sending your reply without first reading the request. This might lead to an (albeit unlikely) race where you close the connection before the request is fully delivered to the server. Then, when the request does arrive, it will trigger a reset to the client, and the response could be dropped. So, you should add code to make the delivery of your reply more robust:

printf("client connected\n");
send(connfd, reply, strlen(reply), 0);
shutdown(connfd, SHUT_WR);
while (recv(connfd, sendBuff, sizeof(sendBuff), 0) > 0) {}
close(connfd);

鉴于我无法通过响应重现您的问题,但也可能是您没有正确设置 iptable 重定向规则.

Given that I cannot reproduce your issue with the response as it is, though, it is also possible that you did not set your iptable redirect rule properly.

这篇关于重定向到 c 中的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆