Monthly Archives: June 2014

ICMP Echo

opensource.apple.com: ping.c

/*
 * pinger --
 *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
 * will be added on by the kernel.  The ID field is our UNIX process ID,
 * and the sequence number is an ascending integer.  The first TIMEVAL_LEN
 * bytes of the data portion are used to hold a UNIX "timeval" struct in
 * host byte-order, to compute the round-trip time.
 */
static void
pinger(void)
{
    [...]
    if ((options & F_TIME) || timing) {
        (void)gettimeofday(&now, NULL);

        if (options & F_TIME)
            icp->icmp_otime = htonl((now.tv_sec % (24*60*60)) * 1000 + now.tv_usec / 1000);
        if (timing)
            bcopy((void *)&now, (void *)&outpack[ICMP_MINLEN + phdr_len], sizeof(struct timeval));
    }
    [...]
}
typedef u_int32_t n_time;       /* ms since 00:00 GMT, byte rev */
#define icmp_otime      icmp_dun.id_ts.its_otime
#define icmp_rtime      icmp_dun.id_ts.its_rtime
#define icmp_ttime      icmp_dun.id_ts.its_ttime

struct icmp {
        u_char  icmp_type;              /* type of message, see below */
        u_char  icmp_code;              /* type sub code */
        u_short icmp_cksum;             /* ones complement cksum of struct */
        union {
                u_char ih_pptr;                 /* ICMP_PARAMPROB */
                struct in_addr ih_gwaddr;       /* ICMP_REDIRECT */
                struct ih_idseq {
                        n_short icd_id;
                        n_short icd_seq;
                } ih_idseq;
                int ih_void;

                /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
                struct ih_pmtu {
                        n_short ipm_void;
                        n_short ipm_nextmtu;
                } ih_pmtu;

                struct ih_rtradv {
                        u_char irt_num_addrs;
                        u_char irt_wpa;
                        u_int16_t irt_lifetime;
                } ih_rtradv;
        } icmp_hun;
        union {
                struct id_ts {                  /* ICMP Timestamp */
                        n_time its_otime;       /* Originate */
                        n_time its_rtime;       /* Receive */
                        n_time its_ttime;       /* Transmit */
                } id_ts;
                struct id_ip  {
                        struct ip idi_ip;
                        /* options and then 64 bits of data */
                } id_ip;
                struct icmp_ra_addr id_radv;
                u_int32_t id_mask;
                char    id_data[1];
        } icmp_dun;
};

Wireshark-bugs: For ICMP Time Response, In detail pane, Timestamp is incorrectly decoded for MS Windows

Reference (1):
http://tools.ietf.org/html/rfc778

“The timestamp values are in milliseconds from midnight
UT and are stored right-justified in the 32-bit fields shown
above. Ordinarily, all time calculations are performed
modulo-24 hours in milliseconds.”


/* Converts a little-endian byte order unsigned long to host byte order. */
uint32 LETOHL(uint32 ul);

/*
 * RFC 792 for basic ICMP.
 * RFC 1191 for ICMP_FRAG_NEEDED (with MTU of next hop).
 * RFC 1256 for router discovery messages.
 * RFC 2002 and 3012 for Mobile IP stuff.
 */
