[1] | 1 | #include "globals.h"
|
---|
| 2 |
|
---|
| 3 | static AES_KEY aeskey;
|
---|
| 4 |
|
---|
| 5 | void aes_set_key(char *key)
|
---|
| 6 | {
|
---|
| 7 | AES_set_decrypt_key(key, 128, &aeskey);
|
---|
| 8 | AES_set_encrypt_key(key, 128, &client[cs_idx].aeskey);
|
---|
| 9 | }
|
---|
| 10 |
|
---|
| 11 | void aes_decrypt(uchar *buf, int n)
|
---|
| 12 | {
|
---|
| 13 | int i;
|
---|
| 14 | for(i=0; i<n; i+=16)
|
---|
| 15 | AES_decrypt(buf+i, buf+i, &aeskey);
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | void aes_encrypt_idx(int idx, uchar *buf, int n)
|
---|
| 19 | {
|
---|
| 20 | int i;
|
---|
| 21 | for(i=0; i<n; i+=16)
|
---|
| 22 | AES_encrypt(buf+i, buf+i, &client[idx].aeskey);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | char *remote_txt(void)
|
---|
| 26 | {
|
---|
| 27 | if (is_server)
|
---|
| 28 | return("client");
|
---|
| 29 | else
|
---|
| 30 | return("remote server");
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | char *trim(txt)
|
---|
| 34 | char *txt;
|
---|
| 35 | {
|
---|
| 36 | register int l;
|
---|
| 37 | register char *p1, *p2;
|
---|
| 38 |
|
---|
| 39 | if (*txt==' ')
|
---|
| 40 | {
|
---|
| 41 | for (p1=p2=txt;
|
---|
| 42 | (*p1==' ') || (*p1=='\t') || (*p1=='\n') || (*p1=='\r');
|
---|
| 43 | p1++);
|
---|
| 44 | while (*p1)
|
---|
| 45 | *p2++=*p1++;
|
---|
| 46 | *p2='\0';
|
---|
| 47 | }
|
---|
| 48 | if ((l=strlen(txt))>0)
|
---|
| 49 | for (p1=txt+l-1;
|
---|
| 50 | (*p1==' ') || (*p1=='\t') || (*p1=='\n') || (*p1=='\r');
|
---|
| 51 | *p1--='\0');
|
---|
| 52 |
|
---|
| 53 | return(txt);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | char *strtolower(char *txt)
|
---|
| 57 | {
|
---|
| 58 | char *p;
|
---|
| 59 | for (p=txt; *p; p++)
|
---|
| 60 | *p=tolower(*p);
|
---|
| 61 | return(txt);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | int gethexval(char c)
|
---|
| 65 | {
|
---|
| 66 | if ((c>='0') && (c<='9')) return(c-'0');
|
---|
| 67 | if ((c>='A') && (c<='F')) return(c-'A'+10);
|
---|
| 68 | if ((c>='a') && (c<='f')) return(c-'a'+10);
|
---|
| 69 | return(-1);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | int cs_atob(uchar *buf, char *asc, int n)
|
---|
| 73 | {
|
---|
| 74 | int i, rc;
|
---|
| 75 | for (i=0; i<n; i++)
|
---|
| 76 | {
|
---|
| 77 | if ((rc=(gethexval(asc[i<<1])<<4)|gethexval(asc[(i<<1)+1]))&0x100)
|
---|
| 78 | return(-1);
|
---|
| 79 | buf[i]=rc;
|
---|
| 80 | }
|
---|
| 81 | return(n);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | ulong cs_atoi(char *asc, int l, int val_on_err)
|
---|
| 85 | {
|
---|
| 86 | int i, n=0;
|
---|
| 87 | ulong rc=0;
|
---|
| 88 | for (i=((l-1)<<1), errno=0; (i>=0) && (n<4); i-=2)
|
---|
| 89 | {
|
---|
| 90 | int b;
|
---|
| 91 | b=(gethexval(asc[i])<<4) | gethexval(asc[i+1]);
|
---|
| 92 | if (b<0)
|
---|
| 93 | {
|
---|
| 94 | errno=EINVAL;
|
---|
| 95 | rc=(val_on_err) ? 0xFFFFFFFF : 0;
|
---|
| 96 | break;
|
---|
| 97 | }
|
---|
| 98 | rc|=b<<(n<<3);
|
---|
| 99 | n++;
|
---|
| 100 | }
|
---|
| 101 | return(rc);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | int byte_atob(char *asc)
|
---|
| 105 | {
|
---|
| 106 | int rc;
|
---|
| 107 |
|
---|
| 108 | if (strlen(trim(asc))!=2)
|
---|
| 109 | rc=(-1);
|
---|
| 110 | else
|
---|
| 111 | if ((rc=(gethexval(asc[0])<<4)|gethexval(asc[1]))&0x100)
|
---|
| 112 | rc=(-1);
|
---|
| 113 | return(rc);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | long word_atob(char *asc)
|
---|
| 117 | {
|
---|
| 118 | long rc;
|
---|
| 119 |
|
---|
| 120 | if (strlen(trim(asc))!=4)
|
---|
| 121 | rc=(-1);
|
---|
| 122 | else
|
---|
| 123 | {
|
---|
| 124 | rc=gethexval(asc[0])<<12 | gethexval(asc[1])<<8 |
|
---|
| 125 | gethexval(asc[2])<<4 | gethexval(asc[3]);
|
---|
| 126 | if (rc&0x10000)
|
---|
| 127 | rc=(-1);
|
---|
| 128 | }
|
---|
| 129 | return(rc);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | int key_atob(char *asc, uchar *bin)
|
---|
| 133 | {
|
---|
| 134 | int i, n1, n2, rc;
|
---|
| 135 | for (i=rc=0; i<32; i+=2)
|
---|
| 136 | {
|
---|
| 137 | if ((n1=gethexval(asc[i ]))<0) rc=(-1);
|
---|
| 138 | if ((n2=gethexval(asc[i+1]))<0) rc=(-1);
|
---|
| 139 | bin[i>>1]=(n1<<4)+(n2&0xff);
|
---|
| 140 | }
|
---|
| 141 | return(rc);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | int key_atob14(char *asc, uchar *bin)
|
---|
| 145 | {
|
---|
| 146 | int i, n1, n2, rc;
|
---|
| 147 | for (i=rc=0; i<28; i+=2)
|
---|
| 148 | {
|
---|
| 149 | if ((n1=gethexval(asc[i ]))<0) rc=(-1);
|
---|
| 150 | if ((n2=gethexval(asc[i+1]))<0) rc=(-1);
|
---|
| 151 | bin[i>>1]=(n1<<4)+(n2&0xff);
|
---|
| 152 | }
|
---|
| 153 | return(rc);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | char *key_btoa(char *asc, uchar *bin)
|
---|
| 157 | {
|
---|
| 158 | int i;//, n1, n2, rc;
|
---|
| 159 | static char buf[33];
|
---|
| 160 | if (!asc)
|
---|
| 161 | asc=buf;
|
---|
| 162 | for (i=0; i<16; i++)
|
---|
| 163 | sprintf(asc+(i<<1), "%02X", bin[i]);
|
---|
| 164 | return(asc);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | char *cs_hexdump(int m, uchar *buf, int n)
|
---|
| 168 | {
|
---|
| 169 | int i;
|
---|
| 170 | static char dump[520];
|
---|
| 171 |
|
---|
| 172 | dump[i=0]='\0';
|
---|
| 173 | m=(m)?3:2;
|
---|
| 174 | if (m*n>=sizeof(dump)) n=(sizeof(dump)/m)-1;
|
---|
| 175 | while (i<n)
|
---|
| 176 | sprintf(dump+(m*i++), "%02X%s", *buf++, (m>2)?" ":"");
|
---|
| 177 | return(dump);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | static int inet_byteorder=0;
|
---|
| 181 | in_addr_t cs_inet_order(in_addr_t n)
|
---|
| 182 | {
|
---|
| 183 | if (!inet_byteorder)
|
---|
| 184 | inet_byteorder=((inet_addr("1.2.3.4")+1)==inet_addr("1.2.3.5")) ? 1 : 2;
|
---|
| 185 | switch (inet_byteorder)
|
---|
| 186 | {
|
---|
| 187 | case 1:
|
---|
| 188 | break;
|
---|
| 189 | case 2:
|
---|
| 190 | n=((n&0xff000000) >> 24 ) |
|
---|
| 191 | ((n&0x00ff0000) >> 8 ) |
|
---|
| 192 | ((n&0x0000ff00) << 8 ) |
|
---|
| 193 | ((n&0x000000ff) << 24 );
|
---|
| 194 | break;
|
---|
| 195 | }
|
---|
| 196 | return(n);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | char *cs_inet_ntoa(in_addr_t n)
|
---|
| 200 | {
|
---|
| 201 | struct in_addr in;
|
---|
| 202 | in.s_addr=cs_inet_order(n);
|
---|
| 203 | return((char *)inet_ntoa(in));
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | in_addr_t cs_inet_addr(char *txt)
|
---|
| 207 | {
|
---|
| 208 | in_addr_t n;
|
---|
| 209 | if (!inet_byteorder)
|
---|
| 210 | inet_byteorder=((inet_addr("1.2.3.4")+1)==inet_addr("1.2.3.5")) ? 1 : 2;
|
---|
| 211 | switch (inet_byteorder)
|
---|
| 212 | {
|
---|
| 213 | case 1:
|
---|
| 214 | n=inet_addr(txt);
|
---|
| 215 | break;
|
---|
| 216 | case 2:
|
---|
| 217 | n=inet_network(txt);
|
---|
| 218 | break;
|
---|
| 219 | }
|
---|
| 220 | return(n);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | ulong b2i(int n, uchar *b)
|
---|
| 224 | {
|
---|
| 225 | switch(n)
|
---|
| 226 | {
|
---|
| 227 | case 2:
|
---|
| 228 | return ((b[0]<<8) | b[1]);
|
---|
| 229 | case 3:
|
---|
| 230 | return ((b[0]<<16) | (b[1]<<8) | b[2]);
|
---|
| 231 | case 4:
|
---|
| 232 | return ((b[0]<<24) | (b[1]<<16) | (b[2]<<8) | b[3]);
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | ullong b2ll(int n, uchar *b)
|
---|
| 237 | {
|
---|
| 238 | int i;
|
---|
| 239 | ullong k=0;
|
---|
| 240 | for(i=0; i<n; k+=b[i++])
|
---|
| 241 | k<<=8;
|
---|
| 242 | return(k);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | uchar *i2b(int n, ulong i)
|
---|
| 246 | {
|
---|
| 247 | static uchar b[4];
|
---|
| 248 | switch(n)
|
---|
| 249 | {
|
---|
| 250 | case 2:
|
---|
| 251 | b[0]=(i>> 8) & 0xff;
|
---|
| 252 | b[1]=(i ) & 0xff;
|
---|
| 253 | break;
|
---|
| 254 | case 3:
|
---|
| 255 | b[0]=(i>>16) & 0xff;
|
---|
| 256 | b[1]=(i>> 8) & 0xff;
|
---|
| 257 | b[2]=(i ) & 0xff;
|
---|
| 258 | case 4:
|
---|
| 259 | b[0]=(i>>24) & 0xff;
|
---|
| 260 | b[1]=(i>>16) & 0xff;
|
---|
| 261 | b[2]=(i>> 8) & 0xff;
|
---|
| 262 | b[3]=(i ) & 0xff;
|
---|
| 263 | break;
|
---|
| 264 | }
|
---|
| 265 | return(b);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | ulong a2i(char *asc, int bytes)
|
---|
| 269 | {
|
---|
| 270 | int i, n;
|
---|
| 271 | ulong rc;
|
---|
| 272 | for (rc=i=0, n=strlen(trim(asc))-1; i<(abs(bytes)<<1); n--, i++)
|
---|
| 273 | if (n>=0)
|
---|
| 274 | {
|
---|
| 275 | int rcl;
|
---|
| 276 | if ((rcl=gethexval(asc[n]))<0)
|
---|
| 277 | {
|
---|
| 278 | errno=EINVAL;
|
---|
| 279 | return(0x1F1F1F);
|
---|
| 280 | }
|
---|
| 281 | rc|=(rcl<<(i<<2));
|
---|
| 282 | }
|
---|
| 283 | else
|
---|
| 284 | if (bytes<0)
|
---|
| 285 | rc|=(0xf<<(i<<2));
|
---|
| 286 | errno=0;
|
---|
| 287 | return(rc);
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | int boundary(int exp, int n)
|
---|
| 291 | {
|
---|
| 292 | return((((n-1)>>exp)+1)<<exp);
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | void cs_ftime(struct timeb *tp)
|
---|
| 296 | {
|
---|
| 297 | #ifdef NO_FTIME
|
---|
| 298 | struct timeval tv;
|
---|
| 299 | gettimeofday(&tv, (struct timezone *)0);
|
---|
| 300 | tp->time=tv.tv_sec;
|
---|
| 301 | tp->millitm=tv.tv_usec/1000;
|
---|
| 302 | #else
|
---|
| 303 | ftime(tp);
|
---|
| 304 | #endif
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | void cs_sleepms(int msec)
|
---|
| 308 | {
|
---|
| 309 | struct timeval tv;
|
---|
| 310 | tv.tv_sec = msec / 1000;
|
---|
| 311 | tv.tv_usec = (msec % 1000) * 1000;
|
---|
| 312 | select(0, 0, 0, 0, &tv);
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | int bytes_available(int fd)
|
---|
| 316 | {
|
---|
| 317 | struct pollfd pfds;
|
---|
| 318 | pfds.fd=fd;
|
---|
| 319 | pfds.events=POLLIN;
|
---|
| 320 | pfds.revents=0;
|
---|
| 321 | if (poll(&pfds, 1, 0)!=1)
|
---|
| 322 | return(0);
|
---|
| 323 | else
|
---|
| 324 | return(((pfds.revents)&POLLIN)==POLLIN);
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 |
|
---|
| 328 | #ifdef OS_CYGWIN32
|
---|
| 329 | #include <windows.h>
|
---|
| 330 | void cs_setpriority(int prio)
|
---|
| 331 | {
|
---|
| 332 | HANDLE WinId;
|
---|
| 333 | ulong wprio;
|
---|
| 334 | switch((prio+20)/10)
|
---|
| 335 | {
|
---|
| 336 | case 0: wprio=REALTIME_PRIORITY_CLASS; break;
|
---|
| 337 | case 1: wprio=HIGH_PRIORITY_CLASS; break;
|
---|
| 338 | case 2: wprio=NORMAL_PRIORITY_CLASS; break;
|
---|
| 339 | default: wprio=IDLE_PRIORITY_CLASS; break;
|
---|
| 340 | }
|
---|
| 341 | WinId=GetCurrentProcess();
|
---|
| 342 | SetPriorityClass(WinId, wprio);
|
---|
| 343 | }
|
---|
| 344 | #else
|
---|
| 345 | void cs_setpriority(int prio)
|
---|
| 346 | {
|
---|
| 347 | #ifdef PRIO_PROCESS
|
---|
| 348 | setpriority(PRIO_PROCESS, 0, prio); // ignore errors
|
---|
| 349 | #endif
|
---|
| 350 | }
|
---|
| 351 | #endif
|
---|