1 /* testping.c - Test ping for debugging
2 * Copyright (c) 2006,2012 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved.
3 * This is confidential unpublished proprietary source code of the author.
4 * NO WARRANTY, not even implied warranties. Contains trade secrets.
5 * Distribution prohibited unless authorized in writing. See file COPYING.
6 * Special grant: testping.c may be used with zxid open source project under
7 * same licensing terms as zxid itself.
8 * $Id$
9 *
10 * 15.4.2006, created over Easter holiday --Sampo
11 * 16.8.2012, modified license grant to allow use with ZXID.org --Sampo
12 */
13
14 #include "platform.h"
15 #include "errmac.h"
16 #include "akbox.h"
17 #include "hiios.h"
18
19 #include <ctype.h>
20 #include <memory.h>
21 #include <netinet/in.h> /* htons(3) and friends */
22
23 #define MIN_PING 5
24 #define MAX_PING 10
25
26 /* Called by: test_ping */
test_ping_reply(struct hi_thr * hit,struct hi_io * io,struct hi_pdu * req)27 void test_ping_reply(struct hi_thr* hit, struct hi_io* io, struct hi_pdu* req)
28 {
29 int i;
30 int n = req->ap - req->m;
31 struct hi_pdu* resp = hi_pdu_alloc(hit,"test_ping_reply");
32 if (!resp) { hi_dump(hit->shf); NEVERNEVER("*** out of pdus in bad place %d", n); }
33 memcpy(resp->ap, req->m, n);
34 resp->ap += n;
35 for (i = n-1; i; --i) /* all but the first letter */
36 resp->m[i] = toupper(resp->m[i]);
37 D("test_ping(%.*s) %d chars", n, resp->m, n);
38 hi_send(hit, io, 0, req, resp);
39 }
40
41 /* Called by: */
test_ping(struct hi_thr * hit,struct hi_io * io)42 void test_ping(struct hi_thr* hit, struct hi_io* io)
43 {
44 struct hi_pdu* req = io->cur_pdu;
45 int n = req->ap - req->m;
46
47 if (n < MIN_PING) { /* too little, need more */
48 req->need = MIN_PING - n;
49 return;
50 }
51
52 /* Got enough. Associate request with frontend. */
53
54 hi_add_to_reqs(hit, io, req, MIN_PING);
55 test_ping_reply(hit, io, req);
56 }
57
58 /* EOF -- testping.c */
59