2 * Copyright (c) 2006-2009 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <sys/types.h>
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
33 #include <sys/socket.h>
41 struct user users[USERS];
44 init_users(in_addr_t my_ip, int netbits)
49 int created_users = 0;
53 in_addr_t netmask = 0;
55 struct in_addr ipstart;
57 for (i = 0; i < netbits; i++) {
58 netmask = (netmask << 1) | 1;
60 netmask <<= (32 - netbits);
61 net.s_addr = htonl(netmask);
62 ipstart.s_addr = my_ip & net.s_addr;
64 maxusers = (1 << (32-netbits)) - 3; /* 3: Net addr, broadcast addr, iodined addr */
66 memset(users, 0, USERS * sizeof(struct user));
67 for (i = 0; i < USERS; i++) {
70 snprintf(newip, sizeof(newip), "0.0.0.%d", i + skip + 1);
71 ip = ipstart.s_addr + inet_addr(newip);
72 if (ip == my_ip && skip == 0) {
73 /* This IP was taken by iodined */
75 snprintf(newip, sizeof(newip), "0.0.0.%d", i + skip + 1);
76 ip = ipstart.s_addr + inet_addr(newip);
81 users[i].disabled = 1;
83 users[i].disabled = 0;
86 users[i].inpacket.len = 0;
87 users[i].inpacket.offset = 0;
88 users[i].outpacket.len = 0;
90 users[i].out_acked_seqno = 0;
91 users[i].out_acked_fragment = 0;
92 users[i].fragsize = 4096;
99 users_waiting_on_reply()
105 for (i = 0; i < USERS; i++) {
106 if (users[i].active && !users[i].disabled &&
107 users[i].last_pkt + 60 > time(NULL) &&
108 users[i].q.id != 0) {
117 find_user_by_ip(uint32_t ip)
123 for (i = 0; i < USERS; i++) {
124 if (users[i].active && !users[i].disabled &&
125 users[i].last_pkt + 60 > time(NULL) &&
126 ip == users[i].tun_ip) {
135 all_users_waiting_to_send()
143 for (i = 0; i < USERS; i++) {
144 if (users[i].active && !users[i].disabled &&
145 users[i].last_pkt + 60 > now &&
146 users[i].outpacket.len == 0) {
155 find_available_user()
159 for (i = 0; i < USERS; i++) {
160 /* Not used at all or not used in one minute */
161 if ((!users[i].active || users[i].last_pkt + 60 < time(NULL)) && !users[i].disabled) {
163 users[i].last_pkt = time(NULL);
172 user_switch_codec(int userid, struct encoder *enc)
174 if (userid < 0 || userid >= USERS)
177 users[userid].encoder = enc;