File: bptrace.c

package info (click to toggle)
ion 3.2.1%2Bdfsg-1.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 23,768 kB
  • ctags: 11,049
  • sloc: ansic: 141,798; sh: 22,848; makefile: 7,818; python: 1,638; sql: 311; perl: 197; awk: 178; xml: 50; java: 19
file content (249 lines) | stat: -rw-r--r-- 6,112 bytes parent folder | download | duplicates (2)
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
/*
	bptrace.c:	network trace utility, using BP status reports.
									*/
/*									*/
/*	Copyright (c) 2008, California Institute of Technology.		*/
/*	All rights reserved.						*/
/*	Author: Scott Burleigh, Jet Propulsion Laboratory		*/
/*									*/

#define _GNU_SOURCE
#include <bpP.h>

static void	setFlag(int *srrFlags, char *arg)
{
	if (strcmp(arg, "rcv") == 0)
	{
		(*srrFlags) |= BP_RECEIVED_RPT;
	}

	if (strcmp(arg, "ct") == 0)
	{
		(*srrFlags) |= BP_CUSTODY_RPT;
	}

	if (strcmp(arg, "fwd") == 0)
	{
		(*srrFlags) |= BP_FORWARDED_RPT;
	}

	if (strcmp(arg, "dlv") == 0)
	{
		(*srrFlags) |= BP_DELIVERED_RPT;
	}

	if (strcmp(arg, "del") == 0)
	{
		(*srrFlags) |= BP_DELETED_RPT;
	}
}

static void	setFlags(int *srrFlags, char *flagString)
{
	char	*cursor = flagString;
	char	*comma;

	while (1)
	{
		comma = strchr(cursor, ',');
		if (comma)
		{
			*comma = '\0';
			setFlag(srrFlags, cursor);
			*comma = ',';
			cursor = comma + 1;
			continue;
		}

		setFlag(srrFlags, cursor);
		return;
	}
}

static int	run_bptrace(char *ownEid, char *destEid, char *reportToEid,
			int ttl, char *svcClass, char *trace, char *flags)
{
	int		priority = 0;
	BpExtendedCOS	extendedCOS = { 0, 0, 0 };
	BpCustodySwitch	custodySwitch = NoCustodyRequested;
	int		srrFlags = 0;
	BpSAP		sap;
	Sdr		sdr;
	Object		newBundle;

	if (!bp_parse_class_of_service(svcClass, &extendedCOS, &custodySwitch,
			&priority))
	{
		putErrmsg("Invalid class of service for bptrace.", svcClass);
		return 0;
	}

	if (flags)
	{
		setFlags(&srrFlags, flags);
	}

	if (bp_attach() < 0)
	{
		putErrmsg("bptrace can't attach to BP.", NULL);
		return 0;
	}

	if (bp_open(ownEid, &sap) < 0)
	{
		putErrmsg("bptrace can't open own endpoint.", ownEid);
		return 0;
	}

        if (*trace == '@')
        {
            Object      fileRef;
            struct stat	statbuf;
            int		aduLength;
            Object	traceZco;
            char        *fileName;

            fileName = trace + 1;
            if (stat(fileName, &statbuf) < 0)
            {
                    bp_close(sap);
                    putSysErrmsg("Can't stat the file", fileName);
                    return 0;
            }

            aduLength = statbuf.st_size;
            sdr = bp_get_sdr();
            CHKZERO(sdr_begin_xn(sdr));
            fileRef = zco_create_file_ref(sdr, fileName, NULL);
            if (sdr_end_xn(sdr) < 0 || fileRef == 0)
            {
                    bp_close(sap);
                    putErrmsg("bptrace can't create file ref.", fileName);
                    return 0;
            }

            traceZco = ionCreateZco(ZcoFileSource, fileRef, 0, aduLength, NULL);
            if (traceZco == 0)
            {
                    putErrmsg("bptrace can't create ZCO.", fileName);
            }
	    else
	    {
		if (bp_send(sap, destEid, reportToEid, ttl, priority,
				custodySwitch, srrFlags, 0, &extendedCOS,
				traceZco, &newBundle) <= 0)
		{
			putErrmsg("bptrace can't send file in bundle.",
					fileName);
		}
            }

            CHKZERO(sdr_begin_xn(sdr));
            zco_destroy_file_ref(sdr, fileRef);
            if (sdr_end_xn(sdr) < 0)
            {
                    putErrmsg("bptrace can't destroy file reference.", NULL);
            }
        }
        else
        {
            int		msgLength = strlen(trace) + 1;
            Object	msg;
            Object	traceZco;
            
            sdr = bp_get_sdr();
            CHKZERO(sdr_begin_xn(sdr));
            msg = sdr_malloc(sdr, msgLength);
            if (msg)
            {
            	sdr_write(sdr, msg, trace, msgLength);
	    }

	    if (sdr_end_xn(sdr) < 0)
	    {
                    bp_close(sap);
                    putErrmsg("No space for bptrace text.", NULL);
                    return 0;
            }

            traceZco = ionCreateZco(ZcoSdrSource, msg, 0, msgLength, NULL);
            if (traceZco == 0)
            {
                    putErrmsg("bptrace can't create ZCO", NULL);
            }
	    else
	    {
            	if (bp_send(sap, destEid, reportToEid, ttl, priority,
				custodySwitch, srrFlags, 0, &extendedCOS,
				traceZco, &newBundle) <= 0)
            	{
                    putErrmsg("bptrace can't send message.", NULL);
		}
            }
        }

	bp_close(sap);
	bp_detach();
	return 0;
}

