Skip to content

Commit 63a7127

Browse files
committed
New API to "unlisten" to a socket.
1 parent 48e0b52 commit 63a7127

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/libncp.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,20 @@ int ncp_close (int connection)
208208
return -1;
209209
return 0;
210210
}
211+
212+
int ncp_unlisten (unsigned socket)
213+
{
214+
type (WIRE_UNLISTEN);
215+
add (socket >> 24);
216+
add (socket >> 16);
217+
add (socket >> 8);
218+
add (socket);
219+
if (transact () == -1)
220+
return -1;
221+
if (u32 (message + 1) != socket)
222+
return -1;
223+
if (message[5] == 0)
224+
return 0;
225+
else
226+
return -1;
227+
}

src/ncp.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,22 @@ static void reply_close (uint8_t i)
638638
client.sun_path, strerror (errno));
639639
}
640640

641+
static void reply_unlisten (uint32_t socket, uint8_t e)
642+
{
643+
uint8_t reply[6];
644+
fprintf (stderr, "NCP: Application unlisten reply socket %u: %u\n",
645+
socket, e);
646+
reply[0] = WIRE_UNLISTEN+1;
647+
reply[1] = socket >> 24;
648+
reply[2] = socket >> 16;
649+
reply[3] = socket >> 8;
650+
reply[4] = socket;
651+
reply[5] = e;
652+
if (sendto (fd, reply, sizeof reply, 0, (struct sockaddr *)&client, len) == -1)
653+
fprintf (stderr, "NCP: sendto %s error: %s.\n",
654+
client.sun_path, strerror (errno));
655+
}
656+
641657
static void maybe_reply (int i)
642658
{
643659
if ((connection[i].flags & CONN_GOT_BOTH) == CONN_GOT_BOTH) {
@@ -1697,6 +1713,22 @@ static void app_close (void)
16971713
unless_cls (i, cls_timeout);
16981714
}
16991715

1716+
static void app_unlisten (void)
1717+
{
1718+
uint32_t socket;
1719+
int i;
1720+
1721+
socket = app[1] << 24 | app[2] << 16 | app[3] << 8 | app[4];
1722+
fprintf (stderr, "NCP: Application unlisten to socket %u\n", socket);
1723+
if ((i = find_listen (socket)) != -1) {
1724+
listening[i].sock = 0;
1725+
reply_unlisten (socket, 0);
1726+
} else {
1727+
fprintf (stderr, "NCP: Not listening to %u.\n", socket);
1728+
reply_unlisten (socket, 1);
1729+
}
1730+
}
1731+
17001732
static void application (void)
17011733
{
17021734
ssize_t n;
@@ -1724,6 +1756,7 @@ static void application (void)
17241756
case WIRE_WRITE: app_write (n - 2); break;
17251757
case WIRE_INTERRUPT: app_interrupt (); break;
17261758
case WIRE_CLOSE: app_close (); break;
1759+
case WIRE_UNLISTEN: app_unlisten (); break;
17271760
default: fprintf (stderr, "NCP: bad application request.\n"); break;
17281761
}
17291762
}

src/ncp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ extern int ncp_read (int connection, void *data, int *length);
88
extern int ncp_write (int connection, void *data, int *length);
99
extern int ncp_interrupt (int connection);
1010
extern int ncp_close (int connection);
11+
extern int ncp_unlisten (unsigned socket);

src/wire.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define WIRE_WRITE 9
88
#define WIRE_INTERRUPT 11
99
#define WIRE_CLOSE 13
10+
#define WIRE_UNLISTEN 15
1011

1112
static int wire_check (int type, int size)
1213
{
@@ -25,6 +26,8 @@ static int wire_check (int type, int size)
2526
case WIRE_INTERRUPT+1: return size == 2;
2627
case WIRE_CLOSE: return size == 2;
2728
case WIRE_CLOSE+1: return size == 2;
29+
case WIRE_UNLISTEN: return size == 5;
30+
case WIRE_UNLISTEN+1: return size == 6;
2831
default: return 0;
2932
}
3033
}

0 commit comments

Comments
 (0)