main.c 7.66 KB
Newer Older
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
/*
 * agent-main.c --
 *
 *      Delay node agent main file
 *
 *
 * @COPYRIGHT@
 */

/*********************************INCLUDES********************************/
#define REAL_WORLD 1

#if REAL_WORLD
  #include "main.h"
#else
  #include "main-d.h"
#endif
/*********************************INCLUDES********************************/

/************************GLOBALS*****************************************/
/* for reading in lines from config. files*/
char line[MAX_LINE_LENGTH];

/* address_tuple_t for subscribing to events*/
address_tuple_t event_t = NULL;

/* handle returned by event_register*/
event_handle_t handle;

/* temporary buffer for forming the server URL*/
char buf[BUFSIZ];

/* This holds the mapping between links as relevant to the event system,
   and the physical interfaces and pipe numbers
 */
structlink_map link_map[MAX_LINKS];

/* holds the number of entries in the link_map*/
int link_index = 0;

/* Raw socket*/
int s_dummy;

/* agent IP address */
char *ipaddr = NULL;

static int debug = 0;
48
49

char buf_link [MAX_LINKS][MAX_LINE_LENGTH];
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
/************************GLOBALS*****************************************/


/************************** FUNCTION DEFS *******************************/


/************************* main **************************************

 ************************* main **************************************/

int main(int argc, char **argv)
{
  char c;
  char *server = NULL;
  char * port  = NULL;
  char *map_file = NULL;
  FILE *mp = NULL;
  //char *log = NULL;
  char buf[BUFSIZ];

#if REAL_WORLD
  char ipbuf[BUFSIZ];
#endif
  
  opterr = 0;

  /* get params from the optstring */
  while ((c = getopt(argc, argv, "s:p:f:d")) != -1) {
        switch (c) {
	  case 'd':
	      debug++;
	      break;
          case 's':
              server = optarg;
              break;
	  case 'p':
	      port = optarg;
	      break;
	  case 'f':
	      map_file = optarg;
	      break;
	  case '?':
          default:
	      usage(argv[0]);
	      break;
        }
    }

  /*Check if all params are specified, otherwise, print usage and exit*/
  if(NULL == server || NULL == map_file)
      usage(argv[0]);

  if (debug)
     loginit(0, "/tmp/agentlog");
  else
     loginit(1, "agent-thing");

  /* open the map file*/
  if(NULL == (mp = fopen(map_file,"r")))
    {
      error("cannot open %s \n", map_file);
      exit(-1);
    }

  {

116
    char * temp = NULL;
117
118
    char *sep = " \n";

119
120
121
122
123
    while(fgets(buf_link[link_index], 256, mp)){
      temp = buf_link[link_index];
      link_map[link_index].linkname = strsep(&temp, sep);
      link_map[link_index].linktype = strsep(&temp, sep);
      link_map[link_index].interfaces[0] = strsep(&temp, sep);
124
125
126
	
      if(!strcmp(link_map[link_index].linktype,"duplex"))
        {
127
128
129
          link_map[link_index].interfaces[1] = strsep(&temp,sep);
          link_map[link_index].pipes[0] = atoi(strsep(&temp,sep));
	  link_map[link_index].pipes[1] = atoi(strsep(&temp,sep));
130
131
	}
	else{
132
	  link_map[link_index].pipes[0] = atoi(strsep(&temp,sep));
133
134
135
136
	}
      link_index++;
    }
  }
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
304
305
  /* close the map-file*/
  fclose(mp);
    
  /* create a raw socket to configure Dummynet through setsockopt*/
  
#if REAL_WORLD
  s_dummy = socket( AF_INET, SOCK_RAW, IPPROTO_RAW );
  if ( s_dummy < 0 ){
    error("cant create raw socket\n");
    return 1;
  }

/* this gets the current pipe params from dummynet and populates
     the link map table
   */
  if (get_link_info() == 0){
    error("cant get the link pipe params from dummynet\n");
    return 1;
  }

  /* dump the link_map to log*/
  dump_link_map();
  
  
 /*
  * Get our IP address. Thats how we name ourselves to the
  * Testbed Event System. 
  */
  if (ipaddr == NULL) {
     struct hostent	*he;
     struct in_addr	myip;
	    
     if (gethostname(buf, sizeof(buf)) < 0) {
	    error("could not get hostname");
	    return 1;
	}

      if (! (he = gethostbyname(buf))) {
	   error("could not get IP address from hostname");
	   return 1;
       }
       memcpy((char *)&myip, he->h_addr, he->h_length);
       strcpy(ipbuf, inet_ntoa(myip));
       ipaddr = ipbuf;
   }
#endif
  
  /* Convert server/port to elvin thing.
   */
   if (server) {
	    snprintf(buf, sizeof(buf), "elvin://%s%s%s",
		     server,
		     (port ? ":"  : ""),
		     (port ? port : ""));
	    server = buf;
    }
  
  /* register with the event system*/

  handle = event_register(server, 0);
   if (handle == NULL) {
       error("could not register with event system\n");
       return 1;
     }

  info("registered with the event server\n");
 /* allocate an address_tuple*/
  event_t = address_tuple_alloc();

  /* fill up the tuple, before calling event_subscribe*/
  fill_tuple(event_t);
      
  /*subscribe to the event*/
   if (event_subscribe(handle, agent_callback, event_t, NULL) == NULL) {
        error("could not subscribe to %d event\n",event_t->eventtype);
        return 1;
      }

  info("subscribed...\n");
  /* free the memory for the address tuple*/
  address_tuple_free(event_t);

  info("entering the main loop\n");
  /* enter the event loop */
   event_main(handle);
  
  /*now daemonise*/

#ifdef DEBUG
  info("exiting function main\n");
#endif
  return 0;
}

