File: globus_ftp_control.c

package info (click to toggle)
globus-ftp-control 9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 2,500 kB
  • sloc: ansic: 23,825; sh: 11,412; makefile: 246
file content (373 lines) | stat: -rw-r--r-- 9,488 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * Copyright 1999-2006 University of Chicago
 * 
 * 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 GLOBUS_DONT_DOCUMENT_INTERNAL

/**
 * @file globus_ftp_control.c
 * @brief Globus FTP Control API
 */

#include "globus_ftp_control.h"
#include "globus_i_ftp_control.h"
#ifndef TARGET_ARCH_WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include "version.h"

#undef GLOBUS_FAILURE
#define GLOBUS_FAILURE globus_error_put(GLOBUS_ERROR_NO_INFO)

static int globus_l_ftp_control_activate(void);
static int globus_l_ftp_control_deactivate(void);

globus_module_descriptor_t globus_i_ftp_control_module =
{
    "globus_ftp_control",
    globus_l_ftp_control_activate,
    globus_l_ftp_control_deactivate,
    GLOBUS_NULL,
    GLOBUS_NULL,
    &local_version
};

/**
 * Debugging level
 *
 * 1 through 3 enable debug output for control channel
 * 4 through 6 enable debug output for control and data channel
 */
int globus_i_ftp_control_debug_level = 0;

static
int
globus_l_ftp_control_activate(void)
{
    int                                rc;
    char *                              tmp_string;

    rc = globus_module_activate(GLOBUS_IO_MODULE);
    if(rc != GLOBUS_SUCCESS)
    {
        return rc;
    }
    
    tmp_string = globus_module_getenv("GLOBUS_FTP_CONTROL_DEBUG_LEVEL");
    if(tmp_string != GLOBUS_NULL)
    {
	globus_i_ftp_control_debug_level = atoi(tmp_string);

	if(globus_i_ftp_control_debug_level < 0)
	{
	    globus_i_ftp_control_debug_level = 0;
	}
    }
    
    globus_i_ftp_control_debug_printf(1,
        (stderr, "globus_l_ftp_control_activate() entering\n"));
        
    rc = globus_module_activate(GLOBUS_THREAD_MODULE);
    if(rc != GLOBUS_SUCCESS)
    {
        goto exit;
    }
    rc = globus_module_activate(GLOBUS_COMMON_MODULE);
    if(rc != GLOBUS_SUCCESS)
    {
        goto exit;
    }
    rc = globus_module_activate(GLOBUS_GSI_GSSAPI_MODULE);
    if(rc != GLOBUS_SUCCESS)
    {
        goto exit;
    }
    rc = (int)globus_i_ftp_control_server_activate();
    if(rc != GLOBUS_SUCCESS)
    {
        goto exit;
    }
    rc = (int)globus_i_ftp_control_client_activate();
    if(rc != GLOBUS_SUCCESS)
    {
        goto exit;
    }
    rc = (int)globus_i_ftp_control_data_activate();

exit:
    globus_i_ftp_control_debug_printf(1,
        (stderr, "globus_l_ftp_control_activate() exiting\n"));
    return rc;
}

static
int
globus_l_ftp_control_deactivate(void)
{
    int                             rc;
    
    globus_i_ftp_control_debug_printf(1,
        (stderr, "globus_l_ftp_control_deactivate() entering\n"));
        
    rc = (int)globus_i_ftp_control_data_deactivate();
    if(rc != GLOBUS_SUCCESS)
    {
        goto exit;
    }
    rc = (int)globus_i_ftp_control_client_deactivate();
    if(rc != GLOBUS_SUCCESS)
    {
        goto exit;
    }
    rc = (int)globus_i_ftp_control_server_deactivate();
    if(rc != GLOBUS_SUCCESS)
    {
        goto exit;
    }
    rc = globus_module_deactivate(GLOBUS_IO_MODULE);
    if(rc != GLOBUS_SUCCESS)
    {
        goto exit;
    }
    rc = globus_module_deactivate(GLOBUS_GSI_GSSAPI_MODULE);
    if(rc != GLOBUS_SUCCESS)
    {
        goto exit;
    }
    rc = globus_module_deactivate(GLOBUS_THREAD_MODULE);
    if(rc != GLOBUS_SUCCESS)
    {
        goto exit;
    }
    rc = globus_module_deactivate(GLOBUS_COMMON_MODULE);

exit:    
    globus_i_ftp_control_debug_printf(1,
        (stderr, "globus_l_ftp_control_deactivate() exiting\n"));
    return rc;
}

/*
 *  access functions for globus_ftp_control_parallelism_t
 */
globus_result_t
globus_i_ftp_parallelism_copy(
    globus_ftp_control_parallelism_t *             dest_parallelism,
    globus_ftp_control_parallelism_t *             src_parallelism)
{
    /*
     *  for now there are no pointers in any of the sub classes of
     *  globus_i_ftp_parallelism_base_t so we can just do a mem
     *  copy.
     */

    memcpy(
        dest_parallelism, 
        src_parallelism, 
        sizeof(globus_ftp_control_parallelism_t));

    if(dest_parallelism->mode == GLOBUS_FTP_CONTROL_PARALLELISM_NONE)
    {
        dest_parallelism->base.size = 1;
    }

    /* TODO check src_parallelism for valid members */
    return GLOBUS_SUCCESS;
}

