the problem with the code
Ted Lemon
Ted.Lemon at nominum.com
Wed Aug 6 18:52:59 UTC 2008
On Aug 6, 2008, at 7:33 AM, DÔ Phan-Cam-Thach wrote:
> I have a problem while reading the code. I dont understand much of
> struct universe which is copied below. I find that there are the
> functions in this structure, ex:
> struct option_cache *(*lookup_func) (struct universe *,
> struct option_state *,
> unsigned);
> I dont understand this model much (there are functions in a
> structure). And i dont understand why dont they put these functions
> out of the structure instead of in the structure? Besides, i dont
> understand what this function is used for?
Putting functions in a structure is a way of doing object-oriented
programming in C. The idea is that you may have several different
data objects that behave similarly, but require different underlying
code to implement that behavior. So you put the data for these
objects in a common structure, and you include in that structure
pointers to the code that implements the object's behavior.
Then a function that uses that object's data can call the
implementation functions for the object from the structure, rather
than having to figure out what kind of object it is, and then directly
call that object's implementation functions. It's a lot cleaner.
"Universe" is my navel-gazing name for a DHCP option space. So for
example, there's a DHCP option space for all the DHCP options. And
there's another one for all the Netware/IP encapsulated options. And
the FQDN option has its own option space. This allows you to extract
parts of the FQDN option when you're writing a configuration file.
If you grep in options.c for the names of the functions that are in
the universe data structure, you can see how they're used. They're
all initialized in places like common/tables.c and server/stable.c (if
I remember correctly - it's been a while since I wrote this code).
More information about the dhcp-users
mailing list