static void
dissect_icmp(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
{
    [...]

    /* Decode the second 4 bytes of the packet. */
    switch (icmp_type) {
    
    [...]
    
    case ICMP_ECHOREPLY:
    case ICMP_ECHO:
        
        [...]
        
        /* Interpret the first 8 bytes of the icmp data as a timestamp
         * But only if it does look like it's a timestamp.
         *
         * FIXME:
         *    Timestamps could be in different formats depending on the OS
         */
        ts.secs  = tvb_get_ntohl(tvb, 8);
        ts.nsecs = tvb_get_ntohl(tvb, 8 + 4);   /* Leave at microsec resolution for now */
        
        if (abs((guint32) (ts.secs - pinfo->fd->abs_ts.secs)) >=
            3600 * 24 || ts.nsecs >= 1000000) {
            /* Timestamp does not look right in BE, try LE representation */
            ts.secs  = tvb_get_letohl(tvb, 8);
            ts.nsecs = tvb_get_letohl(tvb, 8 + 4);  /* Leave at microsec resolution for now */
        }
        if (abs((guint32) (ts.secs - pinfo->fd->abs_ts.secs)) < 3600 * 24 && ts.nsecs < 1000000) {
            ts.nsecs *= 1000;   /* Convert to nanosec resolution */
            proto_tree_add_time(icmp_tree, hf_icmp_data_time,
                                tvb, 8, 8, &ts);
            nstime_delta(&time_relative, &pinfo->fd->abs_ts,
                         &ts);
            ti = proto_tree_add_time(icmp_tree,
                                     hf_icmp_data_time_relative,
                                     tvb, 8, 8,
                                     &time_relative);
            PROTO_ITEM_SET_GENERATED(ti);
            call_dissector(data_handle,
                       tvb_new_subset_remaining(tvb,
                                8 + 8),
                       pinfo, icmp_tree);
        } else {
            call_dissector(data_handle,
                       tvb_new_subset_remaining(tvb, 8),
                       pinfo, icmp_tree);
        }
        break;
        
        [...]
    }
    
    [...]
}

Packet Filter (PF) + ALTQ

Presentation

PF, The OpenBSD Packet Filter: Building The Network You Need, EuroBSDCon 2015, Stockholm, Sweden, October 1st 2015

Tutorials & HowTos

Pf Firewall “how to” – FreeBSD and OpenBSD ( pf.conf )
Paket Filter (PF) von OpenBSD und ALTQ
Getting AltQ working in pf.conf (limiting inbound Tor traffic)
PF Firewall Quick Guide
FreeBSD Tuning and Optimization – performance modifications for 1gig and 10gig networks

Statistics

pfstat
ALTQ statistics?

ntop (Official)
ntopng – High-Speed Web-based Traffic Analysis and Flow Collection (Official)
ntop (Wikipedia)
NTop
Network Monitoring Using Free Linux Tools
Unveiling Application Visibility in ntop and nProbe (both in NetFlow v9 and IPFIX)

FAQ

Table not found

Couldn't manipulate device /dev/pf: No such process
table <hacker> persist {
}

$ pfctl -n -f /etc/pf.conf       # Parse the configuration file, do not actually load rules
$ pfctl -T load -f /etc/pf.conf  # Load only the table definitions
$ pfctl -t hacker -T show        # Show the content of a table

RRDTool

RRDTool – tutorial and graph examples (OpenBSD with pf)
rrd-beginners

Network usage

Network Traffic Monitoring with RRDTool
Monitoring network traffic with iptraf and rrdtool
Monitorix Project by Jordi Sanfeliu
RRDtool with pfctl and spamd
Setting up traffic monitoring using rrdtool (and snmp)
Using SNMP and RRD to monitor your LEAF system

Alternatives

Alternatives to rrdtool?
Java RRD library
rrd4j, RRD4J 3.1 (released 2017-01-01)

BIND9 Reject DNS Root Queries

bind: blackhole for invalid recursive queries?
Disabling Root DNS Server queries on Redhat linux
Ubuntu server 12.04 bind9 dns query rejected

Using FreeBSD’s BPF device with C/C++

Socket Compiler Error

Compile Error in using /usr/include/net/if.h
compile problems on freebsd

SVNWEB

sys/pf
sbin/pfctl

[root@gateway ~]# pfctl -t hacker -T add 192.168.0.2 192.168.0.3 192.168.0.4
1 table created.
3/3 addresses added.

[root@gateway ~]# pfctl -f /etc/pf.conf

[root@gateway ~]# pfctl -t hacker -T show
   192.168.1.1

[root@gateway ~]# pfctl -t hacker -T add 192.168.0.2 192.168.0.3 192.168.0.4
3/3 addresses added.

[root@gateway ~]# pfctl -t hacker -T show
   192.168.0.2
   192.168.0.3
   192.168.0.4
   192.168.1.1
     DIOCRADDADDRS struct pfioc_table *io
	     Add one or	more addresses to a table.  On entry, pfrio_table con-
	     tains the table ID	and pfrio_buffer must point to an array	of
	     struct pfr_addr containing	at least pfrio_size elements to	add to
	     the table.	 pfrio_esize must be the size of struct	pfr_addr.  On
	     exit, pfrio_nadd contains the number of addresses effectively
	     added.

	     struct pfr_addr {
		     union {
			     struct in_addr   _pfra_ip4addr;
			     struct in6_addr  _pfra_ip6addr;
		     }		      pfra_u;
		     u_int8_t	      pfra_af;
		     u_int8_t	      pfra_net;
		     u_int8_t	      pfra_not;
		     u_int8_t	      pfra_fback;
	     };
	     #define pfra_ip4addr    pfra_u._pfra_ip4addr
	     #define pfra_ip6addr    pfra_u._pfra_ip6addr
/usr/include/sys/ioctl.h: ioctl                (dev, DIOCRADDADDRS, &io)
sbin/pfctl/pfctl_radix.c: pfr_add_addrs        (tbl=0xbfbfd198, addr=0x28826100, size=3, nadd=0xbfbfd16c, flags=0)
sbin/pfctl/pfctl_table.c: pfctl_table          (argc=3, argv=0xbfbfdc90, tname=0xbfbfddd3 "hacker", command=0x808831c "add", file=0x0, anchor=0xbfbfd808 "", opts=0)
sbin/pfctl/pfctl_table.c: pfctl_command_tables (argc=3, argv=0xbfbfdc90, tname=0xbfbfddd3 "hacker", command=0x808831c "add", file=0x0, anchor=0xbfbfd808 "", opts=0)
sbin/pfctl/pfctl.c:       main                 (argc=3, argv=0xbfbfdc90)


tbl:
$1 = (struct pfr_table *) 0xbfbfd198
$2 = { pfrt_anchor = '\0' 
       pfrt_name   = "hacker", 
       pfrt_flags  = 0,
       pfrt_fback  = 0 '\0'}

addr:
$3 = (struct pfr_addr *) 0x28826100
$4 = { pfra_u = { _pfra_ip4addr = { s_addr = 33597632 },
                  _pfra_ip6addr = { [...] }
                },
       pfra_af = 2 '\002',
       pfra_net = 32 ' ', 
       pfra_not = 0 '\0',
       pfra_fback = 0 '\0'}

pfctl_radix.c:418    pfr_buf_add          (b=0xbfbfd188, e=0xbfbfcfb0)
pfctl_parser.c:1704  append_addr_host     (b=0xbfbfd188, n=0x28814460, test=0, not=0)
pfctl_parser.c:1659  append_addr          (b=0xbfbfd188, s=0xbfbfddf9 "192.168.0.4", test=0)
pfctl_table.c:418    load_addr            (b=0xbfbfd188, argc=0, argv=0xbfbfdc9c, file=0x0, nonetwork=0)
pfctl_table.c:201    pfctl_table          (argc=3, argv=0xbfbfdc90, tname=0xbfbfddd3 "hacker", command=0x808831c "add", file=0x0, anchor=0xbfbfd808 "", opts=0)
pfctl_table.c:124    pfctl_command_tables (argc=3, argv=0xbfbfdc90, tname=0xbfbfddd3 "hacker", command=0x808831c "add", file=0x0, anchor=0xbfbfd808 "", opts=0)
pfctl.c:2328         main                 (argc=3, argv=0xbfbfdc90)
typedef char *          caddr_t;        /* core address */
typedef const char *    c_caddr_t;      /* core address, pointer to const */
/*
 * Internet address (a structure for historical reasons)
 */
struct in_addr {
	in_addr_t s_addr;
};

Essential Socket Functions

int
main(int argc, char *argv[])
{
    [...]
    while ((ch = getopt(argc, argv,
        "a:AdD:eqf:F:ghi:k:K:mnNOo::Pp:rRs:t:T:vx:z")) != -1) {
        switch (ch) {
        [...]
        case 't':
            tableopt = optarg;
            break;
        case 'T':
            tblcmdopt = pfctl_lookup_option(optarg, tblcmdopt_list);
            if (tblcmdopt == NULL) {
                warnx("Unknown table command '%s'", optarg);
                usage();
            }
            break;
        [...]
        }
    }
    [...]
    if (tblcmdopt != NULL) {
        error = pfctl_command_tables(argc, argv, tableopt,
            tblcmdopt, rulesopt, anchorname, opts);
        rulesopt = NULL;
    }
    [...]
}
enum {
    PFRB_TABLES = 1,
    PFRB_TSTATS,
    PFRB_ADDRS,
    PFRB_ASTATS,
    PFRB_IFACES,
    PFRB_TRANS,
    PFRB_MAX
};

struct pfr_buffer {
    int                 pfrb_type;      /* type of content, see enum above */
    int                 pfrb_size;      /* number of objects in buffer */
    int                 pfrb_msize;     /* maximum number of objects in buffer */
    void               *pfrb_caddr;     /* malloc'ated memory area */
};

/*  int            int            int              void *                */
   {pfrb_type = 3, pfrb_size = 0, pfrb_msize = 0,  pfrb_caddr = 0x0}
   {pfrb_type = 3, pfrb_size = 1, pfrb_msize = 64, pfrb_caddr = 0x28826100}
   {pfrb_type = 3, pfrb_size = 2, pfrb_msize = 64, pfrb_caddr = 0x28826100}


struct pfioc_table {
    struct pfr_table    pfrio_table;
    void               *pfrio_buffer;
    int                 pfrio_esize;
    int                 pfrio_size;
    int                 pfrio_size2;
    int                 pfrio_nadd;
    int                 pfrio_ndel;
    int                 pfrio_nchange;
    int                 pfrio_flags;
    u_int32_t           pfrio_ticket;
};

struct pfr_table {
    char                pfrt_anchor[MAXPATHLEN];
    char                pfrt_name[PF_TABLE_NAME_SIZE];
    u_int32_t           pfrt_flags;
    u_int8_t            pfrt_fback;
};

/*  char *              char *                       u_int32_t       u_int8_t          */
   {pfrt_anchor = '\0', pfrt_name = "hacker", '\0' , pfrt_flags = 0, pfrt_fback = 0 '\0'}

pfrt_flags:
#define PFR_TFLAG_PERSIST       0x00000001
#define PFR_TFLAG_CONST         0x00000002
#define PFR_TFLAG_ACTIVE        0x00000004
#define PFR_TFLAG_INACTIVE      0x00000008
#define PFR_TFLAG_REFERENCED    0x00000010
#define PFR_TFLAG_REFDANCHOR    0x00000020
#define PFR_TFLAG_USRMASK       0x00000003
#define PFR_TFLAG_SETMASK       0x0000003C
#define PFR_TFLAG_ALLMASK       0x0000003F

struct pfr_table.pfrt_fback:
struct pfr_addr.pfra_fback:
enum {
    PFR_FB_NONE,
    PFR_FB_MATCH,
    PFR_FB_ADDED,
    PFR_FB_DELETED,
    PFR_FB_CHANGED,
    PFR_FB_CLEARED,
    PFR_FB_DUPLICATE,
    PFR_FB_NOTMATCH,
    PFR_FB_CONFLICT,
    PFR_FB_MAX
};

struct pfr_addr {
    union {
        struct in_addr   _pfra_ip4addr;
        struct in6_addr  _pfra_ip6addr;
    }                pfra_u;
    u_int8_t         pfra_af;        /**< AF_INET or AF_INET6 */
    u_int8_t         pfra_net;
    u_int8_t         pfra_not;
    u_int8_t         pfra_fback;
};
#define pfra_ip4addr    pfra_u._pfra_ip4addr
#define pfra_ip6addr    pfra_u._pfra_ip6addr

/* union                                                           */
   { pfra_u = { _pfra_ip4addr = { s_addr = 33597632 },
                _pfra_ip6addr = {__u6_addr = { __u6_addr8  = { ... },
                                               __u6_addr16 = { ... },
                                               __u6_addr32 = { ... }
                                             }
                                }
              },
/*   u_int8_t      u_int8_t        u_int8_t       u_int8_t         */
     pfra_af = 2 , pfra_net = 32 , pfra_not = 0 , pfra_fback = 0   }

/***************************************************************/

#define v4      pfa.v4
#define v6      pfa.v6
#define addr8   pfa.addr8
#define addr16  pfa.addr16
#define addr32  pfa.addr32

struct pf_addr {
    union {
        struct in_addr          v4;
        struct in6_addr         v6;
        u_int8_t                addr8[16];
        u_int16_t               addr16[8];
        u_int32_t               addr32[4];
    }                           pfa;           /* 128-bit address */
};

struct pf_addr_wrap {
    union {
        struct {
            struct pf_addr      addr;
            struct pf_addr      mask;
        }                       a;
        char                    ifname[IFNAMSIZ];
        char                    tblname[PF_TABLE_NAME_SIZE];
    }                           v;
    union {
        struct pfi_dynaddr     *dyn;
        struct pfr_ktable      *tbl;
        int                     dyncnt;
        int                     tblcnt;
    }                           p;
    u_int8_t                    type;          /* PF_ADDR_* */
    u_int8_t                    iflags;        /* PFI_AFLAG_* */
};

#define CREATE_TABLE do {                                   \
    table.pfrt_flags |= PFR_TFLAG_PERSIST;                  \
    if ((!(opts & PF_OPT_NOACTION) ||                       \
        (opts & PF_OPT_DUMMYACTION)) &&                     \
        (pfr_add_tables(&table, 1, &nadd, flags)) &&        \
        (errno != EPERM)) {                                 \
            radix_perror();                                 \
            goto _error;                                    \
    }                                                       \
    if (nadd) {                                             \
        warn_namespace_collision(table.pfrt_name);          \
        xprintf(opts, "%d table created", nadd);            \
        if (opts & PF_OPT_NOACTION)                         \
            return (0);                                     \
    }                                                       \
    table.pfrt_flags &= ~PFR_TFLAG_PERSIST;                 \
} while(0)                           

