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