00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <netlink-local.h>
00019 #include <netlink/netlink.h>
00020 #include <netlink/cache.h>
00021 #include <netlink/utils.h>
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 char *nl_cache_ops_get_name(struct nl_cache_ops *ops)
00033 {
00034 return ops->co_name;
00035 }
00036
00037
00038
00039
00040
00041
00042
00043
00044 static struct nl_cache_ops *cache_ops;
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 struct nl_cache_ops *nl_cache_mngt_associate(int protocol, int message_type)
00058 {
00059 int i;
00060 struct nl_cache_ops *ops;
00061
00062 for (ops = cache_ops; ops; ops = ops->co_next)
00063 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
00064 if (ops->co_msgtypes[i].mt_id == message_type &&
00065 ops->co_protocol == protocol)
00066 return ops;
00067
00068 return NULL;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 char *nl_cache_mngt_type2name(struct nl_cache_ops *ops, int msgtype,
00085 char *buf, size_t len)
00086 {
00087 int i;
00088
00089 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
00090 if (ops->co_msgtypes[i].mt_id == msgtype) {
00091 snprintf(buf, len, "%s::%s",
00092 ops->co_name,
00093 ops->co_msgtypes[i].mt_name);
00094 return buf;
00095 }
00096 }
00097
00098 snprintf(buf, len, "%s->0x%x()", ops->co_name, msgtype);
00099 return buf;
00100 }
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 struct nl_cache_ops *nl_cache_mngt_lookup(const char *name)
00117 {
00118 struct nl_cache_ops *ops;
00119
00120 for (ops = cache_ops; ops; ops = ops->co_next)
00121 if (!strcmp(ops->co_name, name))
00122 return ops;
00123
00124 return NULL;
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 int nl_cache_mngt_register(struct nl_cache_ops *ops)
00137 {
00138 if (!ops->co_name)
00139 return nl_error(EINVAL, "No cache name specified");
00140
00141 if (nl_cache_mngt_lookup(ops->co_name))
00142 return nl_error(EEXIST, "Cache operations already exist");
00143
00144 ops->co_next = cache_ops;
00145 cache_ops = ops;
00146
00147 NL_DBG(1, "Registered cache operations %s\n", ops->co_name);
00148
00149 return 0;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
00164 {
00165 struct nl_cache_ops *t, **tp;
00166
00167 for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
00168 if (t == ops)
00169 break;
00170
00171 if (!t)
00172 return nl_error(ENOENT, "No such cache operations");
00173
00174 NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);
00175
00176 *tp = t->co_next;
00177 return 0;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 void nl_cache_mngt_provide(struct nl_cache *cache)
00196 {
00197 struct nl_cache_ops *ops;
00198
00199 ops = nl_cache_mngt_lookup(cache->c_ops->co_name);
00200 if (!ops)
00201 BUG();
00202 else
00203 ops->co_major_cache = cache;
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 void nl_cache_mngt_unprovide(struct nl_cache *cache)
00215 {
00216 struct nl_cache_ops *ops;
00217
00218 ops = nl_cache_mngt_lookup(cache->c_ops->co_name);
00219 if (!ops)
00220 BUG();
00221 else if (ops->co_major_cache == cache)
00222 ops->co_major_cache = NULL;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 struct nl_cache *nl_cache_mngt_require(const char *name)
00236 {
00237 struct nl_cache_ops *ops;
00238
00239 ops = nl_cache_mngt_lookup(name);
00240 if (!ops || !ops->co_major_cache) {
00241 fprintf(stderr, "Application BUG: Your application must "
00242 "call nl_cache_mngt_provide() and\nprovide a valid "
00243 "%s cache to be used for internal lookups.\nSee the "
00244 " API documentation for more details.\n", name);
00245
00246 return NULL;
00247 }
00248
00249 return ops->co_major_cache;
00250 }
00251
00252
00253
00254