Building.Open.Source.Network.Security.Tools.Components.And.Techniques [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Building.Open.Source.Network.Security.Tools.Components.And.Techniques [Electronic resources] - نسخه متنی

Mike D. Schiffman

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید





C Programming Concepts

This book involves a great deal of C code. The following programming concepts are helpful in order to get the most from future chapters.


Programming Libraries


A programming library is a cohesive collection of programming primitives usually grouped together under a common purpose (libpcap is a programming library devoted to packet capture; libnet is a library devoted to packet construction and injection). Generally speaking, libraries consist of two constituent parts: the binary archive where the code implementation is kept and the header files where the interface to the library is specified, along with symbolic constants and macros. Effective documentation, while always helpful, is unfortunately not always part of the package. Well-written library code offers the following benefits to the end user:

Code reuse. Libraries offer the application programmer convenient access to commonly used routines. Rather than having to write a routine to output text to the screen, the programmer can include the Standard C library (libc) and make a function call to printf(). All of the code for printf() is kept inside libc, and the application programmer never has to bother with anything other than learning its interface.

Task focus. A programming library enables the programmer to concentrate on the problem at hand and obviate the tedium of having to write ancillary support code.

Portability. Programming libraries often increase a program's portability to different platforms and operating systems. If all of the architecture-specific code is moved into a library that is portable, making the rest of the program portable is often simple.

Code readability. An often-overlooked benefit of employing programming libraries is the fact that they increase a program's readability and make it easier to follow its logic flow. The more that the workhorse code is moved out of band into libraries, the more the actual code resembles high-level pseudo-code, which is much easier to read, understand, and therefore debug.


Callback Functions


The C programming language natively defines several fundamental datatypes for use as variables (int, char, and short). While a function is not a variable, a pointer to a function is and can be manipulated as a normal C variable (arithmetically, in arrays). Function pointers provide the basis for callback functions. A callback function is simply a function pointer passed as an argument to a function.


Function Reentrace and Thread-Safety


Reentrance or "purity" is the quality of a C function that enables it to be interrupted and subsequently be reinstantiated, either by itself or as an external function, and have it still complete its task as designed without side effects. In other words, the function must be able to handle multiple instantiations of itself with completely predictable results. A reentrant function is also known as "asynchronous signal safe," because an interrupt handler function can safely interrupt it, perform some task (which might include calling the function), and then retur to the original execution context. In order to be reentrant, a function needs to obey three simple rules:

It cannot modify itself. In other words, the function cannot change what it does or how it does it. Its code is set in stone.

It cannot maintain state through multiple instantiations. Each execution of the function must have its own copy of whatever local variables it defines. A reentrant function cannot make use of any static data, modify any global data, or return a pointer to static data.

It cannot invoke a non-reentrant function. Within the context of this book, it is assumed (unless otherwise stated) that all ancillary libc functions are reentrant.

Thread safety is a similar concept to reentrance, but it concerns multithreaded programs. A thread-safe function can be instantiated by several threads executing simultaneously with predictable results. To achieve thread safety, a function must use synchronization primitives to mediate access to shared data (and as such, it might or might not be reentrant). Like reentrant functions, a thread-safe function might not modify any global data or employ static data.


Assertions


Assertions, executed via the assert() macro, are used to test an expression for truth (or validity), and if the expression is false, the calling process will terminate (via a call to abort ()) and a debug line is written to standard error. Most often, you see assertions at the beginning of a function testing to confirm whether or not a given pointer is NULL when if it was, it would cause the program to behave erratically or generate a segmentation fault. The assert() macro is a cheap debugging tool that, if there is a problem, lets the application programmer know where it is with a minimum of hassle.

/ 135