| 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 |
#include <stdlib.h> |
| 50 |
#include <stdio.h> |
| 51 |
#include <string.h> |
| 52 |
#include <netdb.h> |
| 53 |
#include <unistd.h> |
| 54 |
#include <sys/types.h> |
| 55 |
#include <sys/socket.h> |
| 56 |
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__Darwin__) || defined(__CYGWIN__) || defined(__DragonFly__) |
| 57 |
#include <netinet/in.h> |
| 58 |
#endif |
| 59 |
#include <sys/time.h> |
| 60 |
|
| 61 |
|
| 62 |
int main(int argc, char *argv[]) |
| 63 |
{ |
| 64 |
struct sockaddr_in sin; |
| 65 |
struct hostent *hp; |
| 66 |
int s; |
| 67 |
int res; |
| 68 |
char buf[2048]; |
| 69 |
fd_set wfds; |
| 70 |
struct timeval tv; |
| 71 |
|
| 72 |
if (argc != 3) { |
| 73 |
fprintf(stderr, "streamplayer -- A utility for reading from a raw TCP stream.\n"); |
| 74 |
fprintf(stderr, "Written for use with Asterisk (http://www.asterisk.org)\n"); |
| 75 |
fprintf(stderr, "Copyright (C) 2005 -- Russell Bryant -- Digium, Inc.\n\n"); |
| 76 |
fprintf(stderr, "Usage: ./streamplayer <ip> <port>\n"); |
| 77 |
exit(1); |
| 78 |
} |
| 79 |
|
| 80 |
hp = gethostbyname(argv[1]); |
| 81 |
if (!hp) { |
| 82 |
fprintf(stderr, "Unable to lookup IP for host '%s'\n", argv[1]); |
| 83 |
exit(1); |
| 84 |
} |
| 85 |
|
| 86 |
memset(&sin, 0, sizeof(sin)); |
| 87 |
|
| 88 |
sin.sin_family = AF_INET; |
| 89 |
sin.sin_port = htons(atoi(argv[2])); |
| 90 |
memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr)); |
| 91 |
|
| 92 |
s = socket(AF_INET, SOCK_STREAM, 0); |
| 93 |
|
| 94 |
if (s < 0) { |
| 95 |
fprintf(stderr, "Unable to allocate socket!\n"); |
| 96 |
exit(1); |
| 97 |
} |
| 98 |
|
| 99 |
res = connect(s, (struct sockaddr *)&sin, sizeof(sin)); |
| 100 |
|
| 101 |
if (res) { |
| 102 |
fprintf(stderr, "Unable to connect to host!\n"); |
| 103 |
close(s); |
| 104 |
exit(1); |
| 105 |
} |
| 106 |
|
| 107 |
while (1) { |
| 108 |
res = read(s, buf, sizeof(buf)); |
| 109 |
|
| 110 |
if (res < 1) |
| 111 |
break; |
| 112 |
|
| 113 |
memset(&tv, 0, sizeof(tv)); |
| 114 |
FD_ZERO(&wfds); |
| 115 |
FD_SET(1, &wfds); |
| 116 |
|
| 117 |
select(2, NULL, &wfds, NULL, &tv); |
| 118 |
|
| 119 |
if (FD_ISSET(1, &wfds)) { |
| 120 |
if (write(1, buf, res) < 1) { |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
close(s); |
| 127 |
exit(res); |
| 128 |
} |