summaryrefslogtreecommitdiff
path: root/staging/mops_sequence.c
blob: 900472c2b1ff039916b1fae955c915f3f71edbda (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
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
298
299
300
301
302
303
/*
 * Mausezahn - A fast versatile traffic generator
 * Copyright (C) 2010 Herbert Haas
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the 
 * Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
 * 
*/


#include "mz.h"
#include "cli.h"
#include "mops.h"
#include "llist.h"


///////////////////// TOC /////////////////////
//
// int              mops_delete_sequence             (char *name)
// struct mz_ll *   mops_create_sequence             (char *name)
// int              mops_dump_sequence               (char* str)
// int              mops_add_packet_to_sequence      (struct mz_ll *seq, struct mops *mp)
// int              mops_add_delay_to_sequence       (struct mz_ll *seq, struct timespec *t)
// int              mops_delete_packet_from_pseq     (struct mz_ll *seq, int index)
// int              stop_sequence                    (char *name)
// int              stop_all_sequences               ()        


// delete one sequence element (from the global packet_sequence list)
// which must be specified by its name
// 
int mops_delete_sequence (char *name)
{
	struct mz_ll *v;
	
	v = mz_ll_search_name (packet_sequences, name);
	if (v==NULL) return 1; // name not found 

	if (v->state) return 2; // sequence is currently active!
	
	if (mz_ll_delete_element (v)) 
		return -1; // cannot delete head element!
	return 0;
}



struct mz_ll * mops_create_sequence (char *name)
{
	struct mz_ll *cur;
	struct pseq *seq;
	int i;
	
	cur = mz_ll_create_new_element(packet_sequences);
	if (cur==NULL) return NULL;
	strncpy(cur->name, name, MZ_LL_NAME_LEN);
	// add data
	cur->data = (struct pseq*) malloc (sizeof(struct pseq));
	// initialize data
	seq = (struct pseq*) cur->data;
	seq->count = 0;
	for (i=0; i<MAX_PACKET_SEQUENCE_LEN; i++) {
		seq->packet[i] = NULL;  // pointer to the packets
		seq->gap[i].tv_sec = 0;
		seq->gap[i].tv_nsec = 0;
	}
	return cur; 
}
	


// PURPOSE: dumps all sequence objects line-by-line
// 
// ARGUMENTS: Caller must provide a pointer to a string of size MZ_LL_NAME_LEN+(MAX_PACKET_SEQUENCE_LEN*6)
//            (recommendation: 512 bytes !)
// 
// RETURN VALUE: 0 if list is finished, 1 otherwise
// 
// EXAMPLE:   char str[512];
//            while (mops_dump_sequence(str)
//               printf("%s\n", str);
//            
int mops_dump_sequence (char* str)
{
	static int init=0;
	static struct mz_ll *cur;
	struct pseq *seq;
	struct mops *pkt;
	
	char tmp[256], t[16];
	int i, c;
	
	tmp[0]='\0';

	if (init==-1) { // last turn said stop now!
		init=0;
		return 0;
	}
	
        if (init==0) {
		cur=packet_sequences->next;
		if (cur==NULL) {
			str[0]='\0';
			return 0;
		}
		init=1;
	}

	seq = (struct pseq*) cur->data;
	if (seq==NULL) {
		init=-1;
		sprintf(str, "(no sequences found)");
		return 1;
	}

	c = seq->count; // amount of currently stored packets in this sequence object

	// create string with all packet IDs:
	for (i=0; i<c; i++) {
		pkt = seq->packet[i];
		if (pkt == NULL) break;
		snprintf(t, 15, "%i", pkt->id);
		if (strnlen(tmp,256)>249) break;
		strncat(tmp, t, 6);
		if (i<c-1) strncat(tmp,", ", 2);
	}

	snprintf(str, 512, "%s {%s}", cur->name, tmp);

	cur=cur->next;
	if (cur==packet_sequences) init=-1; // stop next turn!
	return 1;
}


// finds next free slot in sequence seq and adds packet mp
// 
// RETURN VALUE: 0 upon success
//              -1 failure: array full
//              -2 failure: cannot add packets with infinite count
//              
int mops_add_packet_to_sequence (struct mz_ll *seq, struct mops *mp)
{
	struct pseq *cur;
	int i; 
	
	if (seq==NULL) return 1;

	// don't add packets with count=0
	if (mp->count==0) return -2;
	
	cur = (struct pseq*) seq->data;
	if (cur->count >= MAX_PACKET_SEQUENCE_LEN) return -1; // packet array full!
	for (i=0; i<MAX_PACKET_SEQUENCE_LEN; i++) { 
		if (cur->packet[i]==NULL) { // found empty slot
			cur->packet[i]=mp;
			cur->count++;
			return 0;
		}
	}
	return 1; // never reach here
}


// adds the given delay 't' to the last packet in the sequence's pseq
// 
// NOTE: return index number of pseq where delay had been added
//       or upon failure: -1 if there is no packet yet defined
//                        -2 if array is full
int mops_add_delay_to_sequence (struct mz_ll *seq, struct timespec *t)
{
	struct pseq *cur;
	int i;
	
	if (seq==NULL) return 1;
	
	cur = (struct pseq*) seq->data;
	i = cur->count;
	if (i>= MAX_PACKET_SEQUENCE_LEN) return -2; // packet array full!
	
	cur->gap[i-1].tv_sec = t->tv_sec;
	cur->gap[i-1].tv_nsec = t->tv_nsec;
	
	return i-1; // note: is -1 if there is no packet yet (count=0)
}


// Deletes packet and associated delay from a pseq for given index
// If index == -1 then the last packet/delay is removed
// 
// NOTE: index range is {1..count}
//
// RETURN VALUES: 0 upon success
//                1 upon failure
//                2 upon failure, index too big
//                
int mops_delete_packet_from_pseq (struct mz_ll *seq, int index)
{
	struct pseq *cur;
	int i;

	if (seq==NULL) return 1;
	cur = (struct pseq*) seq->data;
	if (cur->count==0) return 1; // list is empty, nothing to delete
	if (index>cur->count) return 2;
	if ((index==0) || (index<-1)) return 1; // total invalid index values
	if (index==-1) { // remove last element
		cur->packet[cur->count-1]=NULL;
		cur->gap[cur->count-1].tv_sec=0;
		cur->gap[cur->count-1].tv_nsec=0;
	} else {
		for (i=index-1; i<(cur->count-1); i++) {
		cur->packet[i] = cur->packet[i+1];
		cur->gap[i].tv_sec = cur->gap[i+1].tv_sec;
		cur->gap[i].tv_nsec=cur->gap[i+1].tv_nsec;
		}
	}
	cur->count--;	
	return 0;
}
	

int mops_delete_all_packets_from_pseq (struct mz_ll *seq)
{
	struct pseq *cur;
	int i;
	
	if (seq==NULL) return 1;
	cur = (struct pseq*) seq->data;
	if (cur->count==0) return 1; // list is empty, nothing to delete
	// DELETE ALL:
	cur->count = 0;
	for (i=0; i<MAX_PACKET_SEQUENCE_LEN; i++) {
		cur->packet[i] = NULL;  // pointer to the packets
		cur->gap[i].tv_sec = 0;
		cur->gap[i].tv_nsec = 0;
	}
	return 0;
}



// Stops an active sequence and sets all involved packets from state SEQACT to CONFIG.
// 
// RETURN VALUE: 0 upon success
//               1 if sequence does not exist
//               2 if sequence is not actice
int stop_sequence (char *name)
{
	struct mz_ll *v;
	struct pseq *cur;
	int i;
	
	v = mz_ll_search_name (packet_sequences, name);
	if (v==NULL) return 1; // name not found 
	if (!v->state) return 2; // sequence is not currently active!

	// now stop thread:
	pthread_cancel(v->sequence_thread);

	// reset packet states:
	cur = (struct pseq*) v->data;
	for (i=0; i<cur->count; i++)
		cur->packet[i]->state=MOPS_STATE_CONFIG;

	// reset sequence state:
	v->state = 0;
	return 0;
}


// runs through 'packet_sequences' and cancels all active sequences
// (i. e. stops threads and sets states appropriately)
// 
// Comment: It might seem a bit inefficient to call 'stop_sequence' for the
//          detailed work, but this is the more safer way and it is fast enough.
//          
// RETURN VALUE: Number of stopped sequences.
// 
int stop_all_sequences(void)
{
	struct mz_ll *cur=packet_sequences->next;
	int i=0;
	
	while (cur!=packet_sequences) {
		if (cur!=packet_sequences) { // just for safety
			if (stop_sequence(cur->name)==0) i++;
		}
		cur=cur->next;
	} 
	
	return i;
}