int
pfctl_command_tables(int argc, char *argv[], char *tname,
    const char *command, char *file, const char *anchor, int opts)
{
	if (tname == NULL || command == NULL)
		usage();
	return pfctl_table(argc, argv, tname, command, file, anchor, opts);
}

int
pfctl_table(int argc, char *argv[], char *tname, const char *command,
    char *file, const char *anchor, int opts)
{
    struct pfr_table    table;
    struct pfr_buffer   b, b2;
    struct pfr_addr    *a, *a2;
    int                 nadd = 0;

    [...]

    strlcpy(table.pfrt_name, tname, sizeof(table.pfrt_name);      /**< copy table name */

    [...]
    } else if (!strcmp(command, "add")) {
        b.pfrb_type = PFRB_ADDRS;                                 /**< set type to ADDR */
        if (load_addr(&b, argc, argv, file, 0))                   /**< load_addr(): parse arguments and pass it to struct pfr_buffer */
            goto _error;
        CREATE_TABLE;
        if (opts & PF_OPT_VERBOSE)
            flags |= PFR_FLAG_FEEDBACK;
        RVTEST(pfr_add_addrs(&table, b.pfrb_caddr, b.pfrb_size, &nadd, flags));    /**< pfr_add_addrs(): 
        xprintf(opts, "%d/%d addresses added", nadd, b.pfrb_size);
        if (opts & PF_OPT_VERBOSE)
            PFRB_FOREACH(a, &b)
                if ((opts & PF_OPT_VERBOSE2) || a->pfra_fback)
                    print_addrx(a, NULL, opts & PF_OPT_USEDNS);
    }
    [...]
}

int
load_addr(struct pfr_buffer *b, int argc, char *argv[], char *file,
    int nonetwork)
{
    while (argc--)
        if (append_addr(b, *argv++, nonetwork)) {
            if (errno)
                warn("cannot decode %s", argv[-1]);
            return (-1);
        }
    if (pfr_buf_load(b, file, nonetwork, append_addr)) {
        warn("cannot load %s", file);
        return (-1);
    }
    return (0);
}
struct node_host {
    struct pf_addr_wrap  addr;
    struct pf_addr       bcast;
    struct pf_addr       peer;
    sa_family_t          af;
    u_int8_t             not;
    u_int32_t            ifindex;   /* link-local IPv6 addrs */
    char                *ifname;
    u_int                ifa_flags;
    struct node_host    *next;
    struct node_host    *tail;
};
/*
 * convert a hostname to a list of addresses and put them in the given buffer.
 * test:
 *  if set to 1, only simple addresses are accepted (no netblock, no "!").
 */
int
append_addr(struct pfr_buffer *b, char *s, int test)
{
    char             *r;
    struct node_host    *h, *n;
    int          rv, not = 0;

    for (r = s; *r == '!'; r++)
        not = !not;
    if ((n = host(r)) == NULL) {
        errno = 0;
        return (-1);
    }
    rv = append_addr_host(b, n, test, not);
    do {
        h = n;
        n = n->next;
        free(h);
    } while (n != NULL);
    return (rv);
}

/*
 * same as previous function, but with a pre-parsed input and the ability
 * to "negate" the result. Does not free the node_host list.
 * not:
 *      setting it to 1 is equivalent to adding "!" in front of parameter s.
 */
int
append_addr_host(struct pfr_buffer *b, struct node_host *n, int test, int not)
{
    int          bits;
    struct pfr_addr      addr;

    do {
        bzero(&addr, sizeof(addr));
        addr.pfra_not = n->not ^ not;
        addr.pfra_af = n->af;
        addr.pfra_net = unmask(&n->addr.v.a.mask, n->af);            /**< assign netmask, node_host -> pfr_addr */
        switch (n->af) {
        case AF_INET:
            addr.pfra_ip4addr.s_addr = n->addr.v.a.addr.addr32[0];   /**< assign address, node_host -> pfr_addr */
            bits = 32;
            break;
        case AF_INET6:
            memcpy(&addr.pfra_ip6addr, &n->addr.v.a.addr.v6,
                sizeof(struct in6_addr));
            bits = 128;
            break;
        default:
            errno = EINVAL;
            return (-1);
        }
        if ((test && (not || addr.pfra_net != bits)) ||              /**< test = 0, not = 0 => bypass these lines */
            addr.pfra_net > bits) {
            errno = EINVAL;
            return (-1);
        }
        if (pfr_buf_add(b, &addr))
            return (-1);
    } while ((n = n->next) != NULL);

    return (0);
}

struct node_host *
host(const char *s)
{
    struct node_host    *h = NULL;
    int          mask, v4mask, v6mask, cont = 1;
    char            *p, *q, *ps;

    if ((p = strrchr(s, '/')) != NULL) {
        mask = strtol(p+1, &q, 0);
        if (!q || *q || mask > 128 || q == (p+1)) {
            fprintf(stderr, "invalid netmask '%s'\n", p);
            return (NULL);
        }
        if ((ps = malloc(strlen(s) - strlen(p) + 1)) == NULL)
            err(1, "host: malloc");
        strlcpy(ps, s, strlen(s) - strlen(p) + 1);
        v4mask = v6mask = mask;
    } else {
        if ((ps = strdup(s)) == NULL)
            err(1, "host: strdup");
        v4mask = 32;
        v6mask = 128;
        mask = -1;
    }

    /* interface with this name exists? */
    if (cont && (h = host_if(ps, mask)) != NULL)
        cont = 0;

    /* IPv4 address? */
    if (cont && (h = host_v4(s, mask)) != NULL)
        cont = 0;

    /* IPv6 address? */
    if (cont && (h = host_v6(ps, v6mask)) != NULL)
        cont = 0;

    /* dns lookup */
    if (cont && (h = host_dns(ps, v4mask, v6mask)) != NULL)
        cont = 0;
    free(ps);

    if (h == NULL || cont == 1) {
        fprintf(stderr, "no IP address found for %s\n", s);
        return (NULL);
    }
    return (h);
}

struct node_host *
host_v4(const char *s, int mask)
{
    struct node_host    *h = NULL;
    struct in_addr       ina;
    int          bits = 32;

    memset(&ina, 0, sizeof(struct in_addr));
    if (strrchr(s, '/') != NULL) {
        if ((bits = inet_net_pton(AF_INET, s, &ina, sizeof(ina))) == -1)     /**< parse string, return netmask bits */
            return (NULL);
    } else {
        if (inet_pton(AF_INET, s, &ina) != 1)
            return (NULL);
    }

    h = calloc(1, sizeof(struct node_host));
    if (h == NULL)
        err(1, "address: calloc");
    h->ifname = NULL;
    h->af = AF_INET;
    h->addr.v.a.addr.addr32[0] = ina.s_addr;
    set_ipmask(h, bits);                                                     /**< set IP mask */
    h->next = NULL;
    h->tail = h;

    return (h);
}

void
set_ipmask(struct node_host *h, u_int8_t b)
{
    struct pf_addr  *m, *n;
    int      i, j = 0;

    m = &h->addr.v.a.mask;
    memset(m, 0, sizeof(*m));

    while (b >= 32) {
        m->addr32[j++] = 0xffffffff;
        b -= 32;
    }
    for (i = 31; i > 31-b; --i)
        m->addr32[j] |= (1 << i);
    if (b)
        m->addr32[j] = htonl(m->addr32[j]);

    /* Mask off bits of the address that will never be used. */
    n = &h->addr.v.a.addr;
    if (h->addr.type == PF_ADDR_ADDRMASK)
        for (i = 0; i < 4; i++)
            n->addr32[i] = n->addr32[i] & m->addr32[i];
}
/* buffer management code */

size_t buf_esize[PFRB_MAX] = { 0,
    sizeof(struct pfr_table), sizeof(struct pfr_tstats),
    sizeof(struct pfr_addr), sizeof(struct pfr_astats),
    sizeof(struct pfi_kif), sizeof(struct pfioc_trans_e)
};

/*
 * add one element to the buffer
 */
int
pfr_buf_add(struct pfr_buffer *b, const void *e)
{
    size_t bs;

    if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX ||
        e == NULL) {
        errno = EINVAL;
        return (-1);
    }
    bs = buf_esize[b->pfrb_type];           /**< choose buffer size, ex. sizeof(struct pfr_addr) */
    if (b->pfrb_size == b->pfrb_msize)      /**< no space left */
        if (pfr_buf_grow(b, 0))             /**< increase buffer */
            return (-1);
    memcpy(((caddr_t)b->pfrb_caddr) + bs * b->pfrb_size, e, bs);
    b->pfrb_size++;
    return (0);
}

int
pfr_add_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
    int *nadd, int flags)
{
    struct pfioc_table io;

    if (tbl == NULL || size < 0 || (size && addr == NULL)) {
        errno = EINVAL;
        return (-1);
    }
    bzero(&io, sizeof io);
    io.pfrio_flags = flags;
    io.pfrio_table = *tbl;
    io.pfrio_buffer = addr;
    io.pfrio_esize = sizeof(*addr);
    io.pfrio_size = size;
    if (ioctl(dev, DIOCRADDADDRS, &io))
        return (-1);
    if (nadd != NULL)
        *nadd = io.pfrio_nadd;
    return (0);
}

