2 #include "fwp_contract.h"
3 #include "fwp_contract_table.h"
7 * Negotiates contract for application. Negotiation request is sent to
8 * fwp agent and then waits for response.
10 * \param[in] contract Contract to negotiate
11 * \param[out] vres_id Id of vres after the contract was accepted
13 * \return It returns FWP_CONTNEGT_ACCEPT when the contract was accepted and
14 * identifier of created vres is return in vres_id parameter.
15 * It returns FWP_CONTNEGT_REJECT when the contract was rejected.
17 * If an error occured it returns negative error code.
20 int fwp_contract_negotiate(struct fwp_contract *contract, fwp_vres_d_t *vresdp)
22 fwp_vres_params_t vparams;
23 fwp_contract_status_t status;
24 struct fwp_msgb *msgb;
26 fwp_appcall_id_t appcall_id;
27 struct sockaddr_un from;
28 socklen_t fromlen = sizeof(struct sockaddr_un);
32 msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
33 sizeof(struct fwp_msg_vres_params)/*FWP_MTU*/);
38 appcall_id.callid = fwp_callid++;
39 appcall_id.appid = fwp_appid;
40 fwp_msg_header_deflate(msgb->tail, FWP_CONTNEGT_REQ, &appcall_id);
41 fwp_msgb_put(msgb, sizeof(struct fwp_msg_header));
43 fwp_msg_contract_deflate(msgb->tail, contract);
44 fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
46 /* sendto FWP agent through unix socket
49 FWP_DEBUG("Negotiation request sent to agent\n");
50 _fwp_sendto(fwp_client_sockfd, msgb->data, msgb->len, 0,
51 &fwp_agent_addr, sizeof(fwp_agent_addr));
53 _fwp_recvfrom(len, fwp_client_sockfd, msgb->data, msgb->buf_size, 0,
55 FWP_DEBUG("Negotiation response received from agent\n");
57 fwp_msg_header_inflate(msgb->data, &code, &appcall_id);
58 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_header));
60 fwp_msg_vres_params_inflate(msgb->data, &status, &vparams);
61 fwp_msgb_pull(msgb, sizeof(struct fwp_msg_vres_params));
64 FWP_DEBUG("status = %d\n", status);
65 if (status == FWP_CONT_NEGOTIATED) {
66 FWP_DEBUG("Contract negotiated\n");
67 if ((rc = fwp_vres_create(&vparams, vresdp)) < 0){
70 return FWP_CONTNEGT_ACCEPTED;
73 FWP_DEBUG("Contract rejected\n");
74 return FWP_CONTNEGT_REJECTED;
79 fwp_contract_d_t fwp_contract_create(fwp_contract_t *contract)
82 fwp_contract_data_t *contdata;
84 contdata = fwp_contract_data_new();
85 contdata->vresd = fwp_vres_alloc();
86 contdata->id = fwp_vres_get_id(contdata->vresd);
87 memcpy(&contdata->contract, contract, sizeof(*contract));
88 contdata->status = FWP_CONT_REQUESTED;
90 /* Add to contract table */
91 fwp_contract_table_insert(&fwp_participant_this->contract_table,
96 int fwp_contract_reserve(fwp_contract_d_t contractd)
98 fwp_contract_id_t contract_id = contractd;
99 fwp_contract_data_t *contdata;
102 contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
104 /* Send contract to manager */
105 msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
106 sizeof(struct fwp_msg_contract));
107 fwp_msg_contract_deflate(msgb->data, contdata->id, &contdata->contract);
108 fwp_mngt_send(FWP_MSG_NEGT_RESERVE, msgb,
109 fwp_participant_this, fwp_participant_mngr);
113 int fwp_contract_commit(fwp_contract_d_t contractd)
115 fwp_contract_id_t contract_id = contractd;
116 fwp_contract_data_t *contdata;
119 contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
121 /* Send contract to manager */
122 msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
123 sizeof(struct fwp_msg_contract));
124 fwp_msg_contract_deflate(msgb->data, contdata->id, &contdata->contract);
125 fwp_mngt_send(FWP_MSG_NEGT_COMMIT, msgb,
126 fwp_participant_this, fwp_participant_mngr);
134 int fwp_contract_cancel(unsigned int vres_id)
136 return fwp_vres_close(vres_id);