📄 savenatserver.c
字号:
/****************************************************************************by grandchips #gcc SaveNatServer.c -o SaveNatServer.cgi****************************************************************************/#include<sys/types.h>#include<stdio.h>#include <string.h>#include <unistd.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <netinet/in.h>#include <net/if.h>#include <net/if_arp.h>#include <arpa/inet.h>#include <errno.h>char out_ip[20];void set_nat(char *port,char *ip,char *protocol){char in_ip[20];//char protocol[4];//char portnum[5];char cmd[160];strcpy(in_ip,ip);//prestrcpy(cmd,"/sbin/iptables -t nat -A PREROUTING -d ");strcat(cmd,out_ip);strcat(cmd," -p ");strcat(cmd,protocol);strcat(cmd," --dport ");strcat(cmd,port);strcat(cmd," -j DNAT --to ");strcat(cmd,in_ip);printf("%s......<br>\n",cmd);printf("%s......<br>\n",protocol);system(cmd); //poststrcpy(cmd,"/sbin/iptables -t nat -A POSTROUTING -d ");strcat(cmd,in_ip);strcat(cmd," -p ");strcat(cmd,protocol);strcat(cmd," --dport ");strcat(cmd,port);strcat(cmd," -j SNAT --to-source ");strcat(cmd,out_ip);printf("%s......<br>\n",cmd);printf("%s......<br>\n",protocol);system(cmd); }/**********************************************************************/ main(){struct{char port[5];char ip[20];char protocol[4];} server[10];char test[300];char debug[50];strcpy(debug,"");int char_count;int server_count;int n;int i;char c;int var_num;int write_flag;int space;char_count=0;write_flag=0;space=0;i=0;server_count=0;var_num=1;for (i=0;i<10;i++) //init the struct{strcpy(server[i].port,""); strcpy(server[i].ip,""); strcpy(server[i].protocol,""); //server[i].protocol[3]='\0'; //server[i].port[4]='\0'; //server[i].ip[20]='\0';}if (getenv("CONTENT_LENGTH")) n=atoi(getenv("CONTENT_LENGTH"));i=0;//init subscript while (char_count< n-20) { if (var_num % 4==1) //port server[server_count].port[i]='\0' { while(1) { c=getchar();test[char_count]=c;char_count++; if (c=='&') { //debug[i++]=c; if (space==1) {char_count=n;break;} else {server[server_count].port[i]='\0';var_num++;write_flag=0;i=0;break;} } else if (c=='=') {write_flag=1; space=1;} else { if (write_flag==1) {server[server_count].port[i++]=c; space=0;} else continue; } }//in while } //if else if (var_num % 4==2) //ip { while(1) {c=getchar();test[char_count]=c;char_count++; if (c=='&') { if (space==1) {char_count=n;break;} else {server[server_count].ip[i]='\0';var_num++;write_flag=0;i=0;break;} } else if (c=='=') {write_flag=1; space=1;} else { if (write_flag==1) {server[server_count].ip[i++]=c;space=0;} else continue; } }//in while }//else if else if (var_num % 4==3) //protocol { while(1) {c=getchar();test[char_count]=c;char_count++; if (c=='&') { if (space==1) {char_count=n;break;} else {server[server_count].protocol[i]='\0';var_num++;write_flag=0;i=0;break;} } else if (c=='=') {write_flag=1;space=1; } else { if (write_flag==1) {server[server_count].protocol[i++]=c;space=0;} else continue; } }//in while }//else if else //hostname { while(1) {c=getchar();test[char_count]=c;char_count++; if (c=='&') { var_num++;server_count++;write_flag=0;i=0;break; } else continue; }//in while } //else }//out while//output pageprintf("Contenttype:text/html\n\n");printf("<html>\n");printf("<head><title> Setting Nat Server</title></head>\n");printf("Setting Nat Server..............\n");printf("<body><br>\n");printf("%s<br>\n",test);printf("port:%s<br>\n",&server[0].port[0]);printf("ip:%s<br>\n",server[0].ip);printf("protocol:%s<br>\n",server[0].protocol);printf("port:%s<br>\n",server[1].port);printf("ip:%s<br>\n",server[1].ip);printf("protocol:%s<br>\n",server[1].protocol);printf("port:%s<br>\n",server[2].port);printf("server_count=%d......<br>\n",server_count);printf("debug=%s<br>\n",debug);printf("</body>\n");printf("</html>\n");fflush(stdout);char in_ip_range[20];char in_ip[20]; //get out_ip int sock; struct sockaddr_in sin; struct ifreq ifr; sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == -1) { perror("socket"); return -1; } strncpy(ifr.ifr_name, "eth0", IFNAMSIZ); ifr.ifr_name[IFNAMSIZ - 1] = 0; if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) { perror("ioctl"); return -1; } memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); inet_ntoa(sin.sin_addr);strcpy(out_ip,inet_ntoa(sin.sin_addr) );printf("out_ip:%s\n",out_ip);//get in_ip rangestrncpy(ifr.ifr_name, "eth1", IFNAMSIZ); ifr.ifr_name[IFNAMSIZ - 1] = 0; if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) { perror("ioctl"); return -1; } memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); inet_ntoa(sin.sin_addr);strcpy(in_ip,inet_ntoa(sin.sin_addr) );int count=0;for(i=0;i<strlen(in_ip);i++ ){ if (count==3) {in_ip_range[i]='\0';break;} if (in_ip[i]=='.') count++; in_ip_range[i]=in_ip[i];}strcat(in_ip_range,"0/24");printf("in_ip_range:%s\n",in_ip_range);system("echo 1 >/proc/sys/net/ipv4/ip_forward");system("/sbin/iptables -A FORWARD -i eth1 -j ACCEPT");system("/sbin/iptables -A FORWARD -o eth1 -j ACCEPT");n=server_count;for(server_count=0;server_count<n;server_count++) set_nat(server[server_count].port,server[server_count].ip,server[server_count].protocol);//outchar cmd[160];strcpy(cmd,"/sbin/iptables -t nat -A POSTROUTING -s ");strcat(cmd,in_ip_range);strcat(cmd," -o eth0 ");strcat(cmd," -j SNAT --to-source ");strcat(cmd,out_ip);printf("%s......<br>\n",cmd);system(cmd);//system("/sbin/udhcpd &");///sbin/iptables -t nat -A PREROUTING -d 192.168.1.249 -p tcp --dport 21 -j DNAT --to 192.168.5.3///sbin/iptables -t nat -A POSTROUTING -d 192.168.5.3 -p tcp --dport 21 -j SNAT --to-source 192.168.1.249///sbin/iptables -t nat -A POSTROUTING -s 192.168.5.0/24 -o eth0 -j SNAT --to-source 192.168.1.249}/* int fd; if ( (fd=open("rc.local",O_RDWR|O_CREAT|O_APPEND,0600))<0) {printf("can not open udhcpd.conf to write!<br>\n"); } flock(fd,LOCK_EX); write(fd,static_str,13); flock(fd,LOCK_UN); close(fd);*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -