📄 poll.c
字号:
/*************************************************************************** poll.c - description ------------------- begin : Mon Jun 24 10:43:37 EEST 2002 copyright : (C) 2002 by Petri Turunen email : petri.turunen@pete.fi.eu.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#include<stdio.h>#include<sys/time.h>#include<sys/resource.h>#include<sys/stat.h>#include<sys/types.h>#include<string.h> // Include this for memset()#include<unistd.h> //for gethostname#include<fcntl.h>#include<stdlib.h>#include <sys/wait.h>#include <stdarg.h>#include <time.h> //for time#include "respmd.h"//Global variablesextern char SERVER_IP[]; //defined in respmd.cextern char SERVER_PORT[]; //defined in respmd.cextern char LOCALHOSTN[]; //defined in respmd.cextern char my_host[]; //defined in respmd.c/*********************************************************************///SMTP POLL CODE/*********************************************************************/int poll_smtp(struct smtps_t *smtppointer){ int sockfd; // socket. int n=0,len=0,ok=0; char read_buf[301]="\0"; char response[4]="\0"; char error_msg[100]="\0"; //we use this to send errors to server char err_msg[100]="\0"; //thiw is used to store response error char *serv="25"; struct addrinfo hints, *res, *ressave; struct timeval tv; tv.tv_sec = 40; tv.tv_usec = 0; bzero(&hints, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype=SOCK_STREAM; if((n = getaddrinfo(smtppointer->name,serv,&hints, &res))!=0) { sprintf(error_msg,"respmd: getaddrinfo failed for SMTP server: %s, %s",smtppointer->name,gai_strerror(n)); if(client(SERVER_IP, SERVER_PORT, LOCALHOSTN, "ERROR", error_msg, smtppointer->name, "NONE", "DOWN", "SMTP") < 0) slog(2, "poll.c:smtp: error while sending to server."); //else // slog(2,"poll.c: getaddrinfo failed for SMTP server: %s, %s",smtppointer->name,gai_strerror(n)); smtppointer->down=1; smtppointer->up=0; return 0; } n=0; ressave=res; do { //connect loop starts sockfd=socket(res->ai_family, res->ai_socktype, res->ai_protocol); if(sockfd<0) { slog(2,"poll.c:smtp: Error socket: %s",strerror(errno)); return -1; } // 'Call' the server. n = connect(sockfd, res->ai_addr,res->ai_addrlen); if(n < 0) { if(errno == ETIMEDOUT || errno == EINTR) { sprintf(error_msg,"respmd: SMTP server: %s is down. Connection timeout.",smtppointer->name); if(client(SERVER_IP, SERVER_PORT, LOCALHOSTN, "ERROR", error_msg, smtppointer->name, "NONE", "DOWN", "SMTP") < 0) slog(2, "poll.c:smtp: error while sending to server."); smtppointer->down=1; smtppointer->up=0; } else if(errno == ECONNREFUSED) { sprintf(error_msg,"respmd: SMTP server: %s is down. Connection refused.",smtppointer->name); if(client(SERVER_IP, SERVER_PORT, LOCALHOSTN, "ERROR", error_msg, smtppointer->name, "NONE", "DOWN", "SMTP") < 0) slog(2, "poll.c:smtp: error while sending to server."); } else { sprintf(error_msg,"respmd: SMTP server: %s is down. %s",smtppointer->name,strerror(errno)); if(client(SERVER_IP, SERVER_PORT, LOCALHOSTN, "ERROR", error_msg, smtppointer->name, "NONE", "DOWN", "SMTP") < 0) slog(2, "poll.c:smtp: error while sending to server."); } smtppointer->down=1; smtppointer->up=0; freeaddrinfo(ressave); return 0; } else { break; } //alarm(0); } while( (res = res->ai_next) != NULL); //connect loop end if(res == NULL) slog(2,"poll.c:smtp: connection to: %s failed",smtppointer->name); freeaddrinfo(ressave); //set write timeout setsockopt(sockfd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)); //set read timeout setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)); len=read(sockfd, read_buf, 300); // Read from the server. if(len<0) { len = send_to_server("SMTP","ERROR", 2, smtppointer, "poll.c: Error while reading from: %s %s", smtppointer->name,strerror(errno)); if(len!=0) slog(2,"poll.c:smtp: Error while reading from: %s %s",smtppointer->name,strerror(errno)); close(sockfd); return(0); } read_buf[len]='\0'; strncpy(response,read_buf,3); response[3]='\0'; //slog(1,"DEBUG RESPONSE1: %s",response); if(strcmp(response,"220")==0) //did we get connection { ok++; read_buf[0]='\0'; strcat(read_buf,"HELO "); //Write host where we get the web page strcat(read_buf,my_host); strcat(read_buf,"\r\n"); len=strlen(read_buf); len=write(sockfd, read_buf, len); read_buf[0]='\0'; len=read(sockfd, read_buf, 300); // Read from the server. if(len<0) { len = send_to_server("SMTP","ERROR", 2, smtppointer, "poll.c: Error while reading from: %s %s", smtppointer->name,strerror(errno)); if(len!=0) slog(2,"poll.c:smtp: Error while reading from: %s %s",smtppointer->name,strerror(errno)); close(sockfd); return 0; } read_buf[len]='\0'; strncpy(response,read_buf,3); response[3]='\0'; //slog(1,"DEBUG RESPONSE2: %s LEN: %d",read_buf,len); if(strcmp(response,"250")==0) ok++; else strncpy(err_msg,read_buf,99); len=write(sockfd, "QUIT\r\n", 6); //We wantto close connection read_buf[0]='\0'; len=read(sockfd, read_buf, 300); // Read from the server. if(len<0) { len = send_to_server("SMTP","ERROR", 2, smtppointer, "poll.c: Error while reading from: %s %s", smtppointer->name,strerror(errno)); if(len!=0) slog(2,"poll.c:smtp: Error while reading from: %s %s",smtppointer->name,strerror(errno)); close(sockfd); return 0; } read_buf[len]='\0'; strncpy(response,read_buf,3); response[3]='\0'; if(strcmp(response,"221")==0) //did QUIT go ok ok++; else { err_msg[0]='\0'; strncpy(err_msg,read_buf,99); } //slog(1,"DEBUG RESPONSE3: %s LEN: %d",read_buf,len); //Lets process the results if(ok==3) { if(smtppointer->down==1) { sprintf(error_msg,"respmd: SMTP server: %s is up.",smtppointer->name); if(client(SERVER_IP, SERVER_PORT, LOCALHOSTN, "INFO", error_msg, smtppointer->name, "NONE", "UP", "SMTP") < 0) slog(2, "poll.c:smtp: error while sending to server."); } smtppointer->down=0; smtppointer->up=1; } else { sprintf(error_msg,"respmd: SMTP server: %s is down. %s",smtppointer->name,err_msg); if(client(SERVER_IP, SERVER_PORT, LOCALHOSTN, "ERROR", error_msg, smtppointer->name, "NONE", "DOWN", "SMTP") < 0) slog(2, "poll.c:smtp: error while sending to server."); smtppointer->down=1; smtppointer->up=0; } //Lets process the results ends } else { smtppointer->down=1; smtppointer->up=0; sprintf(error_msg,"respmd: SMTP server: %s is down.",smtppointer->name); if(client(SERVER_IP, SERVER_PORT, LOCALHOSTN, "ERROR", error_msg, smtppointer->name, "NONE", "DOWN", "SMTP") < 0) slog(2, "poll.c:smtp: error while sending to server."); } //did we get connection ends close(sockfd); return(0);}/****************************************************************///HTTP POLL CODE/***************************************************************/int poll_http(struct smtps_t *smtppointer){ int sockfd; // socket. int n=0,len=0,ok=0,len2=0; char read_buf[301]="\0"; char response[4]="\0"; char error_msg[100]="\0"; char *tmp_buff; char *p_tmp_buff; char *back; int return_code=0; int have_proxy=1; char *serv="80"; struct addrinfo hints, *res, *ressave; struct timeval tv; tv.tv_sec = 40; tv.tv_usec = 0; bzero(&hints, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype=SOCK_STREAM; if(smtppointer->proxy[0] != '\0') //do we have http proxy { if(smtppointer->proxy_port[0] != '\0') { have_proxy=0; } else { have_proxy=1; sprintf(error_msg,"poll.c: you didnt give me proxy port. proxy disabled."); if(client(SERVER_IP, SERVER_PORT, LOCALHOSTN, "WARNING", error_msg, LOCALHOSTN, "NONE", "NONE", "INTERNAL") < 0) slog(2, "poll.c:http: error while sending to server."); } } else { have_proxy=1; //we dont have proxy } //do we have http proxy ends if(have_proxy == 1) //we dont have proxy if have_proxy is 1 if(smtppointer->port[0] == '\0') //do we have spesial port n = getaddrinfo(smtppointer->name,serv,&hints, &res); else //we have spesial port n = getaddrinfo(smtppointer->name,smtppointer->port,&hints, &res); else //if have_proxy isnt 1 we have proxy n = getaddrinfo(smtppointer->proxy,smtppointer->proxy_port,&hints, &res); if(n != 0) { if(have_proxy == 1) //we dont have proxy if have_proxy is 1 sprintf(error_msg,"poll.c: getaddrinfo failed for HTTP server: %s, %s",smtppointer->name,gai_strerror(n)); else //if have_proxy isnt 1 we have proxy sprintf(error_msg,"poll.c: getaddrinfo failed for HTTP proxy: %s, %s",smtppointer->proxy,gai_strerror(n)); if(client(SERVER_IP, SERVER_PORT, LOCALHOSTN, "ERROR", error_msg, smtppointer->name, "NONE", "DOWN", "HTTP") < 0) slog(2, "poll.c:http: error while sending to server."); smtppointer->down=1; smtppointer->up=0; return(0); } n=0; ressave=res; do { //connect loop starts sockfd=socket(res->ai_family, res->ai_socktype, res->ai_protocol); if(sockfd<0) { slog(2,"poll.c:http: Error socket: %s",strerror(errno)); return -1; } // 'Call' the server. n = connect(sockfd, res->ai_addr,res->ai_addrlen); if(n < 0) { if(have_proxy == 1) //we dont have proxy if have_proxy is 1 // status 1up 2down send_to_server("HTTP","ERROR", 2, smtppointer, "respmd: HTTP server: %s is down. %s", smtppointer->name,strerror(errno)); else send_to_server("HTTP","ERROR", 2, smtppointer, "respmd: HTTP proxy: %s is down. %s", smtppointer->name,strerror(errno)); freeaddrinfo(ressave); return 0; } else { break; } //alarm(0); } while( (res = res->ai_next) != NULL); //connect loop end if(res == NULL) slog(2,"poll.c:http: connection to: %s failed",smtppointer->name); freeaddrinfo(ressave); //set write timeout setsockopt(sockfd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)); //set read timeout setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)); //start of HTTP talk strcat(read_buf,"GET http://"); //Make page request strcat(read_buf,smtppointer->name); if(smtppointer->proxy[0] != '\0' && smtppointer->port[0] != '\0') { strcat(read_buf,":"); strcat(read_buf,smtppointer->port); } if(smtppointer->url[0]=='\0') strcat(read_buf,"/"); else strcat(read_buf,smtppointer->url); strcat(read_buf," "); strcat(read_buf,"HTTP/1.1\r\n"); len=strlen(read_buf); len=write(sockfd, read_buf, len); //Write page request to the server read_buf[0]='\0'; len=write(sockfd, "Accept: */*\r\n",13); strcat(read_buf,"Host: "); //Write host where we get the web page strcat(read_buf,smtppointer->name); strcat(read_buf,"\r\n"); len=strlen(read_buf); len=write(sockfd, read_buf, len); read_buf[0]='\0'; strcat(read_buf,"User-Agent: "); //Make User-Agent string strcat(read_buf,"Mozilla/4.0 (Compatible; Linux_mon Service Response monitor "); strcat(read_buf,VERSION); strcat(read_buf,")\r\n"); len=strlen(read_buf); len=write(sockfd, read_buf,len); len=write(sockfd, "Connection: close\r\n",19); len=write(sockfd, "\r\n",2); read_buf[0]='\0'; len=read(sockfd, read_buf, 300); // Read from the server. if(len<0) { len = send_to_server("HTTP","ERROR", 2, smtppointer, "poll.c: Error while reading from: %s %s", smtppointer->name,strerror(errno)); if(len!=0) slog(2,"poll.c:http: Error while reading from: %s %s",smtppointer->name,strerror(errno)); close(sockfd); return(0); } if((p_tmp_buff=malloc(len))==NULL){ //lets get memory for editing response slog(2,"poll.c:http: Error Out of memory."); return(0); } tmp_buff = p_tmp_buff; back = strncpy(tmp_buff,read_buf,len); //copy response to tmp variable if(back==NULL) { slog(2,"poll.c:http: Error strncpy."); return(0); } len=0; while(len2!=1) //find first space { if(*(tmp_buff+len)==' ') len2++; len++; } tmp_buff+=len; //cut out protocol version len2=0; len=0; while(len2!=1) //find second space { if(*(tmp_buff+len)==' ') len2++; len++; } response[0]='\0'; strncpy(response,tmp_buff,len); //copy return code to response response[3]='\0'; return_code = atoi(response); response[0]='\0'; if(return_code==200 || return_code==201 || return_code==202) //did we get the OK response ok++; len=4; tmp_buff+=len; //cut out response code len=0; len2=0; while(len2!=1) //find end of the first line of response { if(*(tmp_buff+len)==' ') len2++; len++; } strncpy(response,tmp_buff,len); //copy return code to response response[2]='\0'; if(strcmp(response,"OK")==0) //did we get the OK response ok++; //Lets process the results if(ok==2)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -