diff options
author | Laxmikant Rashinkar <LK.Rashinkar@gmail.com> | 2012-09-19 20:51:34 -0700 |
---|---|---|
committer | Laxmikant Rashinkar <LK.Rashinkar@gmail.com> | 2012-09-19 20:51:34 -0700 |
commit | 1123323fda6d128fb98b0427e0ea5f6a2dc9e632 (patch) | |
tree | 3407a3771a069f812554312ce7c36db625139cc2 /common | |
parent | 3cedfae76a2351bc8b1e5bd2ee33bbf8630dbacf (diff) | |
download | xrdp-proprietary-1123323fda6d128fb98b0427e0ea5f6a2dc9e632.tar.gz xrdp-proprietary-1123323fda6d128fb98b0427e0ea5f6a2dc9e632.zip |
o moved from GNU General Public License to Apache License, Version 2.0
o applied new coding standards to all .c files
o moved some files around
Diffstat (limited to 'common')
-rw-r--r-- | common/arch.h | 39 | ||||
-rw-r--r-- | common/d3des.c | 744 | ||||
-rw-r--r-- | common/defines.h | 40 | ||||
-rw-r--r-- | common/file.c | 568 | ||||
-rw-r--r-- | common/file.h | 42 | ||||
-rw-r--r-- | common/file_loc.h | 40 | ||||
-rw-r--r-- | common/list.c | 299 | ||||
-rw-r--r-- | common/list.h | 42 | ||||
-rw-r--r-- | common/log.c | 931 | ||||
-rw-r--r-- | common/log.h | 39 | ||||
-rw-r--r-- | common/os_calls.c | 2892 | ||||
-rw-r--r-- | common/os_calls.h | 42 | ||||
-rw-r--r-- | common/parse.h | 50 | ||||
-rw-r--r-- | common/ssl_calls.c | 471 | ||||
-rw-r--r-- | common/ssl_calls.h | 36 | ||||
-rw-r--r-- | common/thread_calls.c | 177 | ||||
-rw-r--r-- | common/thread_calls.h | 43 | ||||
-rw-r--r-- | common/trans.c | 693 | ||||
-rw-r--r-- | common/trans.h | 43 | ||||
-rw-r--r-- | common/xrdp_client_info.h | 43 |
20 files changed, 3817 insertions, 3457 deletions
diff --git a/common/arch.h b/common/arch.h index a380511b..b8780926 100644 --- a/common/arch.h +++ b/common/arch.h @@ -1,25 +1,20 @@ -/* - Copyright (c) 2004-2010 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #if !defined(ARCH_H) #define ARCH_H diff --git a/common/d3des.c b/common/d3des.c index 16ec35df..16ee7cde 100644 --- a/common/d3des.c +++ b/common/d3des.c @@ -38,393 +38,471 @@ static unsigned long KnL[32] = { 0L }; static unsigned long KnR[32] = { 0L }; static unsigned long Kn3[32] = { 0L }; static unsigned char Df_Key[24] = { - 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, - 0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10, - 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67 }; + 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, + 0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10, + 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67 }; */ -static unsigned short bytebit[8] = { - 01, 02, 04, 010, 020, 040, 0100, 0200 }; +static unsigned short bytebit[8] = +{ + 01, 02, 04, 010, 020, 040, 0100, 0200 +}; -static unsigned long bigbyte[24] = { - 0x800000L, 0x400000L, 0x200000L, 0x100000L, - 0x80000L, 0x40000L, 0x20000L, 0x10000L, - 0x8000L, 0x4000L, 0x2000L, 0x1000L, - 0x800L, 0x400L, 0x200L, 0x100L, - 0x80L, 0x40L, 0x20L, 0x10L, - 0x8L, 0x4L, 0x2L, 0x1L }; +static unsigned long bigbyte[24] = +{ + 0x800000L, 0x400000L, 0x200000L, 0x100000L, + 0x80000L, 0x40000L, 0x20000L, 0x10000L, + 0x8000L, 0x4000L, 0x2000L, 0x1000L, + 0x800L, 0x400L, 0x200L, 0x100L, + 0x80L, 0x40L, 0x20L, 0x10L, + 0x8L, 0x4L, 0x2L, 0x1L +}; /* Use the key schedule specified in the Standard (ANSI X3.92-1981). */ -static unsigned char pc1[56] = { - 56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, - 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, - 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, - 13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3 }; +static unsigned char pc1[56] = +{ + 56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, + 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, + 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, + 13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3 +}; -static unsigned char totrot[16] = { - 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 }; +static unsigned char totrot[16] = +{ + 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28 +}; -static unsigned char pc2[48] = { - 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, - 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, - 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, - 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 }; +static unsigned char pc2[48] = +{ + 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, + 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, + 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, + 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 +}; /* Thanks to James Gillogly & Phil Karn! */ void rfbDesKey(unsigned char *key, int edf) { - register int i, j, l, m, n; - unsigned char pc1m[56], pcr[56]; - unsigned long kn[32]; - - for ( j = 0; j < 56; j++ ) { - l = pc1[j]; - m = l & 07; - pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0; - } - for( i = 0; i < 16; i++ ) { - if( edf == DE1 ) m = (15 - i) << 1; - else m = i << 1; - n = m + 1; - kn[m] = kn[n] = 0L; - for( j = 0; j < 28; j++ ) { - l = j + totrot[i]; - if( l < 28 ) pcr[j] = pc1m[l]; - else pcr[j] = pc1m[l - 28]; - } - for( j = 28; j < 56; j++ ) { - l = j + totrot[i]; - if( l < 56 ) pcr[j] = pc1m[l]; - else pcr[j] = pc1m[l - 28]; - } - for( j = 0; j < 24; j++ ) { - if( pcr[pc2[j]] ) kn[m] |= bigbyte[j]; - if( pcr[pc2[j+24]] ) kn[n] |= bigbyte[j]; - } - } - cookey(kn); - return; - } + register int i, j, l, m, n; + unsigned char pc1m[56], pcr[56]; + unsigned long kn[32]; + + for ( j = 0; j < 56; j++ ) + { + l = pc1[j]; + m = l & 07; + pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0; + } + + for ( i = 0; i < 16; i++ ) + { + if ( edf == DE1 ) + { + m = (15 - i) << 1; + } + else + { + m = i << 1; + } + + n = m + 1; + kn[m] = kn[n] = 0L; + + for ( j = 0; j < 28; j++ ) + { + l = j + totrot[i]; + + if ( l < 28 ) + { + pcr[j] = pc1m[l]; + } + else + { + pcr[j] = pc1m[l - 28]; + } + } + + for ( j = 28; j < 56; j++ ) + { + l = j + totrot[i]; + + if ( l < 56 ) + { + pcr[j] = pc1m[l]; + } + else + { + pcr[j] = pc1m[l - 28]; + } + } + + for ( j = 0; j < 24; j++ ) + { + if ( pcr[pc2[j]] ) + { + kn[m] |= bigbyte[j]; + } + + if ( pcr[pc2[j + 24]] ) + { + kn[n] |= bigbyte[j]; + } + } + } + + cookey(kn); + return; +} static void cookey(register unsigned long *raw1) { - register unsigned long *cook, *raw0; - unsigned long dough[32]; - register int i; - - cook = dough; - for( i = 0; i < 16; i++, raw1++ ) { - raw0 = raw1++; - *cook = (*raw0 & 0x00fc0000L) << 6; - *cook |= (*raw0 & 0x00000fc0L) << 10; - *cook |= (*raw1 & 0x00fc0000L) >> 10; - *cook++ |= (*raw1 & 0x00000fc0L) >> 6; - *cook = (*raw0 & 0x0003f000L) << 12; - *cook |= (*raw0 & 0x0000003fL) << 16; - *cook |= (*raw1 & 0x0003f000L) >> 4; - *cook++ |= (*raw1 & 0x0000003fL); - } - rfbUseKey(dough); - return; - } + register unsigned long *cook, *raw0; + unsigned long dough[32]; + register int i; + + cook = dough; + + for ( i = 0; i < 16; i++, raw1++ ) + { + raw0 = raw1++; + *cook = (*raw0 & 0x00fc0000L) << 6; + *cook |= (*raw0 & 0x00000fc0L) << 10; + *cook |= (*raw1 & 0x00fc0000L) >> 10; + *cook++ |= (*raw1 & 0x00000fc0L) >> 6; + *cook = (*raw0 & 0x0003f000L) << 12; + *cook |= (*raw0 & 0x0000003fL) << 16; + *cook |= (*raw1 & 0x0003f000L) >> 4; + *cook++ |= (*raw1 & 0x0000003fL); + } + + rfbUseKey(dough); + return; +} void rfbCPKey(register unsigned long *into) { - register unsigned long *from, *endp; + register unsigned long *from, *endp; - from = KnL, endp = &KnL[32]; - while( from < endp ) *into++ = *from++; - return; - } + from = KnL, endp = &KnL[32]; + + while ( from < endp ) + { + *into++ = *from++; + } + + return; +} void rfbUseKey(register unsigned long *from) { - register unsigned long *to, *endp; + register unsigned long *to, *endp; + + to = KnL, endp = &KnL[32]; + + while ( to < endp ) + { + *to++ = *from++; + } - to = KnL, endp = &KnL[32]; - while( to < endp ) *to++ = *from++; - return; - } + return; +} void rfbDes(unsigned char *inblock, unsigned char *outblock) { - unsigned long work[2]; + unsigned long work[2]; - scrunch(inblock, work); - desfunc(work, KnL); - unscrun(work, outblock); - return; - } + scrunch(inblock, work); + desfunc(work, KnL); + unscrun(work, outblock); + return; +} static void scrunch(register unsigned char *outof, register unsigned long *into) { - *into = (*outof++ & 0xffL) << 24; - *into |= (*outof++ & 0xffL) << 16; - *into |= (*outof++ & 0xffL) << 8; - *into++ |= (*outof++ & 0xffL); - *into = (*outof++ & 0xffL) << 24; - *into |= (*outof++ & 0xffL) << 16; - *into |= (*outof++ & 0xffL) << 8; - *into |= (*outof & 0xffL); - return; - } + *into = (*outof++ & 0xffL) << 24; + *into |= (*outof++ & 0xffL) << 16; + *into |= (*outof++ & 0xffL) << 8; + *into++ |= (*outof++ & 0xffL); + *into = (*outof++ & 0xffL) << 24; + *into |= (*outof++ & 0xffL) << 16; + *into |= (*outof++ & 0xffL) << 8; + *into |= (*outof & 0xffL); + return; +} static void unscrun(register unsigned long *outof, register unsigned char *into) { - *into++ = (unsigned char)((*outof >> 24) & 0xffL); - *into++ = (unsigned char)((*outof >> 16) & 0xffL); - *into++ = (unsigned char)((*outof >> 8) & 0xffL); - *into++ = (unsigned char)( *outof++ & 0xffL); - *into++ = (unsigned char)((*outof >> 24) & 0xffL); - *into++ = (unsigned char)((*outof >> 16) & 0xffL); - *into++ = (unsigned char)((*outof >> 8) & 0xffL); - *into = (unsigned char)( *outof & 0xffL); - return; - } - -static unsigned long SP1[64] = { - 0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L, - 0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L, - 0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L, - 0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L, - 0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L, - 0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L, - 0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L, - 0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L, - 0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L, - 0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L, - 0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L, - 0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L, - 0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L, - 0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L, - 0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L, - 0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L }; - -static unsigned long SP2[64] = { - 0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L, - 0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L, - 0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L, - 0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L, - 0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L, - 0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L, - 0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L, - 0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L, - 0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L, - 0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L, - 0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L, - 0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L, - 0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L, - 0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L, - 0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L, - 0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L }; - -static unsigned long SP3[64] = { - 0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L, - 0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L, - 0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L, - 0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L, - 0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L, - 0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L, - 0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L, - 0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L, - 0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L, - 0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L, - 0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L, - 0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L, - 0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L, - 0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L, - 0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L, - 0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L }; - -static unsigned long SP4[64] = { - 0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L, - 0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L, - 0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L, - 0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L, - 0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L, - 0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L, - 0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L, - 0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L, - 0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L, - 0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L, - 0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L, - 0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L, - 0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L, - 0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L, - 0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L, - 0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L }; - -static unsigned long SP5[64] = { - 0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L, - 0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L, - 0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L, - 0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L, - 0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L, - 0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L, - 0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L, - 0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L, - 0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L, - 0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L, - 0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L, - 0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L, - 0x42080100L, 0x00080100L, 0x42000000L, 0x42080100L, - 0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L, - 0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L, - 0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L }; - -static unsigned long SP6[64] = { - 0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L, - 0x20400000L, 0x00000010L, 0x20404010L, 0x00400000L, - 0x20004000L, 0x00404010L, 0x00400000L, 0x20000010L, - 0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L, - 0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L, - 0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L, - 0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L, - 0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L, - 0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L, - 0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L, - 0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L, - 0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L, - 0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L, - 0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L, - 0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L, - 0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L }; - -static unsigned long SP7[64] = { - 0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L, - 0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L, - 0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L, - 0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L, - 0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L, - 0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L, - 0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L, - 0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L, - 0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L, - 0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L, - 0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L, - 0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L, - 0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L, - 0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L, - 0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L, - 0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L }; - -static unsigned long SP8[64] = { - 0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L, - 0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L, - 0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L, - 0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L, - 0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L, - 0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L, - 0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L, - 0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L, - 0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L, - 0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L, - 0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L, - 0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L, - 0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L, - 0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L, - 0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L, - 0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L }; - -static void desfunc(register unsigned long* block, register unsigned long *keys) + *into++ = (unsigned char)((*outof >> 24) & 0xffL); + *into++ = (unsigned char)((*outof >> 16) & 0xffL); + *into++ = (unsigned char)((*outof >> 8) & 0xffL); + *into++ = (unsigned char)( *outof++ & 0xffL); + *into++ = (unsigned char)((*outof >> 24) & 0xffL); + *into++ = (unsigned char)((*outof >> 16) & 0xffL); + *into++ = (unsigned char)((*outof >> 8) & 0xffL); + *into = (unsigned char)( *outof & 0xffL); + return; +} + +static unsigned long SP1[64] = +{ + 0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L, + 0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L, + 0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L, + 0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L, + 0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L, + 0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L, + 0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L, + 0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L, + 0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L, + 0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L, + 0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L, + 0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L, + 0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L, + 0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L, + 0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L, + 0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L +}; + +static unsigned long SP2[64] = +{ + 0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L, + 0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L, + 0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L, + 0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L, + 0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L, + 0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L, + 0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L, + 0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L, + 0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L, + 0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L, + 0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L, + 0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L, + 0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L, + 0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L, + 0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L, + 0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L +}; + +static unsigned long SP3[64] = +{ + 0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L, + 0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L, + 0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L, + 0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L, + 0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L, + 0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L, + 0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L, + 0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L, + 0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L, + 0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L, + 0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L, + 0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L, + 0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L, + 0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L, + 0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L, + 0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L +}; + +static unsigned long SP4[64] = +{ + 0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L, + 0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L, + 0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L, + 0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L, + 0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L, + 0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L, + 0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L, + 0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L, + 0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L, + 0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L, + 0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L, + 0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L, + 0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L, + 0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L, + 0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L, + 0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L +}; + +static unsigned long SP5[64] = +{ + 0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L, + 0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L, + 0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L, + 0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L, + 0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L, + 0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L, + 0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L, + 0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L, + 0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L, + 0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L, + 0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L, + 0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L, + 0x42080100L, 0x00080100L, 0x42000000L, 0x42080100L, + 0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L, + 0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L, + 0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L +}; + +static unsigned long SP6[64] = +{ + 0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L, + 0x20400000L, 0x00000010L, 0x20404010L, 0x00400000L, + 0x20004000L, 0x00404010L, 0x00400000L, 0x20000010L, + 0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L, + 0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L, + 0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L, + 0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L, + 0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L, + 0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L, + 0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L, + 0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L, + 0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L, + 0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L, + 0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L, + 0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L, + 0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L +}; + +static unsigned long SP7[64] = +{ + 0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L, + 0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L, + 0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L, + 0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L, + 0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L, + 0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L, + 0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L, + 0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L, + 0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L, + 0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L, + 0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L, + 0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L, + 0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L, + 0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L, + 0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L, + 0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L +}; + +static unsigned long SP8[64] = +{ + 0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L, + 0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L, + 0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L, + 0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L, + 0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L, + 0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L, + 0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L, + 0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L, + 0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L, + 0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L, + 0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L, + 0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L, + 0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L, + 0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L, + 0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L, + 0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L +}; + +static void desfunc(register unsigned long *block, register unsigned long *keys) { - register unsigned long fval, work, right, leftt; - register int round; - - leftt = block[0]; - right = block[1]; - work = ((leftt >> 4) ^ right) & 0x0f0f0f0fL; - right ^= work; - leftt ^= (work << 4); - work = ((leftt >> 16) ^ right) & 0x0000ffffL; - right ^= work; - leftt ^= (work << 16); - work = ((right >> 2) ^ leftt) & 0x33333333L; - leftt ^= work; - right ^= (work << 2); - work = ((right >> 8) ^ leftt) & 0x00ff00ffL; - leftt ^= work; - right ^= (work << 8); - right = ((right << 1) | ((right >> 31) & 1L)) & 0xffffffffL; - work = (leftt ^ right) & 0xaaaaaaaaL; - leftt ^= work; - right ^= work; - leftt = ((leftt << 1) | ((leftt >> 31) & 1L)) & 0xffffffffL; - - for( round = 0; round < 8; round++ ) { - work = (right << 28) | (right >> 4); - work ^= *keys++; - fval = SP7[ work & 0x3fL]; - fval |= SP5[(work >> 8) & 0x3fL]; - fval |= SP3[(work >> 16) & 0x3fL]; - fval |= SP1[(work >> 24) & 0x3fL]; - work = right ^ *keys++; - fval |= SP8[ work & 0x3fL]; - fval |= SP6[(work >> 8) & 0x3fL]; - fval |= SP4[(work >> 16) & 0x3fL]; - fval |= SP2[(work >> 24) & 0x3fL]; - leftt ^= fval; - work = (leftt << 28) | (leftt >> 4); - work ^= *keys++; - fval = SP7[ work & 0x3fL]; - fval |= SP5[(work >> 8) & 0x3fL]; - fval |= SP3[(work >> 16) & 0x3fL]; - fval |= SP1[(work >> 24) & 0x3fL]; - work = leftt ^ *keys++; - fval |= SP8[ work & 0x3fL]; - fval |= SP6[(work >> 8) & 0x3fL]; - fval |= SP4[(work >> 16) & 0x3fL]; - fval |= SP2[(work >> 24) & 0x3fL]; - right ^= fval; - } - - right = (right << 31) | (right >> 1); - work = (leftt ^ right) & 0xaaaaaaaaL; - leftt ^= work; - right ^= work; - leftt = (leftt << 31) | (leftt >> 1); - work = ((leftt >> 8) ^ right) & 0x00ff00ffL; - right ^= work; - leftt ^= (work << 8); - work = ((leftt >> 2) ^ right) & 0x33333333L; - right ^= work; - leftt ^= (work << 2); - work = ((right >> 16) ^ leftt) & 0x0000ffffL; - leftt ^= work; - right ^= (work << 16); - work = ((right >> 4) ^ leftt) & 0x0f0f0f0fL; - leftt ^= work; - right ^= (work << 4); - *block++ = right; - *block = leftt; - return; - } + register unsigned long fval, work, right, leftt; + register int round; + + leftt = block[0]; + right = block[1]; + work = ((leftt >> 4) ^ right) & 0x0f0f0f0fL; + right ^= work; + leftt ^= (work << 4); + work = ((leftt >> 16) ^ right) & 0x0000ffffL; + right ^= work; + leftt ^= (work << 16); + work = ((right >> 2) ^ leftt) & 0x33333333L; + leftt ^= work; + right ^= (work << 2); + work = ((right >> 8) ^ leftt) & 0x00ff00ffL; + leftt ^= work; + right ^= (work << 8); + right = ((right << 1) | ((right >> 31) & 1L)) & 0xffffffffL; + work = (leftt ^ right) & 0xaaaaaaaaL; + leftt ^= work; + right ^= work; + leftt = ((leftt << 1) | ((leftt >> 31) & 1L)) & 0xffffffffL; + + for ( round = 0; round < 8; round++ ) + { + work = (right << 28) | (right >> 4); + work ^= *keys++; + fval = SP7[ work & 0x3fL]; + fval |= SP5[(work >> 8) & 0x3fL]; + fval |= SP3[(work >> 16) & 0x3fL]; + fval |= SP1[(work >> 24) & 0x3fL]; + work = right ^ *keys++; + fval |= SP8[ work & 0x3fL]; + fval |= SP6[(work >> 8) & 0x3fL]; + fval |= SP4[(work >> 16) & 0x3fL]; + fval |= SP2[(work >> 24) & 0x3fL]; + leftt ^= fval; + work = (leftt << 28) | (leftt >> 4); + work ^= *keys++; + fval = SP7[ work & 0x3fL]; + fval |= SP5[(work >> 8) & 0x3fL]; + fval |= SP3[(work >> 16) & 0x3fL]; + fval |= SP1[(work >> 24) & 0x3fL]; + work = leftt ^ *keys++; + fval |= SP8[ work & 0x3fL]; + fval |= SP6[(work >> 8) & 0x3fL]; + fval |= SP4[(work >> 16) & 0x3fL]; + fval |= SP2[(work >> 24) & 0x3fL]; + right ^= fval; + } + + right = (right << 31) | (right >> 1); + work = (leftt ^ right) & 0xaaaaaaaaL; + leftt ^= work; + right ^= work; + leftt = (leftt << 31) | (leftt >> 1); + work = ((leftt >> 8) ^ right) & 0x00ff00ffL; + right ^= work; + leftt ^= (work << 8); + work = ((leftt >> 2) ^ right) & 0x33333333L; + right ^= work; + leftt ^= (work << 2); + work = ((right >> 16) ^ leftt) & 0x0000ffffL; + leftt ^= work; + right ^= (work << 16); + work = ((right >> 4) ^ leftt) & 0x0f0f0f0fL; + leftt ^= work; + right ^= (work << 4); + *block++ = right; + *block = leftt; + return; +} /* Validation sets: * * Single-length key, single-length plaintext - - * Key : 0123 4567 89ab cdef + * Key : 0123 4567 89ab cdef * Plain : 0123 4567 89ab cde7 * Cipher : c957 4425 6a5e d31d * * Double-length key, single-length plaintext - - * Key : 0123 4567 89ab cdef fedc ba98 7654 3210 + * Key : 0123 4567 89ab cdef fedc ba98 7654 3210 * Plain : 0123 4567 89ab cde7 * Cipher : 7f1d 0a77 826b 8aff * * Double-length key, double-length plaintext - - * Key : 0123 4567 89ab cdef fedc ba98 7654 3210 + * Key : 0123 4567 89ab cdef fedc ba98 7654 3210 * Plain : 0123 4567 89ab cdef 0123 4567 89ab cdff * Cipher : 27a0 8440 406a df60 278f 47cf 42d6 15d7 * * Triple-length key, single-length plaintext - - * Key : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567 + * Key : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567 * Plain : 0123 4567 89ab cde7 * Cipher : de0b 7c06 ae5e 0ed5 * * Triple-length key, double-length plaintext - - * Key : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567 + * Key : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567 * Plain : 0123 4567 89ab cdef 0123 4567 89ab cdff * Cipher : ad0d 1b30 ac17 cf07 0ed1 1c63 81e4 4de5 * diff --git a/common/defines.h b/common/defines.h index 8e2328a7..502a41e8 100644 --- a/common/defines.h +++ b/common/defines.h @@ -1,24 +1,22 @@ -/* - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - xrdp: A Remote Desktop Protocol server. - Copyright (C) Jay Sorg 2004-2009 - - main define/macro file - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * main define/macro file + */ #ifndef DEFINES_H #define DEFINES_H diff --git a/common/file.c b/common/file.c index 808d94dc..c8be7af5 100644 --- a/common/file.c +++ b/common/file.c @@ -1,26 +1,22 @@ -/* - Copyright (c) 2004-2012 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - read a config file -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * read a config file + */ #include "arch.h" #include "os_calls.h" @@ -33,250 +29,276 @@ returns 0 if everything is ok returns 1 if problem reading file */ static int APP_CC -l_file_read_sections(int fd, int max_file_size, struct list* names) +l_file_read_sections(int fd, int max_file_size, struct list *names) { - struct stream* s; - char text[256]; - char c; - int in_it; - int in_it_index; - int len; - int index; - int rv; - - rv = 0; - g_file_seek(fd, 0); - in_it_index = 0; - in_it = 0; - g_memset(text, 0, 256); - list_clear(names); - make_stream(s); - init_stream(s, max_file_size); - len = g_file_read(fd, s->data, max_file_size); - if (len > 0) - { - s->end = s->p + len; - for (index = 0; index < len; index++) + struct stream *s; + char text[256]; + char c; + int in_it; + int in_it_index; + int len; + int index; + int rv; + + rv = 0; + g_file_seek(fd, 0); + in_it_index = 0; + in_it = 0; + g_memset(text, 0, 256); + list_clear(names); + make_stream(s); + init_stream(s, max_file_size); + len = g_file_read(fd, s->data, max_file_size); + + if (len > 0) { - in_uint8(s, c); - if (c == '[') - { - in_it = 1; - } - else if (c == ']') - { - list_add_item(names, (tbus)g_strdup(text)); - in_it = 0; - in_it_index = 0; - g_memset(text, 0, 256); - } - else if (in_it) - { - text[in_it_index] = c; - in_it_index++; - } + s->end = s->p + len; + + for (index = 0; index < len; index++) + { + in_uint8(s, c); + + if (c == '[') + { + in_it = 1; + } + else if (c == ']') + { + list_add_item(names, (tbus)g_strdup(text)); + in_it = 0; + in_it_index = 0; + g_memset(text, 0, 256); + } + else if (in_it) + { + text[in_it_index] = c; + in_it_index++; + } + } } - } - else if (len < 0) - { - rv = 1; - } - free_stream(s); - return rv; + else if (len < 0) + { + rv = 1; + } + + free_stream(s); + return rv; } /*****************************************************************************/ static int APP_CC -file_read_line(struct stream* s, char* text) +file_read_line(struct stream *s, char *text) { - int i; - int skip_to_end; - int at_end; - char c; - char* hold; - - skip_to_end = 0; - if (!s_check_rem(s, 1)) - { - return 1; - } - hold = s->p; - i = 0; - in_uint8(s, c); - while (c != 10 && c != 13) - { - if (c == '#' || c == '!' || c == ';') - { - skip_to_end = 1; - } - if (!skip_to_end) - { - text[i] = c; - i++; - } - if (s_check_rem(s, 1)) + int i; + int skip_to_end; + int at_end; + char c; + char *hold; + + skip_to_end = 0; + + if (!s_check_rem(s, 1)) { - in_uint8(s, c); + return 1; } - else + + hold = s->p; + i = 0; + in_uint8(s, c); + + while (c != 10 && c != 13) { - c = 0; - break; + if (c == '#' || c == '!' || c == ';') + { + skip_to_end = 1; + } + + if (!skip_to_end) + { + text[i] = c; + i++; + } + + if (s_check_rem(s, 1)) + { + in_uint8(s, c); + } + else + { + c = 0; + break; + } } - } - if (c == 10 || c == 13) - { - at_end = 0; - while (c == 10 || c == 13) + + if (c == 10 || c == 13) { - if (s_check_rem(s, 1)) - { - in_uint8(s, c); - } - else - { - at_end = 1; - break; - } + at_end = 0; + + while (c == 10 || c == 13) + { + if (s_check_rem(s, 1)) + { + in_uint8(s, c); + } + else + { + at_end = 1; + break; + } + } + + if (!at_end) + { + s->p--; + } } - if (!at_end) + + text[i] = 0; + + if (text[0] == '[') { - s->p--; + s->p = hold; + return 1; } - } - text[i] = 0; - if (text[0] == '[') - { - s->p = hold; - return 1; - } - return 0; + + return 0; } /*****************************************************************************/ /* returns error */ static int APP_CC -file_split_name_value(char* text, char* name, char* value) +file_split_name_value(char *text, char *name, char *value) { - int len; - int i; - int value_index; - int name_index; - int on_to; - - value_index = 0; - name_index = 0; - on_to = 0; - name[0] = 0; - value[0] = 0; - len = g_strlen(text); - for (i = 0; i < len; i++) - { - if (text[i] == '=') - { - on_to = 1; - } - else if (on_to) - { - value[value_index] = text[i]; - value_index++; - value[value_index] = 0; - } - else + int len; + int i; + int value_index; + int name_index; + int on_to; + + value_index = 0; + name_index = 0; + on_to = 0; + name[0] = 0; + value[0] = 0; + len = g_strlen(text); + + for (i = 0; i < len; i++) { - name[name_index] = text[i]; - name_index++; - name[name_index] = 0; + if (text[i] == '=') + { + on_to = 1; + } + else if (on_to) + { + value[value_index] = text[i]; + value_index++; + value[value_index] = 0; + } + else + { + name[name_index] = text[i]; + name_index++; + name[name_index] = 0; + } } - } - g_strtrim(name, 3); /* trim both right and left */ - g_strtrim(value, 3); /* trim both right and left */ - return 0; + + g_strtrim(name, 3); /* trim both right and left */ + g_strtrim(value, 3); /* trim both right and left */ + return 0; } /*****************************************************************************/ /* return error */ static int APP_CC -l_file_read_section(int fd, int max_file_size, const char* section, - struct list* names, struct list* values) +l_file_read_section(int fd, int max_file_size, const char *section, + struct list *names, struct list *values) { - struct stream* s; - char text[512]; - char name[512]; - char value[512]; - char* lvalue; - char c; - int in_it; - int in_it_index; - int len; - int index; - int file_size; - - file_size = 32 * 1024; /* 32 K file size limit */ - g_file_seek(fd, 0); - in_it_index = 0; - in_it = 0; - g_memset(text, 0, 512); - list_clear(names); - list_clear(values); - make_stream(s); - init_stream(s, file_size); - len = g_file_read(fd, s->data, file_size); - if (len > 0) - { - s->end = s->p + len; - for (index = 0; index < len; index++) + struct stream *s; + char text[512]; + char name[512]; + char value[512]; + char *lvalue; + char c; + int in_it; + int in_it_index; + int len; + int index; + int file_size; + + file_size = 32 * 1024; /* 32 K file size limit */ + g_file_seek(fd, 0); + in_it_index = 0; + in_it = 0; + g_memset(text, 0, 512); + list_clear(names); + list_clear(values); + make_stream(s); + init_stream(s, file_size); + len = g_file_read(fd, s->data, file_size); + + if (len > 0) { - in_uint8(s, c); - if (c == '[') - { - in_it = 1; - } - else if (c == ']') - { - if (g_strcasecmp(section, text) == 0) + s->end = s->p + len; + + for (index = 0; index < len; index++) { - file_read_line(s, text); - while (file_read_line(s, text) == 0) - { - if (g_strlen(text) > 0) + in_uint8(s, c); + + if (c == '[') { - file_split_name_value(text, name, value); - list_add_item(names, (tbus)g_strdup(name)); - if (value[0] == '$') - { - lvalue = g_getenv(value + 1); - if (lvalue != 0) - { - list_add_item(values, (tbus)g_strdup(lvalue)); - } - else + in_it = 1; + } + else if (c == ']') + { + if (g_strcasecmp(section, text) == 0) { - list_add_item(values, (tbus)g_strdup("")); + file_read_line(s, text); + + while (file_read_line(s, text) == 0) + { + if (g_strlen(text) > 0) + { + file_split_name_value(text, name, value); + list_add_item(names, (tbus)g_strdup(name)); + + if (value[0] == '$') + { + lvalue = g_getenv(value + 1); + + if (lvalue != 0) + { + list_add_item(values, (tbus)g_strdup(lvalue)); + } + else + { + list_add_item(values, (tbus)g_strdup("")); + } + } + else + { + list_add_item(values, (tbus)g_strdup(value)); + } + } + } + + free_stream(s); + return 0; } - } - else - { - list_add_item(values, (tbus)g_strdup(value)); - } + + in_it = 0; + in_it_index = 0; + g_memset(text, 0, 512); + } + else if (in_it) + { + text[in_it_index] = c; + in_it_index++; } - } - free_stream(s); - return 0; } - in_it = 0; - in_it_index = 0; - g_memset(text, 0, 512); - } - else if (in_it) - { - text[in_it_index] = c; - in_it_index++; - } } - } - free_stream(s); - return 1; + + free_stream(s); + return 1; } /*****************************************************************************/ @@ -285,9 +307,9 @@ l_file_read_section(int fd, int max_file_size, const char* section, returns 1 if problem reading file */ /* 32 K file size limit */ int APP_CC -file_read_sections(int fd, struct list* names) +file_read_sections(int fd, struct list *names) { - return l_file_read_sections(fd, 32 * 1024, names); + return l_file_read_sections(fd, 32 * 1024, names); } /*****************************************************************************/ @@ -295,35 +317,39 @@ file_read_sections(int fd, struct list* names) /* this function should be prefered over file_read_sections because it can read any file size */ int APP_CC -file_by_name_read_sections(const char* file_name, struct list* names) +file_by_name_read_sections(const char *file_name, struct list *names) { - int fd; - int file_size; - int rv; + int fd; + int file_size; + int rv; - file_size = g_file_get_size(file_name); - if (file_size < 1) - { - return 1; - } - fd = g_file_open(file_name); - if (fd < 1) - { - return 1; - } - rv = l_file_read_sections(fd, file_size, names); - g_file_close(fd); - return rv; + file_size = g_file_get_size(file_name); + + if (file_size < 1) + { + return 1; + } + + fd = g_file_open(file_name); + + if (fd < 1) + { + return 1; + } + + rv = l_file_read_sections(fd, file_size, names); + g_file_close(fd); + return rv; } /*****************************************************************************/ /* return error */ /* 32 K file size limit */ int APP_CC -file_read_section(int fd, const char* section, - struct list* names, struct list* values) +file_read_section(int fd, const char *section, + struct list *names, struct list *values) { - return l_file_read_section(fd, 32 * 1024, section, names, values); + return l_file_read_section(fd, 32 * 1024, section, names, values); } /*****************************************************************************/ @@ -331,24 +357,28 @@ file_read_section(int fd, const char* section, /* this function should be prefered over file_read_section because it can read any file size */ int APP_CC -file_by_name_read_section(const char* file_name, const char* section, - struct list* names, struct list* values) +file_by_name_read_section(const char *file_name, const char *section, + struct list *names, struct list *values) { - int fd; - int file_size; - int rv; + int fd; + int file_size; + int rv; - file_size = g_file_get_size(file_name); - if (file_size < 1) - { - return 1; - } - fd = g_file_open(file_name); - if (fd < 1) - { - return 1; - } - rv = l_file_read_section(fd, file_size, section, names, values); - g_file_close(fd); - return rv; + file_size = g_file_get_size(file_name); + + if (file_size < 1) + { + return 1; + } + + fd = g_file_open(file_name); + + if (fd < 1) + { + return 1; + } + + rv = l_file_read_section(fd, file_size, section, names, values); + g_file_close(fd); + return rv; } diff --git a/common/file.h b/common/file.h index f5345c4c..c6ce0654 100644 --- a/common/file.h +++ b/common/file.h @@ -1,26 +1,22 @@ -/* - Copyright (c) 2004-2010 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - read a config file -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * read a config file + */ #if !defined(FILE_H) #define FILE_H diff --git a/common/file_loc.h b/common/file_loc.h index acbdd2fc..db312fb4 100644 --- a/common/file_loc.h +++ b/common/file_loc.h @@ -1,24 +1,22 @@ -/* - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - xrdp: A Remote Desktop Protocol server. - Copyright (C) Jay Sorg 2004-2010 - - default file locations for log, config, etc - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * default file locations for log, config, etc + */ #if !defined(FILE_LOC_H) #define FILE_LOC_H diff --git a/common/list.c b/common/list.c index f67a08ab..0c27d596 100644 --- a/common/list.c +++ b/common/list.c @@ -1,217 +1,226 @@ -/* - Copyright (c) 2004-2010 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - simple list -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * simple list + */ #include "arch.h" #include "os_calls.h" #include "list.h" /*****************************************************************************/ -struct list* APP_CC +struct list *APP_CC list_create(void) { - struct list* self; + struct list *self; - self = (struct list*)g_malloc(sizeof(struct list), 1); - self->grow_by = 10; - self->alloc_size = 10; - self->items = (tbus*)g_malloc(sizeof(tbus) * 10, 1); - return self; + self = (struct list *)g_malloc(sizeof(struct list), 1); + self->grow_by = 10; + self->alloc_size = 10; + self->items = (tbus *)g_malloc(sizeof(tbus) * 10, 1); + return self; } /*****************************************************************************/ void APP_CC -list_delete(struct list* self) +list_delete(struct list *self) { - int i; - - if (self == 0) - { - return; - } - if (self->auto_free) - { - for (i = 0; i < self->count; i++) + int i; + + if (self == 0) + { + return; + } + + if (self->auto_free) { - g_free((void*)self->items[i]); - self->items[i] = 0; + for (i = 0; i < self->count; i++) + { + g_free((void *)self->items[i]); + self->items[i] = 0; + } } - } - g_free(self->items); - g_free(self); + + g_free(self->items); + g_free(self); } /*****************************************************************************/ void APP_CC -list_add_item(struct list* self, tbus item) +list_add_item(struct list *self, tbus item) { - tbus* p; - int i; - - if (self->count >= self->alloc_size) - { - i = self->alloc_size; - self->alloc_size += self->grow_by; - p = (tbus*)g_malloc(sizeof(tbus) * self->alloc_size, 1); - g_memcpy(p, self->items, sizeof(tbus) * i); - g_free(self->items); - self->items = p; - } - self->items[self->count] = item; - self->count++; + tbus *p; + int i; + + if (self->count >= self->alloc_size) + { + i = self->alloc_size; + self->alloc_size += self->grow_by; + p = (tbus *)g_malloc(sizeof(tbus) * self->alloc_size, 1); + g_memcpy(p, self->items, sizeof(tbus) * i); + g_free(self->items); + self->items = p; + } + + self->items[self->count] = item; + self->count++; } /*****************************************************************************/ tbus APP_CC -list_get_item(struct list* self, int index) +list_get_item(struct list *self, int index) { - if (index < 0 || index >= self->count) - { - return 0; - } - return self->items[index]; + if (index < 0 || index >= self->count) + { + return 0; + } + + return self->items[index]; } /*****************************************************************************/ void APP_CC -list_clear(struct list* self) +list_clear(struct list *self) { - int i; + int i; - if (self->auto_free) - { - for (i = 0; i < self->count; i++) + if (self->auto_free) { - g_free((void*)self->items[i]); - self->items[i] = 0; + for (i = 0; i < self->count; i++) + { + g_free((void *)self->items[i]); + self->items[i] = 0; + } } - } - g_free(self->items); - self->count = 0; - self->grow_by = 10; - self->alloc_size = 10; - self->items = (tbus*)g_malloc(sizeof(tbus) * 10, 1); + + g_free(self->items); + self->count = 0; + self->grow_by = 10; + self->alloc_size = 10; + self->items = (tbus *)g_malloc(sizeof(tbus) * 10, 1); } /*****************************************************************************/ int APP_CC -list_index_of(struct list* self, tbus item) +list_index_of(struct list *self, tbus item) { - int i; + int i; - for (i = 0; i < self->count; i++) - { - if (self->items[i] == item) + for (i = 0; i < self->count; i++) { - return i; + if (self->items[i] == item) + { + return i; + } } - } - return -1; + + return -1; } /*****************************************************************************/ void APP_CC -list_remove_item(struct list* self, int index) +list_remove_item(struct list *self, int index) { - int i; + int i; - if (index >= 0 && index < self->count) - { - if (self->auto_free) + if (index >= 0 && index < self->count) { - g_free((void*)self->items[index]); - self->items[index] = 0; + if (self->auto_free) + { + g_free((void *)self->items[index]); + self->items[index] = 0; + } + + for (i = index; i < (self->count - 1); i++) + { + self->items[i] = self->items[i + 1]; + } + + self->count--; } - for (i = index; i < (self->count - 1); i++) - { - self->items[i] = self->items[i + 1]; - } - self->count--; - } } /*****************************************************************************/ void APP_CC -list_insert_item(struct list* self, int index, tbus item) +list_insert_item(struct list *self, int index, tbus item) { - tbus* p; - int i; - - if (index == self->count) - { - list_add_item(self, item); - return; - } - if (index >= 0 && index < self->count) - { - self->count++; - if (self->count > self->alloc_size) + tbus *p; + int i; + + if (index == self->count) { - i = self->alloc_size; - self->alloc_size += self->grow_by; - p = (tbus*)g_malloc(sizeof(tbus) * self->alloc_size, 1); - g_memcpy(p, self->items, sizeof(tbus) * i); - g_free(self->items); - self->items = p; + list_add_item(self, item); + return; } - for (i = (self->count - 2); i >= index; i--) + + if (index >= 0 && index < self->count) { - self->items[i + 1] = self->items[i]; + self->count++; + + if (self->count > self->alloc_size) + { + i = self->alloc_size; + self->alloc_size += self->grow_by; + p = (tbus *)g_malloc(sizeof(tbus) * self->alloc_size, 1); + g_memcpy(p, self->items, sizeof(tbus) * i); + g_free(self->items); + self->items = p; + } + + for (i = (self->count - 2); i >= index; i--) + { + self->items[i + 1] = self->items[i]; + } + + self->items[index] = item; } - self->items[index] = item; - } } /*****************************************************************************/ /* append one list to another using strdup for each item in the list */ /* begins copy at start_index, a zero based index on the soure list */ void APP_CC -list_append_list_strdup(struct list* self, struct list* dest, int start_index) +list_append_list_strdup(struct list *self, struct list *dest, int start_index) { - int index; - tbus item; - char* dup; - - for (index = start_index; index < self->count; index++) - { - item = list_get_item(self, index); - dup = g_strdup((char*)item); - list_add_item(dest, (tbus)dup); - } + int index; + tbus item; + char *dup; + + for (index = start_index; index < self->count; index++) + { + item = list_get_item(self, index); + dup = g_strdup((char *)item); + list_add_item(dest, (tbus)dup); + } } /*****************************************************************************/ void APP_CC -list_dump_items(struct list* self) +list_dump_items(struct list *self) { - int index; - - if (self->count == 0) - { - g_writeln("List is empty"); - } - for (index = 0; index < self->count; index++) - { - g_writeln("%d: %s", index, list_get_item(self, index)); - } + int index; + + if (self->count == 0) + { + g_writeln("List is empty"); + } + + for (index = 0; index < self->count; index++) + { + g_writeln("%d: %s", index, list_get_item(self, index)); + } } diff --git a/common/list.h b/common/list.h index b7cd10b4..2c0f9339 100644 --- a/common/list.h +++ b/common/list.h @@ -1,26 +1,22 @@ -/* - Copyright (c) 2004-2010 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - simple list -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * simple list + */ #if !defined(LIST_H) #define LIST_H diff --git a/common/log.c b/common/log.c index 390b2bff..9b2e9289 100644 --- a/common/log.c +++ b/common/log.c @@ -1,25 +1,20 @@ -/* - Copyright (c) 2005-2012 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #include <sys/types.h> #include <sys/stat.h> @@ -41,7 +36,7 @@ #include "log.h" /* Here we store the current state and configuration of the log */ -static struct log_config* staticLogConfig = NULL; +static struct log_config *staticLogConfig = NULL; /* This file first start with all private functions. In the end of the file the public functions is defined */ @@ -54,15 +49,17 @@ static struct log_config* staticLogConfig = NULL; * */ int DEFAULT_CC -internal_log_file_open(const char* fname) +internal_log_file_open(const char *fname) { - int ret = -1; - if (fname != NULL) - { - ret = open(fname, O_WRONLY | O_CREAT | O_APPEND | O_SYNC, - S_IRUSR | S_IWUSR); - } - return ret; + int ret = -1; + + if (fname != NULL) + { + ret = open(fname, O_WRONLY | O_CREAT | O_APPEND | O_SYNC, + S_IRUSR | S_IWUSR); + } + + return ret; } /** @@ -75,22 +72,22 @@ internal_log_file_open(const char* fname) int DEFAULT_CC internal_log_xrdp2syslog(const enum logLevels lvl) { - switch (lvl) - { - case LOG_LEVEL_ALWAYS: - return LOG_CRIT; - case LOG_LEVEL_ERROR: - return LOG_ERR; - case LOG_LEVEL_WARNING: - return LOG_WARNING; - case LOG_LEVEL_INFO: - return LOG_INFO; - case LOG_LEVEL_DEBUG: - return LOG_DEBUG; - default: - g_writeln("Undefined log level - programming error"); - return LOG_DEBUG; - } + switch (lvl) + { + case LOG_LEVEL_ALWAYS: + return LOG_CRIT; + case LOG_LEVEL_ERROR: + return LOG_ERR; + case LOG_LEVEL_WARNING: + return LOG_WARNING; + case LOG_LEVEL_INFO: + return LOG_INFO; + case LOG_LEVEL_DEBUG: + return LOG_DEBUG; + default: + g_writeln("Undefined log level - programming error"); + return LOG_DEBUG; + } } /** @@ -101,117 +98,121 @@ internal_log_xrdp2syslog(const enum logLevels lvl) * */ void DEFAULT_CC -internal_log_lvl2str(const enum logLevels lvl, char* str) +internal_log_lvl2str(const enum logLevels lvl, char *str) { - switch (lvl) - { - case LOG_LEVEL_ALWAYS: - snprintf(str, 9, "%s", "[CORE ] "); - break; - case LOG_LEVEL_ERROR: - snprintf(str, 9, "%s", "[ERROR] "); - break; - case LOG_LEVEL_WARNING: - snprintf(str, 9, "%s", "[WARN ] "); - break; - case LOG_LEVEL_INFO: - snprintf(str, 9, "%s", "[INFO ] "); - break; - case LOG_LEVEL_DEBUG: - snprintf(str, 9, "%s", "[DEBUG] "); - break; - default: - snprintf(str, 9, "%s", "PRG ERR!"); - g_writeln("Programming error - undefined log level!!!"); - } + switch (lvl) + { + case LOG_LEVEL_ALWAYS: + snprintf(str, 9, "%s", "[CORE ] "); + break; + case LOG_LEVEL_ERROR: + snprintf(str, 9, "%s", "[ERROR] "); + break; + case LOG_LEVEL_WARNING: + snprintf(str, 9, "%s", "[WARN ] "); + break; + case LOG_LEVEL_INFO: + snprintf(str, 9, "%s", "[INFO ] "); + break; + case LOG_LEVEL_DEBUG: + snprintf(str, 9, "%s", "[DEBUG] "); + break; + default: + snprintf(str, 9, "%s", "PRG ERR!"); + g_writeln("Programming error - undefined log level!!!"); + } } /******************************************************************************/ enum logReturns DEFAULT_CC -internal_log_start(struct log_config* l_cfg) +internal_log_start(struct log_config *l_cfg) { - enum logReturns ret = LOG_GENERAL_ERROR; - if (0 == l_cfg) - { - ret = LOG_ERROR_MALLOC; - return ret; - } + enum logReturns ret = LOG_GENERAL_ERROR; - /* if logfile is NULL, we return error */ - if (0 == l_cfg->log_file) - { - g_writeln("log_file not properly assigned"); - return ret; - } + if (0 == l_cfg) + { + ret = LOG_ERROR_MALLOC; + return ret; + } - /* if progname is NULL, we ureturn error */ - if (0 == l_cfg->program_name) - { - g_writeln("program_name not properly assigned"); - return ret; - } + /* if logfile is NULL, we return error */ + if (0 == l_cfg->log_file) + { + g_writeln("log_file not properly assigned"); + return ret; + } - /* open file */ - l_cfg->fd = internal_log_file_open(l_cfg->log_file); + /* if progname is NULL, we ureturn error */ + if (0 == l_cfg->program_name) + { + g_writeln("program_name not properly assigned"); + return ret; + } - if (-1 == l_cfg->fd) - { - return LOG_ERROR_FILE_OPEN; - } + /* open file */ + l_cfg->fd = internal_log_file_open(l_cfg->log_file); - /* if syslog is enabled, open it */ - if (l_cfg->enable_syslog) - { - openlog(l_cfg->program_name, LOG_CONS | LOG_PID, LOG_DAEMON); - } + if (-1 == l_cfg->fd) + { + return LOG_ERROR_FILE_OPEN; + } + + /* if syslog is enabled, open it */ + if (l_cfg->enable_syslog) + { + openlog(l_cfg->program_name, LOG_CONS | LOG_PID, LOG_DAEMON); + } #ifdef LOG_ENABLE_THREAD - pthread_mutexattr_init(&(l_cfg->log_lock_attr)); - pthread_mutex_init(&(l_cfg->log_lock), &(l_cfg->log_lock_attr)); + pthread_mutexattr_init(&(l_cfg->log_lock_attr)); + pthread_mutex_init(&(l_cfg->log_lock), &(l_cfg->log_lock_attr)); #endif - return LOG_STARTUP_OK; + return LOG_STARTUP_OK; } /******************************************************************************/ enum logReturns DEFAULT_CC -internal_log_end(struct log_config* l_cfg) +internal_log_end(struct log_config *l_cfg) { - enum logReturns ret = LOG_GENERAL_ERROR; - /* if log is closed, quit silently */ - if (0 == l_cfg) - { + enum logReturns ret = LOG_GENERAL_ERROR; + + /* if log is closed, quit silently */ + if (0 == l_cfg) + { + return ret; + } + + /* closing log file */ + log_message(LOG_LEVEL_ALWAYS, "shutting down log subsystem..."); + + if (0 > l_cfg->fd) + { + /* closing logfile... */ + g_file_close(l_cfg->fd); + } + + /* if syslog is enabled, close it */ + if (l_cfg->enable_syslog) + { + closelog(); + } + + /* freeing allocated memory */ + if (0 != l_cfg->log_file) + { + g_free(l_cfg->log_file); + l_cfg->log_file = 0; + } + + if (0 != l_cfg->program_name) + { + g_free(l_cfg->program_name); + l_cfg->program_name = 0; + } + + ret = LOG_STARTUP_OK; return ret; - } - - /* closing log file */ - log_message(LOG_LEVEL_ALWAYS, "shutting down log subsystem..."); - - if (0 > l_cfg->fd) - { - /* closing logfile... */ - g_file_close(l_cfg->fd); - } - - /* if syslog is enabled, close it */ - if (l_cfg->enable_syslog) - { - closelog(); - } - - /* freeing allocated memory */ - if (0 != l_cfg->log_file) - { - g_free(l_cfg->log_file); - l_cfg->log_file = 0; - } - if (0 != l_cfg->program_name) - { - g_free(l_cfg->program_name); - l_cfg->program_name = 0; - } - ret = LOG_STARTUP_OK; - return ret; } /** @@ -220,177 +221,192 @@ internal_log_end(struct log_config* l_cfg) * @return */ enum logLevels DEFAULT_CC -internal_log_text2level(char* buf) +internal_log_text2level(char *buf) { - if (0 == g_strcasecmp(buf, "0") || - 0 == g_strcasecmp(buf, "core")) - { - return LOG_LEVEL_ALWAYS; - } - else if (0 == g_strcasecmp(buf, "1") || - 0 == g_strcasecmp(buf, "error")) - { - return LOG_LEVEL_ERROR; - } - else if (0 == g_strcasecmp(buf, "2") || - 0 == g_strcasecmp(buf, "warn") || - 0 == g_strcasecmp(buf, "warning")) - { - return LOG_LEVEL_WARNING; - } - else if (0 == g_strcasecmp(buf, "3") || - 0 == g_strcasecmp(buf, "info")) - { - return LOG_LEVEL_INFO; - } - else if (0 == g_strcasecmp(buf, "4") || - 0 == g_strcasecmp(buf, "debug")) - { + if (0 == g_strcasecmp(buf, "0") || + 0 == g_strcasecmp(buf, "core")) + { + return LOG_LEVEL_ALWAYS; + } + else if (0 == g_strcasecmp(buf, "1") || + 0 == g_strcasecmp(buf, "error")) + { + return LOG_LEVEL_ERROR; + } + else if (0 == g_strcasecmp(buf, "2") || + 0 == g_strcasecmp(buf, "warn") || + 0 == g_strcasecmp(buf, "warning")) + { + return LOG_LEVEL_WARNING; + } + else if (0 == g_strcasecmp(buf, "3") || + 0 == g_strcasecmp(buf, "info")) + { + return LOG_LEVEL_INFO; + } + else if (0 == g_strcasecmp(buf, "4") || + 0 == g_strcasecmp(buf, "debug")) + { + return LOG_LEVEL_DEBUG; + } + + g_writeln("Your configured log level is corrupt - we use debug log level"); return LOG_LEVEL_DEBUG; - } - g_writeln("Your configured log level is corrupt - we use debug log level"); - return LOG_LEVEL_DEBUG; } enum logReturns DEFAULT_CC -internalReadConfiguration(const char* inFilename, const char* applicationName) +internalReadConfiguration(const char *inFilename, const char *applicationName) { - int fd; - enum logReturns ret = LOG_GENERAL_ERROR; - struct list* sec; - struct list* param_n; - struct list* param_v; - - if (inFilename == NULL) - { - g_writeln("The inifile is null to readConfiguration!"); - return ret; - } - fd = g_file_open(inFilename); - if (-1 == fd) - { - ret = LOG_ERROR_NO_CFG; - g_writeln("We could not open the configuration file to read log parameters"); - return ret; - } - /* we initialize the memory for the configuration and set all content - to zero. */ - ret = internalInitAndAllocStruct(); - if (ret != LOG_STARTUP_OK) - { - return ret; - } - - sec = list_create(); - sec->auto_free = 1; - file_read_sections(fd, sec); - param_n = list_create(); - param_n->auto_free = 1; - param_v = list_create(); - param_v->auto_free = 1; - - /* read logging config */ - ret = internal_config_read_logging(fd, staticLogConfig, param_n, - param_v, applicationName); - if (ret != LOG_STARTUP_OK) - { + int fd; + enum logReturns ret = LOG_GENERAL_ERROR; + struct list *sec; + struct list *param_n; + struct list *param_v; + + if (inFilename == NULL) + { + g_writeln("The inifile is null to readConfiguration!"); + return ret; + } + + fd = g_file_open(inFilename); + + if (-1 == fd) + { + ret = LOG_ERROR_NO_CFG; + g_writeln("We could not open the configuration file to read log parameters"); + return ret; + } + + /* we initialize the memory for the configuration and set all content + to zero. */ + ret = internalInitAndAllocStruct(); + + if (ret != LOG_STARTUP_OK) + { + return ret; + } + + sec = list_create(); + sec->auto_free = 1; + file_read_sections(fd, sec); + param_n = list_create(); + param_n->auto_free = 1; + param_v = list_create(); + param_v->auto_free = 1; + + /* read logging config */ + ret = internal_config_read_logging(fd, staticLogConfig, param_n, + param_v, applicationName); + + if (ret != LOG_STARTUP_OK) + { + return ret; + } + + /* cleanup */ + list_delete(sec); + list_delete(param_v); + list_delete(param_n); + g_file_close(fd); return ret; - } - /* cleanup */ - list_delete(sec); - list_delete(param_v); - list_delete(param_n); - g_file_close(fd); - return ret; } /******************************************************************************/ enum logReturns DEFAULT_CC -internal_config_read_logging(int file, struct log_config* lc, - struct list* param_n, - struct list* param_v, - const char* applicationName) +internal_config_read_logging(int file, struct log_config *lc, + struct list *param_n, + struct list *param_v, + const char *applicationName) { - int i; - char* buf; - char* temp_buf; - - list_clear(param_v); - list_clear(param_n); - - /* setting defaults */ - lc->program_name = g_strdup(applicationName); - lc->log_file = 0; - lc->fd = 0; - lc->log_level = LOG_LEVEL_DEBUG; - lc->enable_syslog = 0; - lc->syslog_level = LOG_LEVEL_DEBUG; - - file_read_section(file, SESMAN_CFG_LOGGING, param_n, param_v); - for (i = 0; i < param_n->count; i++) - { - buf = (char*)list_get_item(param_n, i); - if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_FILE)) - { - lc->log_file = g_strdup((char*)list_get_item(param_v, i)); - if (lc->log_file != NULL) - { - if (lc->log_file[0] != '/') + int i; + char *buf; + char *temp_buf; + + list_clear(param_v); + list_clear(param_n); + + /* setting defaults */ + lc->program_name = g_strdup(applicationName); + lc->log_file = 0; + lc->fd = 0; + lc->log_level = LOG_LEVEL_DEBUG; + lc->enable_syslog = 0; + lc->syslog_level = LOG_LEVEL_DEBUG; + + file_read_section(file, SESMAN_CFG_LOGGING, param_n, param_v); + + for (i = 0; i < param_n->count; i++) + { + buf = (char *)list_get_item(param_n, i); + + if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_FILE)) { - temp_buf = (char*)g_malloc(512, 0); - g_snprintf(temp_buf, 511, "%s/%s", XRDP_LOG_PATH, lc->log_file); - g_free(lc->log_file); - lc->log_file = temp_buf; + lc->log_file = g_strdup((char *)list_get_item(param_v, i)); + + if (lc->log_file != NULL) + { + if (lc->log_file[0] != '/') + { + temp_buf = (char *)g_malloc(512, 0); + g_snprintf(temp_buf, 511, "%s/%s", XRDP_LOG_PATH, lc->log_file); + g_free(lc->log_file); + lc->log_file = temp_buf; + } + } + } + + if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_LEVEL)) + { + lc->log_level = internal_log_text2level((char *)list_get_item(param_v, i)); + } + + if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_ENABLE_SYSLOG)) + { + lc->enable_syslog = text2bool((char *)list_get_item(param_v, i)); + } + + if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_SYSLOG_LEVEL)) + { + lc->syslog_level = internal_log_text2level((char *)list_get_item(param_v, i)); } - } - } - if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_LEVEL)) - { - lc->log_level = internal_log_text2level((char*)list_get_item(param_v, i)); - } - if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_ENABLE_SYSLOG)) - { - lc->enable_syslog = text2bool((char*)list_get_item(param_v, i)); } - if (0 == g_strcasecmp(buf, SESMAN_CFG_LOG_SYSLOG_LEVEL)) + + if (0 == lc->log_file) { - lc->syslog_level = internal_log_text2level((char*)list_get_item(param_v, i)); + lc->log_file = g_strdup("./sesman.log"); } - } - if (0 == lc->log_file) - { - lc->log_file = g_strdup("./sesman.log"); - } + /* try to create path if not exist */ + g_create_path(lc->log_file); - /* try to create path if not exist */ - g_create_path(lc->log_file); - - g_printf("logging configuration:\r\n"); - g_printf("\tLogFile: %s\r\n", lc->log_file); - g_printf("\tLogLevel: %i\r\n", lc->log_level); - g_printf("\tEnableSyslog: %i\r\n", lc->enable_syslog); - g_printf("\tSyslogLevel: %i\r\n", lc->syslog_level); - return LOG_STARTUP_OK; + g_printf("logging configuration:\r\n"); + g_printf("\tLogFile: %s\r\n", lc->log_file); + g_printf("\tLogLevel: %i\r\n", lc->log_level); + g_printf("\tEnableSyslog: %i\r\n", lc->enable_syslog); + g_printf("\tSyslogLevel: %i\r\n", lc->syslog_level); + return LOG_STARTUP_OK; } enum logReturns DEFAULT_CC internalInitAndAllocStruct(void) { - enum logReturns ret = LOG_GENERAL_ERROR; - staticLogConfig = g_malloc(sizeof(struct log_config), 1); - if (staticLogConfig != NULL) - { - staticLogConfig->fd = -1; - staticLogConfig->enable_syslog = 0; - ret = LOG_STARTUP_OK; - } - else - { - g_writeln("could not allocate memory for log struct"); - ret = LOG_ERROR_MALLOC; - } - return ret; + enum logReturns ret = LOG_GENERAL_ERROR; + staticLogConfig = g_malloc(sizeof(struct log_config), 1); + + if (staticLogConfig != NULL) + { + staticLogConfig->fd = -1; + staticLogConfig->enable_syslog = 0; + ret = LOG_STARTUP_OK; + } + else + { + g_writeln("could not allocate memory for log struct"); + ret = LOG_ERROR_MALLOC; + } + + return ret; } /* @@ -406,60 +422,68 @@ internalInitAndAllocStruct(void) * */ int APP_CC -text2bool(char* s) +text2bool(char *s) { - if (0 == g_strcasecmp(s, "1") || - 0 == g_strcasecmp(s, "true") || - 0 == g_strcasecmp(s, "yes")) - { - return 1; - } - return 0; + if (0 == g_strcasecmp(s, "1") || + 0 == g_strcasecmp(s, "true") || + 0 == g_strcasecmp(s, "yes")) + { + return 1; + } + + return 0; } enum logReturns DEFAULT_CC -log_start_from_param(const struct log_config* iniParams) +log_start_from_param(const struct log_config *iniParams) { - enum logReturns ret = LOG_GENERAL_ERROR; - if (staticLogConfig != NULL) - { - log_message(LOG_LEVEL_ALWAYS, "Log already initialized"); - return ret; - } - if (iniParams == NULL) - { - g_writeln("inparam to log_start_from_param is NULL"); - return ret; - } - else - { - /*Copy the struct information*/ - ret = internalInitAndAllocStruct(); - if (ret != LOG_STARTUP_OK) + enum logReturns ret = LOG_GENERAL_ERROR; + + if (staticLogConfig != NULL) { - g_writeln("internalInitAndAllocStruct failed"); - return ret; - } - staticLogConfig->enable_syslog = iniParams->enable_syslog; - staticLogConfig->fd = iniParams->fd; - staticLogConfig->log_file = g_strdup(iniParams->log_file); - staticLogConfig->log_level = iniParams->log_level; - staticLogConfig->log_lock = iniParams->log_lock; - staticLogConfig->log_lock_attr = iniParams->log_lock_attr; - staticLogConfig->program_name = g_strdup(iniParams->program_name); - staticLogConfig->syslog_level = iniParams->syslog_level; - ret = internal_log_start(staticLogConfig); - if (ret != LOG_STARTUP_OK) + log_message(LOG_LEVEL_ALWAYS, "Log already initialized"); + return ret; + } + + if (iniParams == NULL) { - g_writeln("Could not start log"); - if (staticLogConfig != NULL) - { - g_free(staticLogConfig); - staticLogConfig = NULL; - } + g_writeln("inparam to log_start_from_param is NULL"); + return ret; + } + else + { + /*Copy the struct information*/ + ret = internalInitAndAllocStruct(); + + if (ret != LOG_STARTUP_OK) + { + g_writeln("internalInitAndAllocStruct failed"); + return ret; + } + + staticLogConfig->enable_syslog = iniParams->enable_syslog; + staticLogConfig->fd = iniParams->fd; + staticLogConfig->log_file = g_strdup(iniParams->log_file); + staticLogConfig->log_level = iniParams->log_level; + staticLogConfig->log_lock = iniParams->log_lock; + staticLogConfig->log_lock_attr = iniParams->log_lock_attr; + staticLogConfig->program_name = g_strdup(iniParams->program_name); + staticLogConfig->syslog_level = iniParams->syslog_level; + ret = internal_log_start(staticLogConfig); + + if (ret != LOG_STARTUP_OK) + { + g_writeln("Could not start log"); + + if (staticLogConfig != NULL) + { + g_free(staticLogConfig); + staticLogConfig = NULL; + } + } } - } - return ret; + + return ret; } /** @@ -470,34 +494,40 @@ log_start_from_param(const struct log_config* iniParams) * @return 0 on success */ enum logReturns DEFAULT_CC -log_start(const char* iniFile, const char* applicationName) +log_start(const char *iniFile, const char *applicationName) { - enum logReturns ret = LOG_GENERAL_ERROR; - if (applicationName == NULL) - { - g_writeln("Programming error your application name cannot be null"); - return ret; - } - ret = internalReadConfiguration(iniFile, applicationName); - if (ret == LOG_STARTUP_OK) - { - ret = internal_log_start(staticLogConfig); - if (ret != LOG_STARTUP_OK) + enum logReturns ret = LOG_GENERAL_ERROR; + + if (applicationName == NULL) { - g_writeln("Could not start log"); - if (staticLogConfig != NULL) - { - g_free(staticLogConfig); - staticLogConfig = NULL; - } - } - } - else - { - g_writeln("Error reading configuration for log based on config: %s", - iniFile); - } - return ret; + g_writeln("Programming error your application name cannot be null"); + return ret; + } + + ret = internalReadConfiguration(iniFile, applicationName); + + if (ret == LOG_STARTUP_OK) + { + ret = internal_log_start(staticLogConfig); + + if (ret != LOG_STARTUP_OK) + { + g_writeln("Could not start log"); + + if (staticLogConfig != NULL) + { + g_free(staticLogConfig); + staticLogConfig = NULL; + } + } + } + else + { + g_writeln("Error reading configuration for log based on config: %s", + iniFile); + } + + return ret; } /** @@ -507,123 +537,132 @@ log_start(const char* iniFile, const char* applicationName) enum logReturns DEFAULT_CC log_end(void) { - enum logReturns ret = LOG_GENERAL_ERROR; - ret = internal_log_end(staticLogConfig); - if (staticLogConfig != NULL) - { - g_free(staticLogConfig); - staticLogConfig = NULL; - } - return ret; + enum logReturns ret = LOG_GENERAL_ERROR; + ret = internal_log_end(staticLogConfig); + + if (staticLogConfig != NULL) + { + g_free(staticLogConfig); + staticLogConfig = NULL; + } + + return ret; } enum logReturns DEFAULT_CC -log_message(const enum logLevels lvl, const char* msg, ...) +log_message(const enum logLevels lvl, const char *msg, ...) { - char buff[LOG_BUFFER_SIZE + 31]; /* 19 (datetime) 4 (space+cr+lf+\0) */ - va_list ap; - int len = 0; - enum logReturns rv = LOG_STARTUP_OK; - int writereply = 0; - time_t now_t; - struct tm* now; - if (staticLogConfig == NULL) - { - g_writeln("The log reference is NULL - log not initialized properly"); - return LOG_ERROR_NO_CFG; - } - if (0 > staticLogConfig->fd && staticLogConfig->enable_syslog == 0) - { - return LOG_ERROR_FILE_NOT_OPEN; - } - - now_t = time(&now_t); - now = localtime(&now_t); - - snprintf(buff, 21, "[%.4d%.2d%.2d-%.2d:%.2d:%.2d] ", now->tm_year + 1900, - now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, - now->tm_sec); - - internal_log_lvl2str(lvl, buff + 20); - - va_start(ap, msg); - len = vsnprintf(buff + 28, LOG_BUFFER_SIZE, msg, ap); - va_end(ap); - - /* checking for truncated messages */ - if (len > LOG_BUFFER_SIZE) - { - log_message(LOG_LEVEL_WARNING, "next message will be truncated"); - } - - /* forcing the end of message string */ + char buff[LOG_BUFFER_SIZE + 31]; /* 19 (datetime) 4 (space+cr+lf+\0) */ + va_list ap; + int len = 0; + enum logReturns rv = LOG_STARTUP_OK; + int writereply = 0; + time_t now_t; + struct tm *now; + + if (staticLogConfig == NULL) + { + g_writeln("The log reference is NULL - log not initialized properly"); + return LOG_ERROR_NO_CFG; + } + + if (0 > staticLogConfig->fd && staticLogConfig->enable_syslog == 0) + { + return LOG_ERROR_FILE_NOT_OPEN; + } + + now_t = time(&now_t); + now = localtime(&now_t); + + snprintf(buff, 21, "[%.4d%.2d%.2d-%.2d:%.2d:%.2d] ", now->tm_year + 1900, + now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, + now->tm_sec); + + internal_log_lvl2str(lvl, buff + 20); + + va_start(ap, msg); + len = vsnprintf(buff + 28, LOG_BUFFER_SIZE, msg, ap); + va_end(ap); + + /* checking for truncated messages */ + if (len > LOG_BUFFER_SIZE) + { + log_message(LOG_LEVEL_WARNING, "next message will be truncated"); + } + + /* forcing the end of message string */ #ifdef _WIN32 - buff[len + 28] = '\r'; - buff[len + 29] = '\n'; - buff[len + 30] = '\0'; + buff[len + 28] = '\r'; + buff[len + 29] = '\n'; + buff[len + 30] = '\0'; #else #ifdef _MACOS - buff[len + 28] = '\r'; - buff[len + 29] = '\0'; + buff[len + 28] = '\r'; + buff[len + 29] = '\0'; #else - buff[len + 28] = '\n'; - buff[len + 29] = '\0'; + buff[len + 28] = '\n'; + buff[len + 29] = '\0'; #endif #endif - if (staticLogConfig->enable_syslog && (lvl <= staticLogConfig->syslog_level)) - { - /* log to syslog*/ - /* %s fix compiler warning 'not a string literal' */ - syslog(internal_log_xrdp2syslog(lvl), "(%d)(%ld)%s", g_getpid(), - tc_get_threadid(), buff + 20); - } + if (staticLogConfig->enable_syslog && (lvl <= staticLogConfig->syslog_level)) + { + /* log to syslog*/ + /* %s fix compiler warning 'not a string literal' */ + syslog(internal_log_xrdp2syslog(lvl), "(%d)(%ld)%s", g_getpid(), + tc_get_threadid(), buff + 20); + } - if (lvl <= staticLogConfig->log_level) - { - /* log to console */ - g_printf(buff); + if (lvl <= staticLogConfig->log_level) + { + /* log to console */ + g_printf(buff); - /* log to application logfile */ + /* log to application logfile */ #ifdef LOG_ENABLE_THREAD - pthread_mutex_lock(&(staticLogConfig->log_lock)); + pthread_mutex_lock(&(staticLogConfig->log_lock)); #endif - if (staticLogConfig->fd > 0) - { - writereply = g_file_write(staticLogConfig->fd, buff, g_strlen(buff)); - if (writereply <= 0) - { - rv = LOG_ERROR_NULL_FILE; - } - } + + if (staticLogConfig->fd > 0) + { + writereply = g_file_write(staticLogConfig->fd, buff, g_strlen(buff)); + + if (writereply <= 0) + { + rv = LOG_ERROR_NULL_FILE; + } + } + #ifdef LOG_ENABLE_THREAD - pthread_mutex_unlock(&(staticLogConfig->log_lock)); + pthread_mutex_unlock(&(staticLogConfig->log_lock)); #endif - } - return rv; + } + + return rv; } /** * Return the configured log file name * @return */ -char* DEFAULT_CC -getLogFile(char* replybuf, int bufsize) +char *DEFAULT_CC +getLogFile(char *replybuf, int bufsize) { - if (staticLogConfig) - { - if (staticLogConfig->log_file) + if (staticLogConfig) { - g_strncpy(replybuf, staticLogConfig->log_file, bufsize); + if (staticLogConfig->log_file) + { + g_strncpy(replybuf, staticLogConfig->log_file, bufsize); + } + else + { + g_sprintf(replybuf, "The log_file name is NULL"); + } } else { - g_sprintf(replybuf, "The log_file name is NULL"); + g_snprintf(replybuf, bufsize, "The log is not properly started"); } - } - else - { - g_snprintf(replybuf, bufsize, "The log is not properly started"); - } - return replybuf; + + return replybuf; } diff --git a/common/log.h b/common/log.h index cc355b18..c83a34d3 100644 --- a/common/log.h +++ b/common/log.h @@ -1,25 +1,20 @@ -/* - Copyright (c) 2005-2012 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef LOG_H #define LOG_H diff --git a/common/os_calls.c b/common/os_calls.c index 6b55f2f7..20c84f3e 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -1,28 +1,24 @@ -/* - Copyright (c) 2004-2012 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - generic operating system calls - - put all the os / arch define in here you want -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * generic operating system calls + * + * put all the os / arch define in here you want + */ #if defined(HAVE_CONFIG_H) #include "config_ac.h" @@ -68,7 +64,7 @@ /* for clearenv() */ #if defined(_WIN32) #else -extern char** environ; +extern char **environ; #endif #if defined(__linux__) @@ -90,77 +86,85 @@ static char g_temp_base_org[128] = ""; int APP_CC g_rm_temp_dir(void) { - if (g_temp_base[0] != 0) - { - if (!g_remove_dir(g_temp_base)) + if (g_temp_base[0] != 0) { - printf("g_rm_temp_dir: removing temp directory [%s] failed\n", g_temp_base); + if (!g_remove_dir(g_temp_base)) + { + printf("g_rm_temp_dir: removing temp directory [%s] failed\n", g_temp_base); + } + + g_temp_base[0] = 0; } - g_temp_base[0] = 0; - } - return 0; + + return 0; } /*****************************************************************************/ int APP_CC -g_mk_temp_dir(const char* app_name) +g_mk_temp_dir(const char *app_name) { - if (app_name != 0) - { - if (app_name[0] != 0) + if (app_name != 0) { - if (!g_directory_exist("/tmp/.xrdp")) - { - if (!g_create_dir("/tmp/.xrdp")) + if (app_name[0] != 0) { - printf("g_mk_temp_dir: g_create_dir failed\n"); - return 1; + if (!g_directory_exist("/tmp/.xrdp")) + { + if (!g_create_dir("/tmp/.xrdp")) + { + printf("g_mk_temp_dir: g_create_dir failed\n"); + return 1; + } + + g_chmod_hex("/tmp/.xrdp", 0x1777); + } + + snprintf(g_temp_base, sizeof(g_temp_base), + "/tmp/.xrdp/%s-XXXXXX", app_name); + snprintf(g_temp_base_org, sizeof(g_temp_base_org), + "/tmp/.xrdp/%s-XXXXXX", app_name); + + if (mkdtemp(g_temp_base) == 0) + { + printf("g_mk_temp_dir: mkdtemp failed [%s]\n", g_temp_base); + return 1; + } + } + else + { + printf("g_mk_temp_dir: bad app name\n"); + return 1; } - g_chmod_hex("/tmp/.xrdp", 0x1777); - } - snprintf(g_temp_base, sizeof(g_temp_base), - "/tmp/.xrdp/%s-XXXXXX", app_name); - snprintf(g_temp_base_org, sizeof(g_temp_base_org), - "/tmp/.xrdp/%s-XXXXXX", app_name); - if (mkdtemp(g_temp_base) == 0) - { - printf("g_mk_temp_dir: mkdtemp failed [%s]\n", g_temp_base); - return 1; - } } else { - printf("g_mk_temp_dir: bad app name\n"); - return 1; - } - } - else - { - if (g_temp_base_org[0] == 0) - { - printf("g_mk_temp_dir: g_temp_base_org not set\n"); - return 1; - } - g_strncpy(g_temp_base, g_temp_base_org, 127); - if (mkdtemp(g_temp_base) == 0) - { - printf("g_mk_temp_dir: mkdtemp failed [%s]\n", g_temp_base); + if (g_temp_base_org[0] == 0) + { + printf("g_mk_temp_dir: g_temp_base_org not set\n"); + return 1; + } + + g_strncpy(g_temp_base, g_temp_base_org, 127); + + if (mkdtemp(g_temp_base) == 0) + { + printf("g_mk_temp_dir: mkdtemp failed [%s]\n", g_temp_base); + } } - } - return 0; + + return 0; } /*****************************************************************************/ void APP_CC -g_init(const char* app_name) +g_init(const char *app_name) { #if defined(_WIN32) - WSADATA wsadata; + WSADATA wsadata; - WSAStartup(2, &wsadata); + WSAStartup(2, &wsadata); #endif - setlocale(LC_CTYPE, ""); - g_mk_temp_dir(app_name); + setlocale(LC_CTYPE, ""); + g_mk_temp_dir(app_name); } /*****************************************************************************/ @@ -168,160 +172,168 @@ void APP_CC g_deinit(void) { #if defined(_WIN32) - WSACleanup(); + WSACleanup(); #endif - g_rm_temp_dir(); + g_rm_temp_dir(); } /*****************************************************************************/ /* allocate memory, returns a pointer to it, size bytes are allocated, if zero is non zero, each byte will be set to zero */ -void* APP_CC +void *APP_CC g_malloc(int size, int zero) { - char* rv; + char *rv; + + rv = (char *)malloc(size); - rv = (char*)malloc(size); - if (zero) - { - if (rv != 0) + if (zero) { - memset(rv, 0, size); + if (rv != 0) + { + memset(rv, 0, size); + } } - } - return rv; + + return rv; } /*****************************************************************************/ /* free the memory pointed to by ptr, ptr can be zero */ void APP_CC -g_free(void* ptr) +g_free(void *ptr) { - if (ptr != 0) - { - free(ptr); - } + if (ptr != 0) + { + free(ptr); + } } /*****************************************************************************/ /* output text to stdout, try to use g_write / g_writeln instead to avoid linux / windows EOL problems */ void DEFAULT_CC -g_printf(const char* format, ...) +g_printf(const char *format, ...) { - va_list ap; + va_list ap; - va_start(ap, format); - vfprintf(stdout, format, ap); - va_end(ap); + va_start(ap, format); + vfprintf(stdout, format, ap); + va_end(ap); } /*****************************************************************************/ void DEFAULT_CC -g_sprintf(char* dest, const char* format, ...) +g_sprintf(char *dest, const char *format, ...) { - va_list ap; + va_list ap; - va_start(ap, format); - vsprintf(dest, format, ap); - va_end(ap); + va_start(ap, format); + vsprintf(dest, format, ap); + va_end(ap); } /*****************************************************************************/ void DEFAULT_CC -g_snprintf(char* dest, int len, const char* format, ...) +g_snprintf(char *dest, int len, const char *format, ...) { - va_list ap; + va_list ap; - va_start(ap, format); - vsnprintf(dest, len, format, ap); - va_end(ap); + va_start(ap, format); + vsnprintf(dest, len, format, ap); + va_end(ap); } /*****************************************************************************/ void DEFAULT_CC -g_writeln(const char* format, ...) +g_writeln(const char *format, ...) { - va_list ap; + va_list ap; - va_start(ap, format); - vfprintf(stdout, format, ap); - va_end(ap); + va_start(ap, format); + vfprintf(stdout, format, ap); + va_end(ap); #if defined(_WIN32) - g_printf("\r\n"); + g_printf("\r\n"); #else - g_printf("\n"); + g_printf("\n"); #endif } /*****************************************************************************/ void DEFAULT_CC -g_write(const char* format, ...) +g_write(const char *format, ...) { - va_list ap; + va_list ap; - va_start(ap, format); - vfprintf(stdout, format, ap); - va_end(ap); + va_start(ap, format); + vfprintf(stdout, format, ap); + va_end(ap); } /*****************************************************************************/ /* produce a hex dump */ void APP_CC -g_hexdump(char* p, int len) +g_hexdump(char *p, int len) { - unsigned char* line; - int i; - int thisline; - int offset; + unsigned char *line; + int i; + int thisline; + int offset; - line = (unsigned char*)p; - offset = 0; - while (offset < len) - { - g_printf("%04x ", offset); - thisline = len - offset; - if (thisline > 16) - { - thisline = 16; - } - for (i = 0; i < thisline; i++) - { - g_printf("%02x ", line[i]); - } - for (; i < 16; i++) - { - g_printf(" "); - } - for (i = 0; i < thisline; i++) + line = (unsigned char *)p; + offset = 0; + + while (offset < len) { - g_printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.'); + g_printf("%04x ", offset); + thisline = len - offset; + + if (thisline > 16) + { + thisline = 16; + } + + for (i = 0; i < thisline; i++) + { + g_printf("%02x ", line[i]); + } + + for (; i < 16; i++) + { + g_printf(" "); + } + + for (i = 0; i < thisline; i++) + { + g_printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.'); + } + + g_writeln(""); + offset += thisline; + line += thisline; } - g_writeln(""); - offset += thisline; - line += thisline; - } } /*****************************************************************************/ void APP_CC -g_memset(void* ptr, int val, int size) +g_memset(void *ptr, int val, int size) { - memset(ptr, val, size); + memset(ptr, val, size); } /*****************************************************************************/ void APP_CC -g_memcpy(void* d_ptr, const void* s_ptr, int size) +g_memcpy(void *d_ptr, const void *s_ptr, int size) { - memcpy(d_ptr, s_ptr, size); + memcpy(d_ptr, s_ptr, size); } /*****************************************************************************/ int APP_CC g_getchar(void) { - return getchar(); + return getchar(); } /*****************************************************************************/ @@ -329,40 +341,43 @@ g_getchar(void) int APP_CC g_tcp_set_no_delay(int sck) { - int ret = 1; /* error */ + int ret = 1; /* error */ #if defined(_WIN32) - int option_value; - int option_len; + int option_value; + int option_len; #else - int option_value; - unsigned int option_len; + int option_value; + unsigned int option_len; #endif - option_len = sizeof(option_value); - /* SOL_TCP IPPROTO_TCP */ - if (getsockopt(sck, IPPROTO_TCP, TCP_NODELAY, (char*)&option_value, - &option_len) == 0) - { - if (option_value == 0) + option_len = sizeof(option_value); + + /* SOL_TCP IPPROTO_TCP */ + if (getsockopt(sck, IPPROTO_TCP, TCP_NODELAY, (char *)&option_value, + &option_len) == 0) { - option_value = 1; - option_len = sizeof(option_value); - if (setsockopt(sck, IPPROTO_TCP, TCP_NODELAY, (char*)&option_value, - option_len) == 0) - { - ret = 0; /* success */ - } - else - { - g_writeln("Error setting tcp_nodelay"); - } + if (option_value == 0) + { + option_value = 1; + option_len = sizeof(option_value); + + if (setsockopt(sck, IPPROTO_TCP, TCP_NODELAY, (char *)&option_value, + option_len) == 0) + { + ret = 0; /* success */ + } + else + { + g_writeln("Error setting tcp_nodelay"); + } + } } - } - else - { - g_writeln("Error getting tcp_nodelay"); - } - return ret; + else + { + g_writeln("Error getting tcp_nodelay"); + } + + return ret; } /*****************************************************************************/ @@ -370,40 +385,43 @@ g_tcp_set_no_delay(int sck) int APP_CC g_tcp_set_keepalive(int sck) { - int ret = 1; /* error */ + int ret = 1; /* error */ #if defined(_WIN32) - int option_value; - int option_len; + int option_value; + int option_len; #else - int option_value; - unsigned int option_len; + int option_value; + unsigned int option_len; #endif - option_len = sizeof(option_value); - /* SOL_TCP IPPROTO_TCP */ - if (getsockopt(sck, SOL_SOCKET, SO_KEEPALIVE, (char*)&option_value, - &option_len) == 0) - { - if (option_value == 0) + option_len = sizeof(option_value); + + /* SOL_TCP IPPROTO_TCP */ + if (getsockopt(sck, SOL_SOCKET, SO_KEEPALIVE, (char *)&option_value, + &option_len) == 0) + { + if (option_value == 0) + { + option_value = 1; + option_len = sizeof(option_value); + + if (setsockopt(sck, SOL_SOCKET, SO_KEEPALIVE, (char *)&option_value, + option_len) == 0) + { + ret = 0; /* success */ + } + else + { + g_writeln("Error setting tcp_keepalive"); + } + } + } + else { - option_value = 1; - option_len = sizeof(option_value); - if (setsockopt(sck, SOL_SOCKET, SO_KEEPALIVE, (char*)&option_value, - option_len) == 0) - { - ret = 0; /* success */ - } - else - { - g_writeln("Error setting tcp_keepalive"); - } + g_writeln("Error getting tcp_keepalive"); } - } - else - { - g_writeln("Error getting tcp_keepalive"); - } - return ret; + + return ret; } /*****************************************************************************/ @@ -412,46 +430,52 @@ int APP_CC g_tcp_socket(void) { #if defined(_WIN32) - int rv; - int option_value; - int option_len; + int rv; + int option_value; + int option_len; #else - int rv; - int option_value; - unsigned int option_len; + int rv; + int option_value; + unsigned int option_len; #endif - /* in win32 a socket is an unsigned int, in linux, its an int */ - rv = (int)socket(PF_INET, SOCK_STREAM, 0); - if (rv < 0) - { - return -1; - } - option_len = sizeof(option_value); - if (getsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char*)&option_value, - &option_len) == 0) - { - if (option_value == 0) + /* in win32 a socket is an unsigned int, in linux, its an int */ + rv = (int)socket(PF_INET, SOCK_STREAM, 0); + + if (rv < 0) { - option_value = 1; - option_len = sizeof(option_value); - setsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char*)&option_value, - option_len); + return -1; + } + + option_len = sizeof(option_value); + + if (getsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *)&option_value, + &option_len) == 0) + { + if (option_value == 0) + { + option_value = 1; + option_len = sizeof(option_value); + setsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *)&option_value, + option_len); + } } - } - option_len = sizeof(option_value); - if (getsockopt(rv, SOL_SOCKET, SO_SNDBUF, (char*)&option_value, - &option_len) == 0) - { - if (option_value < (1024 * 32)) + + option_len = sizeof(option_value); + + if (getsockopt(rv, SOL_SOCKET, SO_SNDBUF, (char *)&option_value, + &option_len) == 0) { - option_value = 1024 * 32; - option_len = sizeof(option_value); - setsockopt(rv, SOL_SOCKET, SO_SNDBUF, (char*)&option_value, - option_len); + if (option_value < (1024 * 32)) + { + option_value = 1024 * 32; + option_len = sizeof(option_value); + setsockopt(rv, SOL_SOCKET, SO_SNDBUF, (char *)&option_value, + option_len); + } } - } - return rv; + + return rv; } /*****************************************************************************/ @@ -459,9 +483,9 @@ int APP_CC g_tcp_local_socket(void) { #if defined(_WIN32) - return 0; + return 0; #else - return socket(PF_LOCAL, SOCK_STREAM, 0); + return socket(PF_LOCAL, SOCK_STREAM, 0); #endif } @@ -469,63 +493,67 @@ g_tcp_local_socket(void) void APP_CC g_tcp_close(int sck) { - if (sck == 0) - { - return; - } + if (sck == 0) + { + return; + } + #if defined(_WIN32) - closesocket(sck); + closesocket(sck); #else - close(sck); + close(sck); #endif } /*****************************************************************************/ /* returns error, zero is good */ int APP_CC -g_tcp_connect(int sck, const char* address, const char* port) +g_tcp_connect(int sck, const char *address, const char *port) { - struct sockaddr_in s; - struct hostent* h; + struct sockaddr_in s; + struct hostent *h; - g_memset(&s, 0, sizeof(struct sockaddr_in)); - s.sin_family = AF_INET; - s.sin_port = htons((tui16)atoi(port)); - s.sin_addr.s_addr = inet_addr(address); - if (s.sin_addr.s_addr == INADDR_NONE) - { - h = gethostbyname(address); - if (h != 0) + g_memset(&s, 0, sizeof(struct sockaddr_in)); + s.sin_family = AF_INET; + s.sin_port = htons((tui16)atoi(port)); + s.sin_addr.s_addr = inet_addr(address); + + if (s.sin_addr.s_addr == INADDR_NONE) { - if (h->h_name != 0) - { - if (h->h_addr_list != 0) + h = gethostbyname(address); + + if (h != 0) { - if ((*(h->h_addr_list)) != 0) - { - s.sin_addr.s_addr = *((int*)(*(h->h_addr_list))); - } + if (h->h_name != 0) + { + if (h->h_addr_list != 0) + { + if ((*(h->h_addr_list)) != 0) + { + s.sin_addr.s_addr = *((int *)(*(h->h_addr_list))); + } + } + } } - } } - } - return connect(sck, (struct sockaddr*)&s, sizeof(struct sockaddr_in)); + + return connect(sck, (struct sockaddr *)&s, sizeof(struct sockaddr_in)); } /*****************************************************************************/ /* returns error, zero is good */ int APP_CC -g_tcp_local_connect(int sck, const char* port) +g_tcp_local_connect(int sck, const char *port) { #if defined(_WIN32) - return -1; + return -1; #else - struct sockaddr_un s; + struct sockaddr_un s; - memset(&s, 0, sizeof(struct sockaddr_un)); - s.sun_family = AF_UNIX; - strcpy(s.sun_path, port); - return connect(sck, (struct sockaddr*)&s, sizeof(struct sockaddr_un)); + memset(&s, 0, sizeof(struct sockaddr_un)); + s.sun_family = AF_UNIX; + strcpy(s.sun_path, port); + return connect(sck, (struct sockaddr *)&s, sizeof(struct sockaddr_un)); #endif } @@ -533,65 +561,67 @@ g_tcp_local_connect(int sck, const char* port) int APP_CC g_tcp_set_non_blocking(int sck) { - unsigned long i; + unsigned long i; #if defined(_WIN32) - i = 1; - ioctlsocket(sck, FIONBIO, &i); + i = 1; + ioctlsocket(sck, FIONBIO, &i); #else - i = fcntl(sck, F_GETFL); - i = i | O_NONBLOCK; - fcntl(sck, F_SETFL, i); + i = fcntl(sck, F_GETFL); + i = i | O_NONBLOCK; + fcntl(sck, F_SETFL, i); #endif - return 0; + return 0; } /*****************************************************************************/ /* returns error, zero is good */ int APP_CC -g_tcp_bind(int sck, char* port) +g_tcp_bind(int sck, char *port) { - struct sockaddr_in s; + struct sockaddr_in s; - memset(&s, 0, sizeof(struct sockaddr_in)); - s.sin_family = AF_INET; - s.sin_port = htons((tui16)atoi(port)); - s.sin_addr.s_addr = INADDR_ANY; - return bind(sck, (struct sockaddr*)&s, sizeof(struct sockaddr_in)); + memset(&s, 0, sizeof(struct sockaddr_in)); + s.sin_family = AF_INET; + s.sin_port = htons((tui16)atoi(port)); + s.sin_addr.s_addr = INADDR_ANY; + return bind(sck, (struct sockaddr *)&s, sizeof(struct sockaddr_in)); } /*****************************************************************************/ int APP_CC -g_tcp_local_bind(int sck, char* port) +g_tcp_local_bind(int sck, char *port) { #if defined(_WIN32) - return -1; + return -1; #else - struct sockaddr_un s; + struct sockaddr_un s; - memset(&s, 0, sizeof(struct sockaddr_un)); - s.sun_family = AF_UNIX; - strcpy(s.sun_path, port); - return bind(sck, (struct sockaddr*)&s, sizeof(struct sockaddr_un)); + memset(&s, 0, sizeof(struct sockaddr_un)); + s.sun_family = AF_UNIX; + strcpy(s.sun_path, port); + return bind(sck, (struct sockaddr *)&s, sizeof(struct sockaddr_un)); #endif } /*****************************************************************************/ /* returns error, zero is good */ int APP_CC -g_tcp_bind_address(int sck, char* port, const char* address) +g_tcp_bind_address(int sck, char *port, const char *address) { - struct sockaddr_in s; + struct sockaddr_in s; + + memset(&s, 0, sizeof(struct sockaddr_in)); + s.sin_family = AF_INET; + s.sin_port = htons((tui16)atoi(port)); + s.sin_addr.s_addr = INADDR_ANY; + + if (inet_aton(address, &s.sin_addr) < 0) + { + return -1; /* bad address */ + } - memset(&s, 0, sizeof(struct sockaddr_in)); - s.sin_family = AF_INET; - s.sin_port = htons((tui16)atoi(port)); - s.sin_addr.s_addr = INADDR_ANY; - if (inet_aton(address, &s.sin_addr) < 0) - { - return -1; /* bad address */ - } - return bind(sck, (struct sockaddr*)&s, sizeof(struct sockaddr_in)); + return bind(sck, (struct sockaddr *)&s, sizeof(struct sockaddr_in)); } /*****************************************************************************/ @@ -599,58 +629,61 @@ g_tcp_bind_address(int sck, char* port, const char* address) int APP_CC g_tcp_listen(int sck) { - return listen(sck, 2); + return listen(sck, 2); } /*****************************************************************************/ int APP_CC g_tcp_accept(int sck) { - struct sockaddr_in s; + struct sockaddr_in s; #if defined(_WIN32) - signed int i; + signed int i; #else - unsigned int i; + unsigned int i; #endif - i = sizeof(struct sockaddr_in); - memset(&s, 0, i); - return accept(sck, (struct sockaddr*)&s, &i); + i = sizeof(struct sockaddr_in); + memset(&s, 0, i); + return accept(sck, (struct sockaddr *)&s, &i); } /*****************************************************************************/ void APP_CC -g_write_ip_address(int rcv_sck, char* ip_address, int bytes) +g_write_ip_address(int rcv_sck, char *ip_address, int bytes) { - struct sockaddr_in s; - struct in_addr in; + struct sockaddr_in s; + struct in_addr in; #if defined(_WIN32) - int len; + int len; #else - unsigned int len; + unsigned int len; #endif - int ip_port; - int ok; + int ip_port; + int ok; + + ok = 0; + memset(&s, 0, sizeof(s)); + len = sizeof(s); + + if (getpeername(rcv_sck, (struct sockaddr *)&s, &len) == 0) + { + memset(&in, 0, sizeof(in)); + in.s_addr = s.sin_addr.s_addr; + ip_port = ntohs(s.sin_port); + + if (ip_port != 0) + { + ok = 1; + snprintf(ip_address, bytes, "%s:%d - socket: %d", inet_ntoa(in), + ip_port, rcv_sck); + } + } - ok = 0; - memset(&s, 0, sizeof(s)); - len = sizeof(s); - if (getpeername(rcv_sck,(struct sockaddr*)&s, &len) == 0) - { - memset(&in, 0, sizeof(in)); - in.s_addr = s.sin_addr.s_addr; - ip_port = ntohs(s.sin_port); - if (ip_port != 0) + if (!ok) { - ok = 1; - snprintf(ip_address, bytes, "%s:%d - socket: %d", inet_ntoa(in), - ip_port, rcv_sck); + snprintf(ip_address, bytes, "NULL:NULL - socket: %d", rcv_sck); } - } - if (!ok) - { - snprintf(ip_address, bytes, "NULL:NULL - socket: %d", rcv_sck); - } } /*****************************************************************************/ @@ -658,9 +691,9 @@ void APP_CC g_sleep(int msecs) { #if defined(_WIN32) - Sleep(msecs); + Sleep(msecs); #else - usleep(msecs * 1000); + usleep(msecs * 1000); #endif } @@ -669,31 +702,31 @@ int APP_CC g_tcp_last_error_would_block(int sck) { #if defined(_WIN32) - return WSAGetLastError() == WSAEWOULDBLOCK; + return WSAGetLastError() == WSAEWOULDBLOCK; #else - return (errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINPROGRESS); + return (errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINPROGRESS); #endif } /*****************************************************************************/ int APP_CC -g_tcp_recv(int sck, void* ptr, int len, int flags) +g_tcp_recv(int sck, void *ptr, int len, int flags) { #if defined(_WIN32) - return recv(sck, (char*)ptr, len, flags); + return recv(sck, (char *)ptr, len, flags); #else - return recv(sck, ptr, len, flags); + return recv(sck, ptr, len, flags); #endif } /*****************************************************************************/ int APP_CC -g_tcp_send(int sck, const void* ptr, int len, int flags) +g_tcp_send(int sck, const void *ptr, int len, int flags) { #if defined(_WIN32) - return send(sck, (const char*)ptr, len, flags); + return send(sck, (const char *)ptr, len, flags); #else - return send(sck, ptr, len, flags); + return send(sck, ptr, len, flags); #endif } @@ -703,22 +736,24 @@ int APP_CC g_tcp_socket_ok(int sck) { #if defined(_WIN32) - int opt; - int opt_len; + int opt; + int opt_len; #else - int opt; - unsigned int opt_len; + int opt; + unsigned int opt_len; #endif - opt_len = sizeof(opt); - if (getsockopt(sck, SOL_SOCKET, SO_ERROR, (char*)(&opt), &opt_len) == 0) - { - if (opt == 0) + opt_len = sizeof(opt); + + if (getsockopt(sck, SOL_SOCKET, SO_ERROR, (char *)(&opt), &opt_len) == 0) { - return 1; + if (opt == 0) + { + return 1; + } } - } - return 0; + + return 0; } /*****************************************************************************/ @@ -727,23 +762,26 @@ g_tcp_socket_ok(int sck) int APP_CC g_tcp_can_send(int sck, int millis) { - fd_set wfds; - struct timeval time; - int rv; - - time.tv_sec = millis / 1000; - time.tv_usec = (millis * 1000) % 1000000; - FD_ZERO(&wfds); - if (sck > 0) - { - FD_SET(((unsigned int)sck), &wfds); - rv = select(sck + 1, 0, &wfds, 0, &time); - if (rv > 0) + fd_set wfds; + struct timeval time; + int rv; + + time.tv_sec = millis / 1000; + time.tv_usec = (millis * 1000) % 1000000; + FD_ZERO(&wfds); + + if (sck > 0) { - return g_tcp_socket_ok(sck); + FD_SET(((unsigned int)sck), &wfds); + rv = select(sck + 1, 0, &wfds, 0, &time); + + if (rv > 0) + { + return g_tcp_socket_ok(sck); + } } - } - return 0; + + return 0; } /*****************************************************************************/ @@ -752,144 +790,166 @@ g_tcp_can_send(int sck, int millis) int APP_CC g_tcp_can_recv(int sck, int millis) { - fd_set rfds; - struct timeval time; - int rv; - - time.tv_sec = millis / 1000; - time.tv_usec = (millis * 1000) % 1000000; - FD_ZERO(&rfds); - if (sck > 0) - { - FD_SET(((unsigned int)sck), &rfds); - rv = select(sck + 1, &rfds, 0, 0, &time); - if (rv > 0) + fd_set rfds; + struct timeval time; + int rv; + + time.tv_sec = millis / 1000; + time.tv_usec = (millis * 1000) % 1000000; + FD_ZERO(&rfds); + + if (sck > 0) { - return g_tcp_socket_ok(sck); + FD_SET(((unsigned int)sck), &rfds); + rv = select(sck + 1, &rfds, 0, 0, &time); + + if (rv > 0) + { + return g_tcp_socket_ok(sck); + } } - } - return 0; + + return 0; } /*****************************************************************************/ int APP_CC g_tcp_select(int sck1, int sck2) { - fd_set rfds; - struct timeval time; - int max = 0; - int rv = 0; - - g_memset(&rfds,0,sizeof(fd_set)); - g_memset(&time,0,sizeof(struct timeval)); - - time.tv_sec = 0; - time.tv_usec = 0; - FD_ZERO(&rfds); - if (sck1 > 0) - { - FD_SET(((unsigned int)sck1), &rfds); - } - if (sck2 > 0) - { - FD_SET(((unsigned int)sck2), &rfds); - } - max = sck1; - if (sck2 > max) - { - max = sck2; - } - rv = select(max + 1, &rfds, 0, 0, &time); - if (rv > 0) - { - rv = 0; - if (FD_ISSET(((unsigned int)sck1), &rfds)) + fd_set rfds; + struct timeval time; + int max = 0; + int rv = 0; + + g_memset(&rfds, 0, sizeof(fd_set)); + g_memset(&time, 0, sizeof(struct timeval)); + + time.tv_sec = 0; + time.tv_usec = 0; + FD_ZERO(&rfds); + + if (sck1 > 0) { - rv = rv | 1; + FD_SET(((unsigned int)sck1), &rfds); } - if (FD_ISSET(((unsigned int)sck2), &rfds)) + + if (sck2 > 0) { - rv = rv | 2; + FD_SET(((unsigned int)sck2), &rfds); } - } - else - { - rv = 0; - } - return rv; + + max = sck1; + + if (sck2 > max) + { + max = sck2; + } + + rv = select(max + 1, &rfds, 0, 0, &time); + + if (rv > 0) + { + rv = 0; + + if (FD_ISSET(((unsigned int)sck1), &rfds)) + { + rv = rv | 1; + } + + if (FD_ISSET(((unsigned int)sck2), &rfds)) + { + rv = rv | 2; + } + } + else + { + rv = 0; + } + + return rv; } /*****************************************************************************/ /* returns 0 on error */ tbus APP_CC -g_create_wait_obj(char* name) +g_create_wait_obj(char *name) { #ifdef _WIN32 - tbus obj; + tbus obj; - obj = (tbus)CreateEvent(0, 1, 0, name); - return obj; + obj = (tbus)CreateEvent(0, 1, 0, name); + return obj; #else - tbus obj; - struct sockaddr_un sa; - size_t len; - tbus sck; - int i; - int safety; - int unnamed; + tbus obj; + struct sockaddr_un sa; + size_t len; + tbus sck; + int i; + int safety; + int unnamed; - if (g_temp_base[0] == 0) - { - return 0; - } - sck = socket(PF_UNIX, SOCK_DGRAM, 0); - if (sck < 0) - { - return 0; - } - safety = 0; - g_memset(&sa, 0, sizeof(sa)); - sa.sun_family = AF_UNIX; - unnamed = 1; - if (name != 0) - { - if (name[0] != 0) - { - unnamed = 0; - } - } - if (unnamed) - { - do - { - if (safety > 100) - { - break; - } - safety++; - g_random((char*)&i, sizeof(i)); - len = sizeof(sa.sun_path); - g_snprintf(sa.sun_path, len, "%s/auto_%8.8x", g_temp_base, i); - len = sizeof(sa); - } while (bind(sck, (struct sockaddr*)&sa, len) < 0); - } - else - { - do - { - if (safety > 100) - { - break; - } - safety++; - g_random((char*)&i, sizeof(i)); - len = sizeof(sa.sun_path); - g_snprintf(sa.sun_path, len, "%s/%s_%8.8x", g_temp_base, name, i); - len = sizeof(sa); - } while (bind(sck, (struct sockaddr*)&sa, len) < 0); - } - obj = (tbus)sck; - return obj; + if (g_temp_base[0] == 0) + { + return 0; + } + + sck = socket(PF_UNIX, SOCK_DGRAM, 0); + + if (sck < 0) + { + return 0; + } + + safety = 0; + g_memset(&sa, 0, sizeof(sa)); + sa.sun_family = AF_UNIX; + unnamed = 1; + + if (name != 0) + { + if (name[0] != 0) + { + unnamed = 0; + } + } + + if (unnamed) + { + do + { + if (safety > 100) + { + break; + } + + safety++; + g_random((char *)&i, sizeof(i)); + len = sizeof(sa.sun_path); + g_snprintf(sa.sun_path, len, "%s/auto_%8.8x", g_temp_base, i); + len = sizeof(sa); + } + while (bind(sck, (struct sockaddr *)&sa, len) < 0); + } + else + { + do + { + if (safety > 100) + { + break; + } + + safety++; + g_random((char *)&i, sizeof(i)); + len = sizeof(sa.sun_path); + g_snprintf(sa.sun_path, len, "%s/%s_%8.8x", g_temp_base, name, i); + len = sizeof(sa); + } + while (bind(sck, (struct sockaddr *)&sa, len) < 0); + } + + obj = (tbus)sck; + return obj; #endif } @@ -899,24 +959,26 @@ tbus APP_CC g_create_wait_obj_from_socket(tbus socket, int write) { #ifdef _WIN32 - /* Create and return corresponding event handle for WaitForMultipleObjets */ - WSAEVENT event; - long lnetevent = 0; - - g_memset(&event,0,sizeof(WSAEVENT)); - - event = WSACreateEvent(); - lnetevent = (write ? FD_WRITE : FD_READ) | FD_CLOSE; - if (WSAEventSelect(socket, event, lnetevent) == 0) - { - return (tbus)event; - } - else - { - return 0; - } + /* Create and return corresponding event handle for WaitForMultipleObjets */ + WSAEVENT event; + long lnetevent = 0; + + g_memset(&event, 0, sizeof(WSAEVENT)); + + event = WSACreateEvent(); + lnetevent = (write ? FD_WRITE : FD_READ) | FD_CLOSE; + + if (WSAEventSelect(socket, event, lnetevent) == 0) + { + return (tbus)event; + } + else + { + return 0; + } + #else - return socket; + return socket; #endif } @@ -925,11 +987,13 @@ void APP_CC g_delete_wait_obj_from_socket(tbus wait_obj) { #ifdef _WIN32 - if (wait_obj == 0) - { - return; - } - WSACloseEvent((HANDLE)wait_obj); + + if (wait_obj == 0) + { + return; + } + + WSACloseEvent((HANDLE)wait_obj); #else #endif } @@ -940,39 +1004,47 @@ int APP_CC g_set_wait_obj(tbus obj) { #ifdef _WIN32 - if (obj == 0) - { + + if (obj == 0) + { + return 0; + } + + SetEvent((HANDLE)obj); return 0; - } - SetEvent((HANDLE)obj); - return 0; #else - socklen_t sa_size; - int s; - struct sockaddr_un sa; + socklen_t sa_size; + int s; + struct sockaddr_un sa; - if (obj == 0) - { - return 0; - } - if (g_tcp_can_recv((int)obj, 0)) - { - /* already signalled */ + if (obj == 0) + { + return 0; + } + + if (g_tcp_can_recv((int)obj, 0)) + { + /* already signalled */ + return 0; + } + + sa_size = sizeof(sa); + + if (getsockname((int)obj, (struct sockaddr *)&sa, &sa_size) < 0) + { + return 1; + } + + s = socket(PF_UNIX, SOCK_DGRAM, 0); + + if (s < 0) + { + return 1; + } + + sendto(s, "sig", 4, 0, (struct sockaddr *)&sa, sa_size); + close(s); return 0; - } - sa_size = sizeof(sa); - if (getsockname((int)obj, (struct sockaddr*)&sa, &sa_size) < 0) - { - return 1; - } - s = socket(PF_UNIX, SOCK_DGRAM, 0); - if (s < 0) - { - return 1; - } - sendto(s, "sig", 4, 0, (struct sockaddr*)&sa, sa_size); - close(s); - return 0; #endif } @@ -982,24 +1054,28 @@ int APP_CC g_reset_wait_obj(tbus obj) { #ifdef _WIN32 - if (obj == 0) - { + + if (obj == 0) + { + return 0; + } + + ResetEvent((HANDLE)obj); return 0; - } - ResetEvent((HANDLE)obj); - return 0; #else - char buf[64]; + char buf[64]; + + if (obj == 0) + { + return 0; + } + + while (g_tcp_can_recv((int)obj, 0)) + { + recvfrom((int)obj, &buf, 64, 0, 0, 0); + } - if (obj == 0) - { return 0; - } - while (g_tcp_can_recv((int)obj, 0)) - { - recvfrom((int)obj, &buf, 64, 0, 0, 0); - } - return 0; #endif } @@ -1009,21 +1085,26 @@ int APP_CC g_is_wait_obj_set(tbus obj) { #ifdef _WIN32 - if (obj == 0) - { + + if (obj == 0) + { + return 0; + } + + if (WaitForSingleObject((HANDLE)obj, 0) == WAIT_OBJECT_0) + { + return 1; + } + return 0; - } - if (WaitForSingleObject((HANDLE)obj, 0) == WAIT_OBJECT_0) - { - return 1; - } - return 0; #else - if (obj == 0) - { - return 0; - } - return g_tcp_can_recv((int)obj, 0); + + if (obj == 0) + { + return 0; + } + + return g_tcp_can_recv((int)obj, 0); #endif } @@ -1033,29 +1114,34 @@ int APP_CC g_delete_wait_obj(tbus obj) { #ifdef _WIN32 - if (obj == 0) - { + + if (obj == 0) + { + return 0; + } + + /* Close event handle */ + CloseHandle((HANDLE)obj); return 0; - } - /* Close event handle */ - CloseHandle((HANDLE)obj); - return 0; #else - socklen_t sa_size; - struct sockaddr_un sa; + socklen_t sa_size; + struct sockaddr_un sa; + + if (obj == 0) + { + return 0; + } - if (obj == 0) - { + sa_size = sizeof(sa); + + if (getsockname((int)obj, (struct sockaddr *)&sa, &sa_size) < 0) + { + return 1; + } + + close((int)obj); + unlink(sa.sun_path); return 0; - } - sa_size = sizeof(sa); - if (getsockname((int)obj, (struct sockaddr*)&sa, &sa_size) < 0) - { - return 1; - } - close((int)obj); - unlink(sa.sun_path); - return 0; #endif } @@ -1067,158 +1153,182 @@ g_close_wait_obj(tbus obj) { #ifdef _WIN32 #else - close((int)obj); + close((int)obj); #endif - return 0; + return 0; } /*****************************************************************************/ /* returns error */ int APP_CC -g_obj_wait(tbus* read_objs, int rcount, tbus* write_objs, int wcount, +g_obj_wait(tbus *read_objs, int rcount, tbus *write_objs, int wcount, int mstimeout) { #ifdef _WIN32 - HANDLE handles[256]; - DWORD count; - DWORD error; - int j; - int i; - - j = 0; - count = rcount + wcount; - for (i = 0; i < rcount; i++) - { - handles[j++] = (HANDLE)(read_objs[i]); - } - for (i = 0; i < wcount; i++) - { - handles[j++] = (HANDLE)(write_objs[i]); - } - if (mstimeout < 1) - { - mstimeout = INFINITE; - } - error = WaitForMultipleObjects(count, handles, FALSE, mstimeout); - if (error == WAIT_FAILED) - { - return 1; - } - return 0; -#else - fd_set rfds; - fd_set wfds; - struct timeval time; - struct timeval* ptime = (struct timeval *)NULL; - int i = 0; - int res = 0; - int max = 0; - int sck = 0; - - g_memset(&rfds,0,sizeof(fd_set)); - g_memset(&wfds,0,sizeof(fd_set)); - g_memset(&time,0,sizeof(struct timeval)); - - max = 0; - if (mstimeout < 1) - { - ptime = (struct timeval *)NULL; - } - else - { - time.tv_sec = mstimeout / 1000; - time.tv_usec = (mstimeout % 1000) * 1000; - ptime = &time; - } - FD_ZERO(&rfds); - FD_ZERO(&wfds); - /* Find the highest descriptor number in read_obj */ - if (read_objs!=NULL) - { + HANDLE handles[256]; + DWORD count; + DWORD error; + int j; + int i; + + j = 0; + count = rcount + wcount; + for (i = 0; i < rcount; i++) { - sck = (int)(read_objs[i]); - if (sck > 0) - { - FD_SET(sck, &rfds); - if (sck > max) + handles[j++] = (HANDLE)(read_objs[i]); + } + + for (i = 0; i < wcount; i++) + { + handles[j++] = (HANDLE)(write_objs[i]); + } + + if (mstimeout < 1) + { + mstimeout = INFINITE; + } + + error = WaitForMultipleObjects(count, handles, FALSE, mstimeout); + + if (error == WAIT_FAILED) + { + return 1; + } + + return 0; +#else + fd_set rfds; + fd_set wfds; + struct timeval time; + struct timeval *ptime = (struct timeval *)NULL; + int i = 0; + int res = 0; + int max = 0; + int sck = 0; + + g_memset(&rfds, 0, sizeof(fd_set)); + g_memset(&wfds, 0, sizeof(fd_set)); + g_memset(&time, 0, sizeof(struct timeval)); + + max = 0; + + if (mstimeout < 1) + { + ptime = (struct timeval *)NULL; + } + else + { + time.tv_sec = mstimeout / 1000; + time.tv_usec = (mstimeout % 1000) * 1000; + ptime = &time; + } + + FD_ZERO(&rfds); + FD_ZERO(&wfds); + + /* Find the highest descriptor number in read_obj */ + if (read_objs != NULL) + { + for (i = 0; i < rcount; i++) { - max = sck; /* max holds the highest socket/descriptor number */ + sck = (int)(read_objs[i]); + + if (sck > 0) + { + FD_SET(sck, &rfds); + + if (sck > max) + { + max = sck; /* max holds the highest socket/descriptor number */ + } + } } - } - } - } - else if (rcount>0) - { - g_writeln("Programming error read_objs is null"); - return 1; /* error */ - } - if (write_objs!=NULL) - { - for (i = 0; i < wcount; i++) + } + else if (rcount > 0) { - sck = (int)(write_objs[i]); - if (sck > 0) - { - FD_SET(sck, &wfds); - if (sck > max) + g_writeln("Programming error read_objs is null"); + return 1; /* error */ + } + + if (write_objs != NULL) + { + for (i = 0; i < wcount; i++) { - max = sck; /* max holds the highest socket/descriptor number */ + sck = (int)(write_objs[i]); + + if (sck > 0) + { + FD_SET(sck, &wfds); + + if (sck > max) + { + max = sck; /* max holds the highest socket/descriptor number */ + } + } } - } } - } - else if (wcount > 0) - { - g_writeln("Programming error write_objs is null"); - return 1; /* error */ - } - res = select(max + 1, &rfds, &wfds, 0, ptime); - if (res < 0) - { - /* these are not really errors */ - if ((errno == EAGAIN) || - (errno == EWOULDBLOCK) || - (errno == EINPROGRESS) || - (errno == EINTR)) /* signal occurred */ + else if (wcount > 0) + { + g_writeln("Programming error write_objs is null"); + return 1; /* error */ + } + + res = select(max + 1, &rfds, &wfds, 0, ptime); + + if (res < 0) { - return 0; + /* these are not really errors */ + if ((errno == EAGAIN) || + (errno == EWOULDBLOCK) || + (errno == EINPROGRESS) || + (errno == EINTR)) /* signal occurred */ + { + return 0; + } + + return 1; /* error */ } - return 1; /* error */ - } - return 0; + + return 0; #endif } /*****************************************************************************/ void APP_CC -g_random(char* data, int len) +g_random(char *data, int len) { #if defined(_WIN32) - int index; + int index; + + srand(g_time1()); - srand(g_time1()); - for (index = 0; index < len; index++) - { - data[index] = (char)rand(); /* rand returns a number between 0 and + for (index = 0; index < len; index++) + { + data[index] = (char)rand(); /* rand returns a number between 0 and RAND_MAX */ - } + } + #else - int fd; + int fd; - memset(data, 0x44, len); - fd = open("/dev/urandom", O_RDONLY); - if (fd == -1) - { - fd = open("/dev/random", O_RDONLY); - } - if (fd != -1) - { - if (read(fd, data, len) != len) + memset(data, 0x44, len); + fd = open("/dev/urandom", O_RDONLY); + + if (fd == -1) { + fd = open("/dev/random", O_RDONLY); } - close(fd); - } + + if (fd != -1) + { + if (read(fd, data, len) != len) + { + } + + close(fd); + } + #endif } @@ -1226,35 +1336,37 @@ g_random(char* data, int len) int APP_CC g_abs(int i) { - return abs(i); + return abs(i); } /*****************************************************************************/ int APP_CC -g_memcmp(const void* s1, const void* s2, int len) +g_memcmp(const void *s1, const void *s2, int len) { - return memcmp(s1, s2, len); + return memcmp(s1, s2, len); } /*****************************************************************************/ /* returns -1 on error, else return handle or file descriptor */ int APP_CC -g_file_open(const char* file_name) +g_file_open(const char *file_name) { #if defined(_WIN32) - return (int)CreateFileA(file_name, GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, - 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); + return (int)CreateFileA(file_name, GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); #else - int rv; + int rv; + + rv = open(file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); - rv = open(file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); - if (rv == -1) - { - /* can't open read / write, try to open read only */ - rv = open(file_name, O_RDONLY); - } - return rv; + if (rv == -1) + { + /* can't open read / write, try to open read only */ + rv = open(file_name, O_RDONLY); + } + + return rv; #endif } @@ -1264,48 +1376,52 @@ int APP_CC g_file_close(int fd) { #if defined(_WIN32) - CloseHandle((HANDLE)fd); + CloseHandle((HANDLE)fd); #else - close(fd); + close(fd); #endif - return 0; + return 0; } /*****************************************************************************/ /* read from file, returns the number of bytes read or -1 on error */ int APP_CC -g_file_read(int fd, char* ptr, int len) +g_file_read(int fd, char *ptr, int len) { #if defined(_WIN32) - if (ReadFile((HANDLE)fd, (LPVOID)ptr, (DWORD)len, (LPDWORD)&len, 0)) - { - return len; - } - else - { - return -1; - } + + if (ReadFile((HANDLE)fd, (LPVOID)ptr, (DWORD)len, (LPDWORD)&len, 0)) + { + return len; + } + else + { + return -1; + } + #else - return read(fd, ptr, len); + return read(fd, ptr, len); #endif } /*****************************************************************************/ /* write to file, returns the number of bytes writen or -1 on error */ int APP_CC -g_file_write(int fd, char* ptr, int len) +g_file_write(int fd, char *ptr, int len) { #if defined(_WIN32) - if (WriteFile((HANDLE)fd, (LPVOID)ptr, (DWORD)len, (LPDWORD)&len, 0)) - { - return len; - } - else - { - return -1; - } + + if (WriteFile((HANDLE)fd, (LPVOID)ptr, (DWORD)len, (LPDWORD)&len, 0)) + { + return len; + } + else + { + return -1; + } + #else - return write(fd, ptr, len); + return write(fd, ptr, len); #endif } @@ -1315,19 +1431,21 @@ int APP_CC g_file_seek(int fd, int offset) { #if defined(_WIN32) - int rv; + int rv; + + rv = (int)SetFilePointer((HANDLE)fd, offset, 0, FILE_BEGIN); + + if (rv == (int)INVALID_SET_FILE_POINTER) + { + return -1; + } + else + { + return rv; + } - rv = (int)SetFilePointer((HANDLE)fd, offset, 0, FILE_BEGIN); - if (rv == (int)INVALID_SET_FILE_POINTER) - { - return -1; - } - else - { - return rv; - } #else - return (int)lseek(fd, offset, SEEK_SET); + return (int)lseek(fd, offset, SEEK_SET); #endif } @@ -1338,67 +1456,69 @@ int APP_CC g_file_lock(int fd, int start, int len) { #if defined(_WIN32) - return LockFile((HANDLE)fd, start, 0, len, 0); + return LockFile((HANDLE)fd, start, 0, len, 0); #else - struct flock lock; + struct flock lock; - lock.l_type = F_WRLCK; - lock.l_whence = SEEK_SET; - lock.l_start = start; - lock.l_len = len; - if (fcntl(fd, F_SETLK, &lock) == -1) - { - return 0; - } - return 1; + lock.l_type = F_WRLCK; + lock.l_whence = SEEK_SET; + lock.l_start = start; + lock.l_len = len; + + if (fcntl(fd, F_SETLK, &lock) == -1) + { + return 0; + } + + return 1; #endif } /*****************************************************************************/ /* returns error */ int APP_CC -g_chmod_hex(const char* filename, int flags) +g_chmod_hex(const char *filename, int flags) { #if defined(_WIN32) - return 0; + return 0; #else - int fl; + int fl; - fl = 0; - fl |= (flags & 0x4000) ? S_ISUID : 0; - fl |= (flags & 0x2000) ? S_ISGID : 0; - fl |= (flags & 0x1000) ? S_ISVTX : 0; - fl |= (flags & 0x0400) ? S_IRUSR : 0; - fl |= (flags & 0x0200) ? S_IWUSR : 0; - fl |= (flags & 0x0100) ? S_IXUSR : 0; - fl |= (flags & 0x0040) ? S_IRGRP : 0; - fl |= (flags & 0x0020) ? S_IWGRP : 0; - fl |= (flags & 0x0010) ? S_IXGRP : 0; - fl |= (flags & 0x0004) ? S_IROTH : 0; - fl |= (flags & 0x0002) ? S_IWOTH : 0; - fl |= (flags & 0x0001) ? S_IXOTH : 0; - return chmod(filename, fl); + fl = 0; + fl |= (flags & 0x4000) ? S_ISUID : 0; + fl |= (flags & 0x2000) ? S_ISGID : 0; + fl |= (flags & 0x1000) ? S_ISVTX : 0; + fl |= (flags & 0x0400) ? S_IRUSR : 0; + fl |= (flags & 0x0200) ? S_IWUSR : 0; + fl |= (flags & 0x0100) ? S_IXUSR : 0; + fl |= (flags & 0x0040) ? S_IRGRP : 0; + fl |= (flags & 0x0020) ? S_IWGRP : 0; + fl |= (flags & 0x0010) ? S_IXGRP : 0; + fl |= (flags & 0x0004) ? S_IROTH : 0; + fl |= (flags & 0x0002) ? S_IWOTH : 0; + fl |= (flags & 0x0001) ? S_IXOTH : 0; + return chmod(filename, fl); #endif } /*****************************************************************************/ /* returns error, zero is ok */ int APP_CC -g_chown(const char* name, int uid, int gid) +g_chown(const char *name, int uid, int gid) { - return chown(name, uid, gid); + return chown(name, uid, gid); } /*****************************************************************************/ /* returns error, always zero */ int APP_CC -g_mkdir(const char* dirname) +g_mkdir(const char *dirname) { #if defined(_WIN32) - return 0; + return 0; #else - mkdir(dirname, S_IRWXU); - return 0; + mkdir(dirname, S_IRWXU); + return 0; #endif } @@ -1406,82 +1526,87 @@ g_mkdir(const char* dirname) /* gets the current working directory and puts up to maxlen chars in dirname always returns 0 */ -char* APP_CC -g_get_current_dir(char* dirname, int maxlen) +char *APP_CC +g_get_current_dir(char *dirname, int maxlen) { #if defined(_WIN32) - GetCurrentDirectoryA(maxlen, dirname); - return 0; + GetCurrentDirectoryA(maxlen, dirname); + return 0; #else - if (getcwd(dirname, maxlen) == 0) - { - } - return 0; + + if (getcwd(dirname, maxlen) == 0) + { + } + + return 0; #endif } /*****************************************************************************/ /* returns error, zero on success and -1 on failure */ int APP_CC -g_set_current_dir(char* dirname) +g_set_current_dir(char *dirname) { #if defined(_WIN32) - if (SetCurrentDirectoryA(dirname)) - { - return 0; - } - else - { - return -1; - } + + if (SetCurrentDirectoryA(dirname)) + { + return 0; + } + else + { + return -1; + } + #else - return chdir(dirname); + return chdir(dirname); #endif } /*****************************************************************************/ /* returns boolean, non zero if the file exists */ int APP_CC -g_file_exist(const char* filename) +g_file_exist(const char *filename) { #if defined(_WIN32) - return 0; // use FileAge(filename) <> -1 + return 0; // use FileAge(filename) <> -1 #else - return access(filename, F_OK) == 0; + return access(filename, F_OK) == 0; #endif } /*****************************************************************************/ /* returns boolean, non zero if the directory exists */ int APP_CC -g_directory_exist(const char* dirname) +g_directory_exist(const char *dirname) { #if defined(_WIN32) - return 0; // use GetFileAttributes and check return value - // is not -1 and FILE_ATTRIBUT_DIRECTORY bit is set + return 0; // use GetFileAttributes and check return value + // is not -1 and FILE_ATTRIBUT_DIRECTORY bit is set #else - struct stat st; + struct stat st; + + if (stat(dirname, &st) == 0) + { + return S_ISDIR(st.st_mode); + } + else + { + return 0; + } - if (stat(dirname, &st) == 0) - { - return S_ISDIR(st.st_mode); - } - else - { - return 0; - } #endif } /*****************************************************************************/ /* returns boolean */ int APP_CC -g_create_dir(const char* dirname) +g_create_dir(const char *dirname) { #if defined(_WIN32) - return CreateDirectoryA(dirname, 0); // test this + return CreateDirectoryA(dirname, 0); // test this #else - return mkdir(dirname, (mode_t)-1) == 0; + return mkdir(dirname, (mode_t) - 1) == 0; #endif } @@ -1490,353 +1615,380 @@ g_create_dir(const char* dirname) example /tmp/a/b/c/readme.txt will try to create /tmp/a/b/c returns boolean */ int APP_CC -g_create_path(const char* path) -{ - char* pp; - char* sp; - char* copypath; - int status; - - status = 1; - copypath = g_strdup(path); - pp = copypath; - sp = strchr(pp, '/'); - while (sp != 0) - { - if (sp != pp) - { - *sp = 0; - if (!g_directory_exist(copypath)) - { - if (!g_create_dir(copypath)) +g_create_path(const char *path) +{ + char *pp; + char *sp; + char *copypath; + int status; + + status = 1; + copypath = g_strdup(path); + pp = copypath; + sp = strchr(pp, '/'); + + while (sp != 0) + { + if (sp != pp) { - status = 0; - break; + *sp = 0; + + if (!g_directory_exist(copypath)) + { + if (!g_create_dir(copypath)) + { + status = 0; + break; + } + } + + *sp = '/'; } - } - *sp = '/'; + + pp = sp + 1; + sp = strchr(pp, '/'); } - pp = sp + 1; - sp = strchr(pp, '/'); - } - g_free(copypath); - return status; + + g_free(copypath); + return status; } /*****************************************************************************/ /* returns boolean */ int APP_CC -g_remove_dir(const char* dirname) +g_remove_dir(const char *dirname) { #if defined(_WIN32) - return RemoveDirectoryA(dirname); // test this + return RemoveDirectoryA(dirname); // test this #else - return rmdir(dirname) == 0; + return rmdir(dirname) == 0; #endif } /*****************************************************************************/ /* returns non zero if the file was deleted */ int APP_CC -g_file_delete(const char* filename) +g_file_delete(const char *filename) { #if defined(_WIN32) - return DeleteFileA(filename); + return DeleteFileA(filename); #else - return unlink(filename) != -1; + return unlink(filename) != -1; #endif } /*****************************************************************************/ /* returns file size, -1 on error */ int APP_CC -g_file_get_size(const char* filename) +g_file_get_size(const char *filename) { #if defined(_WIN32) - return -1; + return -1; #else - struct stat st; + struct stat st; + + if (stat(filename, &st) == 0) + { + return (int)(st.st_size); + } + else + { + return -1; + } - if (stat(filename, &st) == 0) - { - return (int)(st.st_size); - } - else - { - return -1; - } #endif } /*****************************************************************************/ /* returns length of text */ int APP_CC -g_strlen(const char* text) +g_strlen(const char *text) { - if (text == NULL) - { - return 0; - } - return strlen(text); + if (text == NULL) + { + return 0; + } + + return strlen(text); } /*****************************************************************************/ /* returns dest */ -char* APP_CC -g_strcpy(char* dest, const char* src) -{ - if (src == 0 && dest != 0) - { - dest[0] = 0; - return dest; - } - if (dest == 0 || src == 0) - { - return 0; - } - return strcpy(dest, src); +char *APP_CC +g_strcpy(char *dest, const char *src) +{ + if (src == 0 && dest != 0) + { + dest[0] = 0; + return dest; + } + + if (dest == 0 || src == 0) + { + return 0; + } + + return strcpy(dest, src); } /*****************************************************************************/ /* returns dest */ -char* APP_CC -g_strncpy(char* dest, const char* src, int len) -{ - char* rv; - - if (src == 0 && dest != 0) - { - dest[0] = 0; - return dest; - } - if (dest == 0 || src == 0) - { - return 0; - } - rv = strncpy(dest, src, len); - dest[len] = 0; - return rv; +char *APP_CC +g_strncpy(char *dest, const char *src, int len) +{ + char *rv; + + if (src == 0 && dest != 0) + { + dest[0] = 0; + return dest; + } + + if (dest == 0 || src == 0) + { + return 0; + } + + rv = strncpy(dest, src, len); + dest[len] = 0; + return rv; } /*****************************************************************************/ /* returns dest */ -char* APP_CC -g_strcat(char* dest, const char* src) +char *APP_CC +g_strcat(char *dest, const char *src) { - if (dest == 0 || src == 0) - { - return dest; - } - return strcat(dest, src); + if (dest == 0 || src == 0) + { + return dest; + } + + return strcat(dest, src); } /*****************************************************************************/ /* if in = 0, return 0 else return newly alloced copy of in */ -char* APP_CC -g_strdup(const char* in) +char *APP_CC +g_strdup(const char *in) { - int len; - char* p; + int len; + char *p; - if (in == 0) - { - return 0; - } - len = g_strlen(in); - p = (char*)g_malloc(len + 1, 0); - if (p != NULL) - { - g_strcpy(p, in); - } - return p; + if (in == 0) + { + return 0; + } + + len = g_strlen(in); + p = (char *)g_malloc(len + 1, 0); + + if (p != NULL) + { + g_strcpy(p, in); + } + + return p; } /*****************************************************************************/ /* if in = 0, return 0 else return newly alloced copy of input string * if the input string is larger than maxlen the returned string will be * truncated. All strings returned will include null termination*/ -char* APP_CC -g_strndup(const char* in, const unsigned int maxlen) +char *APP_CC +g_strndup(const char *in, const unsigned int maxlen) { - int len; - char* p; + int len; + char *p; - if (in == 0) - { - return 0; - } - len = g_strlen(in); - if (len>maxlen) - { - len = maxlen - 1; - } - p = (char*)g_malloc(len + 2, 0); - if (p != NULL) - { - g_strncpy(p, in,len+1); - } - return p; + if (in == 0) + { + return 0; + } + + len = g_strlen(in); + + if (len > maxlen) + { + len = maxlen - 1; + } + + p = (char *)g_malloc(len + 2, 0); + + if (p != NULL) + { + g_strncpy(p, in, len + 1); + } + + return p; } /*****************************************************************************/ int APP_CC -g_strcmp(const char* c1, const char* c2) +g_strcmp(const char *c1, const char *c2) { - return strcmp(c1, c2); + return strcmp(c1, c2); } /*****************************************************************************/ int APP_CC -g_strncmp(const char* c1, const char* c2, int len) +g_strncmp(const char *c1, const char *c2, int len) { - return strncmp(c1, c2, len); + return strncmp(c1, c2, len); } /*****************************************************************************/ int APP_CC -g_strcasecmp(const char* c1, const char* c2) +g_strcasecmp(const char *c1, const char *c2) { #if defined(_WIN32) - return stricmp(c1, c2); + return stricmp(c1, c2); #else - return strcasecmp(c1, c2); + return strcasecmp(c1, c2); #endif } /*****************************************************************************/ int APP_CC -g_strncasecmp(const char* c1, const char* c2, int len) +g_strncasecmp(const char *c1, const char *c2, int len) { #if defined(_WIN32) - return strnicmp(c1, c2, len); + return strnicmp(c1, c2, len); #else - return strncasecmp(c1, c2, len); + return strncasecmp(c1, c2, len); #endif } /*****************************************************************************/ int APP_CC -g_atoi(const char* str) +g_atoi(const char *str) { - if (str == 0) - { - return 0; - } - return atoi(str); -} - -/*****************************************************************************/ -int APP_CC -g_htoi(char* str) -{ - int len; - int index; - int rv; - int val; - int shift; - - rv = 0; - len = strlen(str); - index = len - 1; - shift = 0; - while (index >= 0) - { - val = 0; - switch (str[index]) - { - case '1': - val = 1; - break; - case '2': - val = 2; - break; - case '3': - val = 3; - break; - case '4': - val = 4; - break; - case '5': - val = 5; - break; - case '6': - val = 6; - break; - case '7': - val = 7; - break; - case '8': - val = 8; - break; - case '9': - val = 9; - break; - case 'a': - case 'A': - val = 10; - break; - case 'b': - case 'B': - val = 11; - break; - case 'c': - case 'C': - val = 12; - break; - case 'd': - case 'D': - val = 13; - break; - case 'e': - case 'E': - val = 14; - break; - case 'f': - case 'F': - val = 15; - break; - } - rv = rv | (val << shift); - index--; - shift += 4; - } - return rv; -} - -/*****************************************************************************/ -int APP_CC -g_pos(char* str, const char* to_find) -{ - char* pp; - - pp = strstr(str, to_find); - if (pp == 0) - { - return -1; - } - return (pp - str); + if (str == 0) + { + return 0; + } + + return atoi(str); } /*****************************************************************************/ int APP_CC -g_mbstowcs(twchar* dest, const char* src, int n) +g_htoi(char *str) { - wchar_t* ldest; - int rv; + int len; + int index; + int rv; + int val; + int shift; + + rv = 0; + len = strlen(str); + index = len - 1; + shift = 0; + + while (index >= 0) + { + val = 0; - ldest = (wchar_t*)dest; - rv = mbstowcs(ldest, src, n); - return rv; + switch (str[index]) + { + case '1': + val = 1; + break; + case '2': + val = 2; + break; + case '3': + val = 3; + break; + case '4': + val = 4; + break; + case '5': + val = 5; + break; + case '6': + val = 6; + break; + case '7': + val = 7; + break; + case '8': + val = 8; + break; + case '9': + val = 9; + break; + case 'a': + case 'A': + val = 10; + break; + case 'b': + case 'B': + val = 11; + break; + case 'c': + case 'C': + val = 12; + break; + case 'd': + case 'D': + val = 13; + break; + case 'e': + case 'E': + val = 14; + break; + case 'f': + case 'F': + val = 15; + break; + } + + rv = rv | (val << shift); + index--; + shift += 4; + } + + return rv; +} + +/*****************************************************************************/ +int APP_CC +g_pos(char *str, const char *to_find) +{ + char *pp; + + pp = strstr(str, to_find); + + if (pp == 0) + { + return -1; + } + + return (pp - str); +} + +/*****************************************************************************/ +int APP_CC +g_mbstowcs(twchar *dest, const char *src, int n) +{ + wchar_t *ldest; + int rv; + + ldest = (wchar_t *)dest; + rv = mbstowcs(ldest, src, n); + return rv; } /*****************************************************************************/ int APP_CC -g_wcstombs(char* dest, const twchar* src, int n) +g_wcstombs(char *dest, const twchar *src, int n) { - const wchar_t* lsrc; - int rv; + const wchar_t *lsrc; + int rv; - lsrc = (const wchar_t*)src; - rv = wcstombs(dest, lsrc, n); - return rv; + lsrc = (const wchar_t *)src; + rv = wcstombs(dest, lsrc, n); + return rv; } /*****************************************************************************/ @@ -1845,127 +1997,143 @@ g_wcstombs(char* dest, const twchar* src, int n) /* trim_flags 1 trim left, 2 trim right, 3 trim both, 4 trim through */ /* this will always shorten the string or not change it */ int APP_CC -g_strtrim(char* str, int trim_flags) +g_strtrim(char *str, int trim_flags) { - int index; - int len; - int text1_index; - int got_char; - wchar_t* text; - wchar_t* text1; + int index; + int len; + int text1_index; + int got_char; + wchar_t *text; + wchar_t *text1; + + len = mbstowcs(0, str, 0); + + if (len < 1) + { + return 0; + } + + if ((trim_flags < 1) || (trim_flags > 4)) + { + return 1; + } + + text = (wchar_t *)malloc(len * sizeof(wchar_t) + 8); + text1 = (wchar_t *)malloc(len * sizeof(wchar_t) + 8); + text1_index = 0; + mbstowcs(text, str, len + 1); - len = mbstowcs(0, str, 0); - if (len < 1) - { + switch (trim_flags) + { + case 4: /* trim through */ + + for (index = 0; index < len; index++) + { + if (text[index] > 32) + { + text1[text1_index] = text[index]; + text1_index++; + } + } + + text1[text1_index] = 0; + break; + case 3: /* trim both */ + got_char = 0; + + for (index = 0; index < len; index++) + { + if (got_char) + { + text1[text1_index] = text[index]; + text1_index++; + } + else + { + if (text[index] > 32) + { + text1[text1_index] = text[index]; + text1_index++; + got_char = 1; + } + } + } + + text1[text1_index] = 0; + len = text1_index; + + /* trim right */ + for (index = len - 1; index >= 0; index--) + { + if (text1[index] > 32) + { + break; + } + } + + text1_index = index + 1; + text1[text1_index] = 0; + break; + case 2: /* trim right */ + + /* copy it */ + for (index = 0; index < len; index++) + { + text1[text1_index] = text[index]; + text1_index++; + } + + /* trim right */ + for (index = len - 1; index >= 0; index--) + { + if (text1[index] > 32) + { + break; + } + } + + text1_index = index + 1; + text1[text1_index] = 0; + break; + case 1: /* trim left */ + got_char = 0; + + for (index = 0; index < len; index++) + { + if (got_char) + { + text1[text1_index] = text[index]; + text1_index++; + } + else + { + if (text[index] > 32) + { + text1[text1_index] = text[index]; + text1_index++; + got_char = 1; + } + } + } + + text1[text1_index] = 0; + break; + } + + wcstombs(str, text1, text1_index + 1); + free(text); + free(text1); return 0; - } - if ((trim_flags < 1) || (trim_flags > 4)) - { - return 1; - } - text = (wchar_t*)malloc(len * sizeof(wchar_t) + 8); - text1 = (wchar_t*)malloc(len * sizeof(wchar_t) + 8); - text1_index = 0; - mbstowcs(text, str, len + 1); - switch (trim_flags) - { - case 4: /* trim through */ - for (index = 0; index < len; index++) - { - if (text[index] > 32) - { - text1[text1_index] = text[index]; - text1_index++; - } - } - text1[text1_index] = 0; - break; - case 3: /* trim both */ - got_char = 0; - for (index = 0; index < len; index++) - { - if (got_char) - { - text1[text1_index] = text[index]; - text1_index++; - } - else - { - if (text[index] > 32) - { - text1[text1_index] = text[index]; - text1_index++; - got_char = 1; - } - } - } - text1[text1_index] = 0; - len = text1_index; - /* trim right */ - for (index = len - 1; index >= 0; index--) - { - if (text1[index] > 32) - { - break; - } - } - text1_index = index + 1; - text1[text1_index] = 0; - break; - case 2: /* trim right */ - /* copy it */ - for (index = 0; index < len; index++) - { - text1[text1_index] = text[index]; - text1_index++; - } - /* trim right */ - for (index = len - 1; index >= 0; index--) - { - if (text1[index] > 32) - { - break; - } - } - text1_index = index + 1; - text1[text1_index] = 0; - break; - case 1: /* trim left */ - got_char = 0; - for (index = 0; index < len; index++) - { - if (got_char) - { - text1[text1_index] = text[index]; - text1_index++; - } - else - { - if (text[index] > 32) - { - text1[text1_index] = text[index]; - text1_index++; - got_char = 1; - } - } - } - text1[text1_index] = 0; - break; - } - wcstombs(str, text1, text1_index + 1); - free(text); - free(text1); - return 0; } /*****************************************************************************/ long APP_CC -g_load_library(char* in) +g_load_library(char *in) { #if defined(_WIN32) - return (long)LoadLibraryA(in); + return (long)LoadLibraryA(in); #else - return (long)dlopen(in, RTLD_LOCAL | RTLD_LAZY); + return (long)dlopen(in, RTLD_LOCAL | RTLD_LAZY); #endif } @@ -1973,54 +2141,56 @@ g_load_library(char* in) int APP_CC g_free_library(long lib) { - if (lib == 0) - { - return 0; - } + if (lib == 0) + { + return 0; + } + #if defined(_WIN32) - return FreeLibrary((HMODULE)lib); + return FreeLibrary((HMODULE)lib); #else - return dlclose((void*)lib); + return dlclose((void *)lib); #endif } /*****************************************************************************/ /* returns NULL if not found */ -void* APP_CC -g_get_proc_address(long lib, const char* name) +void *APP_CC +g_get_proc_address(long lib, const char *name) { - if (lib == 0) - { - return 0; - } + if (lib == 0) + { + return 0; + } + #if defined(_WIN32) - return GetProcAddress((HMODULE)lib, name); + return GetProcAddress((HMODULE)lib, name); #else - return dlsym((void*)lib, name); + return dlsym((void *)lib, name); #endif } /*****************************************************************************/ /* does not work in win32 */ int APP_CC -g_system(char* aexec) +g_system(char *aexec) { #if defined(_WIN32) - return 0; + return 0; #else - return system(aexec); + return system(aexec); #endif } /*****************************************************************************/ /* does not work in win32 */ -char* APP_CC +char *APP_CC g_get_strerror(void) { #if defined(_WIN32) - return 0; + return 0; #else - return strerror(errno); + return strerror(errno); #endif } @@ -2029,43 +2199,43 @@ int APP_CC g_get_errno(void) { #if defined(_WIN32) - return GetLastError(); + return GetLastError(); #else - return errno; + return errno; #endif } /*****************************************************************************/ /* does not work in win32 */ int APP_CC -g_execvp(const char* p1, char* args[]) +g_execvp(const char *p1, char *args[]) { #if defined(_WIN32) - return 0; + return 0; #else - int rv; + int rv; - g_rm_temp_dir(); - rv = execvp(p1, args); - g_mk_temp_dir(0); - return rv; + g_rm_temp_dir(); + rv = execvp(p1, args); + g_mk_temp_dir(0); + return rv; #endif } /*****************************************************************************/ /* does not work in win32 */ int APP_CC -g_execlp3(const char* a1, const char* a2, const char* a3) +g_execlp3(const char *a1, const char *a2, const char *a3) { #if defined(_WIN32) - return 0; + return 0; #else - int rv; + int rv; - g_rm_temp_dir(); - rv = execlp(a1, a2, a3, (void*)0); - g_mk_temp_dir(0); - return rv; + g_rm_temp_dir(); + rv = execlp(a1, a2, a3, (void *)0); + g_mk_temp_dir(0); + return rv; #endif } @@ -2076,7 +2246,7 @@ g_signal_child_stop(void (*func)(int)) { #if defined(_WIN32) #else - signal(SIGCHLD, func); + signal(SIGCHLD, func); #endif } @@ -2087,7 +2257,7 @@ g_signal_hang_up(void (*func)(int)) { #if defined(_WIN32) #else - signal(SIGHUP, func); + signal(SIGHUP, func); #endif } @@ -2098,7 +2268,7 @@ g_signal_user_interrupt(void (*func)(int)) { #if defined(_WIN32) #else - signal(SIGINT, func); + signal(SIGINT, func); #endif } @@ -2109,7 +2279,7 @@ g_signal_kill(void (*func)(int)) { #if defined(_WIN32) #else - signal(SIGKILL, func); + signal(SIGKILL, func); #endif } @@ -2120,7 +2290,7 @@ g_signal_terminate(void (*func)(int)) { #if defined(_WIN32) #else - signal(SIGTERM, func); + signal(SIGTERM, func); #endif } @@ -2131,7 +2301,7 @@ g_signal_pipe(void (*func)(int)) { #if defined(_WIN32) #else - signal(SIGPIPE, func); + signal(SIGPIPE, func); #endif } @@ -2142,7 +2312,7 @@ g_signal_usr1(void (*func)(int)) { #if defined(_WIN32) #else - signal(SIGUSR1, func); + signal(SIGUSR1, func); #endif } @@ -2152,16 +2322,18 @@ int APP_CC g_fork(void) { #if defined(_WIN32) - return 0; + return 0; #else - int rv; + int rv; - rv = fork(); - if (rv == 0) /* child */ - { - g_mk_temp_dir(0); - } - return rv; + rv = fork(); + + if (rv == 0) /* child */ + { + g_mk_temp_dir(0); + } + + return rv; #endif } @@ -2171,9 +2343,9 @@ int APP_CC g_setgid(int pid) { #if defined(_WIN32) - return 0; + return 0; #else - return setgid(pid); + return setgid(pid); #endif } @@ -2181,12 +2353,12 @@ g_setgid(int pid) /* returns error, zero is success, non zero is error */ /* does not work in win32 */ int APP_CC -g_initgroups(const char* user, int gid) +g_initgroups(const char *user, int gid) { #if defined(_WIN32) - return 0; + return 0; #else - return initgroups(user, gid); + return initgroups(user, gid); #endif } @@ -2197,9 +2369,9 @@ int APP_CC g_getuid(void) { #if defined(_WIN32) - return 0; + return 0; #else - return getuid(); + return getuid(); #endif } @@ -2210,9 +2382,9 @@ int APP_CC g_setuid(int pid) { #if defined(_WIN32) - return 0; + return 0; #else - return setuid(pid); + return setuid(pid); #endif } @@ -2223,20 +2395,22 @@ int APP_CC g_waitchild(void) { #if defined(_WIN32) - return 0; + return 0; #else - int wstat; - int rv; + int wstat; + int rv; + + rv = waitpid(0, &wstat, WNOHANG); - rv = waitpid(0, &wstat, WNOHANG); - if (rv == -1) - { - if (errno == EINTR) /* signal occurred */ + if (rv == -1) { - rv = 0; + if (errno == EINTR) /* signal occurred */ + { + rv = 0; + } } - } - return rv; + + return rv; #endif } @@ -2247,23 +2421,28 @@ int APP_CC g_waitpid(int pid) { #if defined(_WIN32) - return 0; + return 0; #else - int rv = 0; - if (pid < 0) { - rv = -1; - } - else { - rv = waitpid(pid, 0, 0); - if (rv == -1) + int rv = 0; + + if (pid < 0) { - if (errno == EINTR) /* signal occurred */ - { - rv = 0; - } + rv = -1; } - } - return rv; + else + { + rv = waitpid(pid, 0, 0); + + if (rv == -1) + { + if (errno == EINTR) /* signal occurred */ + { + rv = 0; + } + } + } + + return rv; #endif } @@ -2274,31 +2453,31 @@ g_clearenv(void) { #if defined(_WIN32) #else - environ = 0; + environ = 0; #endif } /*****************************************************************************/ /* does not work in win32 */ int APP_CC -g_setenv(const char* name, const char* value, int rewrite) +g_setenv(const char *name, const char *value, int rewrite) { #if defined(_WIN32) - return 0; + return 0; #else - return setenv(name, value, rewrite); + return setenv(name, value, rewrite); #endif } /*****************************************************************************/ /* does not work in win32 */ -char* APP_CC -g_getenv(const char* name) +char *APP_CC +g_getenv(const char *name) { #if defined(_WIN32) - return 0; + return 0; #else - return getenv(name); + return getenv(name); #endif } @@ -2306,8 +2485,8 @@ g_getenv(const char* name) int APP_CC g_exit(int exit_code) { - _exit(exit_code); - return 0; + _exit(exit_code); + return 0; } /*****************************************************************************/ @@ -2315,9 +2494,9 @@ int APP_CC g_getpid(void) { #if defined(_WIN32) - return (int)GetCurrentProcessId(); + return (int)GetCurrentProcessId(); #else - return (int)getpid(); + return (int)getpid(); #endif } @@ -2327,9 +2506,9 @@ int APP_CC g_sigterm(int pid) { #if defined(_WIN32) - return 0; + return 0; #else - return kill(pid, SIGTERM); + return kill(pid, SIGTERM); #endif } @@ -2337,40 +2516,47 @@ g_sigterm(int pid) /* returns 0 if ok */ /* does not work in win32 */ int APP_CC -g_getuser_info(const char* username, int* gid, int* uid, char* shell, - char* dir, char* gecos) +g_getuser_info(const char *username, int *gid, int *uid, char *shell, + char *dir, char *gecos) { #if defined(_WIN32) - return 1; + return 1; #else - struct passwd* pwd_1; + struct passwd *pwd_1; - pwd_1 = getpwnam(username); - if (pwd_1 != 0) - { - if (gid != 0) - { - *gid = pwd_1->pw_gid; - } - if (uid != 0) - { - *uid = pwd_1->pw_uid; - } - if (dir != 0) - { - g_strcpy(dir, pwd_1->pw_dir); - } - if (shell != 0) - { - g_strcpy(shell, pwd_1->pw_shell); - } - if (gecos != 0) + pwd_1 = getpwnam(username); + + if (pwd_1 != 0) { - g_strcpy(gecos, pwd_1->pw_gecos); + if (gid != 0) + { + *gid = pwd_1->pw_gid; + } + + if (uid != 0) + { + *uid = pwd_1->pw_uid; + } + + if (dir != 0) + { + g_strcpy(dir, pwd_1->pw_dir); + } + + if (shell != 0) + { + g_strcpy(shell, pwd_1->pw_shell); + } + + if (gecos != 0) + { + g_strcpy(gecos, pwd_1->pw_gecos); + } + + return 0; } - return 0; - } - return 1; + + return 1; #endif } @@ -2378,23 +2564,26 @@ g_getuser_info(const char* username, int* gid, int* uid, char* shell, /* returns 0 if ok */ /* does not work in win32 */ int APP_CC -g_getgroup_info(const char* groupname, int* gid) +g_getgroup_info(const char *groupname, int *gid) { #if defined(_WIN32) - return 1; + return 1; #else - struct group* g; + struct group *g; + + g = getgrnam(groupname); - g = getgrnam(groupname); - if (g != 0) - { - if (gid != 0) + if (g != 0) { - *gid = g->gr_gid; + if (gid != 0) + { + *gid = g->gr_gid; + } + + return 0; } - return 0; - } - return 1; + + return 1; #endif } @@ -2403,31 +2592,36 @@ g_getgroup_info(const char* groupname, int* gid) /* if zero is returned, then ok is set */ /* does not work in win32 */ int APP_CC -g_check_user_in_group(const char* username, int gid, int* ok) +g_check_user_in_group(const char *username, int gid, int *ok) { #if defined(_WIN32) - return 1; + return 1; #else - struct group* groups; - int i; + struct group *groups; + int i; - groups = getgrgid(gid); - if (groups == 0) - { - return 1; - } - *ok = 0; - i = 0; - while (0 != groups->gr_mem[i]) - { - if (0 == g_strcmp(groups->gr_mem[i], username)) + groups = getgrgid(gid); + + if (groups == 0) + { + return 1; + } + + *ok = 0; + i = 0; + + while (0 != groups->gr_mem[i]) { - *ok = 1; - break; + if (0 == g_strcmp(groups->gr_mem[i], username)) + { + *ok = 1; + break; + } + + i++; } - i++; - } - return 0; + + return 0; #endif } @@ -2440,9 +2634,9 @@ int APP_CC g_time1(void) { #if defined(_WIN32) - return GetTickCount() / 1000; + return GetTickCount() / 1000; #else - return time(0); + return time(0); #endif } @@ -2453,13 +2647,13 @@ int APP_CC g_time2(void) { #if defined(_WIN32) - return (int)GetTickCount(); + return (int)GetTickCount(); #else - struct tms tm; - clock_t num_ticks = 0; - g_memset(&tm,0,sizeof(struct tms)); - num_ticks = times(&tm); - return (int)(num_ticks * 10); + struct tms tm; + clock_t num_ticks = 0; + g_memset(&tm, 0, sizeof(struct tms)); + num_ticks = times(&tm); + return (int)(num_ticks * 10); #endif } @@ -2470,11 +2664,11 @@ int APP_CC g_time3(void) { #if defined(_WIN32) - return 0; + return 0; #else - struct timeval tp; + struct timeval tp; - gettimeofday(&tp, 0); - return (tp.tv_sec * 1000) + (tp.tv_usec / 1000); + gettimeofday(&tp, 0); + return (tp.tv_sec * 1000) + (tp.tv_usec / 1000); #endif } diff --git a/common/os_calls.h b/common/os_calls.h index 41b221bf..c734c9fe 100644 --- a/common/os_calls.h +++ b/common/os_calls.h @@ -1,26 +1,22 @@ -/* - Copyright (c) 2004-2012 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - generic operating system calls -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * generic operating system calls + */ #if !defined(OS_CALLS_H) #define OS_CALLS_H diff --git a/common/parse.h b/common/parse.h index 9bd6850c..deee7845 100644 --- a/common/parse.h +++ b/common/parse.h @@ -1,30 +1,26 @@ -/* - Copyright (c) 2004-2010 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - Parsing structs and macros - - based on parse.h from rdesktop - this is a super fast stream method, you bet - needed functions g_malloc, g_free, g_memset, g_memcpy -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Parsing structs and macros + * + * based on parse.h from rdesktop + * this is a super fast stream method, you bet + * needed functions g_malloc, g_free, g_memset, g_memcpy + */ #if !defined(PARSE_H) #define PARSE_H diff --git a/common/ssl_calls.c b/common/ssl_calls.c index 3d37ed6d..4cb706f3 100644 --- a/common/ssl_calls.c +++ b/common/ssl_calls.c @@ -1,24 +1,22 @@ -/* - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - xrdp: A Remote Desktop Protocol server. - Copyright (C) Jay Sorg 2004-2010 - - ssl calls - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ssl calls + */ #include <stdlib.h> /* needed for openssl headers */ #include <openssl/ssl.h> @@ -43,197 +41,200 @@ int ssl_init(void) { - SSL_load_error_strings(); - SSL_library_init(); - return 0; + SSL_load_error_strings(); + SSL_library_init(); + return 0; } /*****************************************************************************/ int ssl_finish(void) { - return 0; + return 0; } /* rc4 stuff */ /*****************************************************************************/ -void* APP_CC +void *APP_CC ssl_rc4_info_create(void) { - return g_malloc(sizeof(RC4_KEY), 1); + return g_malloc(sizeof(RC4_KEY), 1); } /*****************************************************************************/ void APP_CC -ssl_rc4_info_delete(void* rc4_info) +ssl_rc4_info_delete(void *rc4_info) { - g_free(rc4_info); + g_free(rc4_info); } /*****************************************************************************/ void APP_CC -ssl_rc4_set_key(void* rc4_info, char* key, int len) +ssl_rc4_set_key(void *rc4_info, char *key, int len) { - RC4_set_key((RC4_KEY*)rc4_info, len, (tui8*)key); + RC4_set_key((RC4_KEY *)rc4_info, len, (tui8 *)key); } /*****************************************************************************/ void APP_CC -ssl_rc4_crypt(void* rc4_info, char* data, int len) +ssl_rc4_crypt(void *rc4_info, char *data, int len) { - RC4((RC4_KEY*)rc4_info, len, (tui8*)data, (tui8*)data); + RC4((RC4_KEY *)rc4_info, len, (tui8 *)data, (tui8 *)data); } /* sha1 stuff */ /*****************************************************************************/ -void* APP_CC +void *APP_CC ssl_sha1_info_create(void) { - return g_malloc(sizeof(SHA_CTX), 1); + return g_malloc(sizeof(SHA_CTX), 1); } /*****************************************************************************/ void APP_CC -ssl_sha1_info_delete(void* sha1_info) +ssl_sha1_info_delete(void *sha1_info) { - g_free(sha1_info); + g_free(sha1_info); } /*****************************************************************************/ void APP_CC -ssl_sha1_clear(void* sha1_info) +ssl_sha1_clear(void *sha1_info) { - SHA1_Init((SHA_CTX*)sha1_info); + SHA1_Init((SHA_CTX *)sha1_info); } /*****************************************************************************/ void APP_CC -ssl_sha1_transform(void* sha1_info, char* data, int len) +ssl_sha1_transform(void *sha1_info, char *data, int len) { - SHA1_Update((SHA_CTX*)sha1_info, data, len); + SHA1_Update((SHA_CTX *)sha1_info, data, len); } /*****************************************************************************/ void APP_CC -ssl_sha1_complete(void* sha1_info, char* data) +ssl_sha1_complete(void *sha1_info, char *data) { - SHA1_Final((tui8*)data, (SHA_CTX*)sha1_info); + SHA1_Final((tui8 *)data, (SHA_CTX *)sha1_info); } /* md5 stuff */ /*****************************************************************************/ -void* APP_CC +void *APP_CC ssl_md5_info_create(void) { - return g_malloc(sizeof(MD5_CTX), 1); + return g_malloc(sizeof(MD5_CTX), 1); } /*****************************************************************************/ void APP_CC -ssl_md5_info_delete(void* md5_info) +ssl_md5_info_delete(void *md5_info) { - g_free(md5_info); + g_free(md5_info); } /*****************************************************************************/ void APP_CC -ssl_md5_clear(void* md5_info) +ssl_md5_clear(void *md5_info) { - MD5_Init((MD5_CTX*)md5_info); + MD5_Init((MD5_CTX *)md5_info); } /*****************************************************************************/ void APP_CC -ssl_md5_transform(void* md5_info, char* data, int len) +ssl_md5_transform(void *md5_info, char *data, int len) { - MD5_Update((MD5_CTX*)md5_info, data, len); + MD5_Update((MD5_CTX *)md5_info, data, len); } /*****************************************************************************/ void APP_CC -ssl_md5_complete(void* md5_info, char* data) +ssl_md5_complete(void *md5_info, char *data) { - MD5_Final((tui8*)data, (MD5_CTX*)md5_info); + MD5_Final((tui8 *)data, (MD5_CTX *)md5_info); } /*****************************************************************************/ static void APP_CC -ssl_reverse_it(char* p, int len) +ssl_reverse_it(char *p, int len) { - int i; - int j; - char temp; - - i = 0; - j = len - 1; - while (i < j) - { - temp = p[i]; - p[i] = p[j]; - p[j] = temp; - i++; - j--; - } + int i; + int j; + char temp; + + i = 0; + j = len - 1; + + while (i < j) + { + temp = p[i]; + p[i] = p[j]; + p[j] = temp; + i++; + j--; + } } /*****************************************************************************/ int APP_CC -ssl_mod_exp(char* out, int out_len, char* in, int in_len, - char* mod, int mod_len, char* exp, int exp_len) +ssl_mod_exp(char *out, int out_len, char *in, int in_len, + char *mod, int mod_len, char *exp, int exp_len) { - BN_CTX* ctx; - BIGNUM lmod; - BIGNUM lexp; - BIGNUM lin; - BIGNUM lout; - int rv; - char* l_out; - char* l_in; - char* l_mod; - char* l_exp; - - l_out = (char*)g_malloc(out_len, 1); - l_in = (char*)g_malloc(in_len, 1); - l_mod = (char*)g_malloc(mod_len, 1); - l_exp = (char*)g_malloc(exp_len, 1); - g_memcpy(l_in, in, in_len); - g_memcpy(l_mod, mod, mod_len); - g_memcpy(l_exp, exp, exp_len); - ssl_reverse_it(l_in, in_len); - ssl_reverse_it(l_mod, mod_len); - ssl_reverse_it(l_exp, exp_len); - ctx = BN_CTX_new(); - BN_init(&lmod); - BN_init(&lexp); - BN_init(&lin); - BN_init(&lout); - BN_bin2bn((tui8*)l_mod, mod_len, &lmod); - BN_bin2bn((tui8*)l_exp, exp_len, &lexp); - BN_bin2bn((tui8*)l_in, in_len, &lin); - BN_mod_exp(&lout, &lin, &lexp, &lmod, ctx); - rv = BN_bn2bin(&lout, (tui8*)l_out); - if (rv <= out_len) - { - ssl_reverse_it(l_out, rv); - g_memcpy(out, l_out, out_len); - } - else - { - rv = 0; - } - BN_free(&lin); - BN_free(&lout); - BN_free(&lexp); - BN_free(&lmod); - BN_CTX_free(ctx); - g_free(l_out); - g_free(l_in); - g_free(l_mod); - g_free(l_exp); - return rv; + BN_CTX *ctx; + BIGNUM lmod; + BIGNUM lexp; + BIGNUM lin; + BIGNUM lout; + int rv; + char *l_out; + char *l_in; + char *l_mod; + char *l_exp; + + l_out = (char *)g_malloc(out_len, 1); + l_in = (char *)g_malloc(in_len, 1); + l_mod = (char *)g_malloc(mod_len, 1); + l_exp = (char *)g_malloc(exp_len, 1); + g_memcpy(l_in, in, in_len); + g_memcpy(l_mod, mod, mod_len); + g_memcpy(l_exp, exp, exp_len); + ssl_reverse_it(l_in, in_len); + ssl_reverse_it(l_mod, mod_len); + ssl_reverse_it(l_exp, exp_len); + ctx = BN_CTX_new(); + BN_init(&lmod); + BN_init(&lexp); + BN_init(&lin); + BN_init(&lout); + BN_bin2bn((tui8 *)l_mod, mod_len, &lmod); + BN_bin2bn((tui8 *)l_exp, exp_len, &lexp); + BN_bin2bn((tui8 *)l_in, in_len, &lin); + BN_mod_exp(&lout, &lin, &lexp, &lmod, ctx); + rv = BN_bn2bin(&lout, (tui8 *)l_out); + + if (rv <= out_len) + { + ssl_reverse_it(l_out, rv); + g_memcpy(out, l_out, out_len); + } + else + { + rv = 0; + } + + BN_free(&lin); + BN_free(&lout); + BN_free(&lexp); + BN_free(&lmod); + BN_CTX_free(ctx); + g_free(l_out); + g_free(l_in); + g_free(l_mod); + g_free(l_exp); + return rv; } #if defined(OLD_RSA_GEN1) @@ -242,61 +243,68 @@ ssl_mod_exp(char* out, int out_len, char* in, int in_len, generates a new rsa key exp is passed in and mod and pri are passed out */ int APP_CC -ssl_gen_key_xrdp1(int key_size_in_bits, char* exp, int exp_len, - char* mod, int mod_len, char* pri, int pri_len) +ssl_gen_key_xrdp1(int key_size_in_bits, char *exp, int exp_len, + char *mod, int mod_len, char *pri, int pri_len) { - int my_e; - RSA* my_key; - char* lmod; - char* lpri; - tui8* lexp; - int error; - int len; - - if ((exp_len != 4) || (mod_len != 64) || (pri_len != 64)) - { - return 1; - } - lmod = (char*)g_malloc(mod_len, 0); - lpri = (char*)g_malloc(pri_len, 0); - lexp = (tui8*)exp; - my_e = lexp[0]; - my_e |= lexp[1] << 8; - my_e |= lexp[2] << 16; - my_e |= lexp[3] << 24; - /* srand is in stdlib.h */ - srand(g_time1()); - my_key = RSA_generate_key(key_size_in_bits, my_e, 0, 0); - error = my_key == 0; - if (error == 0) - { - len = BN_num_bytes(my_key->n); - error = len != mod_len; - } - if (error == 0) - { - BN_bn2bin(my_key->n, (tui8*)lmod); - ssl_reverse_it(lmod, mod_len); - } - if (error == 0) - { - len = BN_num_bytes(my_key->d); - error = len != pri_len; - } - if (error == 0) - { - BN_bn2bin(my_key->d, (tui8*)lpri); - ssl_reverse_it(lpri, pri_len); - } - if (error == 0) - { - g_memcpy(mod, lmod, mod_len); - g_memcpy(pri, lpri, pri_len); - } - RSA_free(my_key); - g_free(lmod); - g_free(lpri); - return error; + int my_e; + RSA *my_key; + char *lmod; + char *lpri; + tui8 *lexp; + int error; + int len; + + if ((exp_len != 4) || (mod_len != 64) || (pri_len != 64)) + { + return 1; + } + + lmod = (char *)g_malloc(mod_len, 0); + lpri = (char *)g_malloc(pri_len, 0); + lexp = (tui8 *)exp; + my_e = lexp[0]; + my_e |= lexp[1] << 8; + my_e |= lexp[2] << 16; + my_e |= lexp[3] << 24; + /* srand is in stdlib.h */ + srand(g_time1()); + my_key = RSA_generate_key(key_size_in_bits, my_e, 0, 0); + error = my_key == 0; + + if (error == 0) + { + len = BN_num_bytes(my_key->n); + error = len != mod_len; + } + + if (error == 0) + { + BN_bn2bin(my_key->n, (tui8 *)lmod); + ssl_reverse_it(lmod, mod_len); + } + + if (error == 0) + { + len = BN_num_bytes(my_key->d); + error = len != pri_len; + } + + if (error == 0) + { + BN_bn2bin(my_key->d, (tui8 *)lpri); + ssl_reverse_it(lpri, pri_len); + } + + if (error == 0) + { + g_memcpy(mod, lmod, mod_len); + g_memcpy(pri, lpri, pri_len); + } + + RSA_free(my_key); + g_free(lmod); + g_free(lpri); + return error; } #else /*****************************************************************************/ @@ -304,60 +312,67 @@ ssl_gen_key_xrdp1(int key_size_in_bits, char* exp, int exp_len, generates a new rsa key exp is passed in and mod and pri are passed out */ int APP_CC -ssl_gen_key_xrdp1(int key_size_in_bits, char* exp, int exp_len, - char* mod, int mod_len, char* pri, int pri_len) +ssl_gen_key_xrdp1(int key_size_in_bits, char *exp, int exp_len, + char *mod, int mod_len, char *pri, int pri_len) { - BIGNUM* my_e; - RSA* my_key; - char* lexp; - char* lmod; - char* lpri; - int error; - int len; - - if ((exp_len != 4) || (mod_len != 64) || (pri_len != 64)) - { - return 1; - } - lexp = (char*)g_malloc(exp_len, 0); - lmod = (char*)g_malloc(mod_len, 0); - lpri = (char*)g_malloc(pri_len, 0); - g_memcpy(lexp, exp, exp_len); - ssl_reverse_it(lexp, exp_len); - my_e = BN_new(); - BN_bin2bn((tui8*)lexp, exp_len, my_e); - my_key = RSA_new(); - error = RSA_generate_key_ex(my_key, key_size_in_bits, my_e, 0) == 0; - if (error == 0) - { - len = BN_num_bytes(my_key->n); - error = len != mod_len; - } - if (error == 0) - { - BN_bn2bin(my_key->n, (tui8*)lmod); - ssl_reverse_it(lmod, mod_len); - } - if (error == 0) - { - len = BN_num_bytes(my_key->d); - error = len != pri_len; - } - if (error == 0) - { - BN_bn2bin(my_key->d, (tui8*)lpri); - ssl_reverse_it(lpri, pri_len); - } - if (error == 0) - { - g_memcpy(mod, lmod, mod_len); - g_memcpy(pri, lpri, pri_len); - } - BN_free(my_e); - RSA_free(my_key); - g_free(lexp); - g_free(lmod); - g_free(lpri); - return error; + BIGNUM *my_e; + RSA *my_key; + char *lexp; + char *lmod; + char *lpri; + int error; + int len; + + if ((exp_len != 4) || (mod_len != 64) || (pri_len != 64)) + { + return 1; + } + + lexp = (char *)g_malloc(exp_len, 0); + lmod = (char *)g_malloc(mod_len, 0); + lpri = (char *)g_malloc(pri_len, 0); + g_memcpy(lexp, exp, exp_len); + ssl_reverse_it(lexp, exp_len); + my_e = BN_new(); + BN_bin2bn((tui8 *)lexp, exp_len, my_e); + my_key = RSA_new(); + error = RSA_generate_key_ex(my_key, key_size_in_bits, my_e, 0) == 0; + + if (error == 0) + { + len = BN_num_bytes(my_key->n); + error = len != mod_len; + } + + if (error == 0) + { + BN_bn2bin(my_key->n, (tui8 *)lmod); + ssl_reverse_it(lmod, mod_len); + } + + if (error == 0) + { + len = BN_num_bytes(my_key->d); + error = len != pri_len; + } + + if (error == 0) + { + BN_bn2bin(my_key->d, (tui8 *)lpri); + ssl_reverse_it(lpri, pri_len); + } + + if (error == 0) + { + g_memcpy(mod, lmod, mod_len); + g_memcpy(pri, lpri, pri_len); + } + + BN_free(my_e); + RSA_free(my_key); + g_free(lexp); + g_free(lmod); + g_free(lpri); + return error; } #endif diff --git a/common/ssl_calls.h b/common/ssl_calls.h index d78bdb0b..f4a63155 100644 --- a/common/ssl_calls.h +++ b/common/ssl_calls.h @@ -1,22 +1,20 @@ -/* - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - xrdp: A Remote Desktop Protocol server. - Copyright (C) Jay Sorg 2004-2010 - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #if !defined(SSL_CALLS_H) #define SSL_CALLS_H diff --git a/common/thread_calls.c b/common/thread_calls.c index 80856fd8..ad944d02 100644 --- a/common/thread_calls.c +++ b/common/thread_calls.c @@ -1,27 +1,22 @@ -/* - Copyright (c) 2004-2010 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - thread calls - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * thread calls + */ #if defined(_WIN32) #include <windows.h> @@ -38,33 +33,36 @@ /* returns error */ #if defined(_WIN32) int APP_CC -tc_thread_create(unsigned long (__stdcall * start_routine)(void*), void* arg) +tc_thread_create(unsigned long (__stdcall *start_routine)(void *), void *arg) { - int rv = 0; - DWORD thread_id = 0; - HANDLE thread = (HANDLE)0; - - /* CreateThread returns handle or zero on error */ - thread = CreateThread(0, 0, start_routine, arg, 0, &thread_id); - rv = !thread; - CloseHandle(thread); - return rv; + int rv = 0; + DWORD thread_id = 0; + HANDLE thread = (HANDLE)0; + + /* CreateThread returns handle or zero on error */ + thread = CreateThread(0, 0, start_routine, arg, 0, &thread_id); + rv = !thread; + CloseHandle(thread); + return rv; } #else int APP_CC -tc_thread_create(void* (* start_routine)(void *), void* arg) +tc_thread_create(void * (* start_routine)(void *), void *arg) { - int rv = 0; - pthread_t thread = (pthread_t)0; + int rv = 0; + pthread_t thread = (pthread_t)0; - g_memset(&thread, 0x00, sizeof(pthread_t)); + g_memset(&thread, 0x00, sizeof(pthread_t)); - /* pthread_create returns error */ - rv = pthread_create(&thread, 0, start_routine, arg); - if (!rv) { - rv = pthread_detach(thread); - } - return rv; + /* pthread_create returns error */ + rv = pthread_create(&thread, 0, start_routine, arg); + + if (!rv) + { + rv = pthread_detach(thread); + } + + return rv; } #endif @@ -73,9 +71,9 @@ tbus APP_CC tc_get_threadid(void) { #if defined(_WIN32) - return (tbus)GetCurrentThreadId(); + return (tbus)GetCurrentThreadId(); #else - return (tbus)pthread_self(); + return (tbus)pthread_self(); #endif } @@ -85,9 +83,9 @@ int APP_CC tc_threadid_equal(tbus tid1, tbus tid2) { #if defined(_WIN32) - return tid1 == tid2; + return tid1 == tid2; #else - return pthread_equal((pthread_t)tid1, (pthread_t)tid2); + return pthread_equal((pthread_t)tid1, (pthread_t)tid2); #endif } @@ -96,13 +94,13 @@ tbus APP_CC tc_mutex_create(void) { #if defined(_WIN32) - return (tbus)CreateMutex(0, 0, 0); + return (tbus)CreateMutex(0, 0, 0); #else - pthread_mutex_t* lmutex; + pthread_mutex_t *lmutex; - lmutex = (pthread_mutex_t*)g_malloc(sizeof(pthread_mutex_t), 0); - pthread_mutex_init(lmutex, 0); - return (tbus)lmutex; + lmutex = (pthread_mutex_t *)g_malloc(sizeof(pthread_mutex_t), 0); + pthread_mutex_init(lmutex, 0); + return (tbus)lmutex; #endif } @@ -111,13 +109,13 @@ void APP_CC tc_mutex_delete(tbus mutex) { #if defined(_WIN32) - CloseHandle((HANDLE)mutex); + CloseHandle((HANDLE)mutex); #else - pthread_mutex_t* lmutex; + pthread_mutex_t *lmutex; - lmutex = (pthread_mutex_t*)mutex; - pthread_mutex_destroy(lmutex); - g_free(lmutex); + lmutex = (pthread_mutex_t *)mutex; + pthread_mutex_destroy(lmutex); + g_free(lmutex); #endif } @@ -126,11 +124,11 @@ int APP_CC tc_mutex_lock(tbus mutex) { #if defined(_WIN32) - WaitForSingleObject((HANDLE)mutex, INFINITE); - return 0; + WaitForSingleObject((HANDLE)mutex, INFINITE); + return 0; #else - pthread_mutex_lock((pthread_mutex_t*)mutex); - return 0; + pthread_mutex_lock((pthread_mutex_t *)mutex); + return 0; #endif } @@ -138,15 +136,18 @@ tc_mutex_lock(tbus mutex) int APP_CC tc_mutex_unlock(tbus mutex) { - int rv = 0; + int rv = 0; #if defined(_WIN32) - ReleaseMutex((HANDLE)mutex); + ReleaseMutex((HANDLE)mutex); #else - if (mutex != 0) { - rv = pthread_mutex_unlock((pthread_mutex_t *)mutex); - } + + if (mutex != 0) + { + rv = pthread_mutex_unlock((pthread_mutex_t *)mutex); + } + #endif - return rv; + return rv; } /*****************************************************************************/ @@ -154,16 +155,16 @@ tbus APP_CC tc_sem_create(int init_count) { #if defined(_WIN32) - HANDLE sem; + HANDLE sem; - sem = CreateSemaphore(0, init_count, init_count + 10, 0); - return (tbus)sem; + sem = CreateSemaphore(0, init_count, init_count + 10, 0); + return (tbus)sem; #else - sem_t * sem = (sem_t *)NULL; + sem_t *sem = (sem_t *)NULL; - sem = (sem_t *)g_malloc(sizeof(sem_t), 0); - sem_init(sem, 0, init_count); - return (tbus)sem; + sem = (sem_t *)g_malloc(sizeof(sem_t), 0); + sem_init(sem, 0, init_count); + return (tbus)sem; #endif } @@ -172,13 +173,13 @@ void APP_CC tc_sem_delete(tbus sem) { #if defined(_WIN32) - CloseHandle((HANDLE)sem); + CloseHandle((HANDLE)sem); #else - sem_t* lsem; + sem_t *lsem; - lsem = (sem_t*)sem; - sem_destroy(lsem); - g_free(lsem); + lsem = (sem_t *)sem; + sem_destroy(lsem); + g_free(lsem); #endif } @@ -187,11 +188,11 @@ int APP_CC tc_sem_dec(tbus sem) { #if defined(_WIN32) - WaitForSingleObject((HANDLE)sem, INFINITE); - return 0; + WaitForSingleObject((HANDLE)sem, INFINITE); + return 0; #else - sem_wait((sem_t*)sem); - return 0; + sem_wait((sem_t *)sem); + return 0; #endif } @@ -200,10 +201,10 @@ int APP_CC tc_sem_inc(tbus sem) { #if defined(_WIN32) - ReleaseSemaphore((HANDLE)sem, 1, 0); - return 0; + ReleaseSemaphore((HANDLE)sem, 1, 0); + return 0; #else - sem_post((sem_t*)sem); - return 0; + sem_post((sem_t *)sem); + return 0; #endif } diff --git a/common/thread_calls.h b/common/thread_calls.h index cc40a7d9..0c853d22 100644 --- a/common/thread_calls.h +++ b/common/thread_calls.h @@ -1,27 +1,22 @@ -/* - Copyright (c) 2004-2010 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - thread calls - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * thread calls + */ #if !defined(THREAD_CALLS_H) #define THREAD_CALLS_H diff --git a/common/trans.c b/common/trans.c index 6b762d00..c287d310 100644 --- a/common/trans.c +++ b/common/trans.c @@ -1,27 +1,22 @@ -/* - Copyright (c) 2008-2010 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - generic transport - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * generic transport + */ #include "os_calls.h" #include "trans.h" @@ -29,394 +24,440 @@ #include "parse.h" /*****************************************************************************/ -struct trans* APP_CC +struct trans *APP_CC trans_create(int mode, int in_size, int out_size) { - struct trans* self = (struct trans *)NULL; - - self = (struct trans*)g_malloc(sizeof(struct trans), 1); - if (self != NULL) { - make_stream(self->in_s); - init_stream(self->in_s, in_size); - make_stream(self->out_s); - init_stream(self->out_s, out_size); - self->mode = mode; - } - return self; + struct trans *self = (struct trans *)NULL; + + self = (struct trans *)g_malloc(sizeof(struct trans), 1); + + if (self != NULL) + { + make_stream(self->in_s); + init_stream(self->in_s, in_size); + make_stream(self->out_s); + init_stream(self->out_s, out_size); + self->mode = mode; + } + + return self; } /*****************************************************************************/ void APP_CC -trans_delete(struct trans* self) +trans_delete(struct trans *self) { - if (self == 0) - { - return; - } - free_stream(self->in_s); - free_stream(self->out_s); - if (self->sck > 0) { - g_tcp_close(self->sck); - } - self->sck = 0; - if (self->listen_filename != 0) - { - g_file_delete(self->listen_filename); - g_free(self->listen_filename); - } - g_free(self); + if (self == 0) + { + return; + } + + free_stream(self->in_s); + free_stream(self->out_s); + + if (self->sck > 0) + { + g_tcp_close(self->sck); + } + + self->sck = 0; + + if (self->listen_filename != 0) + { + g_file_delete(self->listen_filename); + g_free(self->listen_filename); + } + + g_free(self); } /*****************************************************************************/ int APP_CC -trans_get_wait_objs(struct trans* self, tbus* objs, int* count) +trans_get_wait_objs(struct trans *self, tbus *objs, int *count) { - if (self == 0) - { - return 1; - } - if (self->status != TRANS_STATUS_UP) - { - return 1; - } - objs[*count] = self->sck; - (*count)++; - return 0; + if (self == 0) + { + return 1; + } + + if (self->status != TRANS_STATUS_UP) + { + return 1; + } + + objs[*count] = self->sck; + (*count)++; + return 0; } /*****************************************************************************/ int APP_CC -trans_check_wait_objs(struct trans* self) +trans_check_wait_objs(struct trans *self) { - tbus in_sck = (tbus)0; - struct trans* in_trans = (struct trans *)NULL; - int read_bytes = 0; - int to_read = 0; - int read_so_far = 0; - int rv = 0; - - if (self == 0) - { - return 1; - } - if (self->status != TRANS_STATUS_UP) - { - return 1; - } - rv = 0; - if (self->type1 == TRANS_TYPE_LISTENER) /* listening */ - { - if (g_tcp_can_recv(self->sck, 0)) + tbus in_sck = (tbus)0; + struct trans *in_trans = (struct trans *)NULL; + int read_bytes = 0; + int to_read = 0; + int read_so_far = 0; + int rv = 0; + + if (self == 0) { - in_sck = g_tcp_accept(self->sck); - if (in_sck == -1) - { - if (g_tcp_last_error_would_block(self->sck)) - { - /* ok, but shouldn't happen */ - } - else - { - /* error */ - self->status = TRANS_STATUS_DOWN; - return 1; - } - } - if (in_sck != -1) - { - if (self->trans_conn_in != 0) /* is function assigned */ + return 1; + } + + if (self->status != TRANS_STATUS_UP) + { + return 1; + } + + rv = 0; + + if (self->type1 == TRANS_TYPE_LISTENER) /* listening */ + { + if (g_tcp_can_recv(self->sck, 0)) { - in_trans = trans_create(self->mode, self->in_s->size, - self->out_s->size); - in_trans->sck = in_sck; - in_trans->type1 = TRANS_TYPE_SERVER; - in_trans->status = TRANS_STATUS_UP; - if (self->trans_conn_in(self, in_trans) != 0) - { - trans_delete(in_trans); - } + in_sck = g_tcp_accept(self->sck); + + if (in_sck == -1) + { + if (g_tcp_last_error_would_block(self->sck)) + { + /* ok, but shouldn't happen */ + } + else + { + /* error */ + self->status = TRANS_STATUS_DOWN; + return 1; + } + } + + if (in_sck != -1) + { + if (self->trans_conn_in != 0) /* is function assigned */ + { + in_trans = trans_create(self->mode, self->in_s->size, + self->out_s->size); + in_trans->sck = in_sck; + in_trans->type1 = TRANS_TYPE_SERVER; + in_trans->status = TRANS_STATUS_UP; + + if (self->trans_conn_in(self, in_trans) != 0) + { + trans_delete(in_trans); + } + } + else + { + g_tcp_close(in_sck); + } + } } - else + } + else /* connected server or client (2 or 3) */ + { + if (g_tcp_can_recv(self->sck, 0)) { - g_tcp_close(in_sck); + read_so_far = (int)(self->in_s->end - self->in_s->data); + to_read = self->header_size - read_so_far; + + if (to_read > 0) + { + read_bytes = g_tcp_recv(self->sck, self->in_s->end, to_read, 0); + + if (read_bytes == -1) + { + if (g_tcp_last_error_would_block(self->sck)) + { + /* ok, but shouldn't happen */ + } + else + { + /* error */ + self->status = TRANS_STATUS_DOWN; + return 1; + } + } + else if (read_bytes == 0) + { + /* error */ + self->status = TRANS_STATUS_DOWN; + return 1; + } + else + { + self->in_s->end += read_bytes; + } + } + + read_so_far = (int)(self->in_s->end - self->in_s->data); + + if (read_so_far == self->header_size) + { + if (self->trans_data_in != 0) + { + rv = self->trans_data_in(self); + init_stream(self->in_s, 0); + } + } } - } } - } - else /* connected server or client (2 or 3) */ - { - if (g_tcp_can_recv(self->sck, 0)) + + return rv; +} + +/*****************************************************************************/ +int APP_CC +trans_force_read_s(struct trans *self, struct stream *in_s, int size) +{ + int rcvd; + + if (self->status != TRANS_STATUS_UP) + { + return 1; + } + + while (size > 0) { - read_so_far = (int)(self->in_s->end - self->in_s->data); - to_read = self->header_size - read_so_far; - if (to_read > 0) - { - read_bytes = g_tcp_recv(self->sck, self->in_s->end, to_read, 0); - if (read_bytes == -1) + rcvd = g_tcp_recv(self->sck, in_s->end, size, 0); + + if (rcvd == -1) + { + if (g_tcp_last_error_would_block(self->sck)) + { + if (!g_tcp_can_recv(self->sck, 10)) + { + /* check for term here */ + } + } + else + { + /* error */ + self->status = TRANS_STATUS_DOWN; + return 1; + } + } + else if (rcvd == 0) { - if (g_tcp_last_error_would_block(self->sck)) - { - /* ok, but shouldn't happen */ - } - else - { /* error */ self->status = TRANS_STATUS_DOWN; return 1; - } - } - else if (read_bytes == 0) - { - /* error */ - self->status = TRANS_STATUS_DOWN; - return 1; } else { - self->in_s->end += read_bytes; + in_s->end += rcvd; + size -= rcvd; } - } - read_so_far = (int)(self->in_s->end - self->in_s->data); - if (read_so_far == self->header_size) - { - if (self->trans_data_in != 0) - { - rv = self->trans_data_in(self); - init_stream(self->in_s, 0); - } - } } - } - return rv; + + return 0; } /*****************************************************************************/ int APP_CC -trans_force_read_s(struct trans* self, struct stream* in_s, int size) +trans_force_read(struct trans *self, int size) { - int rcvd; + return trans_force_read_s(self, self->in_s, size); +} - if (self->status != TRANS_STATUS_UP) - { - return 1; - } - while (size > 0) - { - rcvd = g_tcp_recv(self->sck, in_s->end, size, 0); - if (rcvd == -1) +/*****************************************************************************/ +int APP_CC +trans_force_write_s(struct trans *self, struct stream *out_s) +{ + int size; + int total; + int sent; + + if (self->status != TRANS_STATUS_UP) { - if (g_tcp_last_error_would_block(self->sck)) - { - if (!g_tcp_can_recv(self->sck, 10)) - { - /* check for term here */ - } - } - else - { - /* error */ - self->status = TRANS_STATUS_DOWN; return 1; - } - } - else if (rcvd == 0) - { - /* error */ - self->status = TRANS_STATUS_DOWN; - return 1; } - else + + size = (int)(out_s->end - out_s->data); + total = 0; + + while (total < size) { - in_s->end += rcvd; - size -= rcvd; + sent = g_tcp_send(self->sck, out_s->data + total, size - total, 0); + + if (sent == -1) + { + if (g_tcp_last_error_would_block(self->sck)) + { + if (!g_tcp_can_send(self->sck, 10)) + { + /* check for term here */ + } + } + else + { + /* error */ + self->status = TRANS_STATUS_DOWN; + return 1; + } + } + else if (sent == 0) + { + /* error */ + self->status = TRANS_STATUS_DOWN; + return 1; + } + else + { + total = total + sent; + } } - } - return 0; + + return 0; } /*****************************************************************************/ int APP_CC -trans_force_read(struct trans* self, int size) +trans_force_write(struct trans *self) { - return trans_force_read_s(self, self->in_s, size); + return trans_force_write_s(self, self->out_s); } /*****************************************************************************/ int APP_CC -trans_force_write_s(struct trans* self, struct stream* out_s) +trans_connect(struct trans *self, const char *server, const char *port, + int timeout) { - int size; - int total; - int sent; + int error; - if (self->status != TRANS_STATUS_UP) - { - return 1; - } - size = (int)(out_s->end - out_s->data); - total = 0; - while (total < size) - { - sent = g_tcp_send(self->sck, out_s->data + total, size - total, 0); - if (sent == -1) + if (self->sck != 0) { - if (g_tcp_last_error_would_block(self->sck)) - { - if (!g_tcp_can_send(self->sck, 10)) - { - /* check for term here */ - } - } - else - { - /* error */ - self->status = TRANS_STATUS_DOWN; - return 1; - } + g_tcp_close(self->sck); } - else if (sent == 0) + + if (self->mode == TRANS_MODE_TCP) /* tcp */ { - /* error */ - self->status = TRANS_STATUS_DOWN; - return 1; + self->sck = g_tcp_socket(); + g_tcp_set_non_blocking(self->sck); + error = g_tcp_connect(self->sck, server, port); + } + else if (self->mode == TRANS_MODE_UNIX) /* unix socket */ + { + self->sck = g_tcp_local_socket(); + g_tcp_set_non_blocking(self->sck); + error = g_tcp_local_connect(self->sck, port); } else { - total = total + sent; + self->status = TRANS_STATUS_DOWN; + return 1; } - } - return 0; -} -/*****************************************************************************/ -int APP_CC -trans_force_write(struct trans* self) -{ - return trans_force_write_s(self, self->out_s); + if (error == -1) + { + if (g_tcp_last_error_would_block(self->sck)) + { + if (g_tcp_can_send(self->sck, timeout)) + { + self->status = TRANS_STATUS_UP; /* ok */ + self->type1 = TRANS_TYPE_CLIENT; /* client */ + return 0; + } + } + + return 1; + } + + self->status = TRANS_STATUS_UP; /* ok */ + self->type1 = TRANS_TYPE_CLIENT; /* client */ + return 0; } /*****************************************************************************/ int APP_CC -trans_connect(struct trans* self, const char* server, const char* port, - int timeout) +trans_listen_address(struct trans *self, char *port, const char *address) { - int error; - - if (self->sck != 0) - { - g_tcp_close(self->sck); - } - if (self->mode == TRANS_MODE_TCP) /* tcp */ - { - self->sck = g_tcp_socket(); - g_tcp_set_non_blocking(self->sck); - error = g_tcp_connect(self->sck, server, port); - } - else if (self->mode == TRANS_MODE_UNIX) /* unix socket */ - { - self->sck = g_tcp_local_socket(); - g_tcp_set_non_blocking(self->sck); - error = g_tcp_local_connect(self->sck, port); - } - else - { - self->status = TRANS_STATUS_DOWN; - return 1; - } - if (error == -1) - { - if (g_tcp_last_error_would_block(self->sck)) + if (self->sck != 0) { - if (g_tcp_can_send(self->sck, timeout)) - { - self->status = TRANS_STATUS_UP; /* ok */ - self->type1 = TRANS_TYPE_CLIENT; /* client */ - return 0; - } + g_tcp_close(self->sck); } - return 1; - } - self->status = TRANS_STATUS_UP; /* ok */ - self->type1 = TRANS_TYPE_CLIENT; /* client */ - return 0; -} -/*****************************************************************************/ -int APP_CC -trans_listen_address(struct trans* self, char* port, const char* address) -{ - if (self->sck != 0) - { - g_tcp_close(self->sck); - } - if (self->mode == TRANS_MODE_TCP) /* tcp */ - { - self->sck = g_tcp_socket(); - g_tcp_set_non_blocking(self->sck); - if (g_tcp_bind_address(self->sck, port, address) == 0) + if (self->mode == TRANS_MODE_TCP) /* tcp */ { - if (g_tcp_listen(self->sck) == 0) - { - self->status = TRANS_STATUS_UP; /* ok */ - self->type1 = TRANS_TYPE_LISTENER; /* listener */ - return 0; - } + self->sck = g_tcp_socket(); + g_tcp_set_non_blocking(self->sck); + + if (g_tcp_bind_address(self->sck, port, address) == 0) + { + if (g_tcp_listen(self->sck) == 0) + { + self->status = TRANS_STATUS_UP; /* ok */ + self->type1 = TRANS_TYPE_LISTENER; /* listener */ + return 0; + } + } } - } - else if (self->mode == TRANS_MODE_UNIX) /* unix socket */ - { - g_free(self->listen_filename); - self->listen_filename = 0; - g_file_delete(port); - self->sck = g_tcp_local_socket(); - g_tcp_set_non_blocking(self->sck); - if (g_tcp_local_bind(self->sck, port) == 0) + else if (self->mode == TRANS_MODE_UNIX) /* unix socket */ { - self->listen_filename = g_strdup(port); - if (g_tcp_listen(self->sck) == 0) - { - g_chmod_hex(port, 0xffff); - self->status = TRANS_STATUS_UP; /* ok */ - self->type1 = TRANS_TYPE_LISTENER; /* listener */ - return 0; - } + g_free(self->listen_filename); + self->listen_filename = 0; + g_file_delete(port); + self->sck = g_tcp_local_socket(); + g_tcp_set_non_blocking(self->sck); + + if (g_tcp_local_bind(self->sck, port) == 0) + { + self->listen_filename = g_strdup(port); + + if (g_tcp_listen(self->sck) == 0) + { + g_chmod_hex(port, 0xffff); + self->status = TRANS_STATUS_UP; /* ok */ + self->type1 = TRANS_TYPE_LISTENER; /* listener */ + return 0; + } + } } - } - return 1; + + return 1; } /*****************************************************************************/ int APP_CC -trans_listen(struct trans* self, char* port) +trans_listen(struct trans *self, char *port) { - return trans_listen_address(self, port, "0.0.0.0"); + return trans_listen_address(self, port, "0.0.0.0"); } /*****************************************************************************/ -struct stream* APP_CC -trans_get_in_s(struct trans* self) +struct stream *APP_CC +trans_get_in_s(struct trans *self) { - struct stream * rv = (struct stream *)NULL; - if (self == NULL) { - rv = (struct stream *)NULL; - } - else { - rv = self->in_s; - } - return rv; + struct stream *rv = (struct stream *)NULL; + + if (self == NULL) + { + rv = (struct stream *)NULL; + } + else + { + rv = self->in_s; + } + + return rv; } /*****************************************************************************/ -struct stream* APP_CC -trans_get_out_s(struct trans* self, int size) +struct stream *APP_CC +trans_get_out_s(struct trans *self, int size) { - struct stream * rv = (struct stream *)NULL; - if (self == NULL) { - rv = (struct stream *)NULL; - } - else { - init_stream(self->out_s, size); - rv = self->out_s; - } - return rv; + struct stream *rv = (struct stream *)NULL; + + if (self == NULL) + { + rv = (struct stream *)NULL; + } + else + { + init_stream(self->out_s, size); + rv = self->out_s; + } + + return rv; } diff --git a/common/trans.h b/common/trans.h index 8e8d942a..36d08a7c 100644 --- a/common/trans.h +++ b/common/trans.h @@ -1,27 +1,22 @@ -/* - Copyright (c) 2008-2010 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - generic transport - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * generic transport + */ #if !defined(TRANS_H) #define TRANS_H diff --git a/common/xrdp_client_info.h b/common/xrdp_client_info.h index f95543a3..596177c4 100644 --- a/common/xrdp_client_info.h +++ b/common/xrdp_client_info.h @@ -1,27 +1,22 @@ -/* - Copyright (c) 2012 Jay Sorg - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included - in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - xrdp / xserver info / caps - -*/ +/** + * xrdp: A Remote Desktop Protocol server. + * + * Copyright (C) Jay Sorg 2004-2012 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * xrdp / xserver info / caps + */ #if !defined(XRDP_CLIENT_INFO_H) #define XRDP_CLIENT_INFO_H |