00001
00044 #include <cfg/os.h>
00045 #include <cfg/clock.h>
00046 #include <dev/board.h>
00047
00048 #include <stdlib.h>
00049 #include <string.h>
00050 #include <stdio.h>
00051 #include <fcntl.h>
00052 #include <io.h>
00053
00054 #include <sys/version.h>
00055 #include <sys/confnet.h>
00056 #include <sys/atom.h>
00057 #include <sys/heap.h>
00058 #include <sys/thread.h>
00059 #include <sys/timer.h>
00060 #include <sys/event.h>
00061 #include <sys/socket.h>
00062
00063 #include <hxmp3/mp3dec.h>
00064
00065 #include <netinet/tcp.h>
00066 #include <arpa/inet.h>
00067 #include <net/route.h>
00068 #include <pro/dhcp.h>
00069
00070 #include "tlv320dac.h"
00071 #include "config.h"
00072 #include "utils.h"
00073 #include "player.h"
00074 #include "shoutcast.h"
00075
00085 u_int StationSelect(u_int idx, int dir)
00086 {
00087 u_int rc = idx;
00088 char *symbol;
00089
00090 if (station[rc].rs_port) {
00091 symbol = station[idx].rs_symbol;
00092 }
00093 else {
00094 symbol = NULL;
00095 }
00096
00097 for (;;) {
00098 if (dir < 0) {
00099 if (rc == 0) {
00100 rc = MAXNUM_STATIONS;
00101 }
00102 rc--;
00103 }
00104 else if (++rc >= MAXNUM_STATIONS) {
00105 rc = 0;
00106 }
00107 if (rc == idx) {
00108 break;
00109 }
00110 if (station[rc].rs_port) {
00111 if (symbol == NULL) {
00112 break;
00113 }
00114 if (dir) {
00115 if (station[rc].rs_symbol == NULL || strcmp(station[rc].rs_symbol, symbol)) {
00116 break;
00117 }
00118 }
00119 else {
00120 if (station[rc].rs_symbol && strcmp(station[rc].rs_symbol, symbol) == 0) {
00121 break;
00122 }
00123 }
00124 }
00125 }
00126 return rc;
00127 }
00128
00134 void StationDisconnect(STATIONINFO *sip)
00135 {
00136 printf("Disconnecting\n");
00137 if (sip) {
00138 if (sip->si_sock) {
00139 NutTcpCloseSocket(sip->si_sock);
00140 }
00141 TcpReleaseHeaderLines(sip->si_header);
00142 free(sip);
00143 }
00144 }
00145
00154 STATIONINFO *StationConnect(STATIONCONF * scp)
00155 {
00156 STATIONINFO *sip = NULL;
00157 TCPSOCKET *sock = NULL;
00158 u_short mss = MAX_TCPSEG_SIZE;
00159 u_short tcpbufsiz = MAX_TCPBUF_SIZE;
00160 u_long rx_to = MAX_TCPRCV_WAIT;
00161 int err = -1;
00162 int cr;
00163 char *line;
00164
00165
00166 if ((sock = NutTcpCreateSocket()) != NULL) {
00167
00168 NutTcpSetSockOpt(sock, TCP_MAXSEG, &mss, sizeof(mss));
00169 NutTcpSetSockOpt(sock, SO_RCVBUF, &tcpbufsiz, sizeof(tcpbufsiz));
00170 NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to));
00171
00172
00173 printf("[CNCT %s:%u", inet_ntoa(scp->rs_ip), scp->rs_port);
00174 if (proxy.proxy_ip) {
00175 printf(" via %s:%u]", inet_ntoa(proxy.proxy_ip), proxy.proxy_port);
00176 cr = NutTcpConnect(sock, proxy.proxy_ip, proxy.proxy_port);
00177 }
00178 else {
00179 putchar(']');
00180 cr = NutTcpConnect(sock, scp->rs_ip, scp->rs_port);
00181 }
00182 if (cr) {
00183 printf("[CERR=%d]\n", NutTcpError(sock));
00184 }
00185 else {
00186 printf("[CNCTD]");
00187
00188
00189 if ((sip = malloc(sizeof(STATIONINFO))) != NULL) {
00190 memset(sip, 0, sizeof(STATIONINFO));
00191 sip->si_sock = sock;
00192 sip->si_scp = scp;
00193
00194
00195
00196
00197 line = malloc(256);
00198
00199 if (proxy.proxy_ip) {
00200
00201 sprintf(line, "GET http://%s:%u/", inet_ntoa(scp->rs_ip), scp->rs_port);
00202 }
00203 else {
00204 strcpy(line, "GET /");
00205 }
00206 if (scp->rs_url_path) {
00207
00208 strcat(line, scp->rs_url_path);
00209 }
00210 strcat(line, " HTTP/1.0\r\n");
00211 err = TcpPutString(sock, line);
00212
00213 if (err == 0) {
00214
00215 sprintf(line, "Host: %s\r\n", inet_ntoa(scp->rs_ip));
00216 err = TcpPutString(sock, line);
00217 }
00218
00219 if (err == 0) {
00220
00221
00222 TcpPutString(sock, "User-Agent: WinampMPEG/2.7\r\n"
00223 "Accept: */*\r\n"
00224 "Icy-MetaData: 1\r\n"
00225 "Connection: close\r\n\r\n");
00226 }
00227
00228 free(line);
00229
00230
00231 if (TcpGetHeaderLines(sock, &sip->si_header) <= 0) {
00232 err = -1;
00233 }
00234 }
00235 }
00236
00237
00238 if (err) {
00239 if (sip) {
00240 StationDisconnect(sip);
00241 sip = NULL;
00242 }
00243 else {
00244 NutTcpCloseSocket(sock);
00245 }
00246 }
00247 }
00248 return sip;
00249 }