Skip to content

Commit a0586b4

Browse files
committed
Sender can now also send streams to an IP that requests it. Useful for fallback UDP mode.
1 parent 932ed9f commit a0586b4

File tree

3 files changed

+82
-34
lines changed

3 files changed

+82
-34
lines changed

server/main.c

+30-14
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ struct TcpClient {
3232
TcpClient *next;
3333
};
3434

35-
TcpClient *clients;
36-
35+
TcpClient *clients=NULL;
3736

3837
int cycleLenMs=60000; //cycle defaults to 1 min
3938
struct timeval cycleStart;
@@ -52,8 +51,13 @@ int cycleRemainingMs() {
5251
}
5352

5453

55-
int createSocket(int port) {
56-
int fd=socket(AF_INET, SOCK_STREAM, 0);
54+
int createSocket(int port, int isUdp) {
55+
int fd;
56+
if (!isUdp) {
57+
fd=socket(AF_INET, SOCK_STREAM, 0);
58+
} else {
59+
fd=socket(AF_INET, SOCK_DGRAM, 0);
60+
}
5761
if (fd<=0) {
5862
perror("creating socket");
5963
exit(1);
@@ -72,11 +76,12 @@ int createSocket(int port) {
7276
exit(1);
7377
}
7478

75-
if (listen(fd, 8)<0) {
76-
perror("listen");
77-
exit(1);
79+
if (!isUdp) {
80+
if (listen(fd, 8)<0) {
81+
perror("listen");
82+
exit(1);
83+
}
7884
}
79-
8085
return fd;
8186
}
8287

@@ -189,12 +194,10 @@ static void handleClient(TcpClient *cl) {
189194

190195

191196
int main(int argc, char **argv) {
192-
int listenFd;
197+
int listenFd, udpFd;
193198
senderInit();
194-
if (argc==1) {
195-
senderAddDest("192.168.5.255");
196-
} else {
197-
senderAddDest(argv[1]);
199+
for (int i=1; i<argc; i++) {
200+
senderAddDest(argv[i], 0);
198201
}
199202

200203
#ifndef SIMULATE_PACKET_LOSS
@@ -207,7 +210,8 @@ int main(int argc, char **argv) {
207210
serdesInit(fecSend, fecGetMaxPacketLength());
208211
hlmuxInit(serdesSend, serdesGetMaxPacketLength());
209212

210-
listenFd=createSocket(2017);
213+
listenFd=createSocket(2017, 0);
214+
udpFd=createSocket(2017, 1);
211215

212216
newCycle();
213217
while(1) {
@@ -216,7 +220,9 @@ int main(int argc, char **argv) {
216220
struct timeval tout;
217221
FD_ZERO(&rfds);
218222
FD_SET(listenFd, &rfds);
223+
FD_SET(udpFd, &rfds);
219224
max=listenFd;
225+
if (max<udpFd) max=udpFd;
220226
for (TcpClient *i=clients; i!=NULL; i=i->next) {
221227
FD_SET(i->fd, &rfds);
222228
if (max<i->fd) max=i->fd;
@@ -255,6 +261,16 @@ int main(int argc, char **argv) {
255261
clients=newc;
256262
printf("Accepted client\n");
257263
}
264+
if (FD_ISSET(udpFd, &rfds)) {
265+
char buf[16];
266+
struct sockaddr src;
267+
int l;
268+
socklen_t srclen;
269+
l=recvfrom(udpFd, buf, sizeof(buf), 0, &src, &srclen);
270+
if (l>0 && buf[0]=='C') {
271+
senderAddDestSockaddr(&src, srclen, 10*60);
272+
}
273+
}
258274

259275
for (TcpClient *i=clients; i!=NULL; i=i->next) {
260276
if (FD_ISSET(i->fd, &rfds)) {

server/sender.c

+50-19
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
#include <stdio.h>
1010
#include <stdlib.h>
1111
#include <string.h>
12+
#include <time.h>
1213

1314
typedef struct SenderDstItem SenderDstItem;
1415

1516
struct SenderDstItem{
16-
char *name;
1717
struct sockaddr *addr;
1818
socklen_t addrlen;
19+
time_t timeout;
1920
SenderDstItem *next;
2021
};
2122

@@ -41,10 +42,34 @@ int senderInit() {
4142
#define str(a) str2(a)
4243
#define str2(a) #a
4344

44-
int senderAddDest(char *hostname) {
45+
int senderAddDestSockaddr(struct sockaddr *addr, socklen_t addrlen, int timeout) {
46+
SenderDstItem *dest;
47+
for (dest=senderDest; dest!=NULL; dest=dest->next) {
48+
if (dest->addrlen==addrlen && memcmp(dest->addr, addr, addrlen)==0) break;
49+
}
50+
if (dest!=NULL) {
51+
//Just modify timeout.
52+
dest->timeout=time(NULL)+timeout;
53+
} else {
54+
//Allocate new dest struct, and everything in it
55+
dest=malloc(sizeof(SenderDstItem));
56+
memset(dest, 0, sizeof(SenderDstItem));
57+
dest->addr=malloc(addrlen);
58+
//Copy over info
59+
memcpy(dest->addr, addr, addrlen);
60+
dest->addrlen=addrlen;
61+
dest->timeout=time(NULL)+timeout;
62+
//Add into linked list
63+
dest->next=senderDest;
64+
senderDest=dest;
65+
}
66+
return 1;
67+
}
68+
69+
70+
int senderAddDest(char *hostname, int timeout) {
4571
const char* portname="2017";
4672
struct addrinfo hints;
47-
SenderDstItem *dest;
4873
memset(&hints,0,sizeof(hints));
4974
//Grab address info for hostname
5075
hints.ai_family=AF_UNSPEC;
@@ -58,21 +83,7 @@ int senderAddDest(char *hostname) {
5883
perror(hostname);
5984
return 0;
6085
}
61-
//Allocate new dest struct, and everything in it
62-
dest=malloc(sizeof(SenderDstItem));
63-
memset(dest, 0, sizeof(SenderDstItem));
64-
dest->addr=malloc(res->ai_addrlen);
65-
dest->name=malloc(strlen(hostname)+1);
66-
//Copy over info
67-
strcpy(dest->name, hostname);
68-
memcpy(dest->addr, res->ai_addr, res->ai_addrlen);
69-
dest->addrlen=res->ai_addrlen;
70-
//Add into linked list
71-
dest->next=senderDest;
72-
senderDest=dest;
73-
//All done.
74-
freeaddrinfo(res);
75-
return 1;
86+
return senderAddDestSockaddr(res->ai_addr, res->ai_addrlen, timeout);
7687
}
7788

7889
#define PAD_LENGTH 0
@@ -89,11 +100,31 @@ void senderSendPkt(uint8_t *packet, size_t len) {
89100
// printf("Sending 0x%X bytes\n", len);
90101
r=sendto(senderFd, ppacket, len, 0, dst->addr, dst->addrlen);
91102
if (r==-1) {
92-
perror(dst->name);
103+
dst->timeout=1; //instantly remove next
93104
}
94105
dst=dst->next;
95106
}
96107
free(ppacket);
108+
109+
//Housekeeping: see if there are dests that need to be killed
110+
int notDone=1;
111+
do {
112+
SenderDstItem *dest, *pdest=NULL;
113+
for (dest=senderDest; dest!=NULL; dest=dest->next) {
114+
if (dest->timeout!=0 && dest->timeout<time(NULL)) break;
115+
pdest=dest;
116+
}
117+
if (dest) {
118+
if (pdest) {
119+
pdest->next=dest->next;
120+
} else {
121+
senderDest=dest->next;
122+
}
123+
free(dest);
124+
} else {
125+
notDone=0;
126+
}
127+
} while (notDone);
97128
}
98129

99130

server/sender.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include "sendif.h"
55

66
int senderInit();
7-
int senderAddDest(char *hostname);
7+
int senderAddDest(char *hostname, int timeout);
8+
int senderAddDestSockaddr(struct sockaddr *addr, socklen_t addrlen, int timeout);
89
void senderSendPkt(uint8_t *packet, size_t len);
910
int senderGetMaxPacketLength();
1011

0 commit comments

Comments
 (0)