Subversion Repositories shark

Rev

Rev 366 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Giacomo Guidi       <giacomo@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/*
 * Copyright (C) 2002 Paolo Gai
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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
 *
 */


#include <ll/sys/types.h>
#include <stdlib.h>
#include <tracer.h>

#include <drivers/udpip.h>

#define UDP_MAXSIZE 1400
#define TRACER_PORT 100

int TracerUDPInit = 0;
UDP_ADDR target,local;
int socket;

extern int TracerOutputType;

struct tracer_udp_header {
  char id[12];
  unsigned int pkt_number;
  unsigned int number_of_events;
  unsigned int total_size;
};

int init_udp_tracer(char *local_ip, char *target_ip) {

  SYS_FLAGS f;
 
  struct net_model m = net_base;

  f = ll_fsave();

  net_setmode(m, TXTASK);
  net_setudpip(m, local_ip, "255.255.255.255");

  if (net_init(&m) != 1)
    return -1;

  ip_str2addr(local_ip,&(local.s_addr));
  local.s_port = TRACER_PORT;
  socket = udp_bind(&local, NULL);
 
  ip_str2addr(target_ip,&(target.s_addr));
  target.s_port = TRACER_PORT;
 
  TracerOutputType = TRACER_UDP_OUTPUT;
  TracerUDPInit = 1;

  ll_frestore(f);

  return 0;

}

int send_udp_event(void *p, int size) {

  static BYTE pkt[UDP_MAXSIZE];
  static BYTE *current = pkt;
  static int total_pkt_size = 0;
  static int events_number = 0;
  static int packet_number = 0;
 
  struct tracer_udp_header head = {"TRACER-V1.0",0,0,0};
  struct tracer_udp_header *phead = (struct tracer_udp_header *)(pkt);

  if (total_pkt_size + size < UDP_MAXSIZE) {

    if (total_pkt_size == 0) {

      memcpy(pkt,&head,sizeof(struct tracer_udp_header));

      current = pkt;
      current += sizeof(struct tracer_udp_header);

      total_pkt_size += sizeof(struct tracer_udp_header);
      phead->total_size = total_pkt_size;

      packet_number++;
      phead->pkt_number = packet_number;

    }

    events_number++;
    phead->number_of_events = events_number;
   
    total_pkt_size += size;
    phead->total_size = total_pkt_size;
   
    //Copy the event
    memcpy(current,p,size);
    current += size;

    return 0;

  } else {

    udp_sendto(socket, pkt, total_pkt_size, &target);

    events_number = 0;
    total_pkt_size = 0;
    current = pkt;
   
    send_udp_event(p,size);
   
    return 0;    

  }

  return -1;

}