OpenVMS Source-Code Demos

SMTP-CHECK.C

#define MODULE_NAME	SMTPCHECK
#define MODULE_IDENT	"V1.0"
/*===========================================================================
 *  Program:	SMTP-CHECK.C
 *  Author:	Hunter Goatley (Process Software Corporation)
 *  Date:	October 17, 2006
 *  Purpose:	A quick and dirty program to connect to a remote SMTP
 *		server, issue a HELO, then QUIT.
 *  VMS Usage:  yada :== $CSMIS$USER3:[ADMCSM.NEIL]SMTP-CHECK.exe
 *              yada 127.0.0.1 80 *
 *===========================================================================
 */
#ifdef VMS
#ifdef __DECC
#pragma module MODULE_NAME MODULE_IDENT
#else
#ifdef vaxc
#module MODULE_NAME MODULE_IDENT
#endif /* vaxc */
#endif /* __DECC */
#include <in.h>
#include <ssdef.h>
#include <netdb.h>
#include <unixio.h>
#include <socket.h>
#define	EXITER(_sts)	return(_sts)
#else
#ifdef _WIN32
#include <winsock2.h>
#define SS$_ABORT 1

/* Define our macro to exit the appliction. */
#define	EXITER(_sts)	\
    WSACleanup() ;	\
    return(_sts) ;
#endif /* _WIN32 */
#endif /* VMS */
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int sock, x;
    struct sockaddr_in sock2_name;
    struct sockaddr_in inet_sa;
    struct hostent *hostentptr;
    char inbuff[2000];
    char target_host[255];
    char target_domain[255];
    char my_host[255];
    char tmp[255];
    char *ptr;
    short wVer;

#ifdef _WIN32
    /*
     *  For WinSock, you have to call WSAStartup first
     */
    WSADATA wsaData;
    wVer = MAKEWORD(2,0) ;              	// request WinSock version 2.0
    if (WSAStartup(wVer,&wsaData) != 0)  {  	// if startup failed
        perror("wsastartup") ;
	EXITER(SS$_ABORT) ;
    }
#endif 
    if (gethostname(my_host, sizeof(my_host)))  {
	perror("error obtaining my name") ;
	EXITER(SS$_ABORT) ;
    }
    /*
    **  Assign channel to 1st BG device.
    */
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
	perror("error creating socket");
	EXITER(SS$_ABORT);
	}

    if (argc != 2)
	{
	printf ("Usage: smtp-check hostname\n");
	exit (1);
	}

    strcpy (target_host, argv[1]);

    printf ("Checking SMTP server on host %s....\n", target_host);

    if ((hostentptr = gethostbyname ((char *) target_host)) == NULL)
	{
	perror( "gethostbyname");
	EXITER(SS$_ABORT);
	}

    sock2_name.sin_family = hostentptr->h_addrtype;
    sock2_name.sin_port = htons(25);		/* SMTP server */
    sock2_name.sin_addr = * ((struct in_addr *) hostentptr->h_addr);
    /*
    **  Connect to the SMTP server
    */
    if (!(connect(sock, (struct sockaddr *)&sock2_name,
		sizeof (sock2_name))) == 0)
	{
	perror("connect");
	EXITER(SS$_ABORT);
	}
    /*
    **   Read the 220 greeting banner
    */
    x = recv (sock, (char *) &inbuff, sizeof(inbuff), 0);
    if (x < 0) {
	perror("recv");
	EXITER(SS$_ABORT);
	}
    /*
    **  Make sure we got the 220 banner.  If so, send out the HELO command.
    */
    if (strstr(inbuff, "220") != 0) {
	sprintf (tmp, "helo %s\015\n", my_host);
	send (sock, (char *) tmp, strlen(tmp), 0);
	}
    /*
    **  Wait for the response from our HELO.
    */
    x = recv (sock, (char *) &inbuff, sizeof(inbuff), 0);
    if (x < 0) {
	perror("recv");
	EXITER(SS$_ABORT);
	}
    /*
    **  If we got a 250, all is well; send QUIT.
    */
    if (strstr(inbuff, "250") != 0) {
	printf("Successfully issued HELO command to %s\n", target_host);
	send (sock, "quit\015\n", 6, 0);
	}
    else
	{
	printf("Error receiving HELO 250 from remote host\n");
	printf("  reply: %s\n", inbuff);
	EXITER(SS$_ABORT);
	}
    /*
    **  Wait for the QUIT ack.
    */
    x = recv (sock, (char *) &inbuff, sizeof(inbuff), 0);
    if (x < 0) {
	perror("recv");
	EXITER(SS$_ABORT);
	}
    /*
    **  Make sure we got the 221 (successful ack).
    */
    if ((x > 0) && (strstr(inbuff, "221") == 0)) {
	printf("Error receiving QUIT 221 from remote host\n");
	inbuff[x] = '\0';
	printf("Buffer is: %s\n", inbuff);
	}
    close(sock);
    EXITER(0) ;
}

Back to OpenVMS
Back to OpenVMS Demo Index
Back to Home
Neil Rieck
Kitchener - Waterloo - Cambridge, Ontario, Canada.