ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/util.h
Revision: 1.14
Committed: Fri Mar 18 01:53:05 2005 UTC (19 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.13: +0 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2     util.h -- process management and other utility functions
3     Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>
4     2000-2002 Guus Sliepen <guus@sliepen.eu.org>
5 pcg 1.13 2003 Marc Lehmann <gvpe@schmorp.de>
6 pcg 1.1
7 pcg 1.13 This file is part of GVPE.
8    
9     GVPE is free software; you can redistribute it and/or modify
10 pcg 1.1 it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13    
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     GNU General Public License for more details.
18    
19     You should have received a copy of the GNU General Public License
20 pcg 1.13 along with gvpe; if not, write to the Free Software
21 pcg 1.1 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22     */
23    
24     #ifndef UTIL_H__
25     #define UTIL_H__
26    
27 pcg 1.11 #include <openssl/rsa.h>
28    
29 pcg 1.10 #include "slog.h"
30 pcg 1.3 #include "iom.h"
31 pcg 1.1
32     /*
33     * check for an existing vped for this net, and write pid to pidfile
34     */
35     extern int write_pidfile (void);
36    
37     /*
38     * kill older vped
39     */
40     extern int kill_other (int signal);
41    
42     /*
43     * Detach from current terminal, write pidfile, kill parent
44     */
45     extern int detach (int do_detach);
46    
47     /*
48     * check wether the given path is an absolute pathname
49     */
50     #define ABSOLUTE_PATH(c) ((c)[0] == '/')
51 pcg 1.10
52 pcg 1.11 /*****************************************************************************/
53    
54 pcg 1.10 typedef u8 mac[6];
55 pcg 1.1
56 pcg 1.8 extern void id2mac (unsigned int id, void *m);
57 pcg 1.1
58 pcg 1.8 #define mac2id(p) ((p)[0] & 0x01 ? 0 : ((p)[4] << 8) | (p)[5])
59 pcg 1.1
60 pcg 1.2 struct sliding_window {
61     u32 v[(WINDOWSIZE + 31) / 32];
62     u32 seq;
63    
64     void reset (u32 seqno)
65     {
66     memset (v, -1, sizeof v);
67     seq = seqno;
68     }
69    
70     bool recv_ok (u32 seqno)
71     {
72     if (seqno <= seq - WINDOWSIZE)
73     slog (L_ERR, _("received duplicate or outdated packet (received %08lx, expected %08lx)\n"
74     "possible replay attack, or just massive packet reordering"), seqno, seq + 1);//D
75     else if (seqno > seq + WINDOWSIZE)
76     slog (L_ERR, _("received duplicate or out-of-sync packet (received %08lx, expected %08lx)\n"
77     "possible replay attack, or just massive packet loss"), seqno, seq + 1);//D
78     else
79     {
80     while (seqno > seq)
81     {
82     seq++;
83    
84     u32 s = seq % WINDOWSIZE;
85     u32 *cell = v + (s >> 5);
86     u32 mask = 1 << (s & 31);
87    
88     *cell &= ~mask;
89     }
90    
91     u32 s = seqno % WINDOWSIZE;
92     u32 *cell = v + (s >> 5);
93     u32 mask = 1 << (s & 31);
94    
95     if (*cell & mask)
96     {
97     slog (L_ERR, _("received duplicate packet (received %08lx, expected %08lx)\n"
98     "possible replay attack, or just packet duplication"), seqno, seq + 1);//D
99     return false;
100     }
101     else
102     {
103     *cell |= mask;
104     return true;
105     }
106     }
107     }
108     };
109    
110 pcg 1.6 typedef callback0<const char *> run_script_cb;
111 pcg 1.5
112     // run a shell script (or actually an external program).
113     void run_script (const run_script_cb &cb, bool wait);
114    
115 pcg 1.7 #if ENABLE_HTTP_PROXY
116     u8 *base64_encode (const u8 *data, unsigned int len);
117     #endif
118 pcg 1.11
119     /*****************************************************************************/
120    
121     typedef u8 rsaclear[RSA_KEYLEN - RSA_OVERHEAD]; // challenge data;
122     typedef u8 rsacrypt[RSA_KEYLEN]; // encrypted challenge
123    
124     static inline void
125     rsa_encrypt (RSA *key, const rsaclear &chg, rsacrypt &encr)
126     {
127     if (RSA_public_encrypt (sizeof chg,
128     (unsigned char *)&chg, (unsigned char *)&encr,
129     key, RSA_PKCS1_OAEP_PADDING) < 0)
130     fatal ("RSA_public_encrypt error");
131     }
132    
133     static inline bool
134     rsa_decrypt (RSA *key, const rsacrypt &encr, rsaclear &chg)
135     {
136     return RSA_private_decrypt (sizeof encr,
137     (unsigned char *)&encr, (unsigned char *)&chg,
138     key, RSA_PKCS1_OAEP_PADDING) > 0;
139     }
140 pcg 1.7
141 pcg 1.1 #endif
142