summaryrefslogtreecommitdiff
path: root/other/b-scan/tmp/modules/mod_ping.c
blob: 73a90a413ee5d65a2ca5fe11c5241709b0677e99 (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
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
/*
 * ping-module for bscan.
 * IDEA: add record-route and source-route feature
 *       and -p pattern [where can we save our time-struct then ?
 */

#include <bscan/bscan.h>
#include <bscan/module.h>
#include <bscan/system.h>
#include <stdio.h>


#ifndef MOD_NAME
#define MOD_NAME	"mod_ping"
#endif

static int process_rcv(struct _opt *);

static int isinit=0;
/*
 * some variables from the binary-process
 */
extern int dlt_len;
extern u_char *align_buf;
extern unsigned short ip_options;
extern struct ip *ip;
extern struct Ether_header *eth;
extern u_int plen, pcaplen;
extern struct timeval *pts;

struct _mopt
{
   int type;
   int size;
} static mopt;

/*
 * static functions prototypes
 */
static int mdo_opt(int, char **, struct _opt *);
static void init_vars(struct _opt *);

/*
 * print out usage informations
 */
void
musage()
{
    printf ("\n"MOD_NAME"\n");
    printf (" -t (echo|time) -s <size>, size of ping-data [default 56].\n");
}


/*
 * return 0 on success, != 0 on failure
 */
int
init(char **modname, int argc, char *argv[], struct _opt *opt)
{
#ifdef DEBUG
	printf("MODULE INIT\n");
#endif
	if (isinit)
		return(-1);

	*modname = MOD_NAME;
	isinit = 1;
  	init_vars(opt);

	if (mdo_opt(argc, argv, opt) != 0)
		return(-1);

	return(0);
}

/*
 * fini-routine. called on cleanup 
 */
int
fini()
{
#ifdef DEBUG
	printf("MODULE FINI\n");
#endif
	return(0);
}


/*
 * Module entry point [entry]
 * RMOD_OK: everything allright. send  the packet out [if first]
 *          or do nothing [MOD_RCV].
 * RMOD_SKIP: proceed with next IP without sending out the packet.
 */
int
callmdl(int entry, struct _opt *opt)
{
#ifdef DEBUG
	printf("MODULE CALLMDL\n");
#endif
	if (entry == MOD_FIRSTPKG)
	{
                add_icmpping (opt->packet + ETH_SIZE + IP_SIZE,	mopt.size, mopt.type);
		add_iphdr (opt->packet + ETH_SIZE, IPPROTO_ICMP, &opt->nt, ICMP_SIZE + mopt.size);
		opt->pkg_len = IP_SIZE + ICMP_SIZE + mopt.size;
		return(RMOD_OK);
	}

	if (entry == MOD_RCV)
		process_rcv(opt);

	return(RMOD_OK);
}


/*
 ***********************************************************
 *  Our OWN/static functions for THIS module               *
 ***********************************************************
 */

/*
 * initialize all local variables.
 * We use some 'unused' variables of the masterprogramm
 */
static void
init_vars(struct _opt *opt)
{
    mopt.size = ICMP_ECHO;
    mopt.size = 56;
}


/*
 * LOCAL/STATIC function, only available in the module
 * return 0 on success, != 0 on failure
 */
static int
mdo_opt(int argc, char *argv[], struct _opt *opt)
{
    extern char *optarg;
    /*extern int optind, opterr, optopt;*/
    int c;

    while ((c = getopt (argc, argv, "t:s:")) != -1)
    {
	switch (c)
	{
	case 't':
	   if (strcasecmp (optarg, "echo") == 0)
	     mopt.type = ICMP_ECHO;
	   else if (strcasecmp (optarg, "time") == 0)
	     mopt.type = ICMP_TSTAMP;
	   else
	     return (-1);
	   break;
	case 's':
	   mopt.size = atoi(optarg);	
	   break;
        case ':':
	    fprintf(stderr, "missing parameter\n");
	    return(-1);
        default:
	    return(-1);
	}
    }
    return(0);
}


/*
 * handle incoming icmp ECHO_REPLY packages
 */
static int
process_rcv(struct _opt *opt)
{
    struct icmp *icmp;
    struct timeval now;
    double rrt;

    if (ip->ip_p != IPPROTO_ICMP)
	return(0);

    if (plen < dlt_len + IP_SIZE + ip_options + sizeof(*icmp))
	return(0);	/* invalid size */

   icmp = (struct icmp *) (align_buf + IP_SIZE + ip_options);

//   if ((icmp->icmp_type != 0) || (icmp->icmp_code != 0))
//	return(0);
	
   memcpy(&now, pts, sizeof(now));
   time_diff((struct timeval *)icmp->icmp_dun.id_data, &now);
   rrt = now.tv_sec * 1000.0 + now.tv_usec / 1000.0;

   printf("%d bytes from %s: icmp_seq=%u ttl=%d time=%.3f ms\n",
	(int)(plen - dlt_len - IP_SIZE - ip_options),
	int_ntoa(ip->ip_src.s_addr), icmp->icmp_hun.ih_idseq.icd_seq,
	ip->ip_ttl, rrt);

   return(0);
   
}