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
|
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "ftn.h"
#include "lutil.h"
#include "ttyio.h"
#include "session.h"
#include "statetbl.h"
extern unsigned INT16 crc16(char*,int);
extern char *reqname(faddr*);
extern int xmrecv(char*);
static int send_bark(void);
static char *nm,*pw,*dt;
int sendbark(void);
int sendbark(void)
{
char *fn;
FILE *fp;
char buf[256],*p;
int rc=0;
fn=reqname(remote->addr);
if ((fp=fopen(fn,"r")) == NULL)
{
debug(10,"no request file for this node");
PUTCHAR(ETB);
return 0;
}
while (fgets(buf,sizeof(buf)-1,fp))
{
nm=buf;
pw=strchr(buf,'!');
/* if ((dt=strchr(buf,'-')) == NULL) */
dt=strchr(buf,'+');
if (pw) *pw++='\0';
if (dt) *dt++='\0';
if (nm)
{
while (isspace(*nm)) nm++;
for (p=nm;(*p != '!') && (*p != '+') &&
(!isspace(*p));p++);
*p='\0';
}
if (pw)
{
while (isspace(*pw)) pw++;
for (p=pw;(*p != '!') && (*p != '+') &&
(!isspace(*p));p++);
*p='\0';
}
else pw="";
if (dt)
{
while (isspace(*nm)) nm++;
for (p=nm;(*p != '!') && (*p != '+') &&
(*p != '-') && (!isspace(*p));p++);
*p='\0';
}
else dt="0";
if (*nm == ';') continue;
loginf("sending bark request for \"%s\", password \"%s\", update \"%s\"",
S(nm),S(pw),S(dt));
if ((rc=send_bark())) break;
}
if (rc == 0) PUTCHAR(ETB);
fclose(fp);
if (rc == 0) unlink(fn);
return rc;
}
SM_DECL(send_bark,"sendbark")
SM_STATES
send,waitack,getfile
SM_NAMES
"send","waitack","getfile"
SM_EDECL
int c;
char buf[256];
unsigned INT16 crc;
int count=0;
sprintf(buf,"%s %s %s",nm,dt,pw);
crc=crc16(buf,strlen(buf));
debug(10,"sending bark packet \"%s\", crc=0x%04x",buf,crc);
SM_START(send)
SM_STATE(send)
if (count++ > 6)
{
loginf("bark request failed");
SM_ERROR;
}
PUTCHAR(ACK);
PUT(buf,strlen(buf));
PUTCHAR(ETX);
PUTCHAR(crc&0xff);
PUTCHAR((crc>>8)&0xff);
if (STATUS) {SM_ERROR;}
else {SM_PROCEED(waitack);}
SM_STATE(waitack)
c=GETCHAR(15);
if (c == TIMEOUT)
{
debug(10,"sendbark got timeout waiting for ACK");
SM_PROCEED(send);
}
else if (c < 0)
{
SM_PROCEED(send);
}
else if (c == ACK)
{
SM_PROCEED(getfile);
}
else if (c == NAK)
{
SM_PROCEED(send);
}
else
{
debug(10,"sendbark got %s waiting for ACK",
printablec(c));
SM_PROCEED(send);
}
SM_STATE(getfile)
switch (xmrecv(NULL))
{
case 0: SM_PROCEED(getfile); break;
case 1: SM_SUCCESS; break;
default: SM_ERROR; break;
}
SM_END
SM_RETURN
|