~ubuntu-branches/ubuntu/edgy/gnome-pilot-conduits/edgy

« back to all changes in this revision

Viewing changes to mal-conduit/mal/common/AGProxy.c

  • Committer: Bazaar Package Importer
  • Author(s): Mike Markley
  • Date: 2005-01-17 01:08:21 UTC
  • Revision ID: james.westby@ubuntu.com-20050117010821-d8omp6psfwhzdmna
Tags: upstream-2.0.12
ImportĀ upstreamĀ versionĀ 2.0.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The contents of this file are subject to the Mozilla Public License
 
2
 * Version 1.0 (the "License"); you may not use this file except in
 
3
 * compliance with the License. You may obtain a copy of the License at
 
4
 * http://www.mozilla.org/MPL/
 
5
 *
 
6
 * Software distributed under the License is distributed on an "AS IS"
 
7
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 
8
 * License for the specific language governing rights and limitations
 
9
 * under the License.
 
10
 *
 
11
 * The Original Code is Mobile Application Link.
 
12
 *
 
13
 * The Initial Developer of the Original Code is AvantGo, Inc.
 
14
 * Portions created by AvantGo, Inc. are Copyright (C) 1997-1999
 
15
 * AvantGo, Inc. All Rights Reserved.
 
16
 *
 
17
 * Contributor(s):
 
18
 */
 
19
 
 
20
#include <AGProxy.h>
 
21
#include <AGBase64.h>
 
22
#include <AGNet.h>
 
23
#include <AGTypes.h>
 
24
#include <AGUtil.h>
 
25
 
 
26
#ifndef __palmos__
 
27
//#include <stdio.h>    (adam) temporary hack - compile this out to build on ce
 
28
#endif
 
29
 
 
30
typedef enum _sockreturncodes {
 
31
    SOCKS_OK = 90,
 
32
    SOCKS_DENIED = 91,
 
33
    SOCKS_COULDNT_CONNECT = 92,
 
34
    SOCKS_BAD_ID = 93,
 
35
    /* Non protocol errors */
 
36
    SOCKS_BAD_VERSION,
 
37
    SOCKS_UNKNOWN_ERROR
 
38
 
 
39
} SocksCode;
 
40
 
 
41
static SocksCode proxySocksParseResponse(void *buffer);
 
42
static char *authEncodePassword(char *name, char *password);
 
43
 
 
44
#define AUTH_STRING "Proxy-authorization: Basic %s\r\n"
 
45
#define BASIC_AUTH_STRING "Authorization: Basic %s\r\n"
 
46
 
 
47
/*---------------------------------------------------------------------------*/
 
48
AGBool AGProxyCheckExclusionArray(AGArray *array, char *addrString)
 
49
{
 
50
    char *token;
 
51
    int len1, len2, i;
 
52
    char *ptr;
 
53
    
 
54
    for (i=0; i < AGArrayCount(array); i++) {
 
55
        
 
56
        token = (char *)array->elements[i];
 
57
 
 
58
        len1 = strlen( token );
 
59
        len2 = strlen( addrString );
 
60
        if( len1 > len2 )
 
61
            continue;
 
62
 
 
63
        ptr = addrString + ( len2 - len1 );
 
64
        if( strcmp( ptr, token) == 0 ) {
 
65
            return TRUE;
 
66
        }
 
67
    
 
68
    }
 
69
    return FALSE;
 
70
 
 
71
}
 
72
/*---------------------------------------------------------------------------*/
 
73
char *AGProxyCreateAuthHeader(char *user, char *pass, AGBool dobasic)
 