#if defined (VXWORKS) || defined (RTEMS) || defined (bionic)
int	bptrace(int a1, int a2, int a3, int a4, int a5,
		int a6, int a7, int a8, int a9, int a10)
{
	char	*ownEid = (char *) a1;
	char	*destEid = (char *) a2;
	char	*traceEid = (char *) a3;
	int	ttl = a4 ? strtol((char *) a4, NULL, 0) : 0;
	char	*classOfService = (char *) a5;
	char	*trace = (char *) a6;
	char	*flagString = (char *) a7;

	if (ownEid == NULL || destEid == NULL || classOfService == NULL
	|| trace == NULL)
	{
		PUTS("Missing argument(s) for bptrace.  Ignored.");
		return 0;
	}
#else
int	main(int argc, char **argv)
{
	char	*ownEid;
	char	*destEid;
	char	*traceEid;
	int	ttl;
	char	*classOfService;
	char	*trace;
	char	*flagString;

	if (argc < 7)
	{
		PUTS("Usage:  bptrace <own EID> <destination EID> <report-to \
EID> <time to live (seconds)> <class of service> '<trace text>' \
[<status report flag string>]");
		PUTS("\tclass of service: " BP_PARSE_CLASS_OF_SERVICE_USAGE);
		PUTS("\tStatus report flag string is a sequence of status \
report flags separated by commas, with no embedded whitespace.");
		PUTS("\tEach status report flag must be one of the following: \
rcv, ct, fwd, dlv, del.");
		PUTS("\tThe status reported in each bundle status report \
message will be the sum of the applicable status flags:");
		PUTS("\t\t 1 = bundle received (rcv)");
		PUTS("\t\t 2 = bundle custody accepted (ct)");
		PUTS("\t\t 4 = bundle forwarded (fwd)");
		PUTS("\t\t 8 = bundle delivered (dlv)");
		PUTS("\t\t16 = bundle deleted (del)");
		return 0;
	}

	ownEid = argv[1];
	destEid = argv[2];
	traceEid = argv[3];
	ttl = atoi(argv[4]);
	classOfService = argv[5];
	trace = argv[6];
	flagString = argc > 7 ? argv[7] : NULL;
#endif
	return run_bptrace(ownEid, destEid, traceEid, ttl, classOfService,
			trace, flagString);
}