This repository has been archived by the owner on May 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathparticipants.c
149 lines (130 loc) · 3.16 KB
/
participants.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "socklib.h"
#include "log.h"
#include "util.h"
#include "participants.h"
#include "udpcast.h"
#ifdef USE_SYSLOG
#include <syslog.h>
#endif
struct participantsDb {
int nrParticipants;
struct clientDesc {
struct sockaddr_in addr;
int used;
int capabilities;
unsigned int rcvbuf;
} clientTable[MAX_CLIENTS];
};
int addParticipant(participantsDb_t,
struct sockaddr_in *addr,
int capabilities,
unsigned int rcvbuf,
int pointopoint);
int isParticipantValid(struct participantsDb *db, int i) {
return db->clientTable[i].used;
}
int removeParticipant(struct participantsDb *db, int i) {
if(db->clientTable[i].used) {
char ipBuffer[16];
flprintf("Disconnecting #%d (%s)\n", i,
getIpString(&db->clientTable[i].addr, ipBuffer));
#ifdef USE_SYSLOG
syslog(LOG_INFO, "Disconnecting #%d (%s)\n", i,
getIpString(&db->clientTable[i].addr, ipBuffer));
#endif
db->clientTable[i].used = 0;
db->nrParticipants--;
}
return 0;
}
int lookupParticipant(struct participantsDb *db, struct sockaddr_in *addr) {
int i;
for (i=0; i < MAX_CLIENTS; i++) {
if (db->clientTable[i].used &&
ipIsEqual(&db->clientTable[i].addr, addr)) {
return i;
}
}
return -1;
}
int nrParticipants(participantsDb_t db) {
return db->nrParticipants;
}
int addParticipant(participantsDb_t db,
struct sockaddr_in *addr,
int capabilities,
unsigned int rcvbuf,
int pointopoint) {
int i;
if((i = lookupParticipant(db, addr)) >= 0)
return i;
for (i=0; i < MAX_CLIENTS; i++) {
if (!db->clientTable[i].used) {
char ipBuffer[16];
db->clientTable[i].addr = *addr;
db->clientTable[i].used = 1;
db->clientTable[i].capabilities = capabilities;
db->clientTable[i].rcvbuf = rcvbuf;
db->nrParticipants++;
flprintf("New connection from %s (#%d) %08x\n",
getIpString(addr, ipBuffer), i, capabilities);
#ifdef USE_SYSLOG
syslog(LOG_INFO, "New connection from %s (#%d)\n",
getIpString(addr, ipBuffer), i);
#endif
return i;
} else if(pointopoint)
return -1;
}
return -1; /* no space left in participant's table */
}
participantsDb_t makeParticipantsDb(void)
{
return MALLOC(struct participantsDb);
}
int getParticipantCapabilities(participantsDb_t db, int i)
{
return db->clientTable[i].capabilities;
}
unsigned int getParticipantRcvBuf(participantsDb_t db, int i)
{
return db->clientTable[i].rcvbuf;
}
struct sockaddr_in *getParticipantIp(participantsDb_t db, int i)
{
return &db->clientTable[i].addr;
}
void printNotSet(participantsDb_t db, char *d)
{
int first=1;
int i;
fprintf(stderr, "[");
for (i=0; i < MAX_CLIENTS; i++) {
if (db->clientTable[i].used) {
if(!BIT_ISSET(i, d)) {
if(!first)
fprintf(stderr,",");
first=0;
fprintf(stderr, "%d", i);
}
}
}
fprintf(stderr, "]");
}
void printSet(participantsDb_t db, char *d)
{
int first=1;
int i;
fprintf(stderr, "[");
for (i=0; i < MAX_CLIENTS; i++) {
if (db->clientTable[i].used) {
if(BIT_ISSET(i, d)) {
if(!first)
fprintf(stderr,",");
first=0;
fprintf(stderr, "%d", i);
}
}
}
fprintf(stderr, "]");
}