-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.c
executable file
·272 lines (207 loc) · 7.84 KB
/
stream.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "stream.h"
#define X(a,b) b,
char *codecStrings[] = {
CODEC_TABLE
};
#undef X
static struct MediaStream * findStream(char *, char *, int, int, struct sniff_rtp *, enum CODEC, char);
static struct MediaStream * addNewStream(char *, char *, int, int, struct sniff_rtp *, enum CODEC, char);
static int checkRTPStream(struct sniff_rtp *);
static int copyStreamValues(struct MediaStream **, char *,char *,int, int, struct sniff_rtp *, enum CODEC, char);
static void deleteStream(struct MediaStream *);
struct MediaStream *Head;
struct MediaStream *Tail;
int streamCount;
extern int checkParameterSets;
extern char *outputBaseFile;
struct MediaStream * streamHandler(char *srcIP, char *dstIP, int srcPort, int dstPort, struct sniff_rtp *rtp, enum CODEC codec, char codecType){
struct MediaStream *currentMS = NULL;
currentMS = findStream(srcIP, dstIP, srcPort, dstPort, rtp, codec, codecType);
if(currentMS == NULL){
int check = checkRTPStream(rtp);
if(check == 1){
currentMS = addNewStream(srcIP, dstIP, srcPort, dstPort, rtp, codec, codecType);
if(currentMS == NULL){
printf("[-]Cannot add new stream from %s(%d) to %s(%d)\n",srcIP, srcPort, dstIP, dstPort);
return NULL;
}
printf("added new stream. :%s(%d) to %s(%d). codec is %02x\n", srcIP, srcPort, dstIP, dstPort, codecType);
return currentMS;
}
//printf("[-]Identified packet is not a valid RTP stream. %s(%d) to %s(%d)\n", srcIP, srcPort, dstIP, dstPort);
return NULL;
}
return currentMS;
}
static struct MediaStream * findStream(char *srcIP, char *dstIP, int srcPort, int dstPort, struct sniff_rtp *rtp, enum CODEC codec, char codecType){
struct MediaStream *currentMS = Head;
while(currentMS != NULL){
if((!strcmp(currentMS->srcIP,srcIP)) && (!strcmp(currentMS->dstIP,dstIP)) && (currentMS->srcPort == srcPort) && ((currentMS->dstPort == dstPort)) && (currentMS->rtpPTR->payloadType == (rtp->payloadType & 0x7F)) && (currentMS->codecType == codecType) && (currentMS->codec == codec))
{
#ifdef ARCH_X64
if(currentMS->rtpPTR->ssrc == bswap_32(rtp->ssrc))
{
return currentMS;
}
#else
if(currentMS->rtpPTR->ssrc == ntohl(rtp->ssrc))
{
return currentMS;
}
#endif
}
currentMS = currentMS -> next;
}
return NULL;
}
static struct MediaStream * addNewStream(char *srcIP, char *dstIP, int srcPort, int dstPort, struct sniff_rtp *rtp, enum CODEC codec, char codecType){
if((Head == NULL) && (Tail == NULL)){
/* Probably the first media stream */
struct MediaStream *currentMediaStream = (struct MediaStream *) malloc(1 * sizeof(struct MediaStream));
if(currentMediaStream == NULL){
printf("[-]Not enough memory available for allocation: %s\n",strerror(errno));
return NULL;
}
int copyreturn = copyStreamValues(¤tMediaStream,srcIP,dstIP,srcPort,dstPort,rtp,codec,codecType);
if(copyreturn < 0){
deleteStream(currentMediaStream);
return NULL;
}
Head = Tail = currentMediaStream;
return currentMediaStream;
}
else{
struct MediaStream *currentMediaStream = (struct MediaStream *) malloc(1 * sizeof(struct MediaStream));
if(currentMediaStream == NULL){
printf("[-]Not enough memory available for allocation: %s\n",strerror(errno));
return NULL;
}
int copyreturn = copyStreamValues(¤tMediaStream,srcIP,dstIP,srcPort,dstPort,rtp,codec,codecType);
if(copyreturn < 0){
deleteStream(currentMediaStream);
return NULL;
}
Tail->next = currentMediaStream;
Tail = currentMediaStream;
return currentMediaStream;
}
}
static int checkRTPStream(struct sniff_rtp *rtp){
#ifdef ARCH_X64
if((bswap_16(rtp->sequence_no) <= 65535) && (bswap_32(rtp->timestamp) <= 4294967295) && (bswap_32(rtp->ssrc) <= 4294967295) && ((rtp->version & 0xC0) == 0x80))
{
return 1;
}
#else
if((ntohs(rtp->sequence_no) <= 65535) && (ntohl(rtp->timestamp) <= 4294967295) && (ntohl(rtp->ssrc) <= 4294967295) && ((rtp->version & 0XC0) == 0x80)){
return 1;
}
#endif
return 0;
}
int copyStreamValues(struct MediaStream **currentMediaStreamPtr, char *srcIP, char *dstIP, int srcPort, int dstPort, struct sniff_rtp *rtp, enum CODEC codec, char codecType){
struct MediaStream *currentMediaStream = *currentMediaStreamPtr;
unsigned int remLength = 0;
strncpy(currentMediaStream->srcIP, srcIP, sizeof(currentMediaStream->srcIP)-1);
strncpy(currentMediaStream->dstIP, dstIP, sizeof(currentMediaStream->dstIP)-1);
currentMediaStream->srcPort = srcPort;
currentMediaStream->dstPort = dstPort;
currentMediaStream->rtpPTR = (struct sniff_rtp *) malloc(1 * sizeof(struct sniff_rtp));
if(currentMediaStream->rtpPTR == NULL){
printf("[-]Not enough memory available for allocation\n");
return -1;
}
currentMediaStream->rtpPTR->payloadType = (rtp->payloadType & 0x7F);
#ifdef ARCH_X64
currentMediaStream->rtpPTR->sequence_no = bswap_16(rtp->sequence_no);
#else
currentMediaStream->rtpPTR->sequence_no = ntohs(rtp->sequence_no);
#endif
#ifdef ARCH_X64
currentMediaStream->rtpPTR->timestamp = bswap_32(rtp->timestamp);
#else
currentMediaStream->rtpPTR->timestamp = ntohl(rtp->timestamp);
#endif
#ifdef ARCH_X64
currentMediaStream->rtpPTR->ssrc = bswap_32(rtp->ssrc);
#else
currentMediaStream->rtpPTR->ssrc = ntohl(rtp->ssrc);
#endif
currentMediaStream->previousSequenceNo = 0;
streamCount += 1;
currentMediaStream->streamNumber = streamCount;
if(checkParameterSets == 1){
currentMediaStream->receivedParameterSets = 0;
}
else{
currentMediaStream->receivedParameterSets = 1;
}
char *codecStringValue = codecStrings[codec];
if(outputBaseFile == NULL)
remLength = (sizeof(currentMediaStream->mediaFileName) - 1) - strlen(codecStringValue);
else
remLength = (sizeof(currentMediaStream->mediaFileName) - 1) - strlen(outputBaseFile);
memset(currentMediaStream->mediaFileName,'\0',sizeof(currentMediaStream->mediaFileName));
if(outputBaseFile == NULL)
strncpy(currentMediaStream->mediaFileName,codecStringValue,(sizeof(currentMediaStream->mediaFileName)-remLength));
else
strncpy(currentMediaStream->mediaFileName,outputBaseFile,(sizeof(currentMediaStream->mediaFileName)-remLength));
char stringStreamNumber[6]; /* Hoping the number of streams don't exceed 99,999 */
snprintf(stringStreamNumber, sizeof(stringStreamNumber), "%d", streamCount);
int restLength = strlen("-media-.wav") + strlen(stringStreamNumber) + 1;
//char tempString[restLength];
char *tempString = (char *) calloc(restLength, sizeof(char));
if(tempString == NULL){
printf("[-]Not enough memory for allocation:%s\n",strerror(errno));
return -1;
}
strcpy(tempString,"-media-");
strcat(tempString,stringStreamNumber);
switch(codec){
case h264:
strcat(tempString,".264");
break;
case g711alaw:
case g711ulaw:
case g722:
case g729:
case g723:
default:
strcat(tempString,".wav");
break;
}
strncat(currentMediaStream->mediaFileName,tempString,remLength);
currentMediaStream->fp = fopen(currentMediaStream->mediaFileName,"w+|b");
if(currentMediaStream->fp == NULL){
printf("[-]Cannot open file %s: %s\n", currentMediaStream->mediaFileName, strerror(errno));
return -1;
}
currentMediaStream->codec = codec;
currentMediaStream->codecType = codecType;
currentMediaStream->count = 0;
currentMediaStream->fuaStart = 0;
currentMediaStream->next = NULL;
return 0;
}
static void deleteStream(struct MediaStream *currentMS){
if(currentMS != NULL)
free(currentMS);
}
void deleteAllStreams(){
struct MediaStream *currentMS = Head;
int streamCounter = 0;
while(currentMS != NULL){
if(currentMS->fp != NULL){
streamCounter += 1;
printf("[+]Stream saved to file %s\n",currentMS->mediaFileName);
fclose(currentMS->fp);
}
struct MediaStream *temp = currentMS->next;
free(currentMS);
currentMS = temp;
}
if(!streamCounter)
printf("[-]No RTP media stream found\n");
else
printf("[+]Number of streams found are %d\n",streamCounter);
}