/** * \brief Process IPFIX message * * \param[in] config plugin configuration * \param[in] message IPFIX message * \return 0 on success */ int intermediate_process_message(void* config, void* message) { plugin_conf *conf = reinterpret_cast<plugin_conf *>(config); struct ipfix_message *msg = reinterpret_cast<struct ipfix_message *>(message); /* Catch closing message */ if (msg->source_status == SOURCE_STATUS_CLOSED) { pass_message(conf->ip_config, msg); return 0; } /* Update counters */ stats_flush_counters(conf); /* Process message */ stats_data *stats = stats_get_rrd_file(conf, htonl(msg->pkt_header->observation_domain_id)); if (!stats) { /* Error occured */ goto err; } /* Update counters */ for (uint16_t i = 0; i < msg->data_records_count; ++i) { stats_update_counters(stats, &(msg->metadata[i])); } pass_message(conf->ip_config, msg); return 0; err: pass_message(conf->ip_config, msg); return 1; }
int main (int argc, char **argv) { int s; int c; int r; int localport = 8888; size_t sinsize; struct sockaddr_in sin; fd_set fds; char *host = NAP_SERVER; int port = NAP_PORT; while ((r = getopt (argc, argv, "hs:p:l:")) != EOF) { switch (r) { case 'l': localport = atoi (optarg); break; case 's': host = optarg; break; case 'p': port = atoi (optarg); break; default: usage (); } } /* accept connection from client */ s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); if (s < 0) { perror ("socket"); exit (1); } c = 1; if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &c, sizeof (c)) != 0) { perror ("setsockopt"); exit (1); } memset (&sin, 0, sizeof (sin)); sin.sin_port = htons (localport); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; if (bind (s, &sin, sizeof (sin)) < 0) { perror ("bind"); exit (1); } if (listen (s, 1) < 0) { perror ("listen"); exit (1); } puts ("waiting for client"); if (select (s + 1, &fds, 0, 0, 0) < 0) { perror ("select"); exit (1); } sinsize = sizeof (sin); c = accept (s, &sin, &sinsize); if (c < 0) { perror ("accept"); exit (1); } puts ("got client"); /* make connection to server */ printf ("connecting to server..."); fflush (stdout); r = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); if (r < 0) { perror ("socket"); exit (1); } memset (&sin, 0, sizeof (sin)); sin.sin_port = htons (port); sin.sin_family = AF_INET; sin.sin_addr.s_addr = inet_addr (host); if (connect (r, &sin, sizeof (sin)) < 0) { perror ("connect"); exit (1); } puts ("connected to server"); for (;;) { FD_ZERO (&fds); FD_SET (r, &fds); FD_SET (c, &fds); if (select (((r > c) ? r : c) + 1, &fds, 0, 0, 0) < 0) { perror ("select"); break; } if (FD_ISSET (r, &fds)) { if (pass_message ("server", r, c) != 0) break; } if (FD_ISSET (c, &fds)) { if (pass_message ("client", c, r) != 0) break; } } close (r); close (s); close (c); exit (0); }