3 #include "fwp_contract_table.h"
4 #include "fwp_agent_table.h"
5 #include "fwp_admctrl.h"
7 //static struct sockaddr_un fwp_laddr; /**< FWP local address */
8 static struct sockaddr_in fwp_raddr; /**< FWP remote address */
9 static int fwp_lsockfd; /**< FWP local sockfd */
10 static int fwp_rsockfd; /**< FWP remote sockfd */
11 static fd_set fwp_input_fdset;
13 /* buffer and socket for incomming message */
14 static unsigned char buffer[FWP_MTU];
15 struct fwp_sockaddr peer;
18 fwp_vres_d_t service_vresd; /**< service vres decriptor */
20 /* List of requested contracts */
21 /*fwp_contractlist_t contractlist_req;*/
22 /* for simplicity only one contract is to be negotiated now*/
23 fwp_contract_data_t *contract_req;
25 /* Admission control test */
26 fwp_admctrl_test_t fwp_admctrl_test = &fwp_admctrl_stupid;
31 * FWP agent inicialization
32 * - opens unix socket for local communication
33 * - opens udp socket for remote comunication
35 * @fwp_mnr_addr_s string that contains the address of AP where
36 * where fwp manager is located
41 fwpmngr_info = my_info;
43 FWP_DEBUG("Management receive endpoint created\n");
44 fwp_receive_endpoint_create(FWP_APP_MNGT_PORT, 0, &mngt_repointd);
45 my_info.port = FWP_APP_MNGT_PORT;
55 * Function waits for remote or local message
57 * @msgb received message
59 * On success, it returns 0 and the pointer to received message in msgb parameter.
60 * On error, it returns negative error code
63 int fwp_mngr_input(struct fwp_msgb **pmsgb)
65 struct fwp_msgb *msgb;
66 struct sockaddr_in *peer_in;
70 memcpy(&fdset, &fwp_input_fdset, sizeof(fd_set));
71 FWP_DEBUG("Waiting for messages\n");
73 if (select(getdtablesize()+1, &fdset, (fd_set *)0,
74 (fd_set *)0, NULL) < 0){
76 perror("Error in select");
80 if (FD_ISSET(fwp_lsockfd, &fdset)){
81 FWP_DEBUG("Management message received from local socket\n");
82 _fwp_recvfrom(len, fwp_lsockfd, buffer, FWP_MTU, 0,
83 peer.addr, &peer.addrlen);
87 FWP_DEBUG("Management message received from remote socket\n");
88 _fwp_recvfrom(len, fwp_rsockfd, buffer, FWP_MTU, 0,
89 peer.addr, &peer.addrlen);
91 /* we have to set UDP port fwp agent is listening on for
94 peer_in = (struct sockaddr_in*) peer.addr;
95 peer_in->sin_port = htons(FWP_AGENT_UDP_PORT);
96 size = sizeof(struct fwp_sockaddr);
99 FWP_DEBUG("Creating fwp msgb len=%d\n",len);
100 /* For future: fwp_socket could be allocated behind data in msgb*/
101 if (!(msgb = fwp_msgb_alloc(size))) {
102 perror("No memory available.\n");
105 /*memcpy(fwp_msgb_put(msgb, len), buffer, len); */
107 fwp_msgb_put(msgb, len);
115 fwp_mngr_contract_negt_response(fwp_contract_data_t *contdata)
117 struct fwp_msgb *msgb;
119 if (!(msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
120 sizeof(struct fwp_msg_vres_params)))) {
122 fprintf(stderr, "No memory available.\n");
126 fwp_msg_header_deflate(msgb->tail, FWP_CONTNEGT_RES,
127 &contdata->contract_call_id);
128 fwp_msgb_put(msgb, sizeof(struct fwp_msg_header));
129 fwp_msg_vres_params_deflate(msgb->tail, contdata->status,
130 &contdata->vres_params);
131 fwp_msgb_put(msgb, sizeof(struct fwp_msg_vres_params));
133 msgb->peer = &contdata->agent->addr;
134 /* TODO: add sending to local app through unix socket */
135 _fwp_vres_send(service_vresd, msgb);
139 static int fwp_mngr_contract_accept(fwp_contract_data_t *contdata)
141 fwp_agent_data_t *agdata;
143 if (contdata->status != FWP_CONT_NEGOTIATED) {
147 agdata = contdata->agent;
149 fwp_contract_table_insert(&agdata->contract_table, contdata);
154 int fwp_mngr_contract_negt_request(fwp_msgb_t *msgb, fwp_appcall_id_t *appcall_id,
155 fwp_contract_data_t **contract_data)
157 fwp_contract_t contract;
158 fwp_contract_data_t *contdata;
160 /* deserialize contract from message */
161 fwp_msg_contract_inflate(msgb->data, &contract);
162 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contract));
164 /* prepare contract data structure */
165 contdata = (fwp_contract_data_t*) malloc(sizeof(fwp_contract_data_t));
166 /* copy contract to contract_data */
167 memcpy(&contdata->contract, &contract, sizeof(contdata->contract));
168 /* set pointer to agent data */
169 contdata->agent = fwp_agent_table_find(msgb->peer);
170 /* set appid and hid */
171 contdata->contract_call_id = *appcall_id;
172 contdata->status = FWP_CONT_REQUESTED;
176 /* put contract data into contract requested list */
177 /*fwp_contractlist_insert(&contractlist_req, contdata);*/
178 *contract_data = contdata;
183 static void fwp_mngr_msg_handler(struct fwp_msgb *msgb)
186 fwp_appcall_id_t appcall_id;
188 fwp_msg_header_inflate(msgb->data, &code, &appcall_id);
189 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_header));
190 FWP_DEBUG("code = %d , appid = %d callid = %d\n", code,
191 appcall_id.appid, appcall_id.callid);
193 fwp_agent_table_insert(msgb->peer);
196 case FWP_CONTNEGT_REQ:
197 FWP_DEBUG("Negotiation Request received\n");
198 fwp_mngr_contract_negt_request(msgb, &appcall_id,
200 FWP_DEBUG("Executing admission control test\n");
201 fwp_admctrl_test(contract_req);
203 /* right now simply add to contract table if negotiated */
204 //fwp_mngr_contract_accept(&msgb->peer, contract_req);
206 FWP_DEBUG("Preparing NEGT_RES msg\n");
207 fwp_mngr_contract_negt_response(contract_req);
210 printf("Invalid message\n.");
217 struct fwp_msgb *msgb;
219 /* start admission control thread */
220 while (1 /*exit_flag*/){
221 fwp_mngr_input(&msgb);
223 fwp_mngr_msg_handler(msgb);
224 FWP_DEBUG("Mngr prepared to wait for next msg.\n");
230 if (fwp_init() < 0) {
231 fprintf(stderr,"FWP initialization failed.\n");
236 if (fwp_mngr_init() < 0) {
237 fprintf(stderr,"FWP manager initialization failed.\n");
242 /* execute agent function */