Chapter 5 Trouble Shooting These plug-in programming tips will help you avoid problems with your Protocol Level Plug-ins.
These plug-in programming tips will help you avoid problems with your Protocol Level Plug-ins.
Planning Memory Use
Handling External Dependencies
Processing DATA Lines
Allocate new memory safely. Use PPRealloc(p,0) instead of free(p).
Set pointer to null. Make sure that your pointers are set to null before calling realloc; this is the safest way to allocate new memory. You cannot pass valid pointers to realloc and expect it to work like malloc. If you have any other pointers pointing to the same data, they will be invalid after the call.
Avoid memory leaks. Be careful to free all the memory you use on a per plug-in basis. Smtpd is a long-lived process. Leaking only a few bytes per connection (per plug-in) can result in a large memory loss.
Use plug-in memory allocation routines. You cannot use system routines that allocate their own memory, for example, strdup. You must write your own routine that uses the plug-in memory allocation routines. For example, the Anti-Relay plug-in uses a routine called ARStrdup.
Use DEFER. If you are processing DATA lines, be sure to use DEFER if you are doing something passive, such as counting bytes.
Terminate correctly on failure. If you are processing DATA lines and want to return a failure, for example, if a message is too large, set the response line and return PP_CLOSE. Do not return PP_DONE; the session will not terminate correctly.