dwp.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C), 2000-2003 by Contributors to the monit codebase. 
00003  * All Rights Reserved.
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include <stdio.h>
00023 #include <errno.h>
00024 
00025 #ifdef HAVE_SYS_TYPES_H
00026 #include <sys/types.h>
00027 #endif
00028 
00029 #include <sys/socket.h>
00030 
00031 #ifdef HAVE_STRING_H
00032 #include <string.h>
00033 #endif
00034 
00035 #ifdef HAVE_NETINET_IN_H
00036 #include <netinet/in.h>
00037 #endif
00038 
00039 #ifdef HAVE_ARPA_INET_H
00040 #include <arpa/inet.h>
00041 #endif
00042 
00043 
00044 #include "protocol.h"
00045 
00063 int check_dwp(Port_T p) {
00064 
00065 #define REQ_LENGTH  1024
00066 
00067   int n;
00068   int status;
00069   char buf[STRLEN];
00070   char proto[STRLEN];
00071   char request[REQ_LENGTH];
00072 
00073   ASSERT(p);
00074 
00075   snprintf(request, REQ_LENGTH,
00076            "HEAD / HTTP/1.1\r\n"
00077            "Connection: close\r\n\r\n");
00078 
00079   if(port_send(p, request, strlen(request), 0) < 0) {
00080     log("DWP: error sending data -- %s\n", STRERROR);
00081     return FALSE;
00082   }
00083   
00084   if(port_recv(p, buf, sizeof(buf), 0) <= 0) {
00085     log("DWP: error receiving data -- %s\n", STRERROR);
00086     return FALSE;
00087   }
00088 
00089   chomp(buf);
00090 
00091   n= sscanf(buf, "%s %d", proto, &status);
00092   if(n!=2 || (status >= 400)) {
00093     log("DWP error: %s\n", buf);
00094     return FALSE;
00095   }
00096 
00097   return TRUE;
00098   
00099 }
00100