?? dnss.c
字號:
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/io.h>
#include <tcl.h>
#include <pthread.h>
#include <semaphore.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define bool int
#define true 1
#define false 0
void MyDnsProc(int *arg);
char *trim(char*dst, char *src);
int AContainB(char *src1, char *src2);
main( ){
int length;
int sock,msgsock;
struct sockaddr_in server;
struct sockaddr_in tcpaddr;
int len;
int b1,b2,b3,b4;
int m_nPort=8901;
u_long tcp_ip=0x00000000;
unsigned long * p=NULL;
int optVal;
int optLen = sizeof(int);
/* 建立套接字 */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("error occurred when opening stream socket\n");
exit(1);
}
/*指定本地主機ip地址*/
server.sin_family = AF_INET;
server.sin_addr.s_addr=htonl(0x0a0d52be);
server.sin_port = htons(8901);
printf("本地ip地址:10.13.82.190 %d\n", ntohs(server.sin_port));
/*binding 套接字*/
if (bind(sock, (struct sockaddr *)&server, sizeof(server)) == -1) {
perror("error occurred when binding stream socket\n");
exit(1);
}
/* 找出指定的端口號并打印出來 */
length = sizeof(server);
if (getsockname(sock, (struct sockaddr *)&server, &length) < 0) {
perror("error occurred when getting socket name\n");
exit(1);
}
printf("socket port: %d\n", ntohs(server.sin_port));
/*開始監聽*/
if(listen(sock, 1)==-1) {
perror("error occurred when start listenning\n");
return 0;
}else
printf("listening successfully.\r\n");
/* accept and receive */
len = sizeof(struct sockaddr);
do { ///do-while代碼段開始。。。。
msgsock= accept(sock, (struct sockaddr *)&tcpaddr, (int *)&len);
if (msgsock== -1)
perror("error occurred when acceptting.\r\n");
tcp_ip=ntohl(tcpaddr.sin_addr.s_addr);
b1=tcp_ip%0x100;
b2=(tcp_ip>>8)%0x100;
b3=(tcp_ip>>16)%0x100;
b4=tcp_ip>>24;
printf("a host with address: %d.%d.%d.%d %d is acceptted\n",
b4,b3,b2,b1,ntohs(tcpaddr.sin_port));
if (getsockopt(msgsock, SOL_SOCKET, SO_RCVBUF, (char*)&optVal,&optLen) != -1)
printf("the receiving buf of msgsock is: %ld\n", optVal);
MyDnsProc(&msgsock);
} while (TRUE); ///do-while結束。。。。
close(sock);
exit(0);
}
void MyDnsProc(int *arg) {
int msgsock=*arg;
char buf[1024];
FILE *fp,*fpM,*fpS,*fpMF,*fpSF;
char User[20]="Unkown";
char DNSFILEPATH[20]="/var/named/";
char DNSFILE[40]="";
char DNSFILEF[40]="";
char HOSTIP[20]="127.0.0.1";
bool USER_VALIDATED=true ;
int k=0;
if((fp=fopen("/var/named/temp_file","w+r+b"))==NULL){
perror("不能打開臨時文件");
exit(4);
}
/*接收數據并寫入臨時文件*/
memset(buf, 0, sizeof(buf));
if((k=recv(msgsock, buf, 1024,0))<0){
perror("error occurred when reading stream message.\n");
exit(0);
}
if (k==0){
printf("\n數據一早就為0了。。。\n");
}
else{
printf("rev=%d\n",k);
printf("接收到的數據: %s\n",buf);
if(k=fwrite(buf,sizeof(buf),1,fp)!=1)
printf("temp_file write error\n");
else{
printf("rev=%d\n",k);
printf("接收到的數據塊寫入文件。\n");
}
}
while(k !=0){
memset(buf, 0, sizeof(buf));
if((k=recv(msgsock, buf, 1024,0))<0){
perror("error occurred when reading stream message.\n");
exit(0);
}else if (k==0)
printf("socket傳輸完畢!!!\n");
else{
printf("rev=%d\n",k);
if(fwrite(buf,sizeof(buf),1,fp)!=1)
printf("temp_file write error\n");
else
printf("數據塊寫文件完畢");
}
}
close(msgsock);
fclose(fp);
if((fp=fopen("/var/named/temp_file","rb"))==NULL){
perror("不能打開臨時文件");
exit(4);
}
/*從臨時文件提取信息*/
memset(buf, 0, sizeof(buf));
printf("從臨時文件提取信息.....\n");
while(fgets(buf,sizeof(buf),fp)!=NULL){
char tempBuf[40];
printf("從文件提取一行信息:\n%s",buf);
memset(tempBuf,0,sizeof(tempBuf));
trim(tempBuf,buf);
if(tempBuf[0]=='L'&&tempBuf[1]=='A'){
tempBuf[strlen(tempBuf)-1]=tempBuf[strlen(tempBuf)];
strcpy(User,&tempBuf[2]);
printf("User=%s\n",User);
}else if(tempBuf[0]=='L'&&tempBuf[1]=='B'){
tempBuf[strlen(tempBuf)-1]=tempBuf[strlen(tempBuf)];
strcpy(DNSFILE,&tempBuf[2]);
printf("DNSFILE=%s\n",DNSFILE);
}else if(tempBuf[0]=='L'&&tempBuf[1]=='C'){
tempBuf[strlen(tempBuf)-1]=tempBuf[strlen(tempBuf)];
strcpy(DNSFILEF,&tempBuf[2]);
printf("DNSFILEF=%s\n",DNSFILEF);
}else if(tempBuf[0]=='L'&&tempBuf[1]=='D'){
tempBuf[strlen(tempBuf)-1]=tempBuf[strlen(tempBuf)];
strcpy(HOSTIP,&tempBuf[2]);
printf("HOSTIP=%s\n",HOSTIP);
}else if(tempBuf[0]=='B'&&tempBuf[1]=='Y'&&tempBuf[2]=='E'){
break;
}
memset(buf, 0, sizeof(buf));
}
fclose(fp);
remove("temp_file");
printf("remove temp_file\n");
/*打開各數據庫文件*/
memset(buf,0,sizeof(buf));
sprintf(buf,"%s%s.static",DNSFILEPATH,DNSFILE);
if((fpM=fopen(buf,"rb"))==NULL){
perror("不能打開模板文件");
exit(4);
}
memset(buf,0,sizeof(buf));
sprintf(buf,"%s%s.static",DNSFILEPATH,DNSFILEF);
if((fpMF=fopen(buf,"rb"))==NULL){
perror("不能打開模板反文件");
exit(4);
}
memset(buf,0,sizeof(buf));
sprintf(buf,"%s%s",DNSFILEPATH,DNSFILE);
if((fpS=fopen(buf,"w+b"))==NULL){
perror("不能打開數據庫文件");
exit(4);
}
memset(buf,0,sizeof(buf));
sprintf(buf,"%s%s",DNSFILEPATH,DNSFILEF);
if((fpSF=fopen(buf,"w+b"))==NULL){
perror("不能打開數據庫反文件");
exit(4);
}
printf("打開數據庫文件完畢!\n");
/*更新數據庫文件*/
memset(buf, 0, sizeof(buf));
while(fgets(buf,sizeof(buf),fpM)){
if((buf[0]=='E')&&(buf[1]=='L')&&(buf[2]=='A')){
char tempStr[40];
memset(tempStr, 0, sizeof(tempStr));
sprintf(tempStr,"ELA IN A %s\n",HOSTIP);
fputs(tempStr,fpS);
}else{
fputs(buf,fpS);
printf("%s",buf);
}
memset(buf, 0, sizeof(buf));
}
printf("fpM更新完畢.\n");
/*更新數據庫反文件*/
memset(buf, 0, sizeof(buf));
while(fgets(buf,sizeof(buf),fpMF)){
char tempBuf[40];
memset(tempBuf,0,sizeof(tempBuf));
trim(tempBuf,buf);
tempBuf[strlen(tempBuf)-1]=tempBuf[strlen(tempBuf)];
if(AContainB(tempBuf,"PTRELA.zyiyi.com.")){
char tempStr[40];
memset(tempStr, 0, sizeof(tempStr));
printf("這里需要更新.....\n");
sprintf(tempStr,"%s IN PTR ELA.zyiyi.com.\n",HOSTIP);
fputs(tempStr,fpSF);
printf("%s IN PTR ELA.zyiyi.com.\n",HOSTIP);
}else{
fputs(buf,fpSF);
printf("%s",buf);
}
memset(buf, 0, sizeof(buf));
}
printf("fpMF更新完畢!\n");
fclose(fpM);
fclose(fpS);
fclose(fpMF);
fclose(fpSF);
printf("\nUser: %s", User );
printf("\nDNSFILE: %s", DNSFILE );
printf("\nDNSFILEF: %s", DNSFILEF);
printf("\nHOSTIP: %s", HOSTIP );
/* ndc reload */
sprintf(buf,"/etc/rc.d/init.d/named reload %s%s",DNSFILEPATH,DNSFILE);
system("/etc/rc.d/init.d/named start");
system(buf);
memset(buf, 0, sizeof(buf));
sprintf(buf,"/etc/rc.d/init.d/named reload %s%s",DNSFILEPATH,DNSFILEF);
system(buf);
printf("\nDnsProc is closed.\n");
}
char *trim(char *dst, char *src){
int i=0,j=0;
for(;i<=strlen(src);i++){
if(src[i]!=' '){
dst[j]=src[i];
j++;
}
}
return dst;
}
int AContainB(char *src1, char *src2){
int i = 0;
int temp1=0,temp2=0;
int eSign=0;
temp1=strlen(src1);
temp2=strlen(src2);
if(temp1<temp2)
return eSign;
while(temp1-i>=temp2){
if(strcmp(&src1[i],src2)==0){
eSign=1;
break;
}
else{
// printf("src1:%s\n",&src1[i]);
// printf("src2:%s\n",src2);
i++;
}
}
return eSign;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -