重定向到端口 [英] Redirect to a PORT

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

问题描述

我正在使用iptables将所有已连接到路由器的客户端重定向到另一个端口(例如8080)

I'm redirecting the clients who are all connected to my router to another port(say 8080) using iptables

iptables -t nat -A PREROUTING -p tcp -j REDIRECT --to-port 8080

我正在运行执行二进制文件,它将客户端重定向到网站.说www.google.com.我正在使用以下代码.

I'm running executing a binary which will redirect the clients to a website. say www.google.com. I'm using the following code.

#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\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";

    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);
}
}

当我这样做时,客户端不会被重定向.它在客户端浏览器的地址栏中显示重定向的地址.并尝试了很长时间才能访问www.google.com.并显示以下错误消息.

when I doing like this the clients are not redirected. Its showing the redirected address in the address bar of the client browser. And trying for long time to goto www.google.com. and end with the following error message.

此页面未正确重定向. Firefox已检测到服务器正在以某种方式重定向对此地址的请求 那将永远不会完成.

This page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

我已经修改了回复变量.即使是现在,它也无法按预期工作. 我该如何解决这个问题...

I've modified the reply variable. Even now its not working as expected. How can i solve this problem...

推荐答案

首先,换行符应为"\r\n".其次,您没有正确终止标头,该标头应该是最后一个标头字段之后的换行符,然后是多余的空行.第三,将Content-Length标头字段设置为1000字节的长度,这可能使客户端期望答复的正文中应具有1000字节,而显然没有.

First of all, newlines should be "\r\n". Secondly you do not properly terminate the header, which should be a newline after the last header field and then an extra empty line. Thirdly, you set the Content-Length header field to a length of 1000 bytes, which might make the client expect that the reply should have 1000 bytes in the body, which clearly it does not.

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

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