Skip to content

Commit 396f727

Browse files
author
Roytak
committed
examples BUGFIX reflect changes in modules and api
Fixed the default config in config.json, removed UNIX sock from the config and added an option to create it with API, also made some changes to the description of the examples. Fixes #472.
1 parent a55fee0 commit 396f727

File tree

3 files changed

+97
-86
lines changed

3 files changed

+97
-86
lines changed

examples/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@ There are two examples `server` and `client` demonstrating a simple NETCONF serv
55
The example server provides `ietf-yang-library` state data that are returned as a reply to `get` RPC. In case an XPath filter is used it is properly applied on these data. If some unsupported parameters are specified, the server replies with a NETCONF error.
66

77
### Server Configuration
8-
The server's default configuration can be found in the `config.json` file. The YANG data stored in this file define three endpoints - two for SSH and one for UNIX socket.
8+
The server's default configuration can be found in the `config.json` file. The YANG data stored in this file define two SSH endpoints - they differ in port and in how clients get authenticated.
99
You can modify this configuration in any way you want, however, configuring the server may fail if the configuration is not valid.
1010

1111
## Example usage
1212
### Server
1313
First start the server:
1414
```
15-
$ server
15+
$ server -u ./example-socket
1616
```
17-
The server will be started and configured per YANG data stored in the file `config.json`. A UNIX socket with the default address `/tmp/.ln2-unix-socket` will be created.
18-
In addition to that two SSH endpoints with the addresses `127.0.0.1:10000` and `127.0.0.1:10001` will be listening.
17+
The server will be started and configured per YANG data stored in the file `config.json`.
18+
Two SSH endpoints with the addresses `127.0.0.1:10000` and `127.0.0.1:10001` will start listening for new connections.
1919
This first endpoint has a single user that can authenticate with a password (which is set to `admin` by default).
2020
The second endpoint has a single user that can authenticate with a publickey (the asymmetric key pair used is stored in `admin_key` and `admin_key.pub`).
21+
The `-u` option specifies that a UNIX socket endpoint will be created and `./example-socket` is the path to where the socket will be listening.
2122

2223
### Client
2324
#### UNIX socket
2425
After the server has been run, in another terminal instance, with the default configuration:
2526
```
26-
$ client -u /tmp/.ln2-unix-socket get "/ietf-yang-library:yang-library/module-set/module[name='ietf-netconf']"
27+
$ client -u ./example-socket get "/ietf-yang-library:yang-library/module-set/module[name='ietf-netconf']"
2728
```
28-
In this case, `-u` means that a connection to an UNIX socket will be attempted and a path to the socket needs to be specified, that is `/tmp/ln2-unix-socket` by default.
29+
In this case, `-u` means that a connection to an UNIX socket will be attempted and a path to the socket needs to be specified.
2930
The `get` parameter is the name of the RPC and `/ietf-yang-library:yang-library/module-set/module[name='ietf-netconf']` is the RPC's optional XPath filter.
3031

3132
##### Server output
@@ -87,10 +88,10 @@ The client received a single `ietf-yang-library` module based on the used filter
8788
#### SSH
8889
After the server has been run, in another terminal instance, with the default configuration:
8990
```
90-
$ client -p 10000 get-config candidate
91+
$ client -p 10000 get-config startup
9192
```
9293
In this case, `-p 10000` is the port to connect to. By default the endpoint with this port has a single authorized client that needs to authenticate with a password.
93-
The parameter `get-config` is the name of the RPC and `candidate` is the source datastore for the retrieved data of the get-config RPC.
94+
The parameter `get-config` is the name of the RPC and `startup` is the source datastore for the retrieved data of the get-config RPC.
9495

9596
##### Server output
9697
```
@@ -123,10 +124,11 @@ [email protected] password: <-- prompts for password, type in 'admin'
123124
<ok/>
124125
</rpc-reply>
125126
```
127+
No `startup` configuration is returned, because the example server lacks this functionality.
126128
The _username_ in the `example.h` header file. The _password_ is located in `config.json`.
127129

128130
If you wish to connect to the SSH public key endpoint, you need to specify its port and the asymmetric key pair to use.
129131
By default the command to connect would look like so:
130132
```
131-
$ ./examples/client -p 10001 -P /home/roman/libnetconf2/examples/admin_key.pub -i /home/roman/libnetconf2/examples/admin_key get
133+
$ client -p 10001 -P ~/libnetconf2/examples/admin_key.pub -i ~/libnetconf2/examples/admin_key get
132134
```

examples/config.json

