forked from CAIDA/mper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scamper_task.c
163 lines (138 loc) · 3.41 KB
/
scamper_task.c
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
/*
* scamper_task.c
*
* $Id: scamper_task.c,v 1.27 2009/02/28 09:06:14 mjl Exp $
*
* Copyright (C) 2005-2009 The University of Waikato
* Author: Matthew Luckie
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "internal.h"
#include "scamper.h"
#include "scamper_addr.h"
#include "scamper_icmp_resp.h"
#include "scamper_task.h"
#include "scamper_queue.h"
#include "scamper_target.h"
#include "scamper_control.h"
#include "scamper_debug.h"
#include "mjl_list.h"
#include "utils.h"
typedef struct task_onhold
{
void (*unhold)(void *param);
void *param;
} task_onhold_t;
void *scamper_task_onhold(scamper_task_t *task, void *param,
void (*unhold)(void *param))
{
task_onhold_t *toh;
dlist_node_t *cookie;
if(task->internal == NULL && (task->internal = dlist_alloc()) == NULL)
{
return NULL;
}
if((toh = malloc(sizeof(task_onhold_t))) == NULL)
{
return NULL;
}
if((cookie = dlist_tail_push(task->internal, toh)) == NULL)
{
free(toh);
return NULL;
}
toh->param = param;
toh->unhold = unhold;
return cookie;
}
int scamper_task_dehold(scamper_task_t *task, void *cookie)
{
task_onhold_t *toh;
assert(task->internal != NULL);
if((toh = dlist_node_pop(task->internal, cookie)) == NULL)
{
return -1;
}
free(toh);
return 0;
}
/*
* scamper_task_alloc
*
* allocate and initialise a task object.
*/
scamper_task_t *scamper_task_alloc(void *data, scamper_task_funcs_t *funcs)
{
scamper_task_t *task;
assert(data != NULL);
assert(funcs != NULL);
if((task = malloc_zero(sizeof(scamper_task_t))) == NULL)
{
printerror(errno, strerror, __func__, "could not malloc task");
goto err;
}
task->funcs = funcs;
task->data = data;
if(scamper_queue_alloc(task) == -1)
{
goto err;
}
return task;
err:
if(task->queue != NULL) scamper_queue_free(task->queue);
free(task);
return NULL;
}
/*
* scamper_task_free
*
* free a task structure.
* this involves freeing the task using the free pointer provided,
* freeing the queue data structure, unholding any tasks blocked, and
* finally freeing the task structure itself.
*/
void scamper_task_free(scamper_task_t *task)
{
task_onhold_t *toh;
task->funcs->task_free(task);
scamper_queue_free(task->queue);
if(task->internal != NULL)
{
while((toh = dlist_head_pop(task->internal)) != NULL)
{
toh->unhold(toh->param);
free(toh);
}
dlist_free(task->internal);
}
if(task->targetset != NULL)
{
scamper_targetset_free(task->targetset);
}
if(task->source_task != NULL)
{
scamper_source_taskdone(task->source, task);
}
else if(task->source != NULL)
{
scamper_source_free(task->source);
}
free(task);
return;
}