File: newsresp.c

package info (click to toggle)
inn2 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,720 kB
  • ctags: 8,983
  • sloc: ansic: 92,499; sh: 13,509; perl: 12,921; makefile: 2,985; yacc: 842; python: 342; lex: 255
file content (297 lines) | stat: -rw-r--r-- 7,444 bytes parent folder | download | duplicates (4)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* newsresp.c - EUnet - bilse */

/*
 * From: Koen De Vleeschauwer <koen@eu.net>
 * Subject: Re: innfeed-users: innfeed: measuring server response time
 * To: jeff.garzik@spinne.com (Jeff Garzik)
 * Date: Tue, 13 May 1997 16:33:27 +0200 (MET DST)
 * Cc: innfeed-users@vix.com
 * 
 * > Is there an easy way to measure server response time, and print it out
 * > on the innfeed status page?  Cyclone's nntpTime measures login banner
 * > response time and an article add and lookup operation. 
 * > 
 * > It seems to me that innfeed could do something very similar.  It could
 * > very easily sample gettimeofday() or Time.Now to determine a remote
 * > server's average response time for lookups, lookup failures, article
 * > send throughput, whatever.
 * > 
 * > These statistics might be invaluable to developers creating advanced
 * > connection and article delivery algorithms.  If I knew, for example,
 * > that a site's article send/save throughput was really fast, but history
 * > lookups were really slow, my algorithm could reserve a channel or two
 * > for TAKETHIS-only use.
 * 
 * We use a stand-alone program which opens up an additional nntp channel
 * from time to time and takes a peek at the various response times.
 * It's also interesting to tune one's own box.
 * I've included the source code; please consider this supplied 'as is';
 * bugs and features alike. SunOS, Solaris and Irix ought to be ok;
 * eg. gcc -traditional -o newsresp ./newsresp.c -lnsl -lsocket on S0laris.
 * If a host has an uncommonly long banner you may have to change a constant 
 * somewhere; forget. Please note one has to interpret the output; 
 * eg. whether one is measuring rtt or history lookup time.
 * 
 * Basic usage is:
 * news 1 % newsresp -n 5 news.eu.net
 * ---------------------------------
 * news.eu.net is 134.222.90.2 port 119
 *  elap  diff
 *   0.0   0.0  Connecting ...
 *   0.0   0.0  OK, waiting for prompt
 *   0.0   0.0  <<< 200 EU.net InterNetNews server INN 1.5.1 17-Dec-1996 re [...]
 *   0.0   0.0  >>> ihave <244796399@a>
 *   0.0   0.0  <<< 335 
 *   0.0   0.0  >>> .
 *   0.0   0.0  <<< 437 Empty article 
 *   0.0   0.0  >>> ihave <244796398@a>
 *   0.0   0.0  <<< 335 
 *   0.0   0.0  >>> .
 *   0.0   0.0  <<< 437 Empty article 
 *   0.0   0.0  >>> ihave <244796397@a>
 *   0.0   0.0  <<< 335 
 *   0.0   0.0  >>> .
 *   0.0   0.0  <<< 437 Empty article 
 *   0.0   0.0  >>> ihave <244796396@a>
 *   0.1   0.0  <<< 335 
 *   0.1   0.0  >>> .
 *   0.1   0.0  <<< 437 Empty article 
 *   0.1   0.0  >>> ihave <244796395@a>
 *   0.1   0.0  <<< 335 
 *   0.1   0.0  >>> .
 *   0.1   0.0  <<< 437 Empty article 
 *   0.1   0.0  >>> quit
 *   0.1   0.0  <<< 205 . 
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>

#define NNTPPORT 119
struct sockaddr_in sock_in;
int sock;
char buf[1024];

main(argc,argv)
int argc;
char *argv[];
{
  int errflg = 0, c;
  extern char *optarg;
  extern int optind;
  struct hostent *host;
  unsigned long temp;
  unsigned numart = 1;
  struct protoent *tcp_proto;
  char **whoP;

  while ( (c = getopt(argc,argv,"n:")) != -1 )
    switch ( c ) {
    case 'n': sscanf(optarg,"%u",&numart); break;
    default : errflg++;
  }
  if ( numart == 0 || optind == argc )
    errflg++;
  if ( errflg ) {
    fprintf(stderr,"Usage: %s [-n articles] host ...\n",argv[0]);
    exit(1);
  }

  if ( (tcp_proto = getprotobyname("tcp")) == 0 )
    fatal("getprotobyname");
  for ( whoP = argv+optind; *whoP != 0; whoP++ ) {
    if ( (sock = socket(PF_INET,SOCK_STREAM,tcp_proto->p_proto)) < 0 )
      fatal("socket");
    temp = inet_addr(*whoP);
    if ( temp != (unsigned long) -1 ) {
      sock_in.sin_addr.s_addr = temp;
      sock_in.sin_family = AF_INET;
    }
    else {
      host = gethostbyname(*whoP);
      if ( host ) {
	sock_in.sin_family = host->h_addrtype;
	memcpy(&sock_in.sin_addr,host->h_addr,host->h_length);
      }
      else {
	fprintf(stderr,"gethostbyname can't find %s\n",*whoP);
	exit(1);
      }
    }
    sock_in.sin_port = htons(NNTPPORT);
    printf("---------------------------------\n%s is %s port %d\n",
	       *whoP,inet_ntoa(sock_in.sin_addr),ntohs(sock_in.sin_port));
    punt(numart);
    close(sock);
  }
}

error(what)
char *what;
{
  ptime(); fflush(stdout);
  perror(what);
}

fatal(what)
char *what;
{
  error(what);
  exit(2);
}

ierror(how,what)
char *how, *what;
{
  printf("Expected %s, bailing out.\n",how);
}

ifatal(how,what)
char *how, *what;
{
  ierror(how,what);
  exit(1);
}

unsigned do_time(start)
unsigned start;
{
  struct timeval now;

  gettimeofday(&now,(struct timezone *)0);
  return ( now.tv_sec*1000 + now.tv_usec/1000 - start );
}


unsigned start, elapsed, diff;

ptime()
{
  diff = elapsed;
  elapsed = do_time(start);
  diff = elapsed - diff;
  printf("%5.1f %5.1f  ",((float)elapsed)/1000.0,((float)diff)/1000.0);
}

massagebuff(bread,buf)
int bread;
char *buf;
{
  char *p;

  if ( bread > 55 )
    strcpy(buf+55," [...]\n");
  else
    buf[bread] = '\0';
  for ( p = buf; *p != '\0'; )
    if ( *p != '\r' )  /* We like to do it RISC style. */
      p++;
    else {
      *p = ' ';
      p++;
    }
}