Lines changed: 68 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,92 @@
22
"ietf-netconf-server:netconf-server": {
33
"listen": {
44
"idle-timeout": 10,
5-
"endpoint": [
6-
{
7-
"name": "ssh-password-auth-endpt",
8-
"ssh": {
9-
"tcp-server-parameters": {
10-
"local-address": "127.0.0.1",
11-
"local-port": 10000
12-
},
13-
"ssh-server-parameters": {
14-
"server-identity": {
15-
"host-key": [
16-
{
17-
"name": "key",
18-
"public-key": {
19-
"inline-definition": {
20-
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
21-
"public-key": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDRIB2eNSRWU+HNWRUGKr76ghCLg8RaMlUCps9lBjnc6ggaJl2Q+TOLn8se2wAdK3lYBMz3dcqR+SlU7eB8wJAc=",
22-
"private-key-format": "ietf-crypto-types:ec-private-key-format",
23-
"cleartext-private-key": "MHcCAQEEICQ2fr9Jt2xluom0YQQ7HseE8YTo5reZRVcQENKUWOrooAoGCCqGSM49AwEHoUQDQgAENEgHZ41JFZT4c1ZFQYqvvqCEIuDxFoyVQKmz2UGOdzqCBomXZD5M4ufyx7bAB0reVgEzPd1ypH5KVTt4HzAkBw=="
24-
}
25-
}
26-
}
27-
]
5+
"endpoints": {
6+
"endpoint": [
7+
{
8+
"name": "ssh-password-auth-endpt",
9+
"ssh": {
10+
"tcp-server-parameters": {
11+
"local-address": "127.0.0.1",
12+
"local-port": 10000
2813
},
29-
"client-authentication": {
30-
"users": {
31-
"user": [
14+
"ssh-server-parameters": {
15+
"server-identity": {
16+
"host-key": [
3217
{
33-
"name": "admin",
34-
"password": "$0$admin"
18+
"name": "key",
19+
"public-key": {
20+
"inline-definition": {
21+
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
22+
"public-key": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDRIB2eNSRWU+HNWRUGKr76ghCLg8RaMlUCps9lBjnc6ggaJl2Q+TOLn8se2wAdK3lYBMz3dcqR+SlU7eB8wJAc=",
23+
"private-key-format": "ietf-crypto-types:ec-private-key-format",
24+
"cleartext-private-key": "MHcCAQEEICQ2fr9Jt2xluom0YQQ7HseE8YTo5reZRVcQENKUWOrooAoGCCqGSM49AwEHoUQDQgAENEgHZ41JFZT4c1ZFQYqvvqCEIuDxFoyVQKmz2UGOdzqCBomXZD5M4ufyx7bAB0reVgEzPd1ypH5KVTt4HzAkBw=="
25+
}
26+
}
3527
}
3628
]
29+
},
30+
"client-authentication": {
31+
"users": {
32+
"user": [
33+
{
34+
"name": "admin",
35+
"password": "$0$admin"
36+
}
37+
]
38+
}
3739
}
3840
}
3941
}
40-
}
41-
},
42-
{
43-
"name": "ssh-pubkey-auth-endpt",
44-
"ssh": {
45-
"tcp-server-parameters": {
46-
"local-address": "127.0.0.1",
47-
"local-port": 10001
48-
},
49-
"ssh-server-parameters": {
50-
"server-identity": {
51-
"host-key": [
52-
{
53-
"name": "key",
54-
"public-key": {
55-
"inline-definition": {
56-
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
57-
"public-key": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDRIB2eNSRWU+HNWRUGKr76ghCLg8RaMlUCps9lBjnc6ggaJl2Q+TOLn8se2wAdK3lYBMz3dcqR+SlU7eB8wJAc=",
58-
"private-key-format": "ietf-crypto-types:ec-private-key-format",
59-
"cleartext-private-key": "MHcCAQEEICQ2fr9Jt2xluom0YQQ7HseE8YTo5reZRVcQENKUWOrooAoGCCqGSM49AwEHoUQDQgAENEgHZ41JFZT4c1ZFQYqvvqCEIuDxFoyVQKmz2UGOdzqCBomXZD5M4ufyx7bAB0reVgEzPd1ypH5KVTt4HzAkBw=="
60-
}
61-
}
62-
}
63-
]
42+
},
43+
{
44+
"name": "ssh-pubkey-auth-endpt",
45+
"ssh": {
46+
"tcp-server-parameters": {
47+
"local-address": "127.0.0.1",
48+
"local-port": 10001
6449
},
65-
"client-authentication": {
66-
"users": {
67-
"user": [
50+
"ssh-server-parameters": {
51+
"server-identity": {
52+
"host-key": [
6853
{
69-
"name": "admin",
70-
"public-keys": {
54+
"name": "key",
55+
"public-key": {
7156
"inline-definition": {
72-
"public-key": [
73-
{
74-
"name": "admin_key.pub",
75-
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
76-
"public-key": "AAAAC3NzaC1lZDI1NTE5AAAAIOr46rptg6BsWhO1JMomuh3cuCYmeuO6JfOUPs/YO35w"
77-
}
78-
]
57+
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
58+
"public-key": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDRIB2eNSRWU+HNWRUGKr76ghCLg8RaMlUCps9lBjnc6ggaJl2Q+TOLn8se2wAdK3lYBMz3dcqR+SlU7eB8wJAc=",
59+
"private-key-format": "ietf-crypto-types:ec-private-key-format",
60+
"cleartext-private-key": "MHcCAQEEICQ2fr9Jt2xluom0YQQ7HseE8YTo5reZRVcQENKUWOrooAoGCCqGSM49AwEHoUQDQgAENEgHZ41JFZT4c1ZFQYqvvqCEIuDxFoyVQKmz2UGOdzqCBomXZD5M4ufyx7bAB0reVgEzPd1ypH5KVTt4HzAkBw=="
7961
}
8062
}
8163
}
8264
]
65+
},
66+
"client-authentication": {
67+
"users": {
68+
"user": [
69+
{
70+
"name": "admin",
71+
"public-keys": {
72+
"inline-definition": {
73+
"public-key": [
74+
{
75+
"name": "admin_key.pub",
76+
"public-key-format": "ietf-crypto-types:ssh-public-key-format",
77+
"public-key": "AAAAC3NzaC1lZDI1NTE5AAAAIOr46rptg6BsWhO1JMomuh3cuCYmeuO6JfOUPs/YO35w"
78+
}
79+
]
80+
}
81+
}
82+
}
83+
]
84+
}
8385
}
8486
}
8587
}
8688
}
87-
},
88-
{
89-
"name": "unix-socket-endpt",
90-
"libnetconf2-netconf-server:unix-socket": {
91-
"path": "/tmp/.ln2-unix-socket"
92-
}
93-
}
94-
]
89+
]
90+
}
9591
}
9692
}
9793
}

examples/server.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,11 @@ help_print()
198198
"\n"
199199
" Available options:\n"
200200
" -h, --help\t \tPrint usage help.\n"
201-
" -u, --unix\t<path>\tCreate a UNIX socket at the place specified by <path>.\n"
202-
" -s, --ssh\t<path>\tCreate a SSH server with the host SSH key located at <path>.\n\n");
201+
" -u, --unix\t<path>\tCreate a UNIX socket endpoint at the place specified by <path>.\n\n");
203202
}
204203

205204
static int
206-
init(struct ly_ctx **context, struct nc_pollsession **ps)
205+
init(const char *unix_socket_path, struct ly_ctx **context, struct nc_pollsession **ps)
207206
{
208207
int rc = 0;
209208
struct lyd_node *config = NULL;
@@ -237,6 +236,14 @@ init(struct ly_ctx **context, struct nc_pollsession **ps)
237236
ERR_MSG_CLEANUP("Error occurred while initializing the server.\n");
238237
}
239238

239+
/* create unix socket endpoint if path was set */
240+
if (unix_socket_path) {
241+
rc = nc_server_add_endpt_unix_socket_listen("unix-socket-endpt", unix_socket_path, -1, -1, -1);
242+
if (rc) {
243+
ERR_MSG_CLEANUP("Creating UNIX socket endpoint failed.\n");
244+
}
245+
}
246+
240247
/* create a new poll session structure, which is used for polling RPCs sent by clients */
241248
*ps = nc_ps_new();
242249
if (!*ps) {
@@ -261,16 +268,18 @@ main(int argc, char **argv)
261268
struct ly_ctx *context = NULL;
262269
struct nc_session *session, *new_session;
263270
struct nc_pollsession *ps = NULL;
271+
const char *unix_socket_path = NULL;
264272

265273
struct option options[] = {
266274
{"help", no_argument, NULL, 'h'},
267275
{"debug", no_argument, NULL, 'd'},
276+
{"unix", required_argument, NULL, 'u'},
268277
{NULL, 0, NULL, 0}
269278
};
270279

271280
opterr = 0;
272281

273-
while ((opt = getopt_long(argc, argv, "hd", options, NULL)) != -1) {
282+
while ((opt = getopt_long(argc, argv, "hdu:", options, NULL)) != -1) {
274283
switch (opt) {
275284
case 'h':
276285
help_print();
@@ -280,13 +289,17 @@ main(int argc, char **argv)
280289
nc_verbosity(NC_VERB_DEBUG);
281290
break;
282291

292+
case 'u':
293+
unix_socket_path = optarg;
294+
break;
295+
283296
default:
284297
ERR_MSG_CLEANUP("Invalid option or missing argument\n");
285298
}
286299
}
287300

288301
/* initialize the server */
289-
r = init(&context, &ps);
302+
r = init(unix_socket_path, &context, &ps);
290303
if (r) {
291304
ERR_MSG_CLEANUP("Initializing the server failed.");
292305
}

0 commit comments

Comments
 (0)