/*********************** usage *********************************************
This function prints the usage to stderr and then exits with an error value

*********************** usage *********************************************/

/* prints the usage and exits with error*/
void usage(char *progname)
{
#ifdef DEBUG
  info("entering function usage\n");
#endif
  
  ERROR("Usage: %s -s server [-p port] -f link-map-file\n",
	    progname);
  exit(-1);
}


/************************* fill_tuple **********************************

************************** fill_tuple **********************************/

void fill_tuple(address_tuple_t at)
{

#ifdef DEBUG
  info("entering function fill_tuple\n");
#endif
  /* fill the objectname, objecttype and the eventtype from the file*/
  at->objname = ADDRESSTUPLE_ANY;
  at->objtype = TBDB_OBJECTTYPE_LINK;
  at->eventtype = ADDRESSTUPLE_ANY;
  at->host = ipaddr;
  
  /*fill in other values, dont know what to fill in yet*/
  at->site = ADDRESSTUPLE_ANY;
  at->expt = ADDRESSTUPLE_ANY;
  at->group= ADDRESSTUPLE_ANY;
  at->scheduler = 0;
#ifdef DEBUG
  info("leaving function fill_tuple\n");
#endif
  return;
}

/***************************dump_link_map******************************/
/*A debugging aid.*/
/***************************dump_link_map******************************/
void dump_link_map(){
  int i;
  for (i = 0; i < link_index; i++){
    info("linkname = %s\n", link_map[i].linkname);
    info("interface[0] = %s \t interface[1] = %s\n", link_map[i].interfaces[0], link_map[i].interfaces[1]);
    info("linktype = %s\n", link_map[i].linktype);
    info("linkstatus = %d \n", link_map[i].stat);
    info("pipes[0] = %d \t pipes[1] = %d \n", link_map[i].pipes[0], link_map[i].pipes[1]);

    info ("--------------------------------------------------\n");
    info("Pipe params.....\n");

    info("Pipe 0 .....\n");

    info("delay = %d, bw = %d plr = %f q_size = %d buckets = %d n_qs = %d flags_p = %d \n",  link_map[i].params[0].delay, link_map[i].params[0].bw, link_map[i].params[0].plr, link_map[i].params[0].q_size, link_map[i].params[0].buckets, link_map[i].params[0].n_qs, link_map[i].params[0].flags_p);

    info("Pipe 1 .....\n");

    info("delay = %d, bw = %d plr = %f q_size = %d buckets = %d n_qs = %d flags_p = %d \n",  link_map[i].params[1].delay, link_map[i].params[1].bw, link_map[i].params[1].plr, link_map[i].params[1].q_size, link_map[i].params[1].buckets, link_map[i].params[1].n_qs, link_map[i].params[1].flags_p);

    info ("--------------------------------------------------\n");
  }
}

/************************** FUNCTION DEFS *******************************/