+++ /dev/null
-#include "fwp_mngt.h"
-
-/**< Local and remote adresses for communication */
-static struct fwp_sockaddr fwp_mngr_sockaddr;
-
-static struct sockaddr_un fwp_laddr; /**< FWP local address */
-static struct sockaddr_in fwp_raddr; /**< FWP remote address */
-static int fwp_lsockfd; /**< FWP local sockfd */
-static int fwp_rsockfd; /**< FWP remote sockfd */
-static fd_set fwp_input_fdset;
-
-/* buffer and socket for incomming message */
-unsigned char buffer[FWP_MTU];
-struct fwp_sockaddr peer;
-
-/* service vres */
-fwp_vres_params_t service_vparams;
-fwp_vres_d_t service_vresd; /**< service vres descriptor */
-
-/**
- * fwp_agent_init
- *
- * FWP agent inicialization
- * - opens unix socket for local communication
- * - opens udp socket for remote comunication
- *
- * @fwp_mnr_addr_s string that contains the address of AP where
- * where fwp manager is located
- *
- */
-int fwp_agent_init(char *fwp_mngr_addr_s)
-{
- struct sockaddr_in *mngr_in;
-
- /* setting AP address */
- FWP_DEBUG("AP address = %s\n", fwp_mngr_addr_s);
- mngr_in = (struct sockaddr_in*) fwp_mngr_sockaddr.addr;
- bzero(mngr_in, sizeof(*mngr_in));
- mngr_in->sin_family = AF_INET;
- mngr_in->sin_addr.s_addr = inet_addr(fwp_mngr_addr_s);
- mngr_in->sin_port = htons(FWP_MNGR_UDP_PORT);
- fwp_mngr_sockaddr.addrlen = sizeof(struct sockaddr_in);
-
- /*setting unix socket the agent will listen on */
- if ((fwp_lsockfd = fwp_create_unix_socket(FWP_AGENT_UNIXPATH,
- &fwp_laddr)) < 0) {
- return (fwp_lsockfd);
- }
-
- if ((fwp_rsockfd = fwp_create_inet_socket(FWP_AGENT_UDP_PORT,
- &fwp_raddr)) < 0) {
- return (fwp_rsockfd);
- }
-
- FD_ZERO(&fwp_input_fdset);
- FD_SET(fwp_lsockfd, &fwp_input_fdset);
- FD_SET(fwp_rsockfd, &fwp_input_fdset);
-
- /* create service vres */
- service_vparams.ac_id = FWP_AC_BK;
- service_vparams.budget = 100;
- service_vparams.period_usec = 1000;
-
- if ((fwp_vres_create(&service_vparams, &service_vresd) < 0)) {
- fprintf(stderr,"fw_vres_open - Unable to create service vres\n");
- return -1;
- }
-
- FWP_DEBUG("Service vres negotiated\n");
-
- return 0;
-}
-
-/**
- * fwp_mngt_input
- *
- * Function waits for remote or local message
- *
- * @msgb received message
- * \return
- * On success, it returns 0 and the pointer to received message in msgb parameter.
- * On error, it returns negative error code
- *
- */
-int fwp_agent_input(struct fwp_msgb **pmsgb)
-{
- struct fwp_msgb *msgb;
- int len;
- fd_set fdset;
- struct sockaddr_un *unsock;
-
- memcpy(&fdset, &fwp_input_fdset, sizeof(fd_set));
- FWP_DEBUG("Waiting for messages\n");
-
- if (select(getdtablesize()+1, &fdset, (fd_set *)0,
- (fd_set *)0, NULL) < 0){
-
- perror("Error in select");
- return (-errno);
- }
-
- if (FD_ISSET(fwp_lsockfd, &fdset)){
- FWP_DEBUG("Management message received from local socket\n");
- peer.addrlen = sizeof(struct sockaddr_un);
- _fwp_recvfrom(len, fwp_lsockfd, buffer, FWP_MTU, 0,
- peer.addr, &peer.addrlen);
-
- unsock = (struct sockaddr_un*) peer.addr;
- printf("Prijate z %s\n",unsock->sun_path);
- }
- else {
- FWP_DEBUG("Management message received from remote socket\n");
- _fwp_recvfrom(len, fwp_rsockfd, buffer, FWP_MTU, 0,
- peer.addr, &peer.addrlen);
-
- }
-
- FWP_DEBUG("Creating fwp msgb len=%d\n",len);
- msgb = NULL;
- /* For future: fwp_socket could be allocated behind data in msgb*/
- if (!(msgb = fwp_msgb_alloc(0))) {
- fprintf(stderr, "No memory available.\n");
- return -ENOMEM;
- }
- /*memcpy(fwp_msgb_put(msgb, len), buffer, len); */
- msgb->data = buffer;
- fwp_msgb_put(msgb, len);
- msgb->peer = &peer;
-
- *pmsgb = msgb;
- return (0);
-}
-
-/**
- * Handler for request messages
- */
-int fwp_agent_request(struct fwp_msgb *msgb, fwp_appcall_id_t *appcall_id)
-{
- fwp_msgb_push(msgb, sizeof(struct fwp_msg_header));
- /* forward request to manager*/
- FWP_DEBUG("Request forwarded to manager\n");
- msgb->peer = &fwp_mngr_sockaddr;
- return _fwp_vres_send(service_vresd, msgb);
-}
-
-/**
- * Handler for response messages
- */
-int fwp_agent_response(struct fwp_msgb *msgb, fwp_appcall_id_t *appcall_id)
-{
- struct sockaddr_un client_addr;
-
- fwp_msgb_push(msgb, sizeof(struct fwp_msg_header));
-
- client_addr.sun_family = AF_UNIX;
- sprintf(client_addr.sun_path,"%s/fwp-%d",
- FWP_CLIENT_UNIXPATH, appcall_id->appid);
-
- /*send response through unix socket */
- _fwp_sendto(fwp_lsockfd, msgb->data, msgb->len, 0,
- &client_addr, sizeof(client_addr));
-
- FWP_DEBUG("Response sent to app\n");
-
- return 0;
-}
-
-/**
- * fwp_agent_msg_handler
- *
- * decodes mngt message and invokes appropriate
- * handler
- *
- * @msgb received message
- *
- */
-void fwp_agent_msg_handler(struct fwp_msgb *msgb)
-{
- unsigned int code;
- fwp_appcall_id_t appcall_id;
-
- /*TODO: make it simplier: forward messages without decoding heder*/
- fwp_msg_header_inflate(msgb->data, &code, &appcall_id);
- fwp_msgb_pull(msgb, sizeof(struct fwp_msg_header));
- FWP_DEBUG("code = %d , callid = %d appid = %d \n", code,
- appcall_id.callid, appcall_id.appid);
-
- switch (code) {
- case FWP_CONTNEGT_REQ:
- FWP_DEBUG("Negotiation Request received\n");
- fwp_agent_request(msgb, &appcall_id);
- break;
-
- case FWP_CONTNEGT_RES:
- FWP_DEBUG("Negotiation Response received\n");
- fwp_agent_response(msgb, &appcall_id);
- break;
-
- default:
- fprintf(stderr,"Invalid message received\n.");
- fwp_msgb_free(msgb);
- }
-}
-
-/**
- * Main agent function
- *
- * receives management message and then invokes handler
- *
- */
-void fwp_agent()
-{
- struct fwp_msgb *msgb;
-
- while (1 /*exit_flag*/){
- if (fwp_agent_input(&msgb) < 0)
- continue;
- if (msgb) fwp_agent_msg_handler(msgb);
- FWP_DEBUG("Agent waiting for next msg.\n");
- }
-}
-
-int main(int argc, char* argv[])
-{
- char *ap;
- int opt;
-
- while ((opt = getopt(argc, argv, "a:")) != -1) {
- switch (opt) {
- case 'a':
- ap = optarg;
- default:
- printf("Usage: %s -a access_point_address", argv[0]);
- break;
- }
- }
-
- if (fwp_init() < 0) {
- fprintf(stderr,"FWP initialization failed.\n");
- exit(1);
-
- }
-
- if (fwp_agent_init(ap) < 0) {
- fprintf(stderr,"FWP agent initialization failed.\n");
- exit(1);
-
- }
-
- /* execute agent function */
- fwp_agent();
-
- return 0;
-}
-
+++ /dev/null
-#include "fwp_agent_table.h"
-#include <stdlib.h>
-
-typedef
-struct {
- unsigned int nr_agents;
- /*pthread_mutex_t lock; */
- /*gavl_cust_root_field_t root;*/
- gavl_node_t *agent_tree;
- pthread_mutex_t lock;
-} fwp_agent_table_t;
-
-GAVL_CUST_NODE_INT_DEC(_fwp_agent_table, fwp_agent_table_t, fwp_agent_data_t,
- unsigned int, agent_tree, tree_node, _addr,
- gavl_cmp_int);
-
-GAVL_CUST_NODE_INT_IMP(_fwp_agent_table, fwp_agent_table_t, fwp_agent_data_t,
- unsigned int, agent_tree, tree_node, _addr,
- gavl_cmp_int);
-
-fwp_agent_table_t fwp_agent_table = {
- .nr_agents = 0,
- .agent_tree = NULL,
- .lock = PTHREAD_MUTEX_INITIALIZER,
-};
-
-void fwp_agent_table_insert(fwp_sockaddr_t *agent_sockaddr)
-{
- struct sockaddr_in *addr_in;
- fwp_agent_data_t *agdata;
-
- agdata = fwp_agent_table_find(agent_sockaddr);
- if (!agdata) {
- /* Alloc new agent data */
- agdata = (fwp_agent_data_t*)malloc(sizeof(fwp_agent_data_t));
- addr_in = (struct sockaddr_in*) agent_sockaddr->addr;
- agdata->_addr = addr_in->sin_addr.s_addr;
- memcpy(&agdata->addr, agent_sockaddr, sizeof(fwp_sockaddr_t));
- fwp_contract_table_init(&agdata->contract_table);
- }
-
- pthread_mutex_lock(&fwp_agent_table.lock);
- _fwp_agent_table_insert(&fwp_agent_table, agdata);
- fwp_agent_table.nr_agents++;
- pthread_mutex_unlock(&fwp_agent_table.lock);
-}
-
-fwp_agent_data_t* fwp_agent_table_find(fwp_sockaddr_t *agent_sockaddr)
-{
- unsigned int addr;
- struct sockaddr_in *addr_in;
-
- addr_in = (struct sockaddr_in*) agent_sockaddr->addr;
- addr = addr_in->sin_addr.s_addr;
-
- return _fwp_agent_table_find(&fwp_agent_table, &addr);
-}
-
-int fwp_agent_table_delete(fwp_agent_data_t *agdata)
-{
- int rv;
-
- pthread_mutex_lock(&fwp_agent_table.lock);
- rv = _fwp_agent_table_delete(&fwp_agent_table, agdata);
- fwp_agent_table.nr_agents--;
- pthread_mutex_unlock(&fwp_agent_table.lock);
-
- return rv;
-}