globus_result_t
globus_ftp_control_set_netlogger(
    globus_ftp_control_handle_t *               handle,
    globus_netlogger_handle_t *                 nl_handle,
    globus_bool_t                               nl_ftp_control,
    globus_bool_t                               nl_globus_io)
{
    globus_result_t                             res;

    res = globus_i_ftp_control_client_set_netlogger(handle, nl_handle);
    if(res != GLOBUS_SUCCESS)
    {
        return res;
    }
    res = globus_i_ftp_control_data_set_netlogger(
              handle, nl_handle, nl_ftp_control, nl_globus_io);

    return res;
}

int
globus_i_ftp_parallelism_get_size(
    globus_ftp_control_parallelism_t *             parallelism)
{
    return parallelism->base.size;
}

int
globus_i_ftp_parallelism_get_max_size(
    globus_ftp_control_parallelism_t *             parallelism)
{
    if(parallelism->mode == GLOBUS_FTP_CONTROL_PARALLELISM_NONE)
    {
        return 1;
    }
/*    else if(parallelism->mode == GLOBUS_FTP_CONTROL_PARALLELISM_FIXED)
    {
        return parallelism->fixed.size;
    }
    else if(parallelism->mode == GLOBUS_FTP_CONTROL_PARALLELISM_AUTOMATIC)
    {
        return parallelism->automatic.max_size;
    }
*/
    return -1;
}

int
globus_i_ftp_parallelism_get_min_size(
    globus_ftp_control_parallelism_t *             parallelism)
{
    if(parallelism->mode == GLOBUS_FTP_CONTROL_PARALLELISM_NONE)
    {
        return 1;
    }
/*    else if(parallelism->mode == GLOBUS_FTP_CONTROL_PARALLELISM_FIXED)
    {
        return parallelism->fixed.size;
    }
    else if(parallelism->mode == GLOBUS_FTP_CONTROL_PARALLELISM_AUTOMATIC)
    {
        return parallelism->automatic.min_size;
    }
*/
    return -1;
}

/*
 *  access functions for globus_ftp_host_port_t
 */
void 
globus_ftp_control_host_port_init(
    globus_ftp_control_host_port_t *              host_port,
    char *                                        host,
    unsigned short                                port)
{
    struct hostent                                hostent;
    char                                          bs_buf[8192];
    int                                           err_no;
    char                                          hostip[30];

    memset(host_port, 0, sizeof(*host_port));
    if(host != GLOBUS_NULL)
    {
	struct in_addr tmp_addr;

        globus_libc_gethostbyname_r(
            host,
            &hostent,
            bs_buf,
            8192,
            &err_no);

	memcpy(&tmp_addr, hostent.h_addr_list[0], sizeof(struct in_addr));
        strcpy(
            hostip, 
            inet_ntoa(tmp_addr));
        sscanf(
            hostip, 
            "%d.%d.%d.%d", 
            &host_port->host[0],
            &host_port->host[1],
            &host_port->host[2],
            &host_port->host[3]);
        host_port->hostlen = 4;
    }
    host_port->port = port;
}

void 
globus_ftp_control_host_port_destroy(
    globus_ftp_control_host_port_t *                   host_port)
{
}

/* XX this is crap.. no idea how big host is.  should probably be at least
 * 50 bytes
 */
void
globus_ftp_control_host_port_get_host(
    globus_ftp_control_host_port_t *                   host_port,
    char *                                             host)
{
    char *                              str;
    
    /* this api doesnt let me play nice with people that arent aware of the new
     * hostlen field in host_port.  since i have tried to assume 4 wherever
     * ipv6 is not allowed, I will assume 4 unless the len is actually 16.
     * this is in the hopes that it is not likely for a random uninitialized int
     * to be 16
     */
    str = globus_libc_ints_to_contact_string(
        host_port->host, host_port->hostlen == 16 ? 16 : 4, 0);
    if(str)
    {
        strcpy(host, str);
        globus_free(str);
    }
    else
    {
        *host = 0;
    }
}

unsigned short
globus_ftp_control_host_port_get_port(
    globus_ftp_control_host_port_t *                   host_port)
{
    return host_port->port;
}

void
globus_ftp_control_host_port_copy(
    globus_ftp_control_host_port_t *                   dest,
    globus_ftp_control_host_port_t *                   src)
{
    memcpy(dest, src, sizeof(*src));
}

globus_result_t
globus_ftp_control_layout_copy(
    globus_ftp_control_layout_t *                       dest,
    globus_ftp_control_layout_t *                       src)
{
    if(dest == GLOBUS_NULL)
    {
        return GLOBUS_FAILURE;
    }
    if(src == GLOBUS_NULL)
    {
        return GLOBUS_FAILURE;
    }

    memcpy(
        (void *)dest, 
        (void *)src, 
        sizeof(globus_ftp_control_layout_t)); 

    return GLOBUS_SUCCESS;
}

#endif /* GLOBUS_DONT_DOCUMENT_INTERNAL */