]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/libfwp/src/fwp_ac.c
Added a possibility to measure saturation bandwidth.
[frescor/fwp.git] / fwp / libfwp / src / fwp_ac.c
1 #include "fwp_ac.h"
2 #include "fwp_util.h"
3
4
5 enum ac_status_t {
6         FWP_AC_CLOSED = 0,
7         FWP_AC_OPENED = 1
8 };
9
10 struct fwp_ac {
11         int sockfd;
12         int aifn;
13         int cwmin;
14         int cwmax;
15         int status;
16         int count;
17 };
18         
19 static struct fwp_ac fwp_ac_table[FWP_AC_NUM];
20
21 const int prio_to_ac[8] = {2,3,3,2,1,1,0,0};
22 const unsigned int ac_to_tos[4] = {224,160,96,64};
23
24 void fwp_ac_table_init()
25 {
26         int id;
27
28         for (id = 0; id < FWP_AC_NUM; id++) {
29                 fwp_ac_table[id].status = FWP_AC_CLOSED;
30                 fwp_ac_table[id].count = 0;
31         }
32 }
33
34 int fwp_ac_open(unsigned int id) 
35 {
36         int sockfd;
37         unsigned int yes=1, tos;
38         struct fwp_ac *ac = &fwp_ac_table[id];
39         
40         //if ((id < 0)||(id >= AC_NUM)||(ac[id].status != OPENED))
41         //      return -1;
42
43         if (ac->status == FWP_AC_OPENED) {
44                 ac->count++;
45                 return 0;
46         }
47
48         if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
49         {
50                 /*perror("Unable to open socket");*/
51                 return -1;
52         }
53         
54         if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
55                 /*perror("Unable to set socket");*/
56                 return -1;
57         }
58
59         tos = ac_to_tos[id];
60         if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
61                 /*perror("Unable to set TOS");*/
62                 close(sockfd);
63                 return -1;
64         }
65
66         ac->sockfd = sockfd;
67         ac->status = FWP_AC_OPENED;
68         ac->count++;
69
70         return 0;
71 }
72
73
74 int fwp_ac_close(unsigned int id) 
75 {
76         struct fwp_ac *ac = &fwp_ac_table[id];
77         
78         if (ac->status != FWP_AC_OPENED) return -1; 
79         if (--ac->count == 0) {
80                 close(ac->sockfd);
81                 ac->status = FWP_AC_CLOSED;
82         }
83         return 0;
84 }
85
86 int fwp_ac_send(unsigned int id, struct fwp_msgb* msgb)
87 {
88         while (sendto(fwp_ac_table[id].sockfd, msgb->head, msgb->len,
89                       /*msgb_>flags*/0, &msgb->addr, msgb->addrlen) < 0) {
90
91                 if (errno == EINTR) continue;
92                 /*perror("Error while sending data");*/
93                 return -1;
94         }
95         
96         return 0;
97 }
98