Pointer Handling

Pointer Arithmetic

arr[ i ] == * ( arr + i )

ex.
sizeof(unsigned long) = 64-bit = 8 byte
unsigned long arr[2];
&arr[0] = (arr + 0) = 0x608ea0
&arr[1] = (arr + 1) = 0x608ea8
(gdb) p &entry.allocator[0]         (gdb) p &((ethernet_header_t *) entry.allocator)[0]  
$19 = (header_t *) 0x608e90         $21 = (struct _ethernet_header_t *) 0x608e90


(gdb) p &entry.allocator[1]         (gdb) p &((ethernet_header_t *) entry.allocator)[1]
$20 = (header_t *) 0x608eb8         $22 = (struct _ethernet_header_t *) 0x608ed0
   
            
(gdb) p sizeof(header_t)            (gdb) p sizeof(ethernet_header_t)
$24 = 40 = 0x28                     $23 = 64 = 0x40

  0x608e90                            0x608e90
+ 0x000028                          + 0x000040
----------                          ----------
  0x608eb8                            0x608ed0
===========                         ===========

NXP LPCXpresso

LPC1343 QuickStart Board
LPC1347 LPCXpresso board
LPC1549 LPCXpresso board (aka. LPCXpresso V2 board)
LPC1769 LPCXpresso board
LPC800 / LPC812 MAX Board
LPCXpresso Experiment Kit
LPC-Link 2 board

LPCXpresso V2 Boards (aka. LPC1549 LPCXpresso board)
LPCXpresso11U37H board (aka. Extended LPCXpresso V2 board)
LPC1549 LPCXpresso Motor Control Kit (BLDC)

LPCOpen Software Development Platform for NXP LPC Microcontrollers
LightWeight IP (LWIP) networking stack for NXP LPC Microcontrollers

  • ARM CMSIS DSP Library
  • emWin, a graphics library
  • SWIM, a simple graphics library
  • LWIP, an open-source networking stack
  • LPCUSBLib, an open-source USB device and host stack
  • FreeRTOS, an open-source RTOS
  • The core library – A set of common core and chip specific drivers that are standardized across platforms
  • Examples & Projects

pkgng

/!\ WARNING /!\
pkg_install EOL is scheduled for 2014-09-01. Please consider migrating to pkgng
http://blogs.freebsdish.org/portmgr/2014/02/03/time-to-bid-farewell-to-the-old-pkg_-tools/
If you do not want to see this message again set NO_WARNING_PKG_INSTALL_EOL=yes in your make.conf

Mail Server Installation

  • DBMail
  • Postfix
  • ClamSMTP
  • AfterLogic WebMail Pro
  • Zimbra
  • amavisd-new
  • spamassassin
  • clamav
  • pigeonhole
  • pigeonhole

Database vs. File-System
Database based mail server
FreeBSD 10: postfix, dovecot, Roundcube, amavisd-new, spamassassin, clamav, pigeonhole

migrating_from_qmail_vpopmail_sql
Importing mailboxes from vpopmail
dbmail + qmail + vpopmail + awk!!! 🙂

qmail

Life with qmail
QMail HOWTO für FreeBSD
My Qmail installation guide
Installing qmail under FreeBSD
SMTP Authentication [Tutorial]
Setting up an SMTP service
Debian qmail Anleitung v1.2
qmail patches
Relaying with qmail (Environment Variable RELAYCLIENT)

TLS / Certificate

Using SMTP-TLS with qmail
My Qmail installation guide
Qmail-TLS patch
smtp-auth + qmail-tls (starttls) + forcetls patch for qmail

