Hi,
Is there any way, by which we can limit a specific function to be
called
only from a specific function ? I dont know the advantage of this.
Someone
asked this question from me in an interview.
thanks for any help ...
Realistically? Not properly in stdc. The following should be
'good enough' for most practical porpoises.
------------protected.h---------------
#define function(x,y) protected_function (__func__, x, y)
------------protected.c---------------
void protected_function (char *caller, int x, int y)
{
char *legal_callers[] = {
"foo",
"bar",
"baz",
};
int allowed = 0;
size_t i;
for (i=0; i<sizeof legal_callers/sizeof legal_callers[0]; i++) {
if (strcmp (caller, legal_callers)==0) {
allowed = 1;
break;
}
}
if (!allowed) {
return;
}
/* Rest of function goes here */
/* Do something with x and y */
}
<OT>
If it is proper security you are looking for, you are out of
luck; there isn't a single programming language that I know
of that has builtin authentication (or identification) for
functions (and objects) and authorisation (or access controls)
for functions (and objects).
There are some experiments with *code-signing* on newer platforms,
but these apply to the entire platform (.Net or, IIRC, Java?) and
probably do not go down to the granularity you want.
The closest you will get is using gcc[1] and the extensions it
provides (builtin_apply_args, builtin_apply and builtin_return[2])
wrapped in a function that does authentication and/or authorisation.
[1] Or any other compiler with the appropriate extensions.
[2] Or the extensions provided that allow this type of thing.
</OT>
The other option is to have only two functions in a
source file. One is the actual function to call and is
static and the other function is merely a wrapper to
authenticate/authorise the credentials supplied and
invoke the static function (or return a pointer to it).
goose,