File: bpsource.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 (232 lines) | stat: -rw-r--r-- 3,923 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
/*
	bpsource.c:	a test bundle source.
									*/
/*									*/
/*	Copyright (c) 2004, California Institute of Technology.		*/
/*	All rights reserved.						*/
/*	Author: Scott Burleigh, Jet Propulsion Laboratory		*/
/*									*/

#include <bp.h>

#define	DEFAULT_TTL 300


static int	_running(int *newState)
{
	int	state = 1;

	if (newState)
	{
		state = *newState;
	}

	return state;
}

static void	_zcoControl(int *controlPtr)
{
	static int	*ptr = NULL;

	if (controlPtr)	/*	Initializing ZCO request cancellation.	*/
	{
		ptr = controlPtr;
	}
	else		/*	Canceling ZCO request.			*/
	{
		ionCancelZcoSpaceRequest(ptr);
	}
}

static void	handleQuit()
{
	int	stop = 0;

	oK(_running(&stop));
	_zcoControl(NULL);
}

#if defined (VXWORKS) || defined (RTEMS) || defined (bionic)
int	bpsource(int a1, int a2, int a3, int a4, int a5,
		int a6, int a7, int a8, int a9, int a10)
{
	char	*destEid = (char *) a1;
	char	*text = (char *) a2;
	int 	ttl = (a5 == 0 ? DEFAULT_TTL : atoi((char *) a5));
#else
int	main(int argc, char **argv)
{
	char	*destEid = NULL;
	char	*text = NULL;
	int 	ttl = 300;

	if(argc > 4) argc=4;

	switch (argc)
	{
	case 4:
		if(argv[3][0]=='-' && argv[3][1]=='t')
		{
			ttl=atoi(&argv[3][2]);
		}else{
			text=argv[3];
		}
		/*Fall Through*/
	case 3:
		if(argv[2][0]=='-' && argv[2][1]=='t')
		{
			ttl=atoi(&argv[2][2]);
		}else{
			text=argv[2];
		}
		/*Fall Through*/
	case 2:
		destEid = argv[1];
		/*Fall Through*/
	default:
		break;
	}


#endif
	Sdr	sdr;
	char	line[256];
	int	lineLength;
	Object	extent;
	Object	bundleZco;
	int	controlZco = 0;
	Object	newBundle;
	int	fd;

	if (destEid == NULL)
	{
		PUTS("Usage: bpsource <destination endpoint ID> ['<text>'] [-t<Bundle TTL>]");
		return 0;
	}

	if (ttl <= 0)
	{
		PUTS("Usage: bpsource <destination endpoint ID> ['<text>'] \
[-t<Bundle TTL>]");
		return 0;
	}

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

	sdr = bp_get_sdr();
	if (text)
	{
		lineLength = strlen(text);
		if (lineLength == 0)
		{
			writeMemo("[?] No text for bpsource to send.");
			bp_detach();
			return 0;
		}

		CHKZERO(sdr_begin_xn(sdr));
		extent = sdr_malloc(sdr, lineLength);
		if (extent)
		{
			sdr_write(sdr, extent, text, lineLength);
		}

		if (sdr_end_xn(sdr) < 0)
		{
			putErrmsg("No space for ZCO extent.", NULL);
			bp_detach();
			return 0;
		}

		bundleZco = ionCreateZco(ZcoSdrSource, extent, 0, lineLength,
				&controlZco);
		if (bundleZco == 0)
		{
			putErrmsg("Can't create ZCO extent.", NULL);
			bp_detach();
			return 0;
		}

		if (bp_send(NULL, destEid, NULL, ttl, BP_STD_PRIORITY,
				NoCustodyRequested, 0, 0, NULL, bundleZco,
				&newBundle) < 1)
		{
			putErrmsg("bpsource can't send ADU.", NULL);
		}

		bp_detach();
		return 0;
	}

#ifndef FSWLOGGER	/*	Need stdin/stdout for interactivity.	*/
	fd = fileno(stdin);
	_zcoControl(&controlZco);
	isignal(SIGINT, handleQuit);
	while (_running(NULL))
	{
		printf(": ");
		fflush(stdout);
		if (igets(fd, line, sizeof line, &lineLength) == NULL)
		{
			if (lineLength == 0)	/*	EOF.		*/
			{
				break;
			}

			putErrmsg("igets failed.", NULL);
			break;
		}

		switch (line[0])
		{
		case '!':
			break;

		case 0:
			continue;

		default:
			CHKZERO(sdr_begin_xn(sdr));
			extent = sdr_malloc(sdr, lineLength);
			if (extent)
			{
				sdr_write(sdr, extent, line, lineLength);
			}

			if (sdr_end_xn(sdr) < 0)
			{
				putErrmsg("No space for ZCO extent.", NULL);
				break;
			}

			bundleZco = ionCreateZco(ZcoSdrSource, extent,
					0, lineLength, &controlZco);
			if (bundleZco == 0)
			{
				putErrmsg("Can't create ZCO extent.", NULL);
				break;
			}

			if (bp_send(NULL, destEid, NULL, ttl, BP_STD_PRIORITY,
					NoCustodyRequested, 0, 0, NULL,
					bundleZco, &newBundle) < 1)
			{
				putErrmsg("bpsource can't send ADU.", NULL);
				break;
			}

			continue;
		}

		break;
	}

	writeMemo("[i] Stopping bpsource.");
	bp_detach();
#endif
	return 0;
}