#include <asm/types.h>
#include "lantronix.h"
#define NOT_FOUND ((unsigned short)-1)
static int debug = 0;
struct commstr {
__u16 _dummy;
__u16 len;
__u32 acc;
__u32 sid;
__u8 code;
__u8 d0B;
__u16 datalen;
__u16 d0E;
__u16 d10;
__u16 d12;
__u16 d14;
__u32 did;
__u32 d1A;
__u16 d1E;
__u16 d20;
__u8 data[];
};
struct devinfo {
__u16 len;
__u16 type;
__u16 vid;
__u16 pid;
__u32 port;
char client[0x40];
char name1[0x20];
__u8 ip[4];
__u16 d70;
__u8 d72;
__u8 d73;
__u8 d74[4];
__u8 d78[4];
__u8 d7C[2];
__u16 d7E;
__u8 d80[0x18];
char sn[0x20];
__u16 dB8;
char name2[0x20];
__u16 dDA;
};
struct cliinfo {
__u16 len;
__u16 type;
char name[0x60];
__u8 ip[4];
__u8 d68[6];
char d6E[0x20];
};
struct udppacket {
__u32 sn;
__u32 firm_ver;
__u32 firm_rev;
__u8 code1;
__u8 code2;
__u16 tcp_port;
__u16 d10;
__u16 d12;
__u16 d14;
__u16 ord;
__u32 d18;
__u16 vid;
__u16 pid;
__u32 usb_port;
};
static int sock;
static struct commstr *salloc = NULL;
void freeall() { /* pro atexit() */
free(salloc);
close(sock);
}
int initsock(char *ip) {
struct hostent *host;
struct sockaddr_in sin;
int s;
if((host = gethostbyname((void*)ip)) == NULL) return -1;
if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) return -1;
sin.sin_family = AF_INET;
sin.sin_port = htons(10010);
memcpy(&(sin.sin_addr), host->h_addr, host->h_length);
if((connect(s, (struct sockaddr*)&sin, sizeof(sin))) == -1) return -1;
return s;
}
void setupstr(struct commstr *str, unsigned char code, unsigned acc, unsigned short d0E,
unsigned short d10, unsigned short d12, unsigned short datalen) {
memset(str, 0, sizeof(struct commstr));
str->code = code;
str->acc = htonl(acc);
str->d0E = htons(d0E);
str->d10 = htons(d10);
str->d12 = htons(d12);
str->datalen = htons(datalen);
str->len = htons(datalen + 32);
memset(str->data, 0, datalen);
}
void sendstr(struct commstr *str) {
unsigned char *stream = (unsigned char*)str + 2;
if(debug > 1) {
int i;
for(i = 0; i < 34 + ntohs(str->datalen); i++)
printf("%02X ", (int)(stream[i]));
puts("");
}
if(send(sock, (void*)stream, 34 + ntohs(str->datalen), 0) < 0) exit(RETVAL_SEND);
}
void sendsimple(unsigned char code, unsigned acc, unsigned short d0E,
unsigned short d10, unsigned short d12) {
setupstr(salloc, code, acc, d0E, d10, d12, 0);
sendstr(salloc);
}
struct commstr *readpacket(int sock) {
int r, len, o;
struct commstr *str;
unsigned char *buf;
unsigned short prebuf;
r = recv(sock, &prebuf, 2, 0);
if(r <= 0) exit(RETVAL_RECV);
len = ntohs(prebuf)+2;
o = 2;
str = malloc(len+2); /* Nezapomente uvolnit! */
if(!str) exit(RETVAL_MALLOC);
buf = (unsigned char*)str + 2;
memcpy(buf, &prebuf, 2);
do {
r = recv(sock, buf+o, len-o, 0);
if(r <= 0) { free(str); exit(RETVAL_RECV); }
else o += r;
} while(o < len);
if(debug > 1) {
int i;
for(i = 0; i < len; i++) {
printf("%02X ", (int)(buf[i]));
if(i == 1000) puts("");
}
puts("");
}
return str;
}
struct commstr *waitpacket(unsigned char code) {
struct commstr *ret;
time_t b = time(NULL);
for(ret = readpacket(sock); ret->code != code; ret = readpacket(sock)) {
free(ret);
if(time(NULL)-b > MAXWAIT) exit(RETVAL_WAIT);
}
return ret;
}
int main() {
struct commstr *ret;
struct devinfo *di;
/*struct cliinfo *ci;*/
int off;
salloc = malloc(sizeof(struct commstr) + MAXDATA);
if(!salloc) return RETVAL_MALLOC;
sock = initsock(LX_ADDRESS);
if(sock == -1) return RETVAL_SOCKET;
atexit(freeall);
setupstr(salloc, 0x01, 0, 2,1,0, 1+strlen(ID_COMPUTER));
salloc->did = htonl(LX_SERIAL);
strcpy((char*)salloc->data, ID_COMPUTER);
sendstr(salloc);
free(waitpacket(0x11));
sendsimple(0x08, 0, 2,3,0);
ret = waitpacket(0x18);
printf("Zařízení %08X\n=================\n\nPočet USB zařízení: %i\nPočet klientů: %i\n",
LX_SERIAL, ntohs(ret->d0E), ntohs(ret->d10));
for(off = 0; ; off += ntohs(*(unsigned short*)(ret->data+off))) {
switch(ret->data[off+2]) {
case 0x04:
di = (struct devinfo*)(ret->data+off);
/*printf("port %i: %04X/%04X %s\t%s",
ntohl(di->port)-1, ntohs(di->vid), ntohs(di->pid), di->name1, di->sn);*/
printf("port %i: %s\t%s",
ntohl(di->port)-1, di->name1, di->sn);
if(di->ip[0]) printf(" [připojený klient: %i.%i.%i.%i %s]",
di->ip[0], di->ip[1], di->ip[2], di->ip[3], di->client);
printf("\n");
break;
/*case 0x05:
ci = (struct cliinfo*)(ret->data+off);
printf("%s\t%i.%i.%i.%i\n", ci->name, ci->ip[0], ci->ip[1], ci->ip[2], ci->ip[3]);
break;*/
case 0x00:
break;
}
if(ret->data[off+2] == 0x00) {
off += 4;
break;
}
}
printf("\n");
free(ret);
return 0;
}