HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mr_q 0:d8f2f7d5f31b 1 /* Copyright (C) 2007 MySQL AB & Michael Widenius
mr_q 0:d8f2f7d5f31b 2
mr_q 0:d8f2f7d5f31b 3 This program is free software; you can redistribute it and/or modify
mr_q 0:d8f2f7d5f31b 4 it under the terms of the GNU General Public License as published by
mr_q 0:d8f2f7d5f31b 5 the Free Software Foundation; version 2 of the License.
mr_q 0:d8f2f7d5f31b 6
mr_q 0:d8f2f7d5f31b 7 This program is distributed in the hope that it will be useful,
mr_q 0:d8f2f7d5f31b 8 but WITHOUT ANY WARRANTY; without even the implied warranty of
mr_q 0:d8f2f7d5f31b 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mr_q 0:d8f2f7d5f31b 10 GNU General Public License for more details.
mr_q 0:d8f2f7d5f31b 11
mr_q 0:d8f2f7d5f31b 12 You should have received a copy of the GNU General Public License
mr_q 0:d8f2f7d5f31b 13 along with this program; if not, write to the Free Software
mr_q 0:d8f2f7d5f31b 14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
mr_q 0:d8f2f7d5f31b 15
mr_q 0:d8f2f7d5f31b 16 #define SCRAMBLE_LENGTH_323 8
mr_q 0:d8f2f7d5f31b 17
mr_q 0:d8f2f7d5f31b 18 #include <string.h>
mr_q 0:d8f2f7d5f31b 19 #include <math.h>
mr_q 0:d8f2f7d5f31b 20
mr_q 0:d8f2f7d5f31b 21 typedef unsigned int uint;
mr_q 0:d8f2f7d5f31b 22 typedef unsigned long ulong;
mr_q 0:d8f2f7d5f31b 23 typedef unsigned char uchar;
mr_q 0:d8f2f7d5f31b 24
mr_q 0:d8f2f7d5f31b 25 struct my_rnd_struct {
mr_q 0:d8f2f7d5f31b 26 unsigned long seed1,seed2,max_value;
mr_q 0:d8f2f7d5f31b 27 double max_value_dbl;
mr_q 0:d8f2f7d5f31b 28 };
mr_q 0:d8f2f7d5f31b 29
mr_q 0:d8f2f7d5f31b 30 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2);
mr_q 0:d8f2f7d5f31b 31 static double my_rnd(struct my_rnd_struct *rand_st);
mr_q 0:d8f2f7d5f31b 32 static void hash_password(ulong *result, const char *password, uint password_len);
mr_q 0:d8f2f7d5f31b 33
mr_q 0:d8f2f7d5f31b 34 /*
mr_q 0:d8f2f7d5f31b 35 Initialize random generator
mr_q 0:d8f2f7d5f31b 36
mr_q 0:d8f2f7d5f31b 37 NOTES
mr_q 0:d8f2f7d5f31b 38 MySQL's password checks depends on this, so don't do any changes
mr_q 0:d8f2f7d5f31b 39 that changes the random numbers that are generated!
mr_q 0:d8f2f7d5f31b 40 */
mr_q 0:d8f2f7d5f31b 41
mr_q 0:d8f2f7d5f31b 42 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2)
mr_q 0:d8f2f7d5f31b 43 {
mr_q 0:d8f2f7d5f31b 44 rand_st->max_value= 0x3FFFFFFFL;
mr_q 0:d8f2f7d5f31b 45 rand_st->max_value_dbl=(double) rand_st->max_value;
mr_q 0:d8f2f7d5f31b 46 rand_st->seed1=seed1%rand_st->max_value ;
mr_q 0:d8f2f7d5f31b 47 rand_st->seed2=seed2%rand_st->max_value;
mr_q 0:d8f2f7d5f31b 48 }
mr_q 0:d8f2f7d5f31b 49
mr_q 0:d8f2f7d5f31b 50 /*
mr_q 0:d8f2f7d5f31b 51 Generate random number.
mr_q 0:d8f2f7d5f31b 52
mr_q 0:d8f2f7d5f31b 53 SYNOPSIS
mr_q 0:d8f2f7d5f31b 54 my_rnd()
mr_q 0:d8f2f7d5f31b 55 rand_st INOUT Structure used for number generation
mr_q 0:d8f2f7d5f31b 56
mr_q 0:d8f2f7d5f31b 57 RETURN VALUE
mr_q 0:d8f2f7d5f31b 58 generated pseudo random number
mr_q 0:d8f2f7d5f31b 59 */
mr_q 0:d8f2f7d5f31b 60
mr_q 0:d8f2f7d5f31b 61 static double my_rnd(struct my_rnd_struct *rand_st)
mr_q 0:d8f2f7d5f31b 62 {
mr_q 0:d8f2f7d5f31b 63 rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
mr_q 0:d8f2f7d5f31b 64 rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
mr_q 0:d8f2f7d5f31b 65 return (((double) rand_st->seed1)/rand_st->max_value_dbl);
mr_q 0:d8f2f7d5f31b 66 }
mr_q 0:d8f2f7d5f31b 67
mr_q 0:d8f2f7d5f31b 68 /*
mr_q 0:d8f2f7d5f31b 69 Generate binary hash from raw text string
mr_q 0:d8f2f7d5f31b 70 Used for Pre-4.1 password handling
mr_q 0:d8f2f7d5f31b 71 SYNOPSIS
mr_q 0:d8f2f7d5f31b 72 hash_password()
mr_q 0:d8f2f7d5f31b 73 result OUT store hash in this location
mr_q 0:d8f2f7d5f31b 74 password IN plain text password to build hash
mr_q 0:d8f2f7d5f31b 75 password_len IN password length (password may be not null-terminated)
mr_q 0:d8f2f7d5f31b 76 */
mr_q 0:d8f2f7d5f31b 77
mr_q 0:d8f2f7d5f31b 78 static void hash_password(ulong *result, const char *password, uint password_len)
mr_q 0:d8f2f7d5f31b 79 {
mr_q 0:d8f2f7d5f31b 80 register ulong nr=1345345333L, add=7, nr2=0x12345671L;
mr_q 0:d8f2f7d5f31b 81 ulong tmp;
mr_q 0:d8f2f7d5f31b 82 const char *password_end= password + password_len;
mr_q 0:d8f2f7d5f31b 83 for (; password < password_end; password++)
mr_q 0:d8f2f7d5f31b 84 {
mr_q 0:d8f2f7d5f31b 85 if (*password == ' ' || *password == '\t')
mr_q 0:d8f2f7d5f31b 86 continue; /* skip space in password */
mr_q 0:d8f2f7d5f31b 87 tmp= (ulong) (uchar) *password;
mr_q 0:d8f2f7d5f31b 88 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
mr_q 0:d8f2f7d5f31b 89 nr2+=(nr2 << 8) ^ nr;
mr_q 0:d8f2f7d5f31b 90 add+=tmp;
mr_q 0:d8f2f7d5f31b 91 }
mr_q 0:d8f2f7d5f31b 92 result[0]=nr & (((ulong) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
mr_q 0:d8f2f7d5f31b 93 result[1]=nr2 & (((ulong) 1L << 31) -1L);
mr_q 0:d8f2f7d5f31b 94 }
mr_q 0:d8f2f7d5f31b 95
mr_q 0:d8f2f7d5f31b 96
mr_q 0:d8f2f7d5f31b 97
mr_q 0:d8f2f7d5f31b 98 /*
mr_q 0:d8f2f7d5f31b 99 Scramble string with password.
mr_q 0:d8f2f7d5f31b 100 Used in pre 4.1 authentication phase.
mr_q 0:d8f2f7d5f31b 101 SYNOPSIS
mr_q 0:d8f2f7d5f31b 102 scramble_323()
mr_q 0:d8f2f7d5f31b 103 to OUT Store scrambled message here. Buffer must be at least
mr_q 0:d8f2f7d5f31b 104 SCRAMBLE_LENGTH_323+1 bytes long
mr_q 0:d8f2f7d5f31b 105 message IN Message to scramble. Message must be at least
mr_q 0:d8f2f7d5f31b 106 SRAMBLE_LENGTH_323 bytes long.
mr_q 0:d8f2f7d5f31b 107 password IN Password to use while scrambling
mr_q 0:d8f2f7d5f31b 108 */
mr_q 0:d8f2f7d5f31b 109
mr_q 0:d8f2f7d5f31b 110 void scramble_323(char *to, const char *message, const char *password)
mr_q 0:d8f2f7d5f31b 111 {
mr_q 0:d8f2f7d5f31b 112 struct my_rnd_struct rand_st;
mr_q 0:d8f2f7d5f31b 113 ulong hash_pass[2], hash_message[2];
mr_q 0:d8f2f7d5f31b 114
mr_q 0:d8f2f7d5f31b 115 if (password && password[0])
mr_q 0:d8f2f7d5f31b 116 {
mr_q 0:d8f2f7d5f31b 117 char extra, *to_start=to;
mr_q 0:d8f2f7d5f31b 118 const char *message_end= message + SCRAMBLE_LENGTH_323;
mr_q 0:d8f2f7d5f31b 119 hash_password(hash_pass,password, (uint) strlen(password));
mr_q 0:d8f2f7d5f31b 120 hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
mr_q 0:d8f2f7d5f31b 121 my_rnd_init(&rand_st,hash_pass[0] ^ hash_message[0],
mr_q 0:d8f2f7d5f31b 122 hash_pass[1] ^ hash_message[1]);
mr_q 0:d8f2f7d5f31b 123 for (; message < message_end; message++)
mr_q 0:d8f2f7d5f31b 124 *to++= (char) (floor(my_rnd(&rand_st)*31)+64);
mr_q 0:d8f2f7d5f31b 125 extra=(char) (floor(my_rnd(&rand_st)*31));
mr_q 0:d8f2f7d5f31b 126 while (to_start != to)
mr_q 0:d8f2f7d5f31b 127 *(to_start++)^=extra;
mr_q 0:d8f2f7d5f31b 128 }
mr_q 0:d8f2f7d5f31b 129 *to= 0;
mr_q 0:d8f2f7d5f31b 130 }
mr_q 0:d8f2f7d5f31b 131