Browse Source

dev_disconnect: support '*N' syntax for disconnecting on the N'th occurance.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
524e56cf56
  1. 21
      lightningd/dev_disconnect.c

21
lightningd/dev_disconnect.c

@ -12,10 +12,12 @@
/* We move the fd IFF we do a disconnect. */
static int dev_disconnect_fd = -1;
static char dev_disconnect_line[200];
static int dev_disconnect_count, dev_disconnect_len;
void dev_disconnect_init(int fd)
{
int r;
char *asterisk;
r = read(fd, dev_disconnect_line, sizeof(dev_disconnect_line)-1);
if (r < 0)
@ -24,7 +26,17 @@ void dev_disconnect_init(int fd)
/* Get first line */
dev_disconnect_line[r] = '\n';
*strchr(dev_disconnect_line, '\n') = '\0';
dev_disconnect_len = strcspn(dev_disconnect_line, "\n");
dev_disconnect_line[dev_disconnect_len] = '\0';
asterisk = strchr(dev_disconnect_line, '*');
if (asterisk) {
dev_disconnect_count = atoi(asterisk+1);
if (dev_disconnect_count < 1)
errx(1, "dev_disconnect invalid count: %s",
dev_disconnect_line);
*asterisk = '\0';
} else
dev_disconnect_count = 1;
/* So we can move forward if we do use the line. */
dev_disconnect_fd = fd;
@ -35,8 +47,13 @@ char dev_disconnect(int pkt_type)
if (!streq(wire_type_name(pkt_type), dev_disconnect_line+1))
return DEV_DISCONNECT_NORMAL;
if (dev_disconnect_count != 1) {
dev_disconnect_count--;
return DEV_DISCONNECT_NORMAL;
}
assert(dev_disconnect_fd != -1);
lseek(dev_disconnect_fd, strlen(dev_disconnect_line)+1, SEEK_CUR);
lseek(dev_disconnect_fd, dev_disconnect_len+1, SEEK_CUR);
status_trace("dev_disconnect: %s", dev_disconnect_line);
return dev_disconnect_line[0];

Loading…
Cancel
Save