Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file messages.c |
| 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * \brief libnetconf2 - NETCONF messages functions |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 24 | #include <string.h> |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 25 | |
| 26 | #include <libyang/libyang.h> |
| 27 | |
| 28 | #include "libnetconf.h" |
| 29 | #include "messages_p.h" |
| 30 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 31 | API struct nc_filter * |
| 32 | nc_filter_new(NC_FILTER type, char *data, int constdata) |
| 33 | { |
| 34 | struct nc_filter *filter; |
| 35 | |
| 36 | if (!data) { |
| 37 | data = ""; |
| 38 | constdata = 1; |
| 39 | } |
| 40 | |
| 41 | filter = malloc(sizeof *filter); |
| 42 | if (!filter) { |
| 43 | ERRMEM; |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | filter->type = type; |
| 48 | filter->refs = 1; |
| 49 | if (constdata) { |
| 50 | filter->data = strdup(data); |
| 51 | } else { |
| 52 | filter->data = data; |
| 53 | } |
| 54 | |
| 55 | return filter; |
| 56 | } |
| 57 | |
| 58 | API void |
| 59 | nc_filter_free(struct nc_filter *filter) |
| 60 | { |
| 61 | if (!filter) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | filter->refs--; |
| 66 | |
| 67 | if (!filter->refs) { |
| 68 | free(filter->data); |
| 69 | free(filter); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | API struct nc_rpc * |
Radek Krejci | 926a574 | 2015-10-31 17:50:49 +0100 | [diff] [blame] | 74 | nc_rpc_get(struct nc_filter *filter) |
| 75 | { |
| 76 | struct nc_rpc_get *rpc; |
| 77 | |
| 78 | rpc = calloc(1, sizeof *rpc); |
| 79 | if (!rpc) { |
| 80 | ERRMEM; |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | rpc->type = NC_RPC_GET; |
| 85 | if (filter) { |
| 86 | filter->refs++; |
| 87 | rpc->filter = filter; |
| 88 | } |
| 89 | |
| 90 | return (struct nc_rpc *)rpc; |
| 91 | } |
| 92 | |
| 93 | API struct nc_rpc * |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 94 | nc_rpc_getconfig(NC_DATASTORE source, struct nc_filter *filter) |
| 95 | { |
| 96 | struct nc_rpc_getconfig *rpc; |
| 97 | |
| 98 | rpc = calloc(1, sizeof *rpc); |
| 99 | if (!rpc) { |
| 100 | ERRMEM; |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | rpc->type = NC_RPC_GETCONFIG; |
| 105 | rpc->source = source; |
| 106 | if (filter) { |
| 107 | filter->refs++; |
| 108 | rpc->filter = filter; |
| 109 | } |
| 110 | |
| 111 | return (struct nc_rpc *)rpc; |
| 112 | } |
| 113 | |
| 114 | API struct nc_rpc * |
| 115 | nc_rpc_lock(NC_DATASTORE target) |
| 116 | { |
| 117 | struct nc_rpc_lock *rpc; |
| 118 | |
| 119 | rpc = malloc(sizeof *rpc); |
| 120 | if (!rpc) { |
| 121 | ERRMEM; |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | rpc->type = NC_RPC_LOCK; |
| 126 | rpc->target = target; |
| 127 | |
| 128 | return (struct nc_rpc *)rpc; |
| 129 | } |
| 130 | |
| 131 | API struct nc_rpc * |
| 132 | nc_rpc_unlock(NC_DATASTORE target) |
| 133 | { |
| 134 | struct nc_rpc_lock *rpc; |
| 135 | |
| 136 | rpc = malloc(sizeof *rpc); |
| 137 | if (!rpc) { |
| 138 | ERRMEM; |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | rpc->type = NC_RPC_UNLOCK; |
| 143 | rpc->target = target; |
| 144 | |
| 145 | return (struct nc_rpc *)rpc; |
| 146 | } |
| 147 | |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 148 | API void |
| 149 | nc_rpc_free(struct nc_rpc *rpc) |
| 150 | { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 151 | struct nc_rpc_server *rpc_server; |
| 152 | struct nc_rpc_getconfig *rpc_getconfig; |
| 153 | |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 154 | if (!rpc) { |
| 155 | return; |
| 156 | } |
| 157 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 158 | switch(rpc->type) { |
| 159 | case NC_RPC_SERVER: |
| 160 | rpc_server = (struct nc_rpc_server *)rpc; |
| 161 | lyxml_free_elem(rpc_server->ctx, rpc_server->root); |
| 162 | lyd_free(rpc_server->tree); |
| 163 | break; |
| 164 | case NC_RPC_GETCONFIG: |
| 165 | rpc_getconfig = (struct nc_rpc_getconfig *)rpc; |
| 166 | nc_filter_free(rpc_getconfig->filter); |
| 167 | break; |
| 168 | case NC_RPC_LOCK: |
| 169 | case NC_RPC_UNLOCK: |
| 170 | /* nothing special needed */ |
| 171 | break; |
| 172 | } |
| 173 | |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 174 | free(rpc); |
| 175 | } |
| 176 | |
| 177 | API void |
| 178 | nc_reply_free(struct nc_reply *reply) |
| 179 | { |
Radek Krejci | 8800a49 | 2015-10-31 17:51:27 +0100 | [diff] [blame^] | 180 | struct lyd_node *node; |
| 181 | |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 182 | if (!reply) { |
| 183 | return; |
| 184 | } |
| 185 | |
Radek Krejci | 8800a49 | 2015-10-31 17:51:27 +0100 | [diff] [blame^] | 186 | switch(reply->type) { |
| 187 | case NC_REPLY_DATA: |
| 188 | for (node = ((struct nc_reply_data *)reply)->data; |
| 189 | node; |
| 190 | node = ((struct nc_reply_data *)reply)->data) { |
| 191 | ((struct nc_reply_data *)reply)->data = node->next; |
| 192 | lyd_free(node); |
| 193 | } |
| 194 | break; |
| 195 | case NC_REPLY_OK: |
| 196 | case NC_REPLY_ERROR: |
| 197 | /* TODO */ |
| 198 | break; |
| 199 | } |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 200 | lyxml_free_elem(reply->ctx, reply->root); |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 201 | free(reply); |
| 202 | } |
| 203 | API void |
| 204 | nc_notif_free(struct nc_notif *notif) |
| 205 | { |
| 206 | if (!notif) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | lyxml_free_elem(notif->ctx, notif->root); |
| 211 | lyd_free(notif->tree); |
| 212 | free(notif); |
| 213 | } |