This repository has been archived by the owner on Nov 27, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
178 lines (144 loc) · 3.57 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#define MAX_CONNECTIONS 100
#define CHUNK_SIZE 256
#define SEND_TIMEOUT 10
#define RECV_TIMEOUT 3
int parseInput(int fd)
{
size_t bufsize = CHUNK_SIZE;
char *buf = (char*)malloc(bufsize);
struct timeval begin;
gettimeofday(&begin, NULL);
// Receive the send message and store it
size_t size = 0;
while(1){
struct timeval now;
gettimeofday(&now, NULL);
double timediff = (now.tv_sec - begin.tv_sec) + 1e-6 * (now.tv_usec - begin.tv_usec);
if((size > 0 && timediff > RECV_TIMEOUT) || timediff > RECV_TIMEOUT * 2){
fprintf(stderr, "Time out on connection!\n");
return -1;
}
char data[CHUNK_SIZE];
int status = recv(fd, data, CHUNK_SIZE, 0);
if(status <= 0){
usleep(10000);
}else{
if(size + status > bufsize){
bufsize <<= 1;
buf = (char*)realloc(buf, bufsize);
}
memcpy(buf + size, data, status);
size += status;
gettimeofday(&begin, NULL);
if(status < CHUNK_SIZE){
break;
}
}
}
// Find sequence ID
int pos = 0;
while(buf[++pos] != '\n');
int seq;
sscanf(buf + pos + sizeof("Cseq:"), "%d", &seq);
#define SEND_MESSAGE(_m, _a...) \
char _b[sizeof(#_m) + 256];\
sprintf(_b, _m, ##_a); \
printf(_m, ##_a); \
write(fd, _b, strlen(_b));
printf("%s\n", buf);
// Send reply
if(strncmp(buf, "OPTIONS", 4) == 0){
SEND_MESSAGE("RTSP/1.0 200 OK\r\nCSeq: %d\r\nPublic: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE\r\n\r\n", seq);
}else if(strncmp(buf, "DESCRIBE", 4) == 0){
const char description[] = "v=0\r\n"
"i=Test server\r\n"
"a=recvonly\r\n"
"a=type:broadcast\r\n"
"m=audio 0 RTP/AVP 14\r\n"
"b=AS:320\r\n"
"b=RR:0\r\n"
"a=rtpmap:14 MPA/90000/2\r\n"
// "a=control:streamid=1\r\n"
// "a=range:-1\r\n"
// "a=length:-1\r\n";
;
SEND_MESSAGE("RTSP/1.0 200 OK\r\nCSeq: %d\r\nContent-Type: application/sdp\r\nContent-Length: %d\r\n\r\n%s", seq, (int)strlen(description), description);
}else if(strncmp(buf, "SETUP", 4) == 0){
SEND_MESSAGE("RTSP/10. 200 OK\r\nCseq: %d\r\nTransport: RTP/AVP;unicast;client_port=4588-4589;server_port=6256-6257\r\n\r\n", seq);
}
return 0;
}
int port, fd, childfd;
void stop(int sig)
{
printf("\n%d - Closing server\n", sig);
close(fd);
close(childfd);
exit(0);
}
int main(int argc, char *argv[])
{
port = 554;
int i;
for(i = 1; i < argc; i++){
if(argv[i][0] == '-'){
switch(argv[i][1]){
case 'p':
sscanf(argv[i + 1], "%d", &port);
i++;
break;
}
}
}
printf("Port: %d\n", port);
childfd = 0;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(fd < 0){
perror(NULL);
close(fd);
return 1;
}
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
int yes = 1;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0){
goto error;
}
if(bind(fd, (struct sockaddr*)&servaddr, sizeof(struct sockaddr_in)) < 0){
goto error;
}
if(listen(fd, MAX_CONNECTIONS) < 0){
goto error;
}
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
signal(SIGINT, stop);
while(1){
struct sockaddr_in cliaddr;
unsigned int clilen = sizeof(cliaddr);
childfd = accept(fd, (struct sockaddr*)&cliaddr, &clilen);
if(childfd > 0){
fcntl(childfd, F_SETFL, fcntl(childfd, F_GETFL) | O_NONBLOCK);
while(parseInput(childfd) != -1);
close(childfd);
}
}
close(fd);
return 0;
error:
printf("Error!\n");
perror(NULL);
close(fd);
close(childfd);
return 1;
}