/* * Copyright (C), 2000-2005 by the monit project group. * All Rights Reserved. * * 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. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #ifdef HAVE_STRING_H #include #endif #include "protocol.h" /** * NTP (Network time procol) version 3 test * * @author Michel Marti, * * @version \$Id: ntp3.c,v 1.1 2005/01/28 17:55:02 martinp Exp $ * */ /* ------------------------------------------------------------- Definitions */ #define NTPLEN 48 #define NTP_LEAP_NOWARN 3 /** Leap Indicator: No warning */ #define NTP_LEAP_NOTSYNC 3 /** Leap Indicator: Clock not synchronized */ #define NTP_VERSION 3 /** Version Number: 3 */ #define NTP_MODE_CLIENT 3 /** Mode: Client */ #define NTP_MODE_SERVER 4 /** Mode: Server */ /* ------------------------------------------------------------------ Public */ int check_ntp3(Socket_T s) { int br; char ntpRequest[NTPLEN]; char ntpResponse[NTPLEN]; ASSERT(s); memset(ntpRequest, 0, NTPLEN); memset(ntpResponse, 0, NTPLEN); /* Prepare NTP request. The first octet consists of: bits 1-2 ... Leap Indicator bits 3-5 ... Version Number bits 6-8 ... Mode */ ntpRequest[0]= (NTP_LEAP_NOTSYNC << 6) | (NTP_VERSION << 3) | (NTP_MODE_CLIENT); /* Send request to NTP server */ if( (br= socket_write(s, ntpRequest, NTPLEN)) <= 0 ) { log("NTP: error sending NTP request -- %s\n", STRERROR); return FALSE; } /* Receive and validate response */ if( (br= socket_read(s, ntpResponse, NTPLEN)) <= 0) { log("NTP: did not receive answer from server -- %s\n", STRERROR); return FALSE; } if( br != NTPLEN ) { log("NTP: Received %d bytes from server, expected %d bytes\n", br, NTPLEN); return FALSE; } /* Compare NTP response. The first octet consists of: bits 1-2 ... Leap Indicator bits 3-5 ... Version Number bits 6-8 ... Mode */ if( ntpResponse[0] != ( (NTP_LEAP_NOWARN << 6) & (NTP_VERSION << 3) & (NTP_MODE_SERVER) ) ) { log("NTP: Server response error\n"); return FALSE; } return TRUE; }