220 XXX.com ESMTP
ehlo user
250-XXX.com
250-STARTTLS
250-AUTH LOGIN PLAIN CRAM-MD5
250-PIPELINING
250 8BITMIME
starttls
454 TLS missing certificate: error:0200100D:system library:fopen:Permission denied (#4.3.0)
$ chown vpopmail:vchkpw /var/qmail/control/servercert.pem
220 XXX.com ESMTP
ehlo user
250-XXX.com
250-STARTTLS
250-AUTH LOGIN PLAIN CRAM-MD5
250-PIPELINING
250 8BITMIME
starttls
220 ready for tls
# Request
$ openssl req -new -nodes -out req.pem -keyout servercert.pem

# Sign
$ openssl req -new -x509 -nodes -out servercert.pem -days 366 -keyout servercert.pem
$ openssl ciphers > tlsclientciphers
$ openssl ciphers > tlsserverciphers

# Display certificate information
$ openssl x509 -text -noout -in servercert.pem

CheatSheet/OpenSSL
The Most Common OpenSSL Commands
OpenSSL usage tips and examples

qmail Toaster

tcp.smtp
Vpopmail – roaming users configuration

vpopmail

Vpopmail

A quick guide to vpopmail with MySQL
UCSPI-TCP MySQL patch

[root@te-clan /usr/ports/mail/qmail-tls]# pkg_info | grep mail
autorespond-2.0.5   Simple autoresponder for qmail
courier-authlib-vchkpw-0.59.3 Vpopmail/vchkpw support for the Courier authentication libr
courier-imap-4.1.3,1 IMAP (and POP3) server that provides access to Maildir mail
ezmlm-idx-0.40_4    Improved version of the ezmlm mailing list manager
p5-Email-Address-1.90.0 RFC 2822 Address Parsing and Creation
p5-Mail-DKIM-0.40   Perl5 module to process and/or create DKIM email
p5-Mail-SpamAssassin-3.3.2_8 A highly efficient mail filter for identifying spam
p5-Mail-Tools-2.12  Perl5 modules for dealing with Internet e-mail messages
qmail-qfilter-2.1_2 Front-end for qmail-queue to run messages through filters
qmail-tls-1.03.20021228_1 A SECURE, reliable, and FAST MTA for UNIX systems WITH TLS 
roundcube-0.2,1     Fully skinnable XHTML/CSS webmail written in PHP
vpopmail-devel-5.5.0_2 Easy virtual domain and authentication package for use with
[root@te-clan ~]# tcpserver -v
tcpserver: usage: tcpserver [ -1UXpPhHrRoOdDqQsSv ] [ -c limit ] [ -C [address[/len]:]limit ] [ -e name=var ] [ -x rules.cdb ] [ -B banner ] [ -g gid ] [ -u uid ] [ -b backlog ] [ -l localname ] [ -t timeout ] [ -n certfile ] host port program

[root@te-clan ~]# which tcpserver
/usr/local/bin/tcpserver

[root@te-clan ~]# readelf -s /usr/local/bin/tcpserver                
Symbol table '.dynsym' contains 144 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00000000004025f4    40 FUNC    GLOBAL DEFAULT  UND waitpid
     2: 000000000050ef40    16 OBJECT  GLOBAL DEFAULT   20 ipl
     3: 000000000050fb90     4 OBJECT  GLOBAL DEFAULT   20 subgetoptpos
     4: 000000000050fba0    32 OBJECT  GLOBAL DEFAULT   20 b
     5: 0000000000402604    55 FUNC    GLOBAL DEFAULT  UND recv
     6: 0000000000402614     0 FUNC    GLOBAL DEFAULT  UND connect
     7: 0000000000402624    52 FUNC    GLOBAL DEFAULT  UND sigemptyset
     8: 000000000050d358     8 OBJECT  GLOBAL DEFAULT   13 buffer_2
     9: 000000000050d390     4 OBJECT  GLOBAL DEFAULT   13 error_perm
    10: 000000000050fbc0    40 OBJECT  GLOBAL DEFAULT   20 localportstr
    11: 000000000050d320     8 OBJECT  GLOBAL DEFAULT   13 limit
    12: 0000000000402634     0 FUNC    GLOBAL DEFAULT  UND munmap
    13: 000000000050d394     4 OBJECT  GLOBAL DEFAULT   13 error_pipe
    14: 000000000050e8f8     0 OBJECT  GLOBAL DEFAULT  ABS _DYNAMIC
    15: 000000000050fb80     4 OBJECT  GLOBAL DEFAULT   20 subgetoptproblem
    16: 000000000050ef58     8 OBJECT  GLOBAL DEFAULT   20 uid
    17: 0000000000402644     0 FUNC    GLOBAL DEFAULT  UND execve
    18: 0000000000402654     0 FUNC    GLOBAL DEFAULT  UND getpid
    19: 0000000000402664    23 FUNC    GLOBAL DEFAULT  UND SSL_get_fd
    20: 000000000050d3e0     4 OBJECT  GLOBAL DEFAULT   13 sig_child
    21: 000000000050ef50     8 OBJECT  GLOBAL DEFAULT   20 gid
    22: 0000000000402674   143 FUNC    GLOBAL DEFAULT  UND SSL_read
    23: 0000000000402684   962 FUNC    GLOBAL DEFAULT  UND SSL_new
    24: 000000000050fb70     8 OBJECT  GLOBAL DEFAULT   20 sgetoptprogname
    25: 0000000000402694  1176 FUNC    GLOBAL DEFAULT  UND SSL_CTX_new
    26: 00000000004026a4    35 FUNC    GLOBAL DEFAULT  UND malloc
    27: 000000000050ef30     8 OBJECT  GLOBAL DEFAULT   20 limit_ip
    28: 000000000050d340     4 OBJECT  GLOBAL DEFAULT   13 verbosity
    29: 000000000050fbf0    20 OBJECT  GLOBAL DEFAULT   20 localipstr
    30: 00000000004026b4   322 FUNC    GLOBAL DEFAULT  UND SSL_library_init
    31: 000000000050d398     4 OBJECT  GLOBAL DEFAULT   13 error_again
    32: 000000000050ef80     8 OBJECT  GLOBAL DEFAULT   20 remotehost
    33: 00000000004026c4     0 FUNC    GLOBAL DEFAULT  UND socket
    34: 00000000004026d4     0 FUNC    GLOBAL DEFAULT  UND setgroups
    35: 00000000004026e4   367 FUNC    GLOBAL DEFAULT  UND SSL_CTX_use_RSAPrivateKey
    36: 000000000050d3d0     4 OBJECT  GLOBAL DEFAULT   13 sig_term
    37: 000000000050fc04     2 OBJECT  GLOBAL DEFAULT   20 remoteport
    38: 00000000004026f4    53 FUNC    GLOBAL DEFAULT  UND lseek
    39: 0000000000402704    99 FUNC    GLOBAL DEFAULT  UND sigaddset
    40: 0000000000402714    31 FUNC    GLOBAL DEFAULT  UND SSL_pending
    41: 0000000000402724    94 FUNC    GLOBAL DEFAULT  UND mmap
    42: 000000000050d3ec     4 OBJECT  GLOBAL DEFAULT   13 subgetoptind
    43: 000000000050d3c0     4 OBJECT  GLOBAL DEFAULT   13 sgetopterr
    44: 0000000000402734    55 FUNC    GLOBAL DEFAULT  UND send
    45: 00000000004025d0     0 FUNC    GLOBAL DEFAULT    8 _init
    46: 0000000000402744     0 FUNC    GLOBAL DEFAULT  UND pipe
    47: 000000000050d3a0     4 OBJECT  GLOBAL DEFAULT   13 error_inprogress
    48: 0000000000403660    11 FUNC    GLOBAL DEFAULT   10 sigterm
    49: 0000000000402754     0 FUNC    GLOBAL DEFAULT  UND accept
    50: 000000000050fc20    40 OBJECT  GLOBAL DEFAULT   20 strnum2
    51: 000000000050d330     4 OBJECT  GLOBAL DEFAULT   13 flagremotehost
    52: 0000000000402764   241 FUNC    GLOBAL DEFAULT  UND SSL_set_bio
    53: 0000000000402774     0 FUNC    GLOBAL DEFAULT  UND write
    54: 000000000050fb98     8 OBJECT  GLOBAL DEFAULT   20 environ
    55: 0000000000402784     0 FUNC    GLOBAL DEFAULT  UND fstat
    56: 000000000050fc48     4 OBJECT  GLOBAL DEFAULT   20 remoteip
    57: 0000000000402794     0 FUNC    GLOBAL DEFAULT  UND bind
    58: 000000000050fc50    16 OBJECT  GLOBAL DEFAULT   20 bspace
    59: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __deregister_frame_info
    60: 000000000050d388     4 OBJECT  GLOBAL DEFAULT   13 error_nodevice
    61: 00000000004027a4     0 FUNC    GLOBAL DEFAULT  UND setsockopt
    62: 000000000050d3d4     4 OBJECT  GLOBAL DEFAULT   13 sig_pipe
    63: 000000000050fc60     2 OBJECT  GLOBAL DEFAULT   20 localport
    64: 00000000004027b4    65 FUNC    GLOBAL DEFAULT  UND SSL_CTX_set_cipher_list
    65: 000000000050d33c     4 OBJECT  GLOBAL DEFAULT   13 flagkillopts
    66: 000000000050ef70     8 OBJECT  GLOBAL DEFAULT   20 fnrules
    67: 000000000050d300     8 OBJECT  GLOBAL DEFAULT   13 __progname
    68: 0000000000403db0   312 FUNC    GLOBAL DEFAULT   10 sigchld
    69: 000000000050d3a4     4 OBJECT  GLOBAL DEFAULT   13 error_timeout
    70: 000000000050d3ac     4 OBJECT  GLOBAL DEFAULT   13 error_io
    71: 00000000004027c4    13 FUNC    GLOBAL DEFAULT  UND __error
    72: 00000000004027d4     0 FUNC    GLOBAL DEFAULT  UND setgid
    73: 000000000050ef60     4 OBJECT  GLOBAL DEFAULT   20 flag1
    74: 00000000004027e4     0 FUNC    GLOBAL DEFAULT  UND read
    75: 000000000050ef88     8 OBJECT  GLOBAL DEFAULT   20 localhost
    76: 000000000050d348     8 OBJECT  GLOBAL DEFAULT   13 banner
    77: 000000000050d3c8     8 OBJECT  GLOBAL DEFAULT   13 sig_ignorehandler
    78: 00000000004027f4   148 FUNC    GLOBAL DEFAULT  UND SSLv23_server_method
    79: 000000000050d38c     4 OBJECT  GLOBAL DEFAULT   13 error_acces
    80: 000000000050d380     4 OBJECT  GLOBAL DEFAULT   13 error_isdir
    81: 000000000050fb88     8 OBJECT  GLOBAL DEFAULT   20 subgetoptarg
    82: 0000000000402804     0 FUNC    GLOBAL DEFAULT  UND listen
    83: 0000000000402814     0 FUNC    GLOBAL DEFAULT  UND fork
    84: 000000000050f020    16 OBJECT  GLOBAL DEFAULT   20 rules_name
    85: 0000000000402824     0 FUNC    GLOBAL DEFAULT  UND sigaction
    86: 000000000050d3b0     4 OBJECT  GLOBAL DEFAULT   13 error_txtbsy
    87: 0000000000402834     0 FUNC    GLOBAL DEFAULT  UND gettimeofday
    88: 0000000000402844    56 FUNC    GLOBAL DEFAULT  UND SSL_accept
    89: 000000000050eee0     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
    90: 000000000050d318     8 OBJECT  GLOBAL DEFAULT   13 backlog
    91: 000000000050d3b8     4 OBJECT  GLOBAL DEFAULT   13 error_nomem
    92: 0000000000402854     6 FUNC    GLOBAL DEFAULT  UND _init_tls
    93: 000000000050f140    96 OBJECT  GLOBAL DEFAULT   20 dns_resolve_tx
    94: 000000000050fc62     4 OBJECT  GLOBAL DEFAULT   20 localip
    95: 000000000050d328     8 OBJECT  GLOBAL DEFAULT   13 timeout
    96: 0000000000402864     0 FUNC    GLOBAL DEFAULT  UND ntohs
    97: 0000000000402874     0 FUNC    GLOBAL DEFAULT  UND getppid
    98: 000000000050d3b4     4 OBJECT  GLOBAL DEFAULT   13 error_noent
    99: 0000000000402884     0 FUNC    GLOBAL DEFAULT  UND poll
   100: 000000000050d384     4 OBJECT  GLOBAL DEFAULT   13 error_proto
   101: 000000000040c2fc     0 FUNC    GLOBAL DEFAULT   11 _fini
   102: 0000000000402894   108 FUNC    GLOBAL DEFAULT  UND getservbyname
   103: 00000000004028a4   121 FUNC    GLOBAL DEFAULT  UND gethostname
   104: 000000000050efa4     4 OBJECT  GLOBAL DEFAULT   20 flagparanoid
   105: 00000000004028b4    57 FUNC    GLOBAL DEFAULT  UND atexit
   106: 000000000050d338     4 OBJECT  GLOBAL DEFAULT   13 flagdelay
   107: 00000000004028c4     0 FUNC    GLOBAL DEFAULT  UND getpeername
   108: 000000000050ef78     4 OBJECT  GLOBAL DEFAULT   20 flagallownorules
   109: 000000000050efa0     4 OBJECT  GLOBAL DEFAULT   20 flagssl
   110: 00000000004028d4   543 FUNC    GLOBAL DEFAULT  UND SSL_CTX_use_certificate_c
   111: 000000000050ef90    16 OBJECT  GLOBAL DEFAULT   20 certfile
   112: 000000000050d3bc     4 OBJECT  GLOBAL DEFAULT   13 error_intr
   113: 000000000050fc80    40 OBJECT  GLOBAL DEFAULT   20 strnum
   114: 000000000050d39c     4 OBJECT  GLOBAL DEFAULT   13 error_wouldblock
   115: 000000000050ef68     8 OBJECT  GLOBAL DEFAULT   20 numchildren
   116: 000000000050eee0     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
   117: 000000000050eaa0     0 OBJECT  GLOBAL DEFAULT  ABS _GLOBAL_OFFSET_TABLE_
   118: 0000000000510640     0 NOTYPE  GLOBAL DEFAULT  ABS _end
   119: 000000000050ef7c     4 OBJECT  GLOBAL DEFAULT   20 flagdeny
   120: 00000000004028e4    77 FUNC    GLOBAL DEFAULT  UND exit
   121: 0000000000402da0   160 FUNC    GLOBAL DEFAULT   10 found
   122: 0000000000510620    32 OBJECT  GLOBAL DEFAULT   20 strerr_sys
   123: 000000000050fcb0    20 OBJECT  GLOBAL DEFAULT   20 remoteipstr
   124: 000000000050fcc8     8 OBJECT  GLOBAL DEFAULT   20 conns
   125: 00000000004028f4     0 FUNC    GLOBAL DEFAULT  UND _exit
   126: 000000000050fce0  2048 OBJECT  GLOBAL DEFAULT   20 tbuf
   127: 0000000000402904     0 FUNC    GLOBAL DEFAULT  UND open
   128: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
   129: 0000000000402914     0 FUNC    GLOBAL DEFAULT  UND fcntl
   130: 000000000050d334     4 OBJECT  GLOBAL DEFAULT   13 flagremoteinfo
   131: 0000000000402924     0 FUNC    GLOBAL DEFAULT  UND setuid
   132: 000000000050fb78     8 OBJECT  GLOBAL DEFAULT   20 sig_defaulthandler
   133: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __register_frame_info
   134: 0000000000402934     0 FUNC    GLOBAL DEFAULT  UND close
   135: 000000000050d3a8     4 OBJECT  GLOBAL DEFAULT   13 error_exist
   136: 0000000000402944   176 FUNC    GLOBAL DEFAULT  UND SSL_write
   137: 0000000000402954     0 FUNC    GLOBAL DEFAULT  UND sigsuspend
   138: 0000000000402964    35 FUNC    GLOBAL DEFAULT  UND free
   139: 000000000050d3e8     4 OBJECT  GLOBAL DEFAULT   13 subgetoptdone
   140: 00000000005104e0    40 OBJECT  GLOBAL DEFAULT   20 remoteportstr
   141: 0000000000402974    85 FUNC    GLOBAL DEFAULT  UND BIO_new_socket
   142: 0000000000402984     0 FUNC    GLOBAL DEFAULT  UND sigprocmask
   143: 0000000000402994     0 FUNC    GLOBAL DEFAULT  UND getsockname

[root@te-clan ~]# ldd /usr/local/bin/tcpserver
/usr/local/bin/tcpserver:
	libssl.so.4 => /usr/lib/libssl.so.4 (0x80064c000)
	libc.so.6 => /lib/libc.so.6 (0x800791000)
	libcrypto.so.4 => /lib/libcrypto.so.4 (0x8009ed000)

[root@te-clan ~]# pkg_info | grep tcp
ucspi-tcp-0.88_2    Command-line tools for building TCP client-server applicati
# patch < ucspi-tcp-0.88-mysql+rss.patch 
Hmm...  Looks like a unified diff to me...
The text leading up to this was:
--------------------------
| Additional info regarding this patch can be found at 
| http://www.tnpi.biz/internet/mail/toaster/patches/tcpserver-mysql.shtml
| 
| v1.2 - 2004.08.01
|
|   same patch, but entirely in unified diff format for folks whose
|   patch program is nit picky.
|
| v1.1 - 2004.05.29
| 
| added RBLSMTPD to IPs in MySQL table so that clients that 
| have authenticated override any IP blacklists (like SORBS)
| 
|
|diff -urN ucspi-tcp-0.88.dist/Makefile ucspi-tcp-0.88/Makefile
|--- ucspi-tcp-0.88.dist/Makefile	Sat Mar 18 10:18:42 2000
|+++ ucspi-tcp-0.88/Makefile	Mon Aug  2 20:36:50 2004
--------------------------
Patching file Makefile using Plan A...
Hunk #1 succeeded at 513.
Hunk #2 succeeded at 746.
Hmm...  The next patch looks like a unified diff to me...
The text leading up to this was:
--------------------------
|diff -urN ucspi-tcp-0.88.dist/conf-cc ucspi-tcp-0.88/conf-cc
|--- ucspi-tcp-0.88.dist/conf-cc	Sat Mar 18 10:18:42 2000
|+++ ucspi-tcp-0.88/conf-cc	Mon Aug  2 20:36:50 2004
--------------------------
Patching file conf-cc using Plan A...
Hunk #1 succeeded at 1.
Hmm...  The next patch looks like a unified diff to me...
The text leading up to this was:
--------------------------
|diff -urN ucspi-tcp-0.88.dist/conf-ld ucspi-tcp-0.88/conf-ld
|--- ucspi-tcp-0.88.dist/conf-ld	Sat Mar 18 10:18:42 2000
|+++ ucspi-tcp-0.88/conf-ld	Mon Aug  2 20:36:50 2004
--------------------------
Patching file conf-ld using Plan A...
Hunk #1 succeeded at 1.
Hmm...  The next patch looks like a unified diff to me...
The text leading up to this was:
--------------------------
|diff -urN ucspi-tcp-0.88.dist/db.c ucspi-tcp-0.88/db.c
|--- ucspi-tcp-0.88.dist/db.c	Wed Dec 31 19:00:00 1969
|+++ ucspi-tcp-0.88/db.c	Mon Aug  2 20:36:50 2004
--------------------------
(Creating file db.c...)
Patching file db.c using Plan A...
Hunk #1 succeeded at 1.
Hmm...  The next patch looks like a unified diff to me...
The text leading up to this was:
--------------------------
|diff -urN ucspi-tcp-0.88.dist/db.h ucspi-tcp-0.88/db.h
|--- ucspi-tcp-0.88.dist/db.h	Wed Dec 31 19:00:00 1969
|+++ ucspi-tcp-0.88/db.h	Mon Aug  2 20:36:50 2004
--------------------------
(Creating file db.h...)
Patching file db.h using Plan A...
Hunk #1 succeeded at 1.
Hmm...  The next patch looks like a unified diff to me...
The text leading up to this was:
--------------------------
|diff -urN ucspi-tcp-0.88.dist/rblsmtpd.c ucspi-tcp-0.88/rblsmtpd.c
|--- ucspi-tcp-0.88.dist/rblsmtpd.c	Sat Mar 18 10:18:42 2000
|+++ ucspi-tcp-0.88/rblsmtpd.c	Mon Aug  2 20:36:50 2004
--------------------------
Patching file rblsmtpd.c using Plan A...
Hunk #1 succeeded at 60.
Hmm...  The next patch looks like a unified diff to me...
The text leading up to this was:
--------------------------
|diff -urN ucspi-tcp-0.88.dist/tcpserver.c ucspi-tcp-0.88/tcpserver.c
|--- ucspi-tcp-0.88.dist/tcpserver.c	Sat Mar 18 10:18:42 2000
|+++ ucspi-tcp-0.88/tcpserver.c	Mon Aug  2 20:36:50 2004
--------------------------
Patching file tcpserver.c using Plan A...
Hunk #1 succeeded at 1.
Hunk #2 succeeded at 26.
Hunk #3 succeeded at 112.
Hunk #4 succeeded at 205.
Hunk #5 succeeded at 258.
Hunk #6 succeeded at 318.
Hunk #7 succeeded at 345.
Hunk #8 succeeded at 417.
Hunk #9 succeeded at 425.
done
db.c:4:19: mysql.h: No such file or directory
db.c:18: error: syntax error before '*' token
db.c:18: warning: data definition has no type or storage class
db.c: In function `open_db':
db.c:32: error: `MYSQL' undeclared (first use in this function)
db.c:32: error: (Each undeclared identifier is reported only once
db.c:32: error: for each function it appears in.)
db.c:32: error: `tmp' undeclared (first use in this function)
db.c:38: error: syntax error before ')' token
db.c: In function `check_db':
db.c:60: error: `MYSQL_RES' undeclared (first use in this function)
db.c:60: error: `res' undeclared (first use in this function)
*** Error code 1
1 error
===> Compilation failed unexpectedly.
Try to set MAKE_JOBS_UNSAFE=yes and rebuild before reporting the failure to
the maintainer.
*** Error code 1

Stop in /usr/ports/sysutils/ucspi-tcp.
[root@te-clan ~]# tcpserver -v
tcpserver: usage: tcpserver [ -1UXpPhHrRoOdDqQvS ] [ -c limit ] [ -x rules.cdb ] [ -B banner ] [ -g gid ] [ -u uid ] [ -b backlog ] [ -l localname ] [ -t timeout ] host port program

[root@te-clan ~]# readelf -s /usr/local/bin/tcpserver  
Symbol table '.dynsym' contains 155 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00000000004029bc     0 FUNC    GLOBAL DEFAULT  UND strcpy
     2: 00000000004029cc    40 FUNC    GLOBAL DEFAULT  UND waitpid
     3: 000000000050fce0     4 OBJECT  GLOBAL DEFAULT   22 subgetoptpos
     4: 000000000050fd00    32 OBJECT  GLOBAL DEFAULT   22 b
     5: 00000000004029dc    55 FUNC    GLOBAL DEFAULT  UND recv
     6: 00000000004029ec     0 FUNC    GLOBAL DEFAULT  UND connect
     7: 00000000004029fc     8 FUNC    GLOBAL DEFAULT  UND mysql_error@libmysqlclient_15 (2)
     8: 0000000000402a0c    52 FUNC    GLOBAL DEFAULT  UND sigemptyset
     9: 0000000000402a1c   365 FUNC    GLOBAL DEFAULT  UND mysql_store_result@libmysqlclient_15 (2)
    10: 000000000050d400     8 OBJECT  GLOBAL DEFAULT   15 buffer_2
    11: 000000000050d434     4 OBJECT  GLOBAL DEFAULT   15 error_perm
    12: 000000000050fd20    40 OBJECT  GLOBAL DEFAULT   22 localportstr
    13: 000000000050d3b8     8 OBJECT  GLOBAL DEFAULT   15 limit
    14: 0000000000402a2c   458 FUNC    GLOBAL DEFAULT  UND snprintf
    15: 0000000000402a3c     0 FUNC    GLOBAL DEFAULT  UND munmap
    16: 000000000050fe80   256 OBJECT  GLOBAL DEFAULT   22 db_database
    17: 000000000050d430     4 OBJECT  GLOBAL DEFAULT   15 error_pipe
    18: 000000000050ea28     0 OBJECT  GLOBAL DEFAULT  ABS _DYNAMIC
    19: 000000000050fcf0     4 OBJECT  GLOBAL DEFAULT   22 subgetoptproblem
    20: 000000000050f128     8 OBJECT  GLOBAL DEFAULT   22 uid
    21: 000000000050ff80   256 OBJECT  GLOBAL DEFAULT   22 db_password
    22: 0000000000000000     4 OBJECT  GLOBAL DEFAULT  UND errno
    23: 0000000000402a4c     0 FUNC    GLOBAL DEFAULT  UND execve
    24: 0000000000402a5c     0 FUNC    GLOBAL DEFAULT  UND getpid
    25: 0000000000402a6c   506 FUNC    GLOBAL DEFAULT  UND fgets
    26: 000000000050d454     4 OBJECT  GLOBAL DEFAULT   15 sig_child
    27: 000000000050f130     8 OBJECT  GLOBAL DEFAULT   22 gid
    28: 0000000000000000     8 OBJECT  GLOBAL DEFAULT  UND __stderrp
    29: 000000000050fcd0     8 OBJECT  GLOBAL DEFAULT   22 sgetoptprogname
    30: 0000000000402a7c    35 FUNC    GLOBAL DEFAULT  UND malloc
    31: 0000000000510080   256 OBJECT  GLOBAL DEFAULT   22 db_user
    32: 000000000050d398     4 OBJECT  GLOBAL DEFAULT   15 verbosity
    33: 000000000050fd50    20 OBJECT  GLOBAL DEFAULT   22 localipstr
    34: 000000000050d42c     4 OBJECT  GLOBAL DEFAULT   15 error_again
    35: 000000000050f100     8 OBJECT  GLOBAL DEFAULT   22 remotehost
    36: 0000000000402a8c   187 FUNC    GLOBAL DEFAULT  UND mysql_init@libmysqlclient_15 (2)
    37: 0000000000402a9c     0 FUNC    GLOBAL DEFAULT  UND socket
    38: 0000000000402aac     0 FUNC    GLOBAL DEFAULT  UND setgroups
    39: 0000000000510180   256 OBJECT  GLOBAL DEFAULT   22 db_host
    40: 000000000050d464     4 OBJECT  GLOBAL DEFAULT   15 sig_term
    41: 000000000050fd64     2 OBJECT  GLOBAL DEFAULT   22 remoteport
    42: 0000000000402abc    53 FUNC    GLOBAL DEFAULT  UND lseek
    43: 0000000000402acc    99 FUNC    GLOBAL DEFAULT  UND sigaddset
    44: 0000000000402adc    94 FUNC    GLOBAL DEFAULT  UND mmap
    45: 000000000050d470     4 OBJECT  GLOBAL DEFAULT   15 subgetoptind
    46: 000000000050d448     4 OBJECT  GLOBAL DEFAULT   15 sgetopterr
    47: 0000000000402aec    55 FUNC    GLOBAL DEFAULT  UND send
    48: 0000000000402afc  3968 FUNC    GLOBAL DEFAULT  UND mysql_real_connect@libmysqlclient_15 (2)
    49: 0000000000402998     0 FUNC    GLOBAL DEFAULT   10 _init
    50: 000000000050d424     4 OBJECT  GLOBAL DEFAULT   15 error_inprogress
    51: 00000000004039f0    14 FUNC    GLOBAL DEFAULT   12 sigterm
    52: 0000000000402b0c     0 FUNC    GLOBAL DEFAULT  UND accept
    53: 000000000050fd80    40 OBJECT  GLOBAL DEFAULT   22 strnum2
    54: 000000000050d3a8     4 OBJECT  GLOBAL DEFAULT   15 flagremotehost
    55: 000000000050d3d0     4 OBJECT  GLOBAL DEFAULT   15 db_port
    56: 0000000000402b1c     0 FUNC    GLOBAL DEFAULT  UND write
    57: 000000000050fcf8     8 OBJECT  GLOBAL DEFAULT   22 environ
    58: 0000000000402b2c     0 FUNC    GLOBAL DEFAULT  UND fstat
    59: 0000000000402b3c   223 FUNC    GLOBAL DEFAULT  UND fprintf
    60: 000000000050fda8     4 OBJECT  GLOBAL DEFAULT   22 remoteip
    61: 0000000000402b4c     0 FUNC    GLOBAL DEFAULT  UND bind
    62: 000000000050fdb0    16 OBJECT  GLOBAL DEFAULT   22 bspace
    63: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __deregister_frame_info
    64: 000000000050d43c     4 OBJECT  GLOBAL DEFAULT   15 error_nodevice
    65: 0000000000402b5c     0 FUNC    GLOBAL DEFAULT  UND setsockopt
    66: 000000000050d460     4 OBJECT  GLOBAL DEFAULT   15 sig_pipe
    67: 000000000050fdc0     2 OBJECT  GLOBAL DEFAULT   22 localport
    68: 000000000050f1f0     8 OBJECT  GLOBAL DEFAULT   22 dbh
    69: 000000000050d39c     4 OBJECT  GLOBAL DEFAULT   15 flagkillopts
    70: 000000000050f110     8 OBJECT  GLOBAL DEFAULT   22 fnrules
    71: 000000000050d380     8 OBJECT  GLOBAL DEFAULT   15 __progname
    72: 0000000000403a00   182 FUNC    GLOBAL DEFAULT   12 sigchld
    73: 000000000050d420     4 OBJECT  GLOBAL DEFAULT   15 error_timeout
    74: 0000000000402b6c   136 FUNC    GLOBAL DEFAULT  UND strstr
    75: 000000000050d418     4 OBJECT  GLOBAL DEFAULT   15 error_io
    76: 0000000000402b7c     0 FUNC    GLOBAL DEFAULT  UND setgid
    77: 000000000050f120     4 OBJECT  GLOBAL DEFAULT   22 flag1
    78: 0000000000402b8c     0 FUNC    GLOBAL DEFAULT  UND read
    79: 000000000050f0f8     8 OBJECT  GLOBAL DEFAULT   22 localhost
    80: 000000000050d3c8     8 OBJECT  GLOBAL DEFAULT   15 banner
    81: 0000000000402b9c   143 FUNC    GLOBAL DEFAULT  UND strcasecmp
    82: 000000000050d468     8 OBJECT  GLOBAL DEFAULT   15 sig_ignorehandler
    83: 000000000050d438     4 OBJECT  GLOBAL DEFAULT   15 error_acces
    84: 000000000050d444     4 OBJECT  GLOBAL DEFAULT   15 error_isdir
    85: 0000000000510280     4 OBJECT  GLOBAL DEFAULT   22 pop_timeout
    86: 000000000050fce8     8 OBJECT  GLOBAL DEFAULT   22 subgetoptarg
    87: 0000000000402bac     0 FUNC    GLOBAL DEFAULT  UND listen
    88: 0000000000402bbc   418 FUNC    GLOBAL DEFAULT  UND mysql_close@libmysqlclient_15 (2)
    89: 0000000000402bcc     0 FUNC    GLOBAL DEFAULT  UND fork
    90: 0000000000402bdc   408 FUNC    GLOBAL DEFAULT  UND sscanf
    91: 000000000050f1a0    16 OBJECT  GLOBAL DEFAULT   22 rules_name
    92: 0000000000402bec     0 FUNC    GLOBAL DEFAULT  UND sigaction
    93: 0000000000402bfc   225 FUNC    GLOBAL DEFAULT  UND mysql_free_result@libmysqlclient_15 (2)
    94: 000000000050d414     4 OBJECT  GLOBAL DEFAULT   15 error_txtbsy
    95: 0000000000402c0c     0 FUNC    GLOBAL DEFAULT  UND gettimeofday
    96: 0000000000402c1c   264 FUNC    GLOBAL DEFAULT  UND fopen
    97: 000000000050f088     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
    98: 000000000050d3c0     8 OBJECT  GLOBAL DEFAULT   15 backlog
    99: 000000000050d40c     4 OBJECT  GLOBAL DEFAULT   15 error_nomem
   100: 0000000000402c2c     6 FUNC    GLOBAL DEFAULT  UND _init_tls
   101: 000000000050f2c0    96 OBJECT  GLOBAL DEFAULT   22 dns_resolve_tx
   102: 000000000050fdc2     4 OBJECT  GLOBAL DEFAULT   22 localip
   103: 0000000000402c3c   377 FUNC    GLOBAL DEFAULT  UND fclose
   104: 000000000050d3b0     8 OBJECT  GLOBAL DEFAULT   15 timeout
   105: 0000000000402c4c     0 FUNC    GLOBAL DEFAULT  UND ntohs
   106: 0000000000402c5c     0 FUNC    GLOBAL DEFAULT  UND getppid
   107: 000000000050d410     4 OBJECT  GLOBAL DEFAULT   15 error_noent
   108: 0000000000402c6c    15 FUNC    GLOBAL DEFAULT  UND mysql_affected_rows@libmysqlclient_15 (2)
   109: 0000000000402c7c     0 FUNC    GLOBAL DEFAULT  UND poll
   110: 000000000050d440     4 OBJECT  GLOBAL DEFAULT   15 error_proto
   111: 000000000040c3fc     0 FUNC    GLOBAL DEFAULT   13 _fini
   112: 0000000000402c8c    55 FUNC    GLOBAL DEFAULT  UND mysql_query@libmysqlclient_15 (2)
   113: 0000000000402c9c   108 FUNC    GLOBAL DEFAULT  UND getservbyname
   114: 0000000000402cac   121 FUNC    GLOBAL DEFAULT  UND gethostname
   115: 000000000050f0f4     4 OBJECT  GLOBAL DEFAULT   22 flagparanoid
   116: 0000000000402cbc    57 FUNC    GLOBAL DEFAULT  UND atexit
   117: 000000000050d3a0     4 OBJECT  GLOBAL DEFAULT   15 flagdelay
   118: 0000000000402ccc     0 FUNC    GLOBAL DEFAULT  UND getpeername
   119: 000000000050f10c     4 OBJECT  GLOBAL DEFAULT   22 flagallownorules
   120: 00000000005102a0   256 OBJECT  GLOBAL DEFAULT   22 db_table
   121: 000000000050d408     4 OBJECT  GLOBAL DEFAULT   15 error_intr
   122: 000000000050fde0    40 OBJECT  GLOBAL DEFAULT   22 strnum
   123: 000000000050d428     4 OBJECT  GLOBAL DEFAULT   15 error_wouldblock
   124: 0000000000402cdc   216 FUNC    GLOBAL DEFAULT  UND fwrite
   125: 000000000050f118     8 OBJECT  GLOBAL DEFAULT   22 numchildren
   126: 000000000050f088     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
   127: 000000000050ebf0     0 OBJECT  GLOBAL DEFAULT  ABS _GLOBAL_OFFSET_TABLE_
   128: 00000000005104c0     0 NOTYPE  GLOBAL DEFAULT  ABS _end
   129: 000000000050f108     4 OBJECT  GLOBAL DEFAULT   22 flagdeny
   130: 0000000000402cec    77 FUNC    GLOBAL DEFAULT  UND exit
   131: 0000000000403190   145 FUNC    GLOBAL DEFAULT   12 found
   132: 00000000005104a0    32 OBJECT  GLOBAL DEFAULT   22 strerr_sys
   133: 0000000000402cfc    33 FUNC    GLOBAL DEFAULT  UND atoi
   134: 000000000050fe10    20 OBJECT  GLOBAL DEFAULT   22 remoteipstr
   135: 0000000000402d0c   123 FUNC    GLOBAL DEFAULT  UND mysql_ping@libmysqlclient_15 (2)
   136: 0000000000402d1c     0 FUNC    GLOBAL DEFAULT  UND _exit
   137: 0000000000402d2c     0 FUNC    GLOBAL DEFAULT  UND open
   138: 000000000050f0f0     4 OBJECT  GLOBAL DEFAULT   22 usemysql
   139: 0000000000402d3c    81 FUNC    GLOBAL DEFAULT  UND strchr
   140: 0000000000402d4c   149 FUNC    GLOBAL DEFAULT  UND fputs
   141: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
   142: 0000000000402d5c     0 FUNC    GLOBAL DEFAULT  UND fcntl
   143: 000000000050d3a4     4 OBJECT  GLOBAL DEFAULT   15 flagremoteinfo
   144: 0000000000402d6c     0 FUNC    GLOBAL DEFAULT  UND setuid
   145: 000000000050fcd8     8 OBJECT  GLOBAL DEFAULT   22 sig_defaulthandler
   146: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __register_frame_info
   147: 0000000000402d7c     0 FUNC    GLOBAL DEFAULT  UND close
   148: 000000000050d41c     4 OBJECT  GLOBAL DEFAULT   15 error_exist
   149: 0000000000402d8c     0 FUNC    GLOBAL DEFAULT  UND sigsuspend
   150: 0000000000402d9c    35 FUNC    GLOBAL DEFAULT  UND free
   151: 000000000050d474     4 OBJECT  GLOBAL DEFAULT   15 subgetoptdone
   152: 000000000050fe40    40 OBJECT  GLOBAL DEFAULT   22 remoteportstr
   153: 0000000000402dac     0 FUNC    GLOBAL DEFAULT  UND sigprocmask
   154: 0000000000402dbc     0 FUNC    GLOBAL DEFAULT  UND getsockname

[root@te-clan ~]# ldd /usr/local/bin/tcpserver  
/usr/local/bin/tcpserver:
	libmysqlclient.so.15 => /usr/local/lib/mysql/libmysqlclient.so.15 (0x80064c000)
	libc.so.6 => /lib/libc.so.6 (0x8007c1000)
	libcrypt.so.3 => /lib/libcrypt.so.3 (0x800a1d000)
	libm.so.4 => /lib/libm.so.4 (0x800b38000)
	libz.so.3 => /lib/libz.so.3 (0x800c60000)

/var/qmail/service/smtpd/log/main/current

@40000000538c4ed3372393e4 tcpserver: status: 1/200
@40000000538c4ed337326cac db_port set to: [3306]
@40000000538c4ed337328034 db_ping() is re-calling open_db!
@40000000538c4ed337e6cc74 tcpserver: pid 74051 from x.x.x.x
@40000000538c4ed337f3fb74 tcpserver: ok 74051 te-clan.ch:195.134.157.20:25 :x.x.x.x::57943
@40000000538c4ed834e148ec tcpserver: end 74051 status 0
@40000000538c4ed834efeeec tcpserver: status: 0/200

@40000000538c6b692688577c use mysql=1
@40000000538c6b6926886eec after db_ping(), before check_db()
@40000000538c6b6926888274 QUERY: SELECT timestamp FROM lastauth WHERE remote_ip='x.x.x.x' AND timestamp>(UNIX_TIMESTAMP()-1800)
@40000000538c6b69268899e4 QUERY(err:0/rows:1): SELECT timestamp FROM lastauth WHERE remote_ip='x.x.x.x' AND timestamp>(UNIX_TIMESTAMP()-1800)
@40000000538c6b692688b154 found entry
@40000000538c6b692688c4dc after check_db(): flagdeny=0
@40000000538c6b692688d864 tcpserver: pid 79280 from 80.218.250.37
@40000000538c6b6926963e14 doit() ip=x.x.x.x, port=55580, host=(null)
@40000000538c6b692696596c doit() before usemysql: flagdeny=0
@40000000538c6b6926966cf4 tcpserver: ok 79280 te-clan.ch:195.134.157.20:25 :x.x.x.x::55580
@40000000538c6b692696807c doit() before exit: flagdeny=0
@40000000538c6b7510181824 tcpserver: end 79280 status 0
@40000000538c6b751026e91c tcpserver: status: 0/200

Courier IMAP

Large-Scale Mail with Postfix, OpenLDAP and Courier
Life With qmail-ldap

Compiling imaplogin.c
imaplogin.c:35:25: courierauth.h: No such file or directory
imaplogin.c:36:30: courierauthdebug.h: No such file or directory

use CFLAGS=/usr/local/include

Error in make around imaplogin.c

Gentoo: Complete Virtual Mail Server
Courier MTA: Debugging authentication problems

tbpql

Port 587

Setup Qmail to Listen on port 587 for SMTP
How do I run qmail on port 587?
Running Toaster QMail SMTP on a 2nd port
How to: qmail-smtpd for roaming users