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 * |
| 74 | nc_rpc_getconfig(NC_DATASTORE source, struct nc_filter *filter) |
| 75 | { |
| 76 | struct nc_rpc_getconfig *rpc; |
| 77 | |
| 78 | rpc = calloc(1, sizeof *rpc); |
| 79 | if (!rpc) { |
| 80 | ERRMEM; |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | rpc->type = NC_RPC_GETCONFIG; |
| 85 | rpc->source = source; |
| 86 | if (filter) { |
| 87 | filter->refs++; |
| 88 | rpc->filter = filter; |
| 89 | } |
| 90 | |
| 91 | return (struct nc_rpc *)rpc; |
| 92 | } |
| 93 | |
| 94 | API struct nc_rpc * |
| 95 | nc_rpc_lock(NC_DATASTORE target) |
| 96 | { |
| 97 | struct nc_rpc_lock *rpc; |
| 98 | |
| 99 | rpc = malloc(sizeof *rpc); |
| 100 | if (!rpc) { |
| 101 | ERRMEM; |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | rpc->type = NC_RPC_LOCK; |
| 106 | rpc->target = target; |
| 107 | |
| 108 | return (struct nc_rpc *)rpc; |
| 109 | } |
| 110 | |
| 111 | API struct nc_rpc * |
| 112 | nc_rpc_unlock(NC_DATASTORE target) |
| 113 | { |
| 114 | struct nc_rpc_lock *rpc; |
| 115 | |
| 116 | rpc = malloc(sizeof *rpc); |
| 117 | if (!rpc) { |
| 118 | ERRMEM; |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | rpc->type = NC_RPC_UNLOCK; |
| 123 | rpc->target = target; |
| 124 | |
| 125 | return (struct nc_rpc *)rpc; |
| 126 | } |
| 127 | |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 128 | API void |
| 129 | nc_rpc_free(struct nc_rpc *rpc) |
| 130 | { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame^] | 131 | struct nc_rpc_server *rpc_server; |
| 132 | struct nc_rpc_getconfig *rpc_getconfig; |
| 133 | |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 134 | if (!rpc) { |
| 135 | return; |
| 136 | } |
| 137 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame^] | 138 | switch(rpc->type) { |
| 139 | case NC_RPC_SERVER: |
| 140 | rpc_server = (struct nc_rpc_server *)rpc; |
| 141 | lyxml_free_elem(rpc_server->ctx, rpc_server->root); |
| 142 | lyd_free(rpc_server->tree); |
| 143 | break; |
| 144 | case NC_RPC_GETCONFIG: |
| 145 | rpc_getconfig = (struct nc_rpc_getconfig *)rpc; |
| 146 | nc_filter_free(rpc_getconfig->filter); |
| 147 | break; |
| 148 | case NC_RPC_LOCK: |
| 149 | case NC_RPC_UNLOCK: |
| 150 | /* nothing special needed */ |
| 151 | break; |
| 152 | } |
| 153 | |
Radek Krejci | a53b3fe | 2015-10-19 17:25:04 +0200 | [diff] [blame] | 154 | free(rpc); |
| 155 | } |
| 156 | |
| 157 | API void |
| 158 | nc_reply_free(struct nc_reply *reply) |
| 159 | { |
| 160 | if (!reply) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | lyxml_free_elem(reply->ctx, reply->root); |
| 165 | lyd_free(reply->tree); |
| 166 | free(reply); |
| 167 | } |
| 168 | API void |
| 169 | nc_notif_free(struct nc_notif *notif) |
| 170 | { |
| 171 | if (!notif) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | lyxml_free_elem(notif->ctx, notif->root); |
| 176 | lyd_free(notif->tree); |
| 177 | free(notif); |
| 178 | } |