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