Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file io.c |
| 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * \brief libnetconf2 - input/output functions |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Miroslav Mareš | 9563b81 | 2017-08-19 17:45:36 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE /* asprintf, signals */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 16 | #include <assert.h> |
| 17 | #include <errno.h> |
| 18 | #include <poll.h> |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 19 | #include <inttypes.h> |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 20 | #include <stdarg.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 24 | #include <signal.h> |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 25 | #include <time.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 26 | |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 27 | #ifdef NC_ENABLED_TLS |
| 28 | # include <openssl/err.h> |
| 29 | #endif |
| 30 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 31 | #include <libyang/libyang.h> |
| 32 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 33 | #include "libnetconf.h" |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 34 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 35 | #define BUFFERSIZE 512 |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 36 | |
| 37 | static ssize_t |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 38 | nc_read(struct nc_session *session, char *buf, size_t count, uint32_t inact_timeout, struct timespec *ts_act_timeout) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 39 | { |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 40 | size_t readd = 0; |
Michal Vasko | 9d8bee6 | 2016-03-03 10:58:24 +0100 | [diff] [blame] | 41 | ssize_t r = -1; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 42 | struct timespec ts_cur, ts_inact_timeout; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 43 | |
| 44 | assert(session); |
| 45 | assert(buf); |
| 46 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 47 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 48 | return -1; |
| 49 | } |
| 50 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 51 | if (!count) { |
| 52 | return 0; |
| 53 | } |
| 54 | |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame^] | 55 | nc_gettimespec_mono(&ts_inact_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 56 | nc_addtimespec(&ts_inact_timeout, inact_timeout); |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 57 | do { |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 58 | switch (session->ti_type) { |
| 59 | case NC_TI_NONE: |
| 60 | return 0; |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 61 | |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 62 | case NC_TI_FD: |
| 63 | /* read via standard file descriptor */ |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 64 | r = read(session->ti.fd.in, buf + readd, count - readd); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 65 | if (r < 0) { |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 66 | if ((errno == EAGAIN) || (errno == EINTR)) { |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 67 | r = 0; |
| 68 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 69 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 70 | ERR("Session %u: reading from file descriptor (%d) failed (%s).", |
| 71 | session->id, session->ti.fd.in, strerror(errno)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 72 | session->status = NC_STATUS_INVALID; |
| 73 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | } else if (r == 0) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 77 | ERR("Session %u: communication file descriptor (%d) unexpectedly closed.", |
| 78 | session->id, session->ti.fd.in); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 79 | session->status = NC_STATUS_INVALID; |
| 80 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 81 | return -1; |
| 82 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 83 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 84 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 85 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 86 | case NC_TI_LIBSSH: |
| 87 | /* read via libssh */ |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 88 | r = ssh_channel_read(session->ti.libssh.channel, buf + readd, count - readd, 0); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 89 | if (r == SSH_AGAIN) { |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 90 | r = 0; |
| 91 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 92 | } else if (r == SSH_ERROR) { |
Michal Vasko | 051d35b | 2016-02-03 15:28:37 +0100 | [diff] [blame] | 93 | ERR("Session %u: reading from the SSH channel failed (%s).", session->id, |
| 94 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 95 | session->status = NC_STATUS_INVALID; |
| 96 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 97 | return -1; |
| 98 | } else if (r == 0) { |
| 99 | if (ssh_channel_is_eof(session->ti.libssh.channel)) { |
Michal Vasko | 454e22b | 2016-01-21 15:34:08 +0100 | [diff] [blame] | 100 | ERR("Session %u: SSH channel unexpected EOF.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 101 | session->status = NC_STATUS_INVALID; |
| 102 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 103 | return -1; |
| 104 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 105 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 106 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 107 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 108 | #endif |
| 109 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 110 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 111 | case NC_TI_OPENSSL: |
| 112 | /* read via OpenSSL */ |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 113 | r = SSL_read(session->ti.tls, buf + readd, count - readd); |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 114 | if (r <= 0) { |
| 115 | int x; |
| 116 | switch (x = SSL_get_error(session->ti.tls, r)) { |
| 117 | case SSL_ERROR_WANT_READ: |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 118 | r = 0; |
| 119 | break; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 120 | case SSL_ERROR_ZERO_RETURN: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 121 | ERR("Session %u: communication socket unexpectedly closed (OpenSSL).", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 122 | session->status = NC_STATUS_INVALID; |
| 123 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 124 | return -1; |
| 125 | default: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 126 | ERR("Session %u: reading from the TLS session failed (SSL code %d).", session->id, x); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 127 | session->status = NC_STATUS_INVALID; |
| 128 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 129 | return -1; |
| 130 | } |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 131 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 132 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 133 | #endif |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 136 | if (r == 0) { |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 137 | /* nothing read */ |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 138 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame^] | 139 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 140 | if ((nc_difftimespec(&ts_cur, &ts_inact_timeout) < 1) || (nc_difftimespec(&ts_cur, ts_act_timeout) < 1)) { |
| 141 | if (nc_difftimespec(&ts_cur, &ts_inact_timeout) < 1) { |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 142 | ERR("Session %u: inactive read timeout elapsed.", session->id); |
| 143 | } else { |
| 144 | ERR("Session %u: active read timeout elapsed.", session->id); |
| 145 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 146 | session->status = NC_STATUS_INVALID; |
| 147 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 148 | return -1; |
| 149 | } |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 150 | } else { |
| 151 | /* something read */ |
| 152 | readd += r; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 153 | |
| 154 | /* reset inactive timeout */ |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame^] | 155 | nc_gettimespec_mono(&ts_inact_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 156 | nc_addtimespec(&ts_inact_timeout, inact_timeout); |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 159 | } while (readd < count); |
| 160 | buf[count] = '\0'; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 161 | |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 162 | return (ssize_t)readd; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static ssize_t |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 166 | nc_read_chunk(struct nc_session *session, size_t len, uint32_t inact_timeout, struct timespec *ts_act_timeout, char **chunk) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 167 | { |
| 168 | ssize_t r; |
| 169 | |
| 170 | assert(session); |
| 171 | assert(chunk); |
| 172 | |
| 173 | if (!len) { |
| 174 | return 0; |
| 175 | } |
| 176 | |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 177 | *chunk = malloc((len + 1) * sizeof **chunk); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 178 | if (!*chunk) { |
| 179 | ERRMEM; |
| 180 | return -1; |
| 181 | } |
| 182 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 183 | r = nc_read(session, *chunk, len, inact_timeout, ts_act_timeout); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 184 | if (r <= 0) { |
| 185 | free(*chunk); |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | /* terminating null byte */ |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 190 | (*chunk)[r] = 0; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 191 | |
| 192 | return r; |
| 193 | } |
| 194 | |
| 195 | static ssize_t |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 196 | nc_read_until(struct nc_session *session, const char *endtag, size_t limit, uint32_t inact_timeout, |
| 197 | struct timespec *ts_act_timeout, char **result) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 198 | { |
| 199 | char *chunk = NULL; |
| 200 | size_t size, count = 0, r, len; |
| 201 | |
| 202 | assert(session); |
| 203 | assert(endtag); |
| 204 | |
| 205 | if (limit && limit < BUFFERSIZE) { |
| 206 | size = limit; |
| 207 | } else { |
| 208 | size = BUFFERSIZE; |
| 209 | } |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 210 | chunk = malloc((size + 1) * sizeof *chunk); |
Radek Krejci | b791b53 | 2015-10-08 15:29:34 +0200 | [diff] [blame] | 211 | if (!chunk) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 212 | ERRMEM; |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | len = strlen(endtag); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 217 | while (1) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 218 | if (limit && count == limit) { |
| 219 | free(chunk); |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 220 | WRN("Session %u: reading limit (%d) reached.", session->id, limit); |
| 221 | ERR("Session %u: invalid input data (missing \"%s\" sequence).", session->id, endtag); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | /* resize buffer if needed */ |
| 226 | if (count == size) { |
| 227 | /* get more memory */ |
| 228 | size = size + BUFFERSIZE; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 229 | chunk = realloc(chunk, (size + 1) * sizeof *chunk); |
| 230 | if (!chunk) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 231 | ERRMEM; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 232 | return -1; |
| 233 | } |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* get another character */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 237 | r = nc_read(session, &(chunk[count]), 1, inact_timeout, ts_act_timeout); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 238 | if (r != 1) { |
| 239 | free(chunk); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | count++; |
| 244 | |
| 245 | /* check endtag */ |
| 246 | if (count >= len) { |
| 247 | if (!strncmp(endtag, &(chunk[count - len]), len)) { |
| 248 | /* endtag found */ |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* terminating null byte */ |
| 255 | chunk[count] = 0; |
| 256 | |
| 257 | if (result) { |
| 258 | *result = chunk; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 259 | } else { |
| 260 | free(chunk); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 261 | } |
| 262 | return count; |
| 263 | } |
| 264 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 265 | /* return NC_MSG_ERROR can change session status */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 266 | NC_MSG_TYPE |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 267 | nc_read_msg(struct nc_session *session, struct lyxml_elem **data) |
| 268 | { |
| 269 | int ret; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 270 | char *msg = NULL, *chunk; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 271 | uint64_t chunk_len, len = 0; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 272 | /* use timeout in milliseconds instead seconds */ |
| 273 | uint32_t inact_timeout = NC_READ_INACT_TIMEOUT * 1000; |
| 274 | struct timespec ts_act_timeout; |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 275 | struct nc_server_reply *reply; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 276 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 277 | assert(session && data); |
| 278 | *data = NULL; |
| 279 | |
| 280 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 281 | ERR("Session %u: invalid session to read from.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 282 | return NC_MSG_ERROR; |
| 283 | } |
| 284 | |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame^] | 285 | nc_gettimespec_mono(&ts_act_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 286 | nc_addtimespec(&ts_act_timeout, NC_READ_ACT_TIMEOUT * 1000); |
| 287 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 288 | /* read the message */ |
| 289 | switch (session->version) { |
| 290 | case NC_VERSION_10: |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 291 | ret = nc_read_until(session, NC_VERSION_10_ENDTAG, 0, inact_timeout, &ts_act_timeout, &msg); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 292 | if (ret == -1) { |
| 293 | goto error; |
| 294 | } |
| 295 | |
| 296 | /* cut off the end tag */ |
| 297 | msg[ret - NC_VERSION_10_ENDTAG_LEN] = '\0'; |
| 298 | break; |
| 299 | case NC_VERSION_11: |
| 300 | while (1) { |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 301 | ret = nc_read_until(session, "\n#", 0, inact_timeout, &ts_act_timeout, NULL); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 302 | if (ret == -1) { |
| 303 | goto error; |
| 304 | } |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 305 | ret = nc_read_until(session, "\n", 0, inact_timeout, &ts_act_timeout, &chunk); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 306 | if (ret == -1) { |
| 307 | goto error; |
| 308 | } |
| 309 | |
| 310 | if (!strcmp(chunk, "#\n")) { |
| 311 | /* end of chunked framing message */ |
| 312 | free(chunk); |
Michal Vasko | 79df326 | 2016-07-13 13:42:17 +0200 | [diff] [blame] | 313 | if (!msg) { |
| 314 | ERR("Session %u: invalid frame chunk delimiters.", session->id); |
| 315 | goto malformed_msg; |
| 316 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | |
| 320 | /* convert string to the size of the following chunk */ |
| 321 | chunk_len = strtoul(chunk, (char **)NULL, 10); |
| 322 | free(chunk); |
| 323 | if (!chunk_len) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 324 | ERR("Session %u: invalid frame chunk size detected, fatal error.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 325 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* now we have size of next chunk, so read the chunk */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 329 | ret = nc_read_chunk(session, chunk_len, inact_timeout, &ts_act_timeout, &chunk); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 330 | if (ret == -1) { |
| 331 | goto error; |
| 332 | } |
| 333 | |
| 334 | /* realloc message buffer, remember to count terminating null byte */ |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 335 | msg = realloc(msg, len + chunk_len + 1); |
| 336 | if (!msg) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 337 | ERRMEM; |
| 338 | goto error; |
| 339 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 340 | memcpy(msg + len, chunk, chunk_len); |
| 341 | len += chunk_len; |
| 342 | msg[len] = '\0'; |
| 343 | free(chunk); |
| 344 | } |
| 345 | |
| 346 | break; |
| 347 | } |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 348 | DBG("Session %u: received message:\n%s\n", session->id, msg); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 349 | |
| 350 | /* build XML tree */ |
Michal Vasko | a4c23d8 | 2016-02-03 15:48:09 +0100 | [diff] [blame] | 351 | *data = lyxml_parse_mem(session->ctx, msg, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 352 | if (!*data) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 353 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 354 | } else if (!(*data)->ns) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 355 | ERR("Session %u: invalid message root element (invalid namespace).", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 356 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 357 | } |
| 358 | free(msg); |
| 359 | msg = NULL; |
| 360 | |
| 361 | /* get and return message type */ |
| 362 | if (!strcmp((*data)->ns->value, NC_NS_BASE)) { |
| 363 | if (!strcmp((*data)->name, "rpc")) { |
| 364 | return NC_MSG_RPC; |
| 365 | } else if (!strcmp((*data)->name, "rpc-reply")) { |
| 366 | return NC_MSG_REPLY; |
| 367 | } else if (!strcmp((*data)->name, "hello")) { |
| 368 | return NC_MSG_HELLO; |
| 369 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 370 | ERR("Session %u: invalid message root element (invalid name \"%s\").", session->id, (*data)->name); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 371 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 372 | } |
| 373 | } else if (!strcmp((*data)->ns->value, NC_NS_NOTIF)) { |
| 374 | if (!strcmp((*data)->name, "notification")) { |
| 375 | return NC_MSG_NOTIF; |
| 376 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 377 | ERR("Session %u: invalid message root element (invalid name \"%s\").", session->id, (*data)->name); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 378 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 379 | } |
| 380 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 381 | ERR("Session %u: invalid message root element (invalid namespace \"%s\").", session->id, (*data)->ns->value); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 382 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 383 | } |
| 384 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 385 | malformed_msg: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 386 | ERR("Session %u: malformed message received.", session->id); |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 387 | if ((session->side == NC_SERVER) && (session->version == NC_VERSION_11)) { |
| 388 | /* NETCONF version 1.1 defines sending error reply from the server (RFC 6241 sec. 3) */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 389 | reply = nc_server_reply_err(nc_err(NC_ERR_MALFORMED_MSG)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 390 | |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 391 | if (nc_write_msg(session, NC_MSG_REPLY, NULL, reply) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 392 | ERR("Session %u: unable to send a \"Malformed message\" error reply, terminating session.", session->id); |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 393 | if (session->status != NC_STATUS_INVALID) { |
| 394 | session->status = NC_STATUS_INVALID; |
| 395 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 396 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 397 | } |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 398 | nc_server_reply_free(reply); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 401 | error: |
| 402 | /* cleanup */ |
| 403 | free(msg); |
| 404 | free(*data); |
| 405 | *data = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 406 | |
| 407 | return NC_MSG_ERROR; |
| 408 | } |
| 409 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 410 | /* return -1 means either poll error or that session was invalidated (socket error), EINTR is handled inside */ |
| 411 | static int |
| 412 | nc_read_poll(struct nc_session *session, int timeout) |
| 413 | { |
Radek Krejci | 5961c70 | 2016-07-15 09:15:18 +0200 | [diff] [blame] | 414 | sigset_t sigmask, origmask; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 415 | int ret = -2; |
| 416 | struct pollfd fds; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 417 | |
| 418 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 419 | ERR("Session %u: invalid session to poll.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 420 | return -1; |
| 421 | } |
| 422 | |
| 423 | switch (session->ti_type) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 424 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 425 | case NC_TI_LIBSSH: |
| 426 | /* EINTR is handled, it resumes waiting */ |
| 427 | ret = ssh_channel_poll_timeout(session->ti.libssh.channel, timeout, 0); |
| 428 | if (ret == SSH_ERROR) { |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 429 | ERR("Session %u: SSH channel poll error (%s).", session->id, |
Michal Vasko | 051d35b | 2016-02-03 15:28:37 +0100 | [diff] [blame] | 430 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 431 | session->status = NC_STATUS_INVALID; |
| 432 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 433 | return -1; |
| 434 | } else if (ret == SSH_EOF) { |
Michal Vasko | 051d35b | 2016-02-03 15:28:37 +0100 | [diff] [blame] | 435 | ERR("Session %u: SSH channel unexpected EOF.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 436 | session->status = NC_STATUS_INVALID; |
| 437 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 438 | return -1; |
| 439 | } else if (ret > 0) { |
| 440 | /* fake it */ |
| 441 | ret = 1; |
| 442 | fds.revents = POLLIN; |
Michal Vasko | 5550cda | 2016-02-03 15:28:57 +0100 | [diff] [blame] | 443 | } else { /* ret == 0 */ |
| 444 | fds.revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 445 | } |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 446 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 447 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 448 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 449 | case NC_TI_OPENSSL: |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 450 | ret = SSL_pending(session->ti.tls); |
| 451 | if (ret) { |
| 452 | /* some buffered TLS data available */ |
| 453 | ret = 1; |
| 454 | fds.revents = POLLIN; |
| 455 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 456 | } |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 457 | |
| 458 | fds.fd = SSL_get_fd(session->ti.tls); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 459 | /* fallthrough */ |
| 460 | #endif |
| 461 | case NC_TI_FD: |
| 462 | if (session->ti_type == NC_TI_FD) { |
| 463 | fds.fd = session->ti.fd.in; |
| 464 | } |
| 465 | |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 466 | fds.events = POLLIN; |
| 467 | fds.revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 468 | |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 469 | sigfillset(&sigmask); |
| 470 | pthread_sigmask(SIG_SETMASK, &sigmask, &origmask); |
| 471 | ret = poll(&fds, 1, timeout); |
| 472 | pthread_sigmask(SIG_SETMASK, &origmask, NULL); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 473 | |
| 474 | break; |
| 475 | |
| 476 | default: |
| 477 | ERRINT; |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | /* process the poll result, unified ret meaning for poll and ssh_channel poll */ |
| 482 | if (ret < 0) { |
| 483 | /* poll failed - something really bad happened, close the session */ |
Radek Krejci | 5961c70 | 2016-07-15 09:15:18 +0200 | [diff] [blame] | 484 | ERR("Session %u: poll error (%s).", session->id, strerror(errno)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 485 | session->status = NC_STATUS_INVALID; |
| 486 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 487 | return -1; |
| 488 | } else { /* status > 0 */ |
| 489 | /* in case of standard (non-libssh) poll, there still can be an error */ |
| 490 | if (fds.revents & POLLHUP) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 491 | ERR("Session %u: communication channel unexpectedly closed.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 492 | session->status = NC_STATUS_INVALID; |
| 493 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 494 | return -1; |
| 495 | } |
| 496 | if (fds.revents & POLLERR) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 497 | ERR("Session %u: communication channel error.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 498 | session->status = NC_STATUS_INVALID; |
| 499 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 500 | return -1; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | return ret; |
| 505 | } |
| 506 | |
| 507 | /* return NC_MSG_ERROR can change session status */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 508 | NC_MSG_TYPE |
| 509 | nc_read_msg_poll(struct nc_session *session, int timeout, struct lyxml_elem **data) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 510 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 511 | int ret; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 512 | |
| 513 | assert(data); |
| 514 | *data = NULL; |
| 515 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 516 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 517 | ERR("Session %u: invalid session to read from.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 518 | return NC_MSG_ERROR; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 519 | } |
| 520 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 521 | ret = nc_read_poll(session, timeout); |
| 522 | if (ret == 0) { |
| 523 | /* timed out */ |
| 524 | return NC_MSG_WOULDBLOCK; |
| 525 | } else if (ret < 0) { |
| 526 | /* poll error, error written */ |
| 527 | return NC_MSG_ERROR; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 530 | return nc_read_msg(session, data); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 531 | } |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 532 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 533 | /* does not really log, only fatal errors */ |
| 534 | int |
| 535 | nc_session_is_connected(struct nc_session *session) |
| 536 | { |
| 537 | int ret; |
| 538 | struct pollfd fds; |
| 539 | |
| 540 | switch (session->ti_type) { |
| 541 | case NC_TI_FD: |
| 542 | fds.fd = session->ti.fd.in; |
| 543 | break; |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 544 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 545 | case NC_TI_LIBSSH: |
Michal Vasko | 840a8a6 | 2017-02-07 10:56:34 +0100 | [diff] [blame] | 546 | return ssh_is_connected(session->ti.libssh.session); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 547 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 548 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 549 | case NC_TI_OPENSSL: |
| 550 | fds.fd = SSL_get_fd(session->ti.tls); |
| 551 | break; |
| 552 | #endif |
| 553 | case NC_TI_NONE: |
| 554 | ERRINT; |
| 555 | return 0; |
| 556 | } |
| 557 | |
Michal Vasko | 840a8a6 | 2017-02-07 10:56:34 +0100 | [diff] [blame] | 558 | if (fds.fd == -1) { |
| 559 | return 0; |
| 560 | } |
| 561 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 562 | fds.events = POLLIN; |
Michal Vasko | 3e9d168 | 2017-02-24 09:50:15 +0100 | [diff] [blame] | 563 | fds.revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 564 | |
| 565 | errno = 0; |
| 566 | while (((ret = poll(&fds, 1, 0)) == -1) && (errno == EINTR)); |
| 567 | |
| 568 | if (ret == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 569 | ERR("Session %u: poll failed (%s).", session->id, strerror(errno)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 570 | return 0; |
| 571 | } else if ((ret > 0) && (fds.revents & (POLLHUP | POLLERR))) { |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | return 1; |
| 576 | } |
| 577 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 578 | #define WRITE_BUFSIZE (2 * BUFFERSIZE) |
| 579 | struct wclb_arg { |
| 580 | struct nc_session *session; |
| 581 | char buf[WRITE_BUFSIZE]; |
| 582 | size_t len; |
| 583 | }; |
| 584 | |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 585 | static int |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 586 | nc_write(struct nc_session *session, const void *buf, size_t count) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 587 | { |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 588 | int c; |
| 589 | size_t written = 0; |
Michal Vasko | e2357e9 | 2016-10-05 14:20:47 +0200 | [diff] [blame] | 590 | #ifdef NC_ENABLED_TLS |
| 591 | unsigned long e; |
| 592 | #endif |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 593 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 594 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 595 | return -1; |
| 596 | } |
| 597 | |
| 598 | /* prevent SIGPIPE this way */ |
| 599 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 600 | ERR("Session %u: communication socket unexpectedly closed.", session->id); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 601 | session->status = NC_STATUS_INVALID; |
| 602 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 603 | return -1; |
| 604 | } |
| 605 | |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 606 | DBG("Session %u: sending message:\n%.*s\n", session->id, count, buf); |
Michal Vasko | 160b791 | 2016-06-20 10:00:53 +0200 | [diff] [blame] | 607 | |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 608 | do { |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 609 | switch (session->ti_type) { |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 610 | case NC_TI_FD: |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 611 | c = write(session->ti.fd.out, (char *)(buf + written), count - written); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 612 | if (c < 0) { |
Michal Vasko | e2146a3 | 2016-09-23 14:20:36 +0200 | [diff] [blame] | 613 | ERR("Session %u: socket error (%s).", session->id, strerror(errno)); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 614 | return -1; |
| 615 | } |
| 616 | break; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 617 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 618 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 619 | case NC_TI_LIBSSH: |
| 620 | if (ssh_channel_is_closed(session->ti.libssh.channel) || ssh_channel_is_eof(session->ti.libssh.channel)) { |
| 621 | if (ssh_channel_is_closed(session->ti.libssh.channel)) { |
| 622 | ERR("Session %u: SSH channel unexpectedly closed.", session->id); |
| 623 | } else { |
| 624 | ERR("Session %u: SSH channel unexpected EOF.", session->id); |
| 625 | } |
| 626 | session->status = NC_STATUS_INVALID; |
| 627 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 628 | return -1; |
Michal Vasko | 454e22b | 2016-01-21 15:34:08 +0100 | [diff] [blame] | 629 | } |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 630 | c = ssh_channel_write(session->ti.libssh.channel, (char *)(buf + written), count - written); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 631 | if ((c == SSH_ERROR) || (c == -1)) { |
| 632 | ERR("Session %u: SSH channel write failed.", session->id); |
| 633 | return -1; |
| 634 | } |
| 635 | break; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 636 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 637 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 638 | case NC_TI_OPENSSL: |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 639 | c = SSL_write(session->ti.tls, (char *)(buf + written), count - written); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 640 | if (c < 1) { |
| 641 | switch ((e = SSL_get_error(session->ti.tls, c))) { |
| 642 | case SSL_ERROR_ZERO_RETURN: |
| 643 | ERR("Session %u: SSL connection was properly closed.", session->id); |
| 644 | return -1; |
| 645 | case SSL_ERROR_WANT_WRITE: |
| 646 | c = 0; |
| 647 | break; |
| 648 | case SSL_ERROR_SYSCALL: |
| 649 | ERR("Session %u: SSL socket error (%s).", session->id, strerror(errno)); |
| 650 | return -1; |
| 651 | case SSL_ERROR_SSL: |
| 652 | ERR("Session %u: SSL error (%s).", session->id, ERR_reason_error_string(e)); |
| 653 | return -1; |
| 654 | default: |
| 655 | ERR("Session %u: unknown SSL error occured.", session->id); |
| 656 | return -1; |
| 657 | } |
| 658 | } |
| 659 | break; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 660 | #endif |
Michal Vasko | 339eea8 | 2016-09-29 11:42:36 +0200 | [diff] [blame] | 661 | default: |
| 662 | ERRINT; |
| 663 | return -1; |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | if (c == 0) { |
| 667 | /* we must wait */ |
| 668 | usleep(NC_TIMEOUT_STEP); |
| 669 | } |
| 670 | |
| 671 | written += c; |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 672 | } while (written < count); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 673 | |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 674 | return written; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 677 | static int |
| 678 | nc_write_starttag_and_msg(struct nc_session *session, const void *buf, size_t count) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 679 | { |
Michal Vasko | fd2db9b | 2016-01-14 16:15:16 +0100 | [diff] [blame] | 680 | int ret = 0, c; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 681 | char chunksize[20]; |
| 682 | |
| 683 | if (session->version == NC_VERSION_11) { |
| 684 | sprintf(chunksize, "\n#%zu\n", count); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 685 | ret = nc_write(session, chunksize, strlen(chunksize)); |
| 686 | if (ret == -1) { |
| 687 | return -1; |
| 688 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 689 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 690 | |
| 691 | c = nc_write(session, buf, count); |
| 692 | if (c == -1) { |
| 693 | return -1; |
| 694 | } |
| 695 | ret += c; |
| 696 | |
| 697 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 698 | } |
| 699 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 700 | static int |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 701 | nc_write_endtag(struct nc_session *session) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 702 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 703 | int ret; |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 704 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 705 | if (session->version == NC_VERSION_11) { |
| 706 | ret = nc_write(session, "\n##\n", 4); |
| 707 | } else { |
| 708 | ret = nc_write(session, "]]>]]>", 6); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 709 | } |
| 710 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 711 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 714 | static int |
| 715 | nc_write_clb_flush(struct wclb_arg *warg) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 716 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 717 | int ret = 0; |
| 718 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 719 | /* flush current buffer */ |
| 720 | if (warg->len) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 721 | ret = nc_write_starttag_and_msg(warg->session, warg->buf, warg->len); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 722 | warg->len = 0; |
| 723 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 724 | |
| 725 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | static ssize_t |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 729 | nc_write_clb(void *arg, const void *buf, size_t count, int xmlcontent) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 730 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 731 | int ret = 0, c; |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 732 | size_t l; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 733 | struct wclb_arg *warg = (struct wclb_arg *)arg; |
| 734 | |
| 735 | if (!buf) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 736 | c = nc_write_clb_flush(warg); |
| 737 | if (c == -1) { |
| 738 | return -1; |
| 739 | } |
| 740 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 741 | |
| 742 | /* endtag */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 743 | c = nc_write_endtag(warg->session); |
| 744 | if (c == -1) { |
| 745 | return -1; |
| 746 | } |
| 747 | ret += c; |
| 748 | |
| 749 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | if (warg->len && (warg->len + count > WRITE_BUFSIZE)) { |
| 753 | /* dump current buffer */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 754 | c = nc_write_clb_flush(warg); |
| 755 | if (c == -1) { |
| 756 | return -1; |
| 757 | } |
| 758 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 759 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 760 | |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 761 | if (!xmlcontent && count > WRITE_BUFSIZE) { |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 762 | /* write directly */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 763 | c = nc_write_starttag_and_msg(warg->session, buf, count); |
| 764 | if (c == -1) { |
| 765 | return -1; |
| 766 | } |
| 767 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 768 | } else { |
| 769 | /* keep in buffer and write later */ |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 770 | if (xmlcontent) { |
| 771 | for (l = 0; l < count; l++) { |
| 772 | if (warg->len + 5 >= WRITE_BUFSIZE) { |
| 773 | /* buffer is full */ |
| 774 | c = nc_write_clb_flush(warg); |
| 775 | if (c == -1) { |
| 776 | return -1; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | switch (((char *)buf)[l]) { |
| 781 | case '&': |
| 782 | ret += 5; |
| 783 | memcpy(&warg->buf[warg->len], "&", 5); |
| 784 | warg->len += 5; |
| 785 | break; |
| 786 | case '<': |
| 787 | ret += 4; |
| 788 | memcpy(&warg->buf[warg->len], "<", 4); |
| 789 | warg->len += 4; |
| 790 | break; |
| 791 | case '>': |
| 792 | /* not needed, just for readability */ |
| 793 | ret += 4; |
| 794 | memcpy(&warg->buf[warg->len], ">", 4); |
| 795 | warg->len += 4; |
| 796 | break; |
| 797 | default: |
| 798 | ret++; |
| 799 | memcpy(&warg->buf[warg->len], &((char *)buf)[l], 1); |
| 800 | warg->len++; |
| 801 | } |
| 802 | } |
| 803 | } else { |
| 804 | memcpy(&warg->buf[warg->len], buf, count); |
| 805 | warg->len += count; /* is <= WRITE_BUFSIZE */ |
| 806 | ret += count; |
| 807 | } |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 808 | } |
| 809 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 810 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 811 | } |
| 812 | |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 813 | static ssize_t |
| 814 | nc_write_xmlclb(void *arg, const void *buf, size_t count) |
| 815 | { |
| 816 | return nc_write_clb(arg, buf, count, 0); |
| 817 | } |
| 818 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 819 | static void |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 820 | nc_write_error_elem(struct wclb_arg *arg, const char *name, uint16_t nam_len, const char *prefix, uint16_t pref_len, |
| 821 | int open, int no_attr) |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 822 | { |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 823 | if (open) { |
| 824 | nc_write_clb((void *)arg, "<", 1, 0); |
| 825 | } else { |
| 826 | nc_write_clb((void *)arg, "</", 2, 0); |
| 827 | } |
| 828 | |
| 829 | if (prefix) { |
| 830 | nc_write_clb((void *)arg, prefix, pref_len, 0); |
| 831 | nc_write_clb((void *)arg, ":", 1, 0); |
| 832 | } |
| 833 | |
| 834 | nc_write_clb((void *)arg, name, nam_len, 0); |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 835 | if (!open || !no_attr) { |
| 836 | nc_write_clb((void *)arg, ">", 1, 0); |
| 837 | } |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | static void |
| 841 | nc_write_error(struct wclb_arg *arg, struct nc_server_error *err, const char *prefix) |
| 842 | { |
Michal Vasko | 3e9d168 | 2017-02-24 09:50:15 +0100 | [diff] [blame] | 843 | uint16_t i, pref_len = 0; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 844 | char str_sid[11]; |
| 845 | |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 846 | if (prefix) { |
| 847 | pref_len = strlen(prefix); |
| 848 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 849 | |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 850 | nc_write_error_elem(arg, "rpc-error", 9, prefix, pref_len, 1, 0); |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 851 | |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 852 | nc_write_error_elem(arg, "error-type", 10, prefix, pref_len, 1, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 853 | switch (err->type) { |
| 854 | case NC_ERR_TYPE_TRAN: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 855 | nc_write_clb((void *)arg, "transport", 9, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 856 | break; |
| 857 | case NC_ERR_TYPE_RPC: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 858 | nc_write_clb((void *)arg, "rpc", 3, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 859 | break; |
| 860 | case NC_ERR_TYPE_PROT: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 861 | nc_write_clb((void *)arg, "protocol", 8, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 862 | break; |
| 863 | case NC_ERR_TYPE_APP: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 864 | nc_write_clb((void *)arg, "application", 11, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 865 | break; |
| 866 | default: |
| 867 | ERRINT; |
| 868 | return; |
| 869 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 870 | |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 871 | nc_write_error_elem(arg, "error-type", 10, prefix, pref_len, 0, 0); |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 872 | |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 873 | nc_write_error_elem(arg, "error-tag", 9, prefix, pref_len, 1, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 874 | switch (err->tag) { |
| 875 | case NC_ERR_IN_USE: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 876 | nc_write_clb((void *)arg, "in-use", 6, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 877 | break; |
| 878 | case NC_ERR_INVALID_VALUE: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 879 | nc_write_clb((void *)arg, "invalid-value", 13, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 880 | break; |
| 881 | case NC_ERR_TOO_BIG: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 882 | nc_write_clb((void *)arg, "too-big", 7, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 883 | break; |
| 884 | case NC_ERR_MISSING_ATTR: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 885 | nc_write_clb((void *)arg, "missing-attribute", 17, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 886 | break; |
| 887 | case NC_ERR_BAD_ATTR: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 888 | nc_write_clb((void *)arg, "bad-attribute", 13, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 889 | break; |
| 890 | case NC_ERR_UNKNOWN_ATTR: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 891 | nc_write_clb((void *)arg, "unknown-attribute", 17, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 892 | break; |
| 893 | case NC_ERR_MISSING_ELEM: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 894 | nc_write_clb((void *)arg, "missing-element", 15, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 895 | break; |
| 896 | case NC_ERR_BAD_ELEM: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 897 | nc_write_clb((void *)arg, "bad-element", 11, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 898 | break; |
| 899 | case NC_ERR_UNKNOWN_ELEM: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 900 | nc_write_clb((void *)arg, "unknown-element", 15, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 901 | break; |
| 902 | case NC_ERR_UNKNOWN_NS: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 903 | nc_write_clb((void *)arg, "unknown-namespace", 17, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 904 | break; |
| 905 | case NC_ERR_ACCESS_DENIED: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 906 | nc_write_clb((void *)arg, "access-denied", 13, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 907 | break; |
| 908 | case NC_ERR_LOCK_DENIED: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 909 | nc_write_clb((void *)arg, "lock-denied", 11, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 910 | break; |
| 911 | case NC_ERR_RES_DENIED: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 912 | nc_write_clb((void *)arg, "resource-denied", 15, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 913 | break; |
| 914 | case NC_ERR_ROLLBACK_FAILED: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 915 | nc_write_clb((void *)arg, "rollback-failed", 15, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 916 | break; |
| 917 | case NC_ERR_DATA_EXISTS: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 918 | nc_write_clb((void *)arg, "data-exists", 11, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 919 | break; |
| 920 | case NC_ERR_DATA_MISSING: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 921 | nc_write_clb((void *)arg, "data-missing", 12, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 922 | break; |
| 923 | case NC_ERR_OP_NOT_SUPPORTED: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 924 | nc_write_clb((void *)arg, "operation-not-supported", 23, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 925 | break; |
| 926 | case NC_ERR_OP_FAILED: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 927 | nc_write_clb((void *)arg, "operation-failed", 16, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 928 | break; |
| 929 | case NC_ERR_MALFORMED_MSG: |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 930 | nc_write_clb((void *)arg, "malformed-message", 17, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 931 | break; |
| 932 | default: |
| 933 | ERRINT; |
| 934 | return; |
| 935 | } |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 936 | nc_write_error_elem(arg, "error-tag", 9, prefix, pref_len, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 937 | |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 938 | nc_write_error_elem(arg, "error-severity", 14, prefix, pref_len, 1, 0); |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 939 | nc_write_clb((void *)arg, "error", 5, 0); |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 940 | nc_write_error_elem(arg, "error-severity", 14, prefix, pref_len, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 941 | |
| 942 | if (err->apptag) { |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 943 | nc_write_error_elem(arg, "error-app-tag", 13, prefix, pref_len, 1, 0); |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 944 | nc_write_clb((void *)arg, err->apptag, strlen(err->apptag), 1); |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 945 | nc_write_error_elem(arg, "error-app-tag", 13, prefix, pref_len, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | if (err->path) { |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 949 | nc_write_error_elem(arg, "error-path", 10, prefix, pref_len, 1, 0); |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 950 | nc_write_clb((void *)arg, err->path, strlen(err->path), 1); |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 951 | nc_write_error_elem(arg, "error-path", 10, prefix, pref_len, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | if (err->message) { |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 955 | nc_write_error_elem(arg, "error-message", 13, prefix, pref_len, 1, 1); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 956 | if (err->message_lang) { |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 957 | nc_write_clb((void *)arg, " xml:lang=\"", 11, 0); |
| 958 | nc_write_clb((void *)arg, err->message_lang, strlen(err->message_lang), 1); |
| 959 | nc_write_clb((void *)arg, "\"", 1, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 960 | } |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 961 | nc_write_clb((void *)arg, ">", 1, 0); |
| 962 | nc_write_clb((void *)arg, err->message, strlen(err->message), 1); |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 963 | nc_write_error_elem(arg, "error-message", 13, prefix, pref_len, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 964 | } |
| 965 | |
Michal Vasko | 90920b0 | 2016-05-20 14:07:00 +0200 | [diff] [blame] | 966 | if ((err->sid > -1) || err->attr_count || err->elem_count || err->ns_count || err->other_count) { |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 967 | nc_write_error_elem(arg, "error-info", 10, prefix, pref_len, 1, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 968 | |
Michal Vasko | 90920b0 | 2016-05-20 14:07:00 +0200 | [diff] [blame] | 969 | if (err->sid > -1) { |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 970 | nc_write_error_elem(arg, "session-id", 10, prefix, pref_len, 1, 0); |
Michal Vasko | 90920b0 | 2016-05-20 14:07:00 +0200 | [diff] [blame] | 971 | sprintf(str_sid, "%u", (uint32_t)err->sid); |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 972 | nc_write_clb((void *)arg, str_sid, strlen(str_sid), 0); |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 973 | nc_write_error_elem(arg, "session-id", 10, prefix, pref_len, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | for (i = 0; i < err->attr_count; ++i) { |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 977 | nc_write_error_elem(arg, "bad-attribute", 13, prefix, pref_len, 1, 0); |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 978 | nc_write_clb((void *)arg, err->attr[i], strlen(err->attr[i]), 1); |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 979 | nc_write_error_elem(arg, "bad-attribute", 13, prefix, pref_len, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | for (i = 0; i < err->elem_count; ++i) { |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 983 | nc_write_error_elem(arg, "bad-element", 11, prefix, pref_len, 1, 0); |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 984 | nc_write_clb((void *)arg, err->elem[i], strlen(err->elem[i]), 1); |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 985 | nc_write_error_elem(arg, "bad-element", 11, prefix, pref_len, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | for (i = 0; i < err->ns_count; ++i) { |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 989 | nc_write_error_elem(arg, "bad-namespace", 13, prefix, pref_len, 1, 0); |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 990 | nc_write_clb((void *)arg, err->ns[i], strlen(err->ns[i]), 1); |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 991 | nc_write_error_elem(arg, "bad-namespace", 13, prefix, pref_len, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | for (i = 0; i < err->other_count; ++i) { |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 995 | lyxml_print_clb(nc_write_xmlclb, (void *)arg, err->other[i], 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 996 | } |
| 997 | |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 998 | nc_write_error_elem(arg, "error-info", 10, prefix, pref_len, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
Michal Vasko | 52bd949 | 2016-12-08 09:37:43 +0100 | [diff] [blame] | 1001 | nc_write_error_elem(arg, "rpc-error", 9, prefix, pref_len, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1004 | /* return -1 can change session status */ |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1005 | int |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 1006 | nc_write_msg(struct nc_session *session, int type, ...) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1007 | { |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1008 | va_list ap; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1009 | int count; |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 1010 | const char *attrs, *base_prefix; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1011 | struct lyd_node *content; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1012 | struct lyxml_elem *rpc_elem; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1013 | struct nc_server_notif *notif; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1014 | struct nc_server_reply *reply; |
| 1015 | struct nc_server_reply_error *error_rpl; |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1016 | char *buf = NULL; |
| 1017 | struct wclb_arg arg; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1018 | const char **capabilities; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1019 | uint32_t *sid = NULL, i; |
Radek Krejci | f9f9348 | 2016-09-21 14:11:15 +0200 | [diff] [blame] | 1020 | int wd = 0; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1021 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1022 | assert(session); |
| 1023 | |
| 1024 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1025 | ERR("Session %u: invalid session to write to.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1026 | return -1; |
| 1027 | } |
| 1028 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1029 | va_start(ap, type); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1030 | |
| 1031 | arg.session = session; |
| 1032 | arg.len = 0; |
| 1033 | |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 1034 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1035 | switch (type) { |
| 1036 | case NC_MSG_RPC: |
| 1037 | content = va_arg(ap, struct lyd_node *); |
| 1038 | attrs = va_arg(ap, const char *); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1039 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1040 | count = asprintf(&buf, "<rpc xmlns=\"%s\" message-id=\"%"PRIu64"\"%s>", |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1041 | NC_NS_BASE, session->opts.client.msgid + 1, attrs ? attrs : ""); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1042 | if (count == -1) { |
| 1043 | ERRMEM; |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 1044 | va_end(ap); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1045 | return -1; |
| 1046 | } |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1047 | nc_write_clb((void *)&arg, buf, count, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1048 | free(buf); |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 1049 | |
Michal Vasko | e6e8b85 | 2016-10-20 14:07:29 +0200 | [diff] [blame] | 1050 | lyd_print_clb(nc_write_xmlclb, (void *)&arg, content, LYD_XML, LYP_WITHSIBLINGS | LYP_NETCONF); |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1051 | nc_write_clb((void *)&arg, "</rpc>", 6, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1052 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1053 | session->opts.client.msgid++; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1054 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1055 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1056 | case NC_MSG_REPLY: |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1057 | rpc_elem = va_arg(ap, struct lyxml_elem *); |
| 1058 | reply = va_arg(ap, struct nc_server_reply *); |
| 1059 | |
Michal Vasko | 7f0b0ff | 2016-11-15 11:02:28 +0100 | [diff] [blame] | 1060 | if (rpc_elem && rpc_elem->ns && rpc_elem->ns->prefix) { |
| 1061 | nc_write_clb((void *)&arg, "<", 1, 0); |
| 1062 | nc_write_clb((void *)&arg, rpc_elem->ns->prefix, strlen(rpc_elem->ns->prefix), 0); |
| 1063 | nc_write_clb((void *)&arg, ":rpc-reply", 10, 0); |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 1064 | base_prefix = rpc_elem->ns->prefix; |
Michal Vasko | 7f0b0ff | 2016-11-15 11:02:28 +0100 | [diff] [blame] | 1065 | } |
| 1066 | else { |
| 1067 | nc_write_clb((void *)&arg, "<rpc-reply", 10, 0); |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 1068 | base_prefix = NULL; |
Michal Vasko | 7f0b0ff | 2016-11-15 11:02:28 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 1071 | /* can be NULL if replying with a malformed-message error */ |
| 1072 | if (rpc_elem) { |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1073 | lyxml_print_clb(nc_write_xmlclb, (void *)&arg, rpc_elem, LYXML_PRINT_ATTRS); |
Radek Krejci | 844662e | 2016-04-13 16:54:43 +0200 | [diff] [blame] | 1074 | nc_write_clb((void *)&arg, ">", 1, 0); |
| 1075 | } else { |
| 1076 | /* but put there at least the correct namespace */ |
| 1077 | nc_write_clb((void *)&arg, "xmlns=\""NC_NS_BASE"\">", 48, 0); |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 1078 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1079 | switch (reply->type) { |
| 1080 | case NC_RPL_OK: |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 1081 | nc_write_clb((void *)&arg, "<", 1, 0); |
| 1082 | if (base_prefix) { |
| 1083 | nc_write_clb((void *)&arg, base_prefix, strlen(base_prefix), 0); |
| 1084 | nc_write_clb((void *)&arg, ":", 1, 0); |
| 1085 | } |
| 1086 | nc_write_clb((void *)&arg, "ok/>", 4, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1087 | break; |
| 1088 | case NC_RPL_DATA: |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 1089 | switch(((struct nc_server_reply_data *)reply)->wd) { |
| 1090 | case NC_WD_UNKNOWN: |
| 1091 | case NC_WD_EXPLICIT: |
| 1092 | wd = LYP_WD_EXPLICIT; |
| 1093 | break; |
| 1094 | case NC_WD_TRIM: |
| 1095 | wd = LYP_WD_TRIM; |
| 1096 | break; |
| 1097 | case NC_WD_ALL: |
| 1098 | wd = LYP_WD_ALL; |
| 1099 | break; |
| 1100 | case NC_WD_ALL_TAG: |
| 1101 | wd = LYP_WD_ALL_TAG; |
| 1102 | break; |
| 1103 | } |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 1104 | lyd_print_clb(nc_write_xmlclb, (void *)&arg, ((struct nc_reply_data *)reply)->data, LYD_XML, |
Michal Vasko | e6e8b85 | 2016-10-20 14:07:29 +0200 | [diff] [blame] | 1105 | LYP_WITHSIBLINGS | LYP_NETCONF | wd); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1106 | break; |
| 1107 | case NC_RPL_ERROR: |
| 1108 | error_rpl = (struct nc_server_reply_error *)reply; |
| 1109 | for (i = 0; i < error_rpl->count; ++i) { |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 1110 | nc_write_error(&arg, error_rpl->err[i], base_prefix); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1111 | } |
| 1112 | break; |
| 1113 | default: |
| 1114 | ERRINT; |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1115 | nc_write_clb((void *)&arg, NULL, 0, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1116 | va_end(ap); |
| 1117 | return -1; |
| 1118 | } |
Michal Vasko | 7f0b0ff | 2016-11-15 11:02:28 +0100 | [diff] [blame] | 1119 | if (rpc_elem && rpc_elem->ns && rpc_elem->ns->prefix) { |
| 1120 | nc_write_clb((void *)&arg, "</", 2, 0); |
| 1121 | nc_write_clb((void *)&arg, rpc_elem->ns->prefix, strlen(rpc_elem->ns->prefix), 0); |
| 1122 | nc_write_clb((void *)&arg, ":rpc-reply>", 11, 0); |
| 1123 | } |
| 1124 | else { |
| 1125 | nc_write_clb((void *)&arg, "</rpc-reply>", 12, 0); |
| 1126 | } |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1127 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1128 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1129 | case NC_MSG_NOTIF: |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1130 | notif = va_arg(ap, struct nc_server_notif *); |
| 1131 | |
| 1132 | nc_write_clb((void *)&arg, "<notification xmlns=\""NC_NS_NOTIF"\">", 21 + 47 + 2, 0); |
| 1133 | nc_write_clb((void *)&arg, "<eventTime>", 11, 0); |
| 1134 | nc_write_clb((void *)&arg, notif->eventtime, strlen(notif->eventtime), 0); |
| 1135 | nc_write_clb((void *)&arg, "</eventTime>", 12, 0); |
| 1136 | lyd_print_clb(nc_write_xmlclb, (void *)&arg, notif->tree, LYD_XML, 0); |
mohitarora24 | 878b296 | 2016-11-09 18:45:33 -0500 | [diff] [blame] | 1137 | nc_write_clb((void *)&arg, "</notification>", 15, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1138 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1139 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1140 | case NC_MSG_HELLO: |
| 1141 | if (session->version != NC_VERSION_10) { |
| 1142 | va_end(ap); |
| 1143 | return -1; |
| 1144 | } |
| 1145 | capabilities = va_arg(ap, const char **); |
| 1146 | sid = va_arg(ap, uint32_t*); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1147 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1148 | count = asprintf(&buf, "<hello xmlns=\"%s\"><capabilities>", NC_NS_BASE); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1149 | if (count == -1) { |
| 1150 | ERRMEM; |
| 1151 | va_end(ap); |
| 1152 | return -1; |
| 1153 | } |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1154 | nc_write_clb((void *)&arg, buf, count, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1155 | free(buf); |
| 1156 | for (i = 0; capabilities[i]; i++) { |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1157 | nc_write_clb((void *)&arg, "<capability>", 12, 0); |
| 1158 | nc_write_clb((void *)&arg, capabilities[i], strlen(capabilities[i]), 1); |
| 1159 | nc_write_clb((void *)&arg, "</capability>", 13, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1160 | } |
| 1161 | if (sid) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1162 | count = asprintf(&buf, "</capabilities><session-id>%u</session-id></hello>", *sid); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1163 | if (count == -1) { |
| 1164 | ERRMEM; |
| 1165 | va_end(ap); |
| 1166 | return -1; |
| 1167 | } |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1168 | nc_write_clb((void *)&arg, buf, count, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1169 | free(buf); |
| 1170 | } else { |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1171 | nc_write_clb((void *)&arg, "</capabilities></hello>", 23, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1172 | } |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1173 | break; |
Michal Vasko | ed46234 | 2016-01-12 12:33:48 +0100 | [diff] [blame] | 1174 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1175 | default: |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1176 | va_end(ap); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1177 | return -1; |
| 1178 | } |
| 1179 | |
| 1180 | /* flush message */ |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1181 | nc_write_clb((void *)&arg, NULL, 0, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1182 | |
| 1183 | va_end(ap); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1184 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 1185 | /* error was already written */ |
| 1186 | return -1; |
| 1187 | } |
| 1188 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1189 | return 0; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1190 | } |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1191 | |
| 1192 | void * |
| 1193 | nc_realloc(void *ptr, size_t size) |
| 1194 | { |
| 1195 | void *ret; |
| 1196 | |
| 1197 | ret = realloc(ptr, size); |
| 1198 | if (!ret) { |
| 1199 | free(ptr); |
| 1200 | } |
| 1201 | |
| 1202 | return ret; |
| 1203 | } |