punt(numart)
int numart;
{
  static char ihave[32],
	      dot[] = ".\r\n",
	      quit[] = "quit\r\n";
  struct timeval start_tv;
  int bread;

  printf(" elap  diff\n");
  diff = elapsed = 0;
  gettimeofday(&start_tv,(struct timezone *)0);
  start = start_tv.tv_sec*1000 + start_tv.tv_usec/1000;

  ptime();
  printf("Connecting ...\n");
  if ( connect(sock,(struct sockaddr*)&sock_in,sizeof(sock_in)) < 0 ) {
    error("connect");
    return(-1);
  }
  ptime();
  printf("OK, waiting for prompt\n");

  if ( (bread=read(sock,buf,sizeof(buf))) < 0 ) {
    error("read socket");
    return(-1);
  }
  massagebuff(bread,buf);
  ptime();
  printf("<<< %s",buf);
  if ( strncmp(buf,"200",3) != 0 && strncmp(buf,"201",3) != 0 ) {
    ierror("200 or 201",buf);
    return(-1);
  }

  do {
    snprintf(ihave,sizeof(ihave),"ihave <%u@a>\r\n",start+numart);
    ptime();
    printf(">>> %s",ihave);
    if ( write(sock,ihave,strlen(ihave)) != strlen(ihave) ) {
      error("write socket");
      return(-1);
    }

    if ( (bread=read(sock,buf,sizeof(buf))) < 0 ) {
      error("read socket");
      return(-1);
    }
    massagebuff(bread,buf);
    ptime();
    printf("<<< %s",buf);
    if ( strncmp(buf,"335",3) != 0 && strncmp(buf,"435",3) != 0 ) {
      ierror("335 or 435 ",buf);
      return(-1);
    }

    if ( strncmp(buf,"335",3) == 0 ) {
      ptime();
      printf(">>> %s",dot);
      if ( write(sock,dot,sizeof(dot)-1) != sizeof(dot)-1 ) {
	error("write socket");
	return(-1);
      }

      if ( (bread=read(sock,buf,sizeof(buf))) < 0 ) {
	error("read socket");
	return(-1);
      }
      massagebuff(bread,buf);
      ptime();
      printf("<<< %s",buf);
      if ( strncmp(buf,"437",3) != 0 && strncmp(buf,"235",3) != 0 ) {
	ierror("437 or 235",buf);
	return(-1);
      }
    }
  } while ( --numart != 0 );

  ptime();
  printf(">>> %s",quit);
  if ( write(sock,quit,sizeof(quit)-1) != sizeof(quit)-1 ) {
    error("write socket");
    return(-1);
  }

  if ( (bread=read(sock,buf,sizeof(buf))) < 0 ) {
    error("read socket");
    return(-1);
  }
  massagebuff(bread,buf);
  ptime();
  printf("<<< %s",buf);
  if ( strncmp(buf,"205",3) != 0 ) {
    ierror("205",buf);
    return(-1);
  }
  return(0);
}