summaryrefslogtreecommitdiffstats
path: root/tclcheckdns/checkdns.C
blob: f3c0590ced2698a4bd6e545e3f2615968edf4e82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (C) 1999-2015
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
#include <netdb.h>

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

#include <tcl.h>

#define SZ_LINE 2048
static int debug =0;

// alarm signal handler
static sigjmp_buf alarmbuf;
static void alarmHandler(int dummy)
{
  siglongjmp(alarmbuf, 1);
}

extern "C" {
  int Tclcheckdns_Init(void *vinterp);
};

static int checkdns(char *name, int delay, int cflag)
{
  int result =0;
 
  // get temp name, look for port
  char tbuf[SZ_LINE];
  unsigned int port=80;
  if (name && *name) {
    char* ss=NULL;
    strncpy(tbuf, name, SZ_LINE-1);
    if ((ss=strchr(tbuf, ':'))) {
      *ss = '\0';
      port = atoi(ss+1);
    }
  }
  else
    *tbuf = '\0';

  // start the alarm, if necessary
  struct sigaction sigact, osigact;
  if (delay>0) {
    if (sigsetjmp(alarmbuf, 1)) {
      if (debug)
	cerr << "alarm activated" << endl;

      result= 1;
      goto done;
    }
    else {
      sigact.sa_handler = alarmHandler;
      sigemptyset(&sigact.sa_mask);
      sigact.sa_flags = 0;
      sigaction(SIGALRM, &sigact, &osigact);

      alarm(delay);
    }
  }

  // get name of host we are interested in
  char host[SZ_LINE];
  if (*tbuf)
    strncpy(host, tbuf, SZ_LINE-1);
  else {
    // use current host
    if (gethostname(host, SZ_LINE) < 0) {
      result =2;
      goto done;
    }
    else if (debug)
      cerr << "gethostname: " << host << endl;
  }

  // try to get info on this host
  struct hostent* h;
  if (!(h=gethostbyname(host))) {
    result =3;
    goto done;
  }
  else if (debug)
    cerr << "gethostbyname: " << h->h_name << '(' << port << ')' << endl;

  // connect, if necessary
  if (cflag) {
    unsigned int ip;
    memcpy(&ip, h->h_addr_list[0], (size_t)h->h_length);
    ip = ntohl(ip);
    int fd;
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
      result =4;
      goto done;
    }
    struct sockaddr_in sock_in;
    memset((char *)&sock_in, 0, sizeof(sock_in));
    sock_in.sin_family = AF_INET;
    sock_in.sin_addr.s_addr = htonl(ip);
    sock_in.sin_port = htons(port);
    int got = connect(fd, (struct sockaddr *)&sock_in, sizeof(sock_in));
    close(fd);
    if (got < 0) {
      result =5;
      goto done;
    }
    else if (debug)
      cerr << "connect succeeded" << endl;
  }

 done:
  if (delay) {
    alarm(0);
    sigaction(SIGALRM, &osigact, NULL);
  }

  return result;
}

static int TclcheckdnsCmd(ClientData clientData, Tcl_Interp *interp,
			  int objc, Tcl_Obj *CONST objv[])
{
  if (objc < 3) {
    Tcl_WrongNumArgs(interp, 1, objv, "host delay [connect]");
    return TCL_ERROR;
  }

  char* host =NULL;
  char* ss = Tcl_GetStringFromObj(objv[1], NULL);
  if (ss && *ss)
    host = ss;

  int delay=5;
  char* tt = Tcl_GetStringFromObj(objv[2], NULL);
  if (tt && *tt)
    delay = atoi(tt);

  int cflag=0;
  if (objc >= 4) {
    char* uu = Tcl_GetStringFromObj(objv[3], NULL);
    if (uu && *uu)
      cflag = atoi(uu);
  }

  ostringstream str;
  str << checkdns(host, delay, cflag) << ends;
  Tcl_AppendResult(interp, str.str().c_str(), NULL);

  return TCL_OK;
}

int Tclcheckdns_Init(void *vinterp)
{
  Tcl_Interp *interp = (Tcl_Interp *)vinterp;

  if (Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0) == NULL)
    return TCL_ERROR;

  Tcl_CreateObjCommand(interp, "checkdns", TclcheckdnsCmd,
		       (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);

  if (Tcl_PkgProvide(interp, PACKAGE_NAME, PACKAGE_VERSION) != TCL_OK)
    return TCL_ERROR;

  return TCL_OK;
}