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
|
/*
* vim:tw=80:ai:tabstop=4:softtabstop=4:shiftwidth=4:expandtab
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright Kevin Timmerman 2007
* (C) Copyright Kevin Timmerman 2008
*/
#include "usblan.h"
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#ifdef _WIN32
#include <winsock.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#define closesocket close
#define SOCKET int
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#endif
#ifdef __FreeBSD__
#include <netinet/in.h>
#endif
#include "libconcord.h"
#include "lc_internal.h"
static SOCKET sock = INVALID_SOCKET;
const char * const remote_ip_address = "169.254.1.2";
const uint16_t remote_port = 3074;
const int connect_timeout = 1; // try to connect for 1 seconds
const char * const http_get_cmd = "\
GET /xmluserrfsetting HTTP/1.1\r\n\
User-Agent: Jakarta Commons-HttpClient/3.1\r\n\
Host: 169.254.1.2\r\n\
\r\n";
int InitializeUsbLan(void)
{
return 0;
}
int ShutdownUsbLan(void)
{
int err=0;
// Close the socket
if (sock != INVALID_SOCKET) {
if ((err = closesocket(sock))) {
report_net_error("closesocket()");
return LC_ERROR_OS_NET;
}
}
return 0;
}
int FindUsbLanRemote(void)
{
int err;
hostent* addr = gethostbyname(remote_ip_address);
if (!addr) {
report_net_error("gethostbyname()");
return LC_ERROR_OS_NET;
}
sockaddr_in sa;
memcpy(&(sa.sin_addr), addr->h_addr, addr->h_length);
sa.sin_family = AF_INET; // TCP/IP
sa.sin_port = htons(remote_port); // Port 3074
sock = socket(sa.sin_family, SOCK_STREAM, 0); // TCP
//sock = socket(sa.sin_family, SOCK_DGRAM, 0); // UDP
// Make the socket non-blocking so it doesn't hang on systems that
// don't have a usbnet remote.
fd_set wset;
FD_ZERO(&wset);
FD_SET(sock, &wset);
struct timeval tv;
tv.tv_sec = connect_timeout;
tv.tv_usec = 0;
#ifdef _WIN32
u_long non_blocking = 1;
if(ioctlsocket(sock, FIONBIO, &non_blocking) != 0) {
report_net_error("ioctlsocket()");
return LC_ERROR_OS_NET;
}
#else
int flags = 0;
if((flags = fcntl(sock, F_GETFL, 0)) < 0) {
report_net_error("fcntl()");
return LC_ERROR_OS_NET;
}
if(fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
report_net_error("fcntl()");
return LC_ERROR_OS_NET;
}
#endif
if ((err = connect(sock,(struct sockaddr*)&sa,sizeof(sa)))) {
#ifdef _WIN32
if (WSAGetLastError() != WSAEWOULDBLOCK) {
#else
if (errno != EINPROGRESS) {
#endif
report_net_error("connect()");
return LC_ERROR_OS_NET;
}
}
if ((err = select(sock+1, NULL, &wset, NULL, &tv)) <= 0) {
report_net_error("select()");
return LC_ERROR_OS_NET;
}
// Change the socket back to blocking which should be fine now that we
// connected.
#ifdef _WIN32
non_blocking = 0;
if(ioctlsocket(sock, FIONBIO, &non_blocking) != 0) {
report_net_error("ioctlsocket()");
return LC_ERROR_OS_NET;
}
#else
if((flags = fcntl(sock, F_GETFL, 0)) < 0) {
report_net_error("fcntl()");
return LC_ERROR_OS_NET;
}
if(fcntl(sock, F_SETFL, flags & ~O_NONBLOCK) < 0) {
report_net_error("fcntl()");
return LC_ERROR_OS_NET;
}
#endif
debug("Connected to USB LAN driver!");
return 0;
}
int UsbLan_Write(unsigned int len, uint8_t *data)
{
int err = send(sock, reinterpret_cast<char*>(data), len, 0);
if (err == SOCKET_ERROR) {
report_net_error("send()");
return LC_ERROR_OS_NET;
}
debug("%i bytes sent", err);
return 0;
}
int UsbLan_Read(unsigned int &len, uint8_t *data)
{
int err = recv(sock, reinterpret_cast<char*>(data), len, 0);
if (err == SOCKET_ERROR) {
report_net_error("recv()");
len = 0;
return LC_ERROR_OS_NET;
}
len = static_cast<unsigned int>(err);
debug("%i bytes received", len);
return 0;
}
int GetXMLUserRFSetting(char **data)
{
int err;
int web_sock;
char buf[4096];
hostent* addr = gethostbyname(remote_ip_address);
if (!addr) {
report_net_error("gethostbyname()");
return LC_ERROR_OS_NET;
}
sockaddr_in sa;
memcpy(&(sa.sin_addr), addr->h_addr, addr->h_length);
sa.sin_family = AF_INET; // TCP/IP
sa.sin_port = htons(80); // Web Server port
web_sock = socket(sa.sin_family, SOCK_STREAM, 0); // TCP
if ((err = connect(web_sock,(struct sockaddr*)&sa,sizeof(sa)))) {
report_net_error("connect()");
return LC_ERROR_OS_NET;
}
debug("Connected to USB LAN web server!");
err = send(web_sock, http_get_cmd, strlen(http_get_cmd), 0);
if (err == SOCKET_ERROR) {
report_net_error("send()");
return LC_ERROR_OS_NET;
}
debug("%i bytes sent", err);
unsigned int len = 0;
char* buf_ptr = buf;
do {
err = recv(web_sock, buf_ptr, sizeof(buf)-len, 0);
if (err == SOCKET_ERROR) {
report_net_error("recv()");
len = 0;
return LC_ERROR_OS_NET;
}
len += err;
buf_ptr += err;
debug("%i bytes received", err);
} while (err > 0); // recv will return 0 when the message is done.
buf[len] = '\0';
buf_ptr = strstr(buf, "\r\n\r\n"); // search for end of the http header
if (buf_ptr == NULL) {
report_net_error("strstr()");
return LC_ERROR_OS_NET;
}
buf_ptr += 4;
*data = new char[strlen(buf_ptr)+1];
strncpy(*data, buf_ptr, strlen(buf_ptr)+1);
return 0;
}
|