⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dnss.c

📁 动态解析主机域名的域名服务器端的一个进程。c语言编写。
💻 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 + -