74
{
 
75
    char *authString;
 
76
    char *header = NULL;
 
77
    int len;
 
78
 
 
79
    authString = authEncodePassword(user, pass);
 
80
    if (authString) {
 
81
        len = strlen(AUTH_STRING) + strlen(authString) + 3;
 
82
        header = (char *)malloc(len);
 
83
        if (!header) {
 
84
            free(authString);
 
85
            return NULL;
 
86
        }
 
87
        if (dobasic)
 
88
            sprintf(header, BASIC_AUTH_STRING, authString);
 
89
        else
 
90
            sprintf(header, AUTH_STRING, authString);
 
91
        free(authString);
 
92
    }
 
93
    return header;
 
94
 
 
95
}
 
96
/*---------------------------------------------------------------------------*/
 
97
char *AGSocksBufCreate(unsigned long laddr, short _port, int *buflen)
 
98
{
 
99
    int minlen;
 
100
    uint8 *buf, *buffer;
 
101
    short port;
 
102
    char *userid = "AGUser";
 
103
 
 
104
    minlen = 9 + strlen(userid);
 
105
    buffer = buf = (uint8*)malloc(minlen);
 
106
 
 
107
    if (!buf)
 
108
        return NULL;
 
109
    
 
110
    /* Version */
 
111
    *buf++ = 4; 
 
112
 
 
113
    /* Socks connect request */
 
114
    *buf++ = 1;
 
115
 
 
116
    /* Write port in network byte order */
 
117
    port = htons(_port);
 
118
    memcpy(buf, &port, 2);
 
119
    buf += 2;
 
120
 
 
121
    /* Write internet addr (Its already in network order) */
 
122
    memcpy(buf, &laddr, 4);
 
123
    buf += 4;
 
124
 
 
125
    memcpy(buf, userid, strlen(userid));
 
126
    buf += strlen(userid);
 
127
 
 
128
    *buf = 0;
 
129
    *buflen = minlen;
 
130
    return (char *)buffer;
 
131
 
 
132
}
 
133
/*---------------------------------------------------------------------------*/
 
134
int AGSocksGetResponse(char *buffer)
 
135
{
 
136
    int rc = 0;
 
137
    
 
138
    switch(proxySocksParseResponse(buffer)) {
 
139
        case SOCKS_OK:
 
140
            return 0;
 
141
        case SOCKS_COULDNT_CONNECT:
 
142
            rc = AG_NET_SOCKS_COULDNT_CONNECT;
 
143
            break;
 
144
        case SOCKS_BAD_ID:
 
145
            rc = AG_NET_SOCKS_BAD_ID;
 
146
            break;        
 
147
        default:
 
148
            rc = AG_NET_SOCKS_ERROR;
 
149
            break;
 
150
    }
 
151
    return 0;
 
152
 
 
153
}
 
154
/*---------------------------------------------------------------------------*/
 
155
static SocksCode proxySocksParseResponse(void *buffer)
 
156
{
 
157
    uint8* buf = (uint8*)buffer;
 
158
 
 
159
    /* Ignore the version string */
 
160
 
 
161
    buf++;
 
162
    switch (*buf) {
 
163
      case SOCKS_OK:
 
164
      case SOCKS_DENIED:
 
165
      case SOCKS_COULDNT_CONNECT:
 
166
      case SOCKS_BAD_ID:
 
167
        return (SocksCode)*buf;
 
168
      default:
 
169
        return SOCKS_UNKNOWN_ERROR;
 
170
    }
 
171
    
 
172
 
 
173
}
 
174
/*---------------------------------------------------------------------------*/
 
175
static char *authEncodePassword(char *name, char *password)
 
176
{
 
177
    char *baseAuthString;
 
178
    int len;
 
179
    char *authString;
 
180
    
 
181
    len = strlen(name) + strlen(password) + 2;
 
182
 
 
183
    baseAuthString = (char*)malloc(len);
 
184
    sprintf(baseAuthString, "%s:%s", name, password);
 
185
 
 
186
    /* create base 64 encoding of "userid:password" */
 
187
    authString = AGBase64Encode((uint8 *)baseAuthString, 0);
 
188
 
 
189
    return authString;
 
190
 
 
191
}
 
192