-
Notifications
You must be signed in to change notification settings - Fork 36
/
server_client.c
222 lines (194 loc) · 6.13 KB
/
server_client.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Copyright (c) 2012, Paula Roquero Fuentes <[email protected]>
Copyright (c) 2012, Bartosz Andrzej Zawada <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED AS IS AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <errno.h>
#include "common.h"
#include "server_client.h"
/* Receive a message with a final empty line and possibly content
* returns: 0 on bad message, -1 on closed socket, message length if ok */
int receive_message(int sockfd, char * buf, int buf_size) {
int ret = 0;
int read = 0;
char * tmp = 0;
size_t tmp_size = 0;
int content_length = 0;
FILE * f = 0;
f = fdopen(sockfd, "r");
do {
read = getline(&tmp, &tmp_size, f);
if (read <= 0) {
fclose(f);
free(tmp);
return(-1);
}
if (read+ret >= buf_size) {
fclose(f);
free(tmp);
return(0);
}
if (!content_length && !strncmp(tmp, "Content-Length:", 15))
if (sscanf(tmp, "Content-Length: %d", &content_length) < 1)
content_length = 0;
strcpy(buf+ret, tmp);
ret += read;
} while (tmp[0] != '\r');
if (content_length) {
content_length += ret;
do {
read = getline(&tmp, &tmp_size, f);
if (read <= 0) {
fclose(f);
free(tmp);
return(-1);
}
if (read+ret >= buf_size) {
fclose(f);
free(tmp);
return(0);
}
memcpy(buf+ret, tmp, read);
ret += read;
} while (ret < content_length);
}
buf[ret] = 0;
fprintf(stderr, "\n########## RECEIVED ##########\n%s\n##############################\n", buf);
free(tmp);
return ret;
}
int extract_uri(char *uri, char **host, char **path) {
char *path_start;
int uri_len;
*host = 0;
*path = 0;
if (!uri)
return(0);
uri_len = strlen(uri);
if (uri_len < 8)
return(0);
*host = 0;
*path = 0;
if (memcmp(uri, "rtsp://", 7))
return(0);
uri += 7;
uri_len -= 7;
path_start = strstr(uri, "/");
if (!path_start) {
/* If there isn't a path */
*host = malloc(uri_len + 1);
if (!*host)
return(0);
strcpy(*host, uri);
(*host)[uri_len] = 0;
*path = 0;
return(1);
}
/* If there is a '/' separator */
if ((path_start - uri) == 0) {
*host = 0;
*path = 0;
return(0);
}
/* Copy host */
*host = malloc(path_start - uri + 1);
if (!*host) {
*host = 0;
*path = 0;
return(0);
}
strncpy(*host, uri, path_start - uri);
(*host)[path_start - uri] = 0;
/* Check if there is a path after the separator */
if (uri_len > path_start - uri + 1) {
*path = malloc(strlen(path_start) + 1);
if (!*path)
return(0);
strcpy(*path, path_start);
(*path)[strlen(path_start)] = 0;
}
return(1);
}
/* Binds two consecutive UDP ports and returns the first port number
* rtp_sockfd: file descriptor for the rtp socket. -1 if error
* rtcp_sockfd: file descriptor for the rtcp socket. -1 if error
* returns: number of the first port or 0 if error
*/
int bind_UDP_ports(int *rtp_sockfd, int *rtcp_sockfd) {
unsigned short first_port;
struct addrinfo hints, *res;
char port_str[6];
int st;
int counter = 0;
do {
*rtp_sockfd = -1;
*rtcp_sockfd = -1;
if (counter == MAX_UDP_BIND_ATTEMPTS)
return(0);
first_port = rand();
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
if (!snprintf(port_str, 5, "%d", first_port))
return(0);
if (getaddrinfo(0, port_str, &hints, &res))
return(0);
*rtp_sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (*rtp_sockfd == -1)
return(0);
st = bind(*rtp_sockfd, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
if (st == -1) {
*rtp_sockfd = -1;
continue;
}
if (!snprintf(port_str, 5, "%d", first_port + 1)) {
close(*rtp_sockfd);
*rtp_sockfd = -1;
return(0);
}
if (getaddrinfo(0, port_str, &hints, &res)) {
close(*rtp_sockfd);
*rtp_sockfd = -1;
return(0);
}
*rtcp_sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (*rtcp_sockfd == -1) {
close(*rtp_sockfd);
*rtp_sockfd = -1;
return(0);
}
st = bind(*rtcp_sockfd, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
if (st == -1) {
close(*rtp_sockfd);
*rtp_sockfd = -1;
*rtcp_sockfd = -1;
continue;
}
++counter;
} while (*rtp_sockfd == -1);
return(first_port);
}
/* Puts the thread to sleep
* thread: Thread which will be put to sleep
* sec: amount of seconds it will sleep
* usec: amount of microseconds it will sleep
* NOTE: It will sleep seconds + nanoseconds.
*/
void time_sleep(int sec, int usec) {
struct timespec t, tr;
t.tv_sec = sec;
t.tv_nsec = usec * 1000;
nanosleep(&t, &tr);
}