Created
October 8, 2019 09:15
-
-
Save mirkobrankovic/d18bc4af88cef1ecd0cc579e845b80b4 to your computer and use it in GitHub Desktop.
client_sender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <netdb.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#define MAX_GELF_MSG_LEN 512 | |
char *randstring(size_t length) { | |
static char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!"; | |
char *randomString = NULL; | |
if (length) { | |
randomString = malloc(sizeof(char) * (length +1)); | |
if (randomString) { | |
for (int n = 0;n < length;n++) { | |
int key = rand() % (int)(sizeof(charset) -1); | |
randomString[n] = charset[key]; | |
} | |
randomString[length] = '\0'; | |
} | |
} | |
return randomString; | |
} | |
int main(int argc, char *argv[]) { | |
int sockfd, portno, n; | |
size_t len; | |
struct sockaddr_in serv_addr; | |
struct hostent *server; | |
char buffer[8192]; | |
char *buf = buffer; | |
strcpy(buffer, "{jsep:{type:answer,sdp:v=0o=- 9135309861160311696 2 IN IP4 89.184.190.129s=VideoRoom 12345t=0 0a=group:BUNDLE 0 1a=msid-semantic: WMS janusm=audio 9 UDP/TLS/RTP/SAVPF 111c=IN IP4 89.184.190.129a=recvonlya=mid:0a=rtcp-muxa=ice-ufrag:Lj0pa=ice-pwd:I5qLnLIhzLjuhgLcSvwOCYa=ice-options:tricklea=fingerprint:sha-256 B3:5C:1A:BD:BA:9D:6D:37:C6:ED:34:78:B6:66:53:7E:11:21:CD:4A:D4:34:53:A1:37:2A:8D:A7:D0:4B:BD:2Ba=setup:activea=rtpmap:111 opus/48000/2a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-levela=extmap:2 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01a=extmap:3 urn:ietf:params:rtp-hdrext:sdes:mida=extmap:4 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-ida=extmap:5 urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-ida=msid:janus janusa0a=ssrc:3355965927 cname:janusa=ssrc:3355965927 msid:janus janusa0a=ssrc:3355965927 mslabel:janusa=ssrc:3355965927 label:janusa0a=candidate:1 1 udp 2013266431 172.18.31.129 27511 typ hosta=candidate:2 1 udp 2013266430 172.18.7.129 21695 typ hosta=candidate:3 1 udp 2013266429 172.18.11.129 31202 typ hosta=candidate:4 1 udp 2013266428 89.184.190.129 20878 typ hosta=end-of-candidatesm=video 9 UDP/TLS/RTP/SAVPF 96c=IN IP4 89.184.190.129a=recvonlya=mid:1a=rtcp-muxa=ice-ufrag:Lj0pa=ice-pwd:I5qLnLIhzLjuhgLcSvwOCYa=ice-options:tricklea=fingerprint:sha-256 B3:5C:1A:BD:BA:9D:6D:37:C6:ED:34:78:B6:66:53:7E:11:21:CD:4A:D4:34:53:A1:37:2A:8D:A7:D0:4B:BD:2Ba=setup:activea=rtpmap:96 VP8/90000a=rtcp-fb:96 ccm fira=rtcp-fb:96 nacka=rtcp-fb:96 nack plia=rtcp-fb:96 goog-remba=rtcp-fb:96 transport-cca=extmap:12 urn:3gpp:video-orientationa=extmap:2 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01a=extmap:11 http://www.webrtc.org/experiments/rtp-hdrext/playout-delaya=extmap:8 http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07a=extmap:3 urn:ietf:params:rtp-hdrext:sdes:mida=extmap:4 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-ida=extmap:5 urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-ida=msid:janus janusv0a=ssrc:1222020561 cname:janusa=ssrc:1222020561 msid:janus janusv0a=ssrc:1222020561 mslabel:janusa=ssrc:1222020561 label:janusv0a=candidate:1 1 udp 2013266431 172.18.31.129 27511 typ hosta=candidate:2 1 udp 2013266430 172.18.7.129 21695 typ hosta=candidate:3 1 udp 2013266429 172.18.11.129 31202 typ hosta=candidate:4 1 udp 2013266428 89.184.190.129 20878 typ hosta=end-of-candidates},owner:local}"); | |
len = strlen(buffer); | |
fprintf(stderr, "Length of buffer: %d\n", len); | |
fprintf(stderr, "----------------------------------------------------------------------------------------------------------\n"); | |
if (argc < 3) { | |
fprintf(stderr,"usage %s hostname port\n", argv[0]); | |
return 0; | |
} | |
//--------------NETWORK PART--------------------------- | |
portno = atoi(argv[2]); | |
sockfd = socket(AF_INET, SOCK_DGRAM, 0); | |
if (sockfd < 0) | |
fprintf(stderr, "ERROR opening socket\n"); | |
server = gethostbyname(argv[1]); | |
if (server == NULL) { | |
fprintf(stderr,"ERROR, no such host\n"); | |
} | |
bzero((char *) &serv_addr, sizeof(serv_addr)); | |
serv_addr.sin_family = AF_INET; | |
bcopy((char *)server->h_addr_list[0], | |
(char *)&serv_addr.sin_addr.s_addr, | |
server->h_length); | |
serv_addr.sin_port = htons(portno); | |
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) | |
fprintf(stderr, "ERROR connecting\n"); | |
//--------------NETWORK PART--------------------------- | |
int total = len / MAX_GELF_MSG_LEN + 1; | |
fprintf(stderr, "Total packets: %d\n", total); | |
fprintf(stderr, "----------------------------------------------------------------------------------------------------------\n"); | |
int offset = 0; | |
char *rnd = randstring(8); | |
//fprintf(stderr, "Buffer: %s\n", buffer); | |
for (int i = 0; i < total; i++) { | |
fprintf(stderr, "Packet No: %d\n", i+1); | |
fprintf(stderr, "----------------------------------------------------------------------------------------------------------\n"); | |
fprintf(stderr, "offset: %d\n", offset); | |
fprintf(stderr, "----------------------------------------------------------------------------------------------------------\n"); | |
int bytesToSend = offset + MAX_GELF_MSG_LEN < len ? MAX_GELF_MSG_LEN : len - offset; | |
fprintf(stderr, "bytesToSend: %d\n", bytesToSend); | |
char chunk[bytesToSend]; | |
// char chunk[bytesToSend + 12]; | |
// chunk[0] = 0x1e; | |
// chunk[1] = 0x0f; | |
// strncpy(chunk + 2, rnd, 8); | |
// chunk[10] = (char)i+1; | |
// chunk[11] = (char)total+1; | |
fprintf(stderr, "----------------------------------------------------------------------------------------------------------\n"); | |
fprintf(stderr, "chunk: %s\n", chunk); | |
fprintf(stderr, "----------------------------------------------------------------------------------------------------------\n"); | |
//TODO | |
strncpy(chunk, buf, bytesToSend); | |
size_t len_chunk = strlen(chunk); | |
fprintf(stderr, "Length od chunk: %d\n", len_chunk); | |
//chunk[bytesToSend+1] = '\0'; | |
buf = buffer+offset; | |
fprintf(stderr, "----------------------------------------------------------------------------------------------------------\n"); | |
fprintf(stderr, "BUF: %s\n", (char *)buf); | |
fprintf(stderr, "----------------------------------------------------------------------------------------------------------\n"); | |
fprintf(stderr, "chunk2: %s\n", chunk); | |
fprintf(stderr, "----------------------------------------------------------------------------------------------------------\n"); | |
// ovo je OK | |
offset += bytesToSend; | |
if (write(sockfd, chunk, bytesToSend) < 0) { | |
// if (write(sockfd, chunk, bytesToSend + 12) < 0) { | |
fprintf(stderr, "ERROR writing to socket\n"); | |
printf(" Value of errno: %s\n ", strerror(errno)); | |
} | |
bzero(chunk, len_chunk); | |
} | |
free(rnd); | |
bzero(buffer, 8192); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment