patch #6736, part II
[mldonkey.git] / src / networks / fasttrack / fst_crypt_ml.c
blobad1c266d4027540c30774f1517a2810004b6d2a0
1 /* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA */
2 /*
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "fst_crypt.h"
22 #include <sys/types.h>
23 #include <unistd.h>
25 #include "caml/mlvalues.h"
26 #include "caml/fail.h"
28 #if defined(__MINGW32__)
29 typedef unsigned int uint;
30 #include <winsock.h>
31 #else
32 #include <netinet/in.h>
33 #endif
34 /************************************************************************/
37 /* Functions for MLdonkey */
40 /************************************************************************/
42 value ml_create_cipher(value unit)
44 return (value) fst_cipher_create();
47 value ml_apply_cipher(value cipher_v, value s_v, value pos_v, value len_v)
49 FSTCipher* cipher = (FSTCipher*) cipher_v;
50 char *s = String_val(s_v);
51 int pos = Int_val(pos_v);
52 int len = Int_val(len_v);
54 /* printf("Apply cipher %X on %d [from %d]\n", cipher, len, pos); */
56 fst_cipher_crypt(cipher, s+pos, len);
58 return Val_unit;
61 value ml_init_cipher(value cipher_v)
63 FSTCipher* cipher = (FSTCipher*) cipher_v;
64 fst_cipher_init(cipher, cipher->seed, cipher->enc_type);
66 return Val_unit;
69 value ml_set_cipher(value cipher_v, value seed_v, value encode_v)
71 FSTCipher* cipher = (FSTCipher*) cipher_v;
72 unsigned int seed = Int32_val(seed_v);
73 unsigned int encode = Int_val(encode_v);
75 /* printf("ml_set_cipher %X seed:%X enc_type: %X\n", cipher, seed, encode); */
77 cipher->enc_type = encode;
78 cipher->seed = seed;
80 return Val_unit;
83 value ml_cipher_packet_get(value s_v, value pos_v, value in_cipher_v)
85 FSTCipher* in_cipher = (FSTCipher*) in_cipher_v;
86 char *s = String_val(s_v);
87 int pos = Int_val(pos_v);
88 unsigned int seed;
89 unsigned int enc_type;
92 printf("ml_cipher_packet_get IN:%X OUT:%X pos %d\n", in_cipher, out_cipher, pos);
94 printf("out seed:%X enc_type: %X\n", out_cipher->seed, out_cipher->enc_type);
97 seed = htonl (((unsigned int*)(s+pos))[0]);
98 enc_type = htonl (((unsigned int*)(s+pos+4))[0]);
99 enc_type = fst_cipher_mangle_enc_type (seed, enc_type);
101 in_cipher->seed = seed;
102 in_cipher->enc_type = enc_type;
103 /* printf("in seed:%X enc_type: %X\n", seed, out_cipher->enc_type); */
105 /* if(enc_type > 0x29) failwith ("ERROR: unsupported encryption"); */
107 return Val_unit;
110 value ml_xor_ciphers(value out_cipher_v, value in_cipher_v){
111 FSTCipher* in_cipher = (FSTCipher*) in_cipher_v;
112 FSTCipher* out_cipher = (FSTCipher*) out_cipher_v;
114 out_cipher->seed ^= in_cipher->seed; /* xor send cipher with received seed */
116 /* the correct behaviour here is to use the enc_type the supernode sent
117 * us for out_cipher too.
119 out_cipher->enc_type = in_cipher->enc_type;
121 return Val_unit;
124 value ml_xor_ciphers2(value out_cipher_v, value in_cipher_v){
125 FSTCipher* in_cipher = (FSTCipher*) in_cipher_v;
126 FSTCipher* out_cipher = (FSTCipher*) out_cipher_v;
127 unsigned int seed = out_cipher->seed;
129 out_cipher->seed ^= in_cipher->seed; /* xor send cipher with received seed */
130 in_cipher->seed ^= seed;
131 fst_cipher_init(out_cipher, out_cipher->seed, out_cipher->enc_type);
133 return Val_unit;
137 value ml_cipher_packet_set(value cipher_v, value s_v, value pos_v)
139 FSTCipher* cipher = (FSTCipher*) cipher_v;
140 char *s = String_val(s_v);
141 int pos = Int_val(pos_v);
143 ((unsigned int*)(s+pos))[0] = htonl(cipher->seed);
144 ((unsigned int*)(s+pos+4))[0] = htonl(
145 fst_cipher_mangle_enc_type(cipher->seed, cipher->enc_type));
147 return Val_unit;
150 value ml_cipher_enc_type(value cipher_v)
152 FSTCipher* cipher = (FSTCipher*) cipher_v;
154 return Val_int(cipher->enc_type);
157 value ml_cipher_packet_set_xored(value cipher_v, value s_v, value pos_v, value xor_cipher_v)
159 FSTCipher* cipher = (FSTCipher*) cipher_v;
160 FSTCipher* xor_cipher = (FSTCipher*) xor_cipher_v;
161 char *s = String_val(s_v);
162 int pos = Int_val(pos_v);
163 unsigned int seed = cipher->seed;
165 seed ^= xor_cipher->seed;
166 ((unsigned int*)(s+pos))[0] = htonl(seed);
167 ((unsigned int*)(s+pos+4))[0] = htonl(
168 fst_cipher_mangle_enc_type(cipher->seed, cipher->enc_type));
170 return Val_unit;
174 value ml_cipher_free(value cipher_v)
176 FSTCipher* cipher = (FSTCipher*) cipher_v;
177 fst_cipher_free(cipher);
179 return Val_unit;