|
|
|
@ -8,41 +8,41 @@
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
|
|
const char *RHOST = "::ffff:172.31.251.3";
|
|
|
|
|
#include "netspool.h"
|
|
|
|
|
|
|
|
|
|
const char *RHOST = "172.31.251.3";
|
|
|
|
|
const char *RPORT = "24555";
|
|
|
|
|
|
|
|
|
|
#define STRBUF (1024)
|
|
|
|
|
|
|
|
|
|
void readstr(int s, char *buf, int len) {
|
|
|
|
|
int readstr(int s, char *buf, int len) {
|
|
|
|
|
char c=0;
|
|
|
|
|
int r;
|
|
|
|
|
while(1) {
|
|
|
|
|
r=recv(s, &c, 1, 0);
|
|
|
|
|
if(r==0){
|
|
|
|
|
puts("remote socket shutdown");
|
|
|
|
|
exit(-3);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
if(r==-1){
|
|
|
|
|
puts("error reading string");
|
|
|
|
|
printf("%d %s\n", errno, strerror(errno));
|
|
|
|
|
exit(-3);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
if(c=='\n') {
|
|
|
|
|
*buf=0;
|
|
|
|
|
return;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
*buf++=c;
|
|
|
|
|
if(c=='\n')
|
|
|
|
|
return;
|
|
|
|
|
len--;
|
|
|
|
|
if(!len) {
|
|
|
|
|
puts("string buffer overflow");
|
|
|
|
|
exit(-3);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sendstr(int s, const char *buf) {
|
|
|
|
|
int sendstr(int s, const char *buf) {
|
|
|
|
|
int l = strlen(buf);
|
|
|
|
|
int c;
|
|
|
|
|
char nl='\n';
|
|
|
|
@ -51,11 +51,11 @@ void sendstr(int s, const char *buf) {
|
|
|
|
|
if(c==-1){
|
|
|
|
|
puts("error sending string");
|
|
|
|
|
printf("%d %s\n", errno, strerror(errno));
|
|
|
|
|
exit(-3);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
if(c==0){
|
|
|
|
|
puts("zero send: may loop infinitely");
|
|
|
|
|
exit(-3);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
l-=c;
|
|
|
|
|
}
|
|
|
|
@ -63,15 +63,132 @@ void sendstr(int s, const char *buf) {
|
|
|
|
|
if(c==-1){
|
|
|
|
|
puts("error sending string");
|
|
|
|
|
printf("%d %s\n", errno, strerror(errno));
|
|
|
|
|
exit(-3);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
if(c==0){
|
|
|
|
|
puts("zero send: may loop infinitely");
|
|
|
|
|
exit(-3);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void netspool_start(s_netspool_state *state, const char *host, const char *port, const char *address, const char *password)
|
|
|
|
|
{
|
|
|
|
|
int s, r;
|
|
|
|
|
struct addrinfo hint;
|
|
|
|
|
struct addrinfo *addrs, *a;
|
|
|
|
|
char strbuf[STRBUF];
|
|
|
|
|
|
|
|
|
|
s = socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
|
state->socket = s;
|
|
|
|
|
if( s==-1 )
|
|
|
|
|
{
|
|
|
|
|
state->state = NS_ERROR;
|
|
|
|
|
state->error = "socket error";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(&hint, 0, sizeof(struct addrinfo));
|
|
|
|
|
hint.ai_socktype = SOCK_STREAM;
|
|
|
|
|
hint.ai_family = PF_INET6;
|
|
|
|
|
hint.ai_flags = AI_V4MAPPED;
|
|
|
|
|
|
|
|
|
|
r = getaddrinfo(host, port!=NULL? port: "24555", &hint, &addrs);
|
|
|
|
|
|
|
|
|
|
if( r ) {
|
|
|
|
|
state->state = NS_ERROR;
|
|
|
|
|
state->error = gai_error(r);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a=addrs;
|
|
|
|
|
r=-1;
|
|
|
|
|
while(a) {
|
|
|
|
|
r = connect(s, a->ai_addr, a->ai_addrlen);
|
|
|
|
|
if(r==-1) {
|
|
|
|
|
printf("%d %s\n", errno, strerror(errno));
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
a=a->ai_next;
|
|
|
|
|
}
|
|
|
|
|
freeaddrinfo(addrs);
|
|
|
|
|
|
|
|
|
|
if(r==-1) {
|
|
|
|
|
state->state = NS_ERROR;
|
|
|
|
|
state->error = "could not connect\n"
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = readstr(s, strbuf, STRBUF);
|
|
|
|
|
if( r ) { state->state = NS_ERROR; state->error = "IO error"; }
|
|
|
|
|
puts(strbuf);
|
|
|
|
|
|
|
|
|
|
snprintf(strbuf, STRBUF, "ADDRESS %s", address);
|
|
|
|
|
r = sendstr(s, strbuf);
|
|
|
|
|
if( r ) { state->state = NS_ERROR; state->error = "IO error"; }
|
|
|
|
|
|
|
|
|
|
snprintf(strbuf, STRBUF, "PASSWORD %s", password);
|
|
|
|
|
r = sendstr(s, strbuf);
|
|
|
|
|
if( r ) { state->state = NS_ERROR; state->error = "IO error"; }
|
|
|
|
|
|
|
|
|
|
state->state = NS_READY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void netspool_query(s_netspool_state *state, const char *what)
|
|
|
|
|
{
|
|
|
|
|
int r = sendstr(state->socket, "GET ALL");
|
|
|
|
|
if( r ) {
|
|
|
|
|
state->state = NS_ERROR;
|
|
|
|
|
state->error = "IO error";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
state->state = NS_RECEIVING;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void netspool_receive(s_netspool_state *state)
|
|
|
|
|
{
|
|
|
|
|
char strbuf[STRBUF];
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
r = readstr(s, strbuf, STRBUF);
|
|
|
|
|
if( r ) { state->state = NS_ERROR; state->error = "IO error"; return; }
|
|
|
|
|
puts(strbuf);
|
|
|
|
|
if(strcmp(strbuf, "QUEUE EMPTY")==0) {
|
|
|
|
|
state->state = NS_READY;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(strncmp(strbuf, "FILENAME ", 9)==0) {
|
|
|
|
|
strcpy(filename, strbuf+9);
|
|
|
|
|
puts(filename);
|
|
|
|
|
} else {
|
|
|
|
|
state->state = NS_ERROR;
|
|
|
|
|
state->error = "expected filename or queue empty"
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = readstr(s, strbuf, STRBUF);
|
|
|
|
|
if( r ) { state->state = NS_ERROR; state->error = "IO error"; return; }
|
|
|
|
|
if(strncmp(strbuf, "BINARY ", 7)==0) {
|
|
|
|
|
/*if(filename[0]==0) {
|
|
|
|
|
puts("no filename");
|
|
|
|
|
exit(-5);
|
|
|
|
|
} */
|
|
|
|
|
sscanf(strbuf+7, "%Lu", &state->length);
|
|
|
|
|
printf("length=%Lu\n", state->length);
|
|
|
|
|
state->state = NS_RECVFILE;
|
|
|
|
|
} else {
|
|
|
|
|
state->state = NS_ERROR;
|
|
|
|
|
state->error = "expected binary"
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
void savefile(const char *fn, unsigned long long l, int s)
|
|
|
|
|
{
|
|
|
|
|
char BUF[STRBUF];
|
|
|
|
@ -105,64 +222,15 @@ void savefile(const char *fn, unsigned long long l, int s)
|
|
|
|
|
close(f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
int s, r;
|
|
|
|
|
struct addrinfo hint;
|
|
|
|
|
struct addrinfo *addrs, *a;
|
|
|
|
|
char strbuf[STRBUF];
|
|
|
|
|
char filename[STRBUF];
|
|
|
|
|
unsigned long long length;
|
|
|
|
|
|
|
|
|
|
s = socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
|
|
memset(&hint, 0, sizeof(struct addrinfo));
|
|
|
|
|
hint.ai_socktype = SOCK_STREAM;
|
|
|
|
|
r = getaddrinfo(RHOST, RPORT, &hint, &addrs);
|
|
|
|
|
if( r ) {
|
|
|
|
|
puts(gai_strerror(r));
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a=addrs;
|
|
|
|
|
r=-1;
|
|
|
|
|
while(a) {
|
|
|
|
|
r = connect(s, a->ai_addr, a->ai_addrlen);
|
|
|
|
|
if(r==-1) {
|
|
|
|
|
printf("%d %s\n", errno, strerror(errno));
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
a=a->ai_next;
|
|
|
|
|
}
|
|
|
|
|
if(r==-1) {
|
|
|
|
|
printf("could not connect\n");
|
|
|
|
|
exit(-2);
|
|
|
|
|
}
|
|
|
|
|
freeaddrinfo(addrs);
|
|
|
|
|
|
|
|
|
|
readstr(s, strbuf, STRBUF);
|
|
|
|
|
puts(strbuf);
|
|
|
|
|
sendstr(s, "ADDRESS 2:111/11");
|
|
|
|
|
sendstr(s, "PASSWORD 123");
|
|
|
|
|
sendstr(s, "GET ALL");
|
|
|
|
|
|
|
|
|
|
filename[0]=0;
|
|
|
|
|
while(1) {
|
|
|
|
|
readstr(s, strbuf, STRBUF);
|
|
|
|
|
puts(strbuf);
|
|
|
|
|
if(strcmp(strbuf, "QUEUE EMPTY")==0) break;
|
|
|
|
|
if(strncmp(strbuf, "FILENAME ", 9)==0) {
|
|
|
|
|
strcpy(filename, strbuf+9);
|
|
|
|
|
puts(filename);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(strncmp(strbuf, "BINARY ", 7)==0) {
|
|
|
|
|
if(filename[0]==0) {
|
|
|
|
|
puts("no filename");
|
|
|
|
|
exit(-5);
|
|
|
|
|
}
|
|
|
|
|
sscanf(strbuf+7, "%Lu", &length);
|
|
|
|
|
printf("length=%Lu\n", length);
|
|
|
|
|
savefile(filename, length, s);
|
|
|
|
|
sprintf(strbuf, "DONE %s", filename);
|
|
|
|
|
sendstr(s, strbuf);
|
|
|
|
@ -178,3 +246,4 @@ void main() {
|
|
|
|
|
close(s);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
*/
|