Skip to content

Commit 40c9f52

Browse files
wyr-7xiaoxiang781216
authored andcommitted
examples/vsock: accept a cid given as a name
A NuttX vsock cid set from a cpu name holds the first four bytes of the name. Let "-c" take either a number or such a name, e.g. "-c prox". Signed-off-by: Yongrong Wang <wangyongrong@xiaomi.com>
1 parent 9c5fbd4 commit 40c9f52

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

‎examples/vsock/vsock_main.c‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <stdlib.h>
2828
#include <stdio.h>
2929
#include <string.h>
30+
#include <sys/param.h>
3031
#include <sys/socket.h>
3132
#include <unistd.h>
3233

@@ -114,7 +115,7 @@ static int safe_recv(int s, char *buf, size_t len, int flags)
114115
****************************************************************************/
115116

116117
static int vsock_client(unsigned int port, size_t size,
117-
size_t count, int cid)
118+
size_t count, unsigned int cid)
118119
{
119120
struct sockaddr_vm addr;
120121
size_t sendlen = 0;
@@ -194,7 +195,7 @@ static int vsock_client(unsigned int port, size_t size,
194195
return ret;
195196
}
196197

197-
static int vsock_server(unsigned int port, size_t size, int cid)
198+
static int vsock_server(unsigned int port, size_t size, unsigned int cid)
198199
{
199200
struct sockaddr_vm peer_addr;
200201
struct sockaddr_vm addr;
@@ -426,9 +427,10 @@ static int vsock_server_conn(unsigned int port, int cid)
426427

427428
int main(int argc, char *argv[])
428429
{
429-
int cid = VMADDR_CID_ANY;
430+
unsigned int cid = VMADDR_CID_ANY;
430431
unsigned int port = 9999;
431-
const char *type = NULL;
432+
FAR const char *type = NULL;
433+
FAR char *endptr;
432434
size_t count = 100;
433435
size_t size = 64;
434436
int is_server = 1;
@@ -459,7 +461,13 @@ int main(int argc, char *argv[])
459461
{
460462
case 'c':
461463
is_server = 0;
462-
cid = strtoul(optarg, NULL, 0);
464+
cid = strtoul(optarg, &endptr, 0);
465+
if (errno != 0 || *endptr != '\0')
466+
{
467+
cid = 0;
468+
strncpy((FAR char *)&cid, optarg,
469+
MIN(sizeof(cid), strlen(optarg)));
470+
}
463471
break;
464472

465473
case 't':

0 commit comments

Comments
 (0)