~ubuntu-branches/ubuntu/karmic/keepalived/karmic

« back to all changes in this revision

Viewing changes to lib/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt
  • Date: 2005-04-29 23:22:40 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050429232240-a8m3jtpi3cvuyyy2
Tags: 1.1.11-3
Added a warning about sarge kernels to README.Debian and 
the package description 

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *
6
6
 * Part:        General program utils.
7
7
 *
8
 
 * Version:     $Id: utils.c,v 1.1.7 2004/04/04 23:28:05 acassen Exp $
 
8
 * Version:     $Id: utils.c,v 1.1.11 2005/03/01 01:22:13 acassen Exp $
9
9
 *
10
10
 * Author:      Alexandre Cassen, <acassen@linux-vs.org>
11
11
 *
19
19
 *              as published by the Free Software Foundation; either version
20
20
 *              2 of the License, or (at your option) any later version.
21
21
 *
22
 
 * Copyright (C) 2001-2004 Alexandre Cassen, <acassen@linux-vs.org>
 
22
 * Copyright (C) 2001-2005 Alexandre Cassen, <acassen@linux-vs.org>
23
23
 */
24
24
 
25
25
#include "utils.h"
26
26
 
 
27
/* global vars */
 
28
int debug = 0;
 
29
 
27
30
/* Display a buffer into a HEXA formated output */
28
31
void
29
 
print_buffer(int count, char *buff)
 
32
dump_buffer(char *buff, int count)
30
33
{
31
34
        int i, j, c;
32
35
        int printnext = 1;
67
70
        }
68
71
}
69
72
 
 
73
/* Compute a checksum */
 
74
u_short
 
75
in_csum(u_short * addr, int len, u_short csum)
 
76
{
 
77
        register int nleft = len;
 
78
        const u_short *w = addr;
 
79
        register u_short answer;
 
80
        register int sum = csum;
 
81
 
 
82
        /*
 
83
         *  Our algorithm is simple, using a 32 bit accumulator (sum),
 
84
         *  we add sequential 16 bit words to it, and at the end, fold
 
85
         *  back all the carry bits from the top 16 bits into the lower
 
86
         *  16 bits.
 
87
         */
 
88
        while (nleft > 1) {
 
89
                sum += *w++;
 
90
                nleft -= 2;
 
91
        }
 
92
 
 
93
        /* mop up an odd byte, if necessary */
 
94
        if (nleft == 1)
 
95
                sum += htons(*(u_char *) w << 8);
 
96
 
 
97
        /*
 
98
         * add back carry outs from top 16 bits to low 16 bits
 
99
         */
 
100
        sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
 
101
        sum += (sum >> 16);                     /* add carry */
 
102
        answer = ~sum;                          /* truncate to 16 bits */
 
103
        return (answer);
 
104
}
 
105
 
70
106
/* IP network to ascii representation */
71
107
char *
72
108
inet_ntop2(uint32_t ip)
168
204
        return 1;
169
205
}
170
206
 
 
207
/*
 
208
 * Return broadcast address from network and netmask.
 
209
 */
 
210
uint32_t
 
211
inet_broadcast(uint32_t network, uint32_t netmask)
 
212
{
 
213
        return 0xffffffff - netmask + network;
 
214
}
 
215
 
 
216
/*
 
217
 * Convert CIDR netmask notation to long notation.
 
218
 */
 
219
uint32_t
 
220
inet_cidrtomask(uint8_t cidr)
 
221
{
 
222
        uint32_t mask = 0;
 
223
        int b;
 
224
 
 
225
        for (b = 0; b < cidr; b++)
 
226
                mask |= (1 << (31 - b));
 
227
        return ntohl(mask);
 
228
}
 
229
 
171
230
/* Getting localhost official canonical name */
172
231
char *
173
232
get_local_name(void)