ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/util.h
(Generate patch)

Comparing gvpe/src/util.h (file contents):
Revision 1.27 by pcg, Sun Aug 10 01:34:36 2008 UTC vs.
Revision 1.34 by root, Thu Jan 9 08:15:05 2014 UTC

1/* 1/*
2 util.h -- process management and other utility functions 2 util.h -- process management and other utility functions
3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> 3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>
4 2000-2002 Guus Sliepen <guus@sliepen.eu.org> 4 2000-2002 Guus Sliepen <guus@sliepen.eu.org>
5 2003-2008 Marc Lehmann <gvpe@schmorp.de> 5 2003-2013 Marc Lehmann <gvpe@schmorp.de>
6 6
7 This file is part of GVPE. 7 This file is part of GVPE.
8 8
9 GVPE is free software; you can redistribute it and/or modify it 9 GVPE is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the 10 under the terms of the GNU General Public License as published by the
35#define UTIL_H__ 35#define UTIL_H__
36 36
37#include <cstring> 37#include <cstring>
38#include <sys/types.h> 38#include <sys/types.h>
39 39
40#include <openssl/rsa.h>
41
42#include "gettext.h" 40#include "gettext.h"
43 41
44#include "slog.h" 42#include "slog.h"
45#include "ev_cpp.h" 43#include "ev_cpp.h"
46#include "callback.h" 44#include "callback.h"
45#include "global.h"
47 46
48typedef ev_tstamp tstamp; 47typedef ev_tstamp tstamp;
49 48
50/* 49/*
51 * check for an existing gvpe for this net, and write pid to pidfile 50 * check for an existing gvpe for this net, and write pid to pidfile
84 { 83 {
85 memset (v, -1, sizeof v); 84 memset (v, -1, sizeof v);
86 seq = seqno; 85 seq = seqno;
87 } 86 }
88 87
89 // 0 == ok, 1 == silently ignore, 2 == error, reset 88 // 0 == ok, 1 == far history, 2 == duplicate in-window, 3 == far future
90 int seqno_classify (u32 seqno) 89 int seqno_classify (u32 seqno)
91 { 90 {
92 if (seqno <= seq - WINDOWSIZE) 91 if (seqno <= seq - WINDOWSIZE)
93 return 1; 92 return 1;
94 else if (seqno > seq + WINDOWSIZE * 16) 93 else if (seqno > seq + WINDOWSIZE * 16)
95 return 2; 94 return 3;
96 else 95 else
97 { 96 {
98 while (seqno > seq) 97 while (seqno > seq)
99 { 98 {
100 seq++; 99 seq++;
109 u32 s = seqno % WINDOWSIZE; 108 u32 s = seqno % WINDOWSIZE;
110 u32 *cell = v + (s >> 5); 109 u32 *cell = v + (s >> 5);
111 u32 mask = 1 << (s & 31); 110 u32 mask = 1 << (s & 31);
112 111
113 if (*cell & mask) 112 if (*cell & mask)
114 return 1; 113 return 2;
115 else 114 else
116 { 115 {
117 *cell |= mask; 116 *cell |= mask;
118 return 0; 117 return 0;
119 } 118 }
120 } 119 }
121 } 120 }
122}; 121};
123 122
124typedef callback<const char * ()> run_script_cb; 123typedef callback<const char *()> run_script_cb;
125 124
126// run a shell script (or actually an external program). 125// run a shell script (or actually an external program).
127pid_t run_script (const run_script_cb &cb, bool wait); 126pid_t run_script (const run_script_cb &cb, bool wait);
127
128void hexdump (const char *header, void *data, int len);
128 129
129#if ENABLE_HTTP_PROXY 130#if ENABLE_HTTP_PROXY
130u8 *base64_encode (const u8 *data, unsigned int len); 131u8 *base64_encode (const u8 *data, unsigned int len);
131#endif 132#endif
132 133
134/* always take more or less the same time to compare */
135bool slow_memeq (const void *a, const void *b, int len);
136
133/*****************************************************************************/ 137/*****************************************************************************/
134 138
135typedef u8 rsaclear[RSA_KEYLEN - RSA_OVERHEAD]; // challenge data; 139void rand_fill (void *data, int len);
136typedef u8 rsacrypt[RSA_KEYLEN]; // encrypted challenge
137 140
138static inline void 141template<class T>
139rsa_encrypt (RSA *key, const rsaclear &chg, rsacrypt &encr) 142inline void rand_fill (T &t)
140{ 143{
141 if (RSA_public_encrypt (sizeof chg, 144 rand_fill (&t, sizeof (T));
142 (unsigned char *)&chg, (unsigned char *)&encr,
143 key, RSA_PKCS1_OAEP_PADDING) < 0)
144 fatal ("RSA_public_encrypt error");
145} 145}
146 146
147static inline bool 147/*****************************************************************************/
148rsa_decrypt (RSA *key, const rsacrypt &encr, rsaclear &chg) 148
149{ 149// run work_cb in another thread, call done_cb in main thread when finished
150 return RSA_private_decrypt (sizeof encr, 150// only one work_cb will execute at any one time.
151 (unsigned char *)&encr, (unsigned char *)&chg, 151void async (callback<void ()> work_cb, callback<void ()> done_cb);
152 key, RSA_PKCS1_OAEP_PADDING) > 0;
153}
154 152
155#endif 153#endif
156 154

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines