~ubuntu-branches/ubuntu/precise/ncbi-tools6/precise

« back to all changes in this revision

Viewing changes to connect/ncbi_server_info.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2005-03-27 12:00:15 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050327120015-embhesp32nj73p9r
Tags: 6.1.20041020-3
* Fix FTBFS under GCC 4.0 caused by inconsistent use of "static" on
  functions.  (Closes: #295110.)
* Add a watch file, now that we can.  (Upstream's layout needs version=3.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  $Id: ncbi_server_info.c,v 6.34 2001/12/06 16:07:44 lavr Exp $
 
1
/*  $Id: ncbi_server_info.c,v 6.53 2003/09/02 21:21:42 lavr Exp $
2
2
 * ===========================================================================
3
3
 *
4
4
 *                            PUBLIC DOMAIN NOTICE
28
28
 * File Description:
29
29
 *   NCBI server meta-address info
30
30
 *
31
 
 * --------------------------------------------------------------------------
32
 
 * $Log: ncbi_server_info.c,v $
33
 
 * Revision 6.34  2001/12/06 16:07:44  lavr
34
 
 * More accurate string length estimation in SERV_WriteInfo()
35
 
 *
36
 
 * Revision 6.33  2001/11/25 22:12:00  lavr
37
 
 * Replaced g_SERV_LocalServerDefault -> SERV_SetLocalServerDefault()
38
 
 *
39
 
 * Revision 6.32  2001/11/16 20:25:53  lavr
40
 
 * +g_SERV_LocalServerDefault as a private global parameter
41
 
 *
42
 
 * Revision 6.31  2001/09/24 20:43:58  lavr
43
 
 * TSERV_Flags reverted to 'int'; expilict cast added in comparison
44
 
 *
45
 
 * Revision 6.30  2001/09/10 21:17:10  lavr
46
 
 * Support written for FIREWALL server type
47
 
 *
48
 
 * Revision 6.29  2001/06/19 19:12:01  lavr
49
 
 * Type change: size_t -> TNCBI_Size; time_t -> TNCBI_Time
50
 
 *
51
 
 * Revision 6.28  2001/06/05 14:11:29  lavr
52
 
 * SERV_MIME_UNDEFINED split into 2 (typed) constants:
53
 
 * SERV_MIME_TYPE_UNDEFINED and SERV_MIME_SUBTYPE_UNDEFINED
54
 
 *
55
 
 * Revision 6.27  2001/06/04 17:01:06  lavr
56
 
 * MIME type/subtype added to server descriptor
57
 
 *
58
 
 * Revision 6.26  2001/05/03 16:35:46  lavr
59
 
 * Local bonus coefficient modified: meaning of negative value changed
60
 
 *
61
 
 * Revision 6.25  2001/04/24 21:38:21  lavr
62
 
 * Additions to code to support new locality and bonus attributes of servers.
63
 
 *
64
 
 * Revision 6.24  2001/03/26 18:39:38  lavr
65
 
 * Casting to (unsigned char) instead of (int) for ctype char.class macros
66
 
 *
67
 
 * Revision 6.23  2001/03/06 23:53:07  lavr
68
 
 * SERV_ReadInfo can now consume either hostname or IP address
69
 
 *
70
 
 * Revision 6.22  2001/03/05 23:10:11  lavr
71
 
 * SERV_WriteInfo & SERV_ReadInfo both take only one argument now
72
 
 *
73
 
 * Revision 6.21  2001/03/02 20:09:14  lavr
74
 
 * Typo fixed
75
 
 *
76
 
 * Revision 6.20  2001/03/01 18:48:19  lavr
77
 
 * NCBID allowed to have '' (special case) as an empty argument
78
 
 *
79
 
 * Revision 6.19  2001/01/03 22:34:44  lavr
80
 
 * MAX_IP_ADDRESS_LEN -> MAX_IP_ADDR_LEN (as everywhere else)
81
 
 *
82
 
 * Revision 6.18  2000/12/29 17:59:38  lavr
83
 
 * Reading and writing of SERV_Info now use SOCK_* utility functions
84
 
 * SOCK_gethostaddr and SOCK_ntoa. More clean code for reading.
85
 
 *
86
 
 * Revision 6.17  2000/12/06 22:19:02  lavr
87
 
 * Binary host addresses are now explicitly stated to be in network byte
88
 
 * order, whereas binary port addresses now use native (host) representation
89
 
 *
90
 
 * Revision 6.16  2000/12/04 17:34:19  beloslyu
91
 
 * the order of include files is important, especially on other Unixes!
92
 
 * Look the man on inet_ntoa
93
 
 *
94
 
 * Revision 6.15  2000/10/20 17:13:30  lavr
95
 
 * Service descriptor parse bug fixed
96
 
 * Unknown type now returns empty text string (instead of abort) in 'SERV_TypeStr'
97
 
 *
98
 
 * Revision 6.14  2000/10/05 21:31:23  lavr
99
 
 * Standalone connection marked "stateful" by default
100
 
 *
101
 
 * Revision 6.13  2000/06/05 20:21:20  lavr
102
 
 * Eliminated gcc warning: "subscript has type `char'" in calls to
103
 
 * classification macros (<ctype.h>) by explicit casting to unsigned chars
104
 
 *
105
 
 * Revision 6.12  2000/05/31 23:12:22  lavr
106
 
 * First try to assemble things together to get working service mapper
107
 
 *
108
 
 * Revision 6.11  2000/05/24 16:45:15  lavr
109
 
 * Introduced replacement for inet_ntoa: my_ntoa
110
 
 *
111
 
 * Revision 6.10  2000/05/23 21:05:33  lavr
112
 
 * Memory leaks fixed (appeared after server-info structure rearrangement)
113
 
 *
114
 
 * Revision 6.9  2000/05/23 19:02:49  lavr
115
 
 * Server-info now includes rate; verbal representation changed
116
 
 *
117
 
 * Revision 6.8  2000/05/22 16:53:11  lavr
118
 
 * Rename service_info -> server_info everywhere (including
119
 
 * file names) as the latter name is more relevant
120
 
 *
121
 
 * Revision 6.7  2000/05/18 14:12:43  lavr
122
 
 * Cosmetic change
123
 
 *
124
 
 * Revision 6.6  2000/05/17 16:15:13  lavr
125
 
 * NCBI_* (for ANSI ext) undone - now "ncbi_ansi_ext.h" does good prototyping
126
 
 *
127
 
 * Revision 6.5  2000/05/17 14:22:32  lavr
128
 
 * Small cosmetic changes
129
 
 *
130
 
 * Revision 6.4  2000/05/16 15:09:02  lavr
131
 
 * Added explicit type casting to get "very smart" compilers happy
132
 
 *
133
 
 * Revision 6.3  2000/05/15 19:06:09  lavr
134
 
 * Use home-made ANSI extensions (NCBI_***)
135
 
 *
136
 
 * Revision 6.2  2000/05/12 21:42:59  lavr
137
 
 * Cleaned up for the C++ compilation, etc.
138
 
 *
139
 
 * Revision 6.1  2000/05/12 18:36:26  lavr
140
 
 * First working revision
141
 
 *
142
 
 * ==========================================================================
143
31
 */
144
32
 
 
33
#include "ncbi_ansi_ext.h"
145
34
#include "ncbi_server_infop.h"
146
 
#include <connect/ncbi_ansi_ext.h>
147
 
#include <connect/ncbi_server_info.h>
148
 
#include <connect/ncbi_socket.h>
149
 
#include <assert.h>
150
35
#include <ctype.h>
151
36
#include <math.h>
152
37
#include <stdio.h>
153
38
#include <stdlib.h>
154
 
#include <string.h>
155
 
 
156
39
 
157
40
#define MAX_IP_ADDR_LEN  16 /* sizeof("255.255.255.255") */
158
41
 
164
47
/* Table of virtual functions
165
48
 */
166
49
typedef struct {
167
 
    char*       (*Write ) (size_t reserve, const USERV_Info* u);
168
 
    SSERV_Info* (*Read  ) (const char** str);
169
 
    size_t      (*SizeOf) (const USERV_Info *u);
170
 
    int/*bool*/ (*Equal ) (const USERV_Info *u1, const USERV_Info *u2);
 
50
    char*       (*Write )(size_t reserve, const USERV_Info* u);
 
51
    SSERV_Info* (*Read  )(const char** str);
 
52
    size_t      (*SizeOf)(const USERV_Info *u);
 
53
    int/*bool*/ (*Equal )(const USERV_Info *u1, const USERV_Info *u2);
171
54
} SSERV_Info_VTable;
172
55
 
173
56
 
209
92
const char* SERV_TypeStr(ESERV_Type type)
210
93
{
211
94
    const SSERV_Attr* attr = s_GetAttrByType(type);
212
 
 
213
95
    if (attr)
214
96
        return attr->tag;
215
97
    return "";
219
101
const char* SERV_ReadType(const char* str, ESERV_Type* type)
220
102
{
221
103
    const SSERV_Attr* attr = s_GetAttrByTag(str);
222
 
 
223
104
    if (!attr)
224
105
        return 0;
225
106
    *type = attr->type;
239
120
    size_t reserve;
240
121
    char* str;
241
122
 
242
 
    if (info->mime_t != SERV_MIME_TYPE_UNDEFINED &&
 
123
    if (info->type != fSERV_Dns &&
 
124
        info->mime_t != SERV_MIME_TYPE_UNDEFINED &&
243
125
        info->mime_s != SERV_MIME_SUBTYPE_UNDEFINED) {
244
126
        char* p;
245
127
        if (!MIME_ComposeContentTypeEx(info->mime_t, info->mime_s,
246
 
                                       eENCOD_None, c_t, sizeof(c_t)))
 
128
                                       info->mime_e, c_t, sizeof(c_t)))
247
129
            return 0;
248
130
        assert(c_t[strlen(c_t) - 2] == '\r' && c_t[strlen(c_t) - 1] == '\n');
249
131
        c_t[strlen(c_t) - 2] = 0;
254
136
    } else
255
137
        *c_t = 0;
256
138
    attr = s_GetAttrByType(info->type);
257
 
    reserve = attr->tag_len+1 + strlen(c_t)+3 + MAX_IP_ADDR_LEN + 5+1/*port*/ +
258
 
        10+1/*algorithm*/ + 12+1/*time*/ + 14+1/*rate*/ + 9+1/*coef*/ +
259
 
        5+1/*locl*/ + 5+1/*sful*/ + 1/*EOL*/;
 
139
    reserve = attr->tag_len+1 + MAX_IP_ADDR_LEN + 1+5/*port*/ + 1+10/*flag*/ +
 
140
        1+9/*coef*/ + 3+strlen(c_t)/*cont.type*/ + 1+5/*locl*/ + 1+5/*priv*/ +
 
141
        1+7/*quorum*/ + 1+14/*rate*/ + 1+5/*sful*/ + 1+12/*time*/ + 1/*EOL*/;
260
142
    /* write server-specific info */
261
143
    if ((str = attr->vtable.Write(reserve, &info->u)) != 0) {
262
144
        char* s = str;
263
 
        int n;
 
145
        size_t n;
264
146
 
265
147
        memcpy(s, attr->tag, attr->tag_len);
266
148
        s += attr->tag_len;
268
150
        s += HostPortToString(info->host, info->port, s, reserve);
269
151
        if ((n = strlen(str + reserve)) != 0) {
270
152
            *s++ = ' ';
271
 
            memmove(s, str + reserve, n+1);
 
153
            memmove(s, str + reserve, n + 1);
272
154
            s = str + strlen(str);
273
155
        }
274
 
        if (*c_t)
275
 
            s += sprintf(s, " C=%s", c_t);
 
156
 
276
157
        assert(info->flag < (int)(sizeof(k_FlagTag)/sizeof(k_FlagTag[0])));
277
158
        if (k_FlagTag[info->flag] && *k_FlagTag[info->flag])
278
159
            s += sprintf(s, " %s", k_FlagTag[info->flag]);
279
 
        s += sprintf(s, " T=%lu R=%.2f B=%.2f L=%s", (unsigned long)info->time,
280
 
                     info->rate, info->coef, info->locl ? "yes" : "no");
281
 
        if (!(info->type & fSERV_Http))
 
160
        s += sprintf(s, " B=%.2f", info->coef);
 
161
        if (*c_t)
 
162
            s += sprintf(s, " C=%s", c_t);
 
163
        s += sprintf(s, " L=%s", info->locl & 0x0F ? "yes" : "no");
 
164
        if (info->type != fSERV_Dns && (info->locl & 0xF0))
 
165
            s += sprintf(s, " P=yes");
 
166
        if (info->host && info->quorum) {
 
167
            if (info->quorum == (unsigned short)(-1))
 
168
                s += sprintf(s, " Q=yes");
 
169
            else
 
170
                s += sprintf(s, " Q=%hu", info->quorum);
 
171
        }
 
172
        s += sprintf(s," R=%.*f", fabs(info->rate) < 0.01 ? 3 : 2, info->rate);
 
173
        if (!(info->type & fSERV_Http) && info->type != fSERV_Dns)
282
174
            s += sprintf(s, " S=%s", info->sful ? "yes" : "no");
 
175
        s += sprintf(s, " T=%lu", (unsigned long)info->time);
283
176
    }
284
177
    return str;
285
178
}
290
183
    /* detect server type */
291
184
    ESERV_Type  type;
292
185
    const char* str = SERV_ReadType(info_str, &type);
293
 
    int/*bool*/ mime, time, rate, coef, locl, sful;
 
186
    int/*bool*/ coef, mime, locl, priv, quorum, rate, sful, time;
294
187
    unsigned short port;                /* host (native) byte order */
295
188
    unsigned int host;                  /* network byte order       */
296
189
    SSERV_Info *info;
297
 
    int n;
298
190
 
299
191
    if (!str || (*str && !isspace((unsigned char)(*str))))
300
192
        return 0;
310
202
    info->host = host;
311
203
    if (port)
312
204
        info->port = port;
313
 
    mime = time = rate = coef = locl = sful = 0; /* unassigned */
 
205
    coef = mime = locl = priv = quorum = rate = sful = time = 0;/*unassigned*/
314
206
    /* continue reading server info: optional parts: ... */
315
207
    while (*str && isspace((unsigned char)(*str)))
316
208
        str++;
317
209
    while (*str) {
318
210
        if (*(str + 1) == '=') {
319
 
            EMIME_Type    mime_t;
320
 
            EMIME_SubType mime_s;
321
 
            unsigned long t;
322
 
            char s[4];
323
 
            double d;
 
211
            int            n;
 
212
            double         d;
 
213
            unsigned short h;
 
214
            unsigned long  t;
 
215
            char           s[4];
 
216
            EMIME_Type     mime_t;
 
217
            EMIME_SubType  mime_s;
 
218
            EMIME_Encoding mime_e;
324
219
            
325
220
            switch (toupper(*str++)) {
326
 
            case 'C':
327
 
                if (!mime && MIME_ParseContentTypeEx(str + 1,
328
 
                                                     &mime_t, &mime_s, 0)) {
329
 
                    info->mime_t = mime_t;
330
 
                    info->mime_s = mime_s;
331
 
                    mime = 1;
332
 
                    while (*str && !isspace((unsigned char)(*str)))
333
 
                        str++;
334
 
                }
335
 
                break;
336
 
            case 'T':
337
 
                if (!time && sscanf(str, "=%lu%n", &t, &n) >= 1) {
338
 
                    str += n;
339
 
                    info->time = (TNCBI_Time) t;
340
 
                    time = 1;
341
 
                }
342
 
                break;
343
 
            case 'R':
344
 
                if (!rate && sscanf(str, "=%lf%n", &d, &n) >= 1) {
345
 
                    str += n;
346
 
                    if (fabs(d) < 0.01)
347
 
                        d = 0.0;
348
 
                    else if (fabs(d) > 100000.0)
349
 
                        d = (d < 0 ? -1.0 : 1.0)*100000.0;
350
 
                    info->rate = d;
351
 
                    rate = 1;
352
 
                }
353
 
                break;
354
221
            case 'B':
355
222
                if (!coef && sscanf(str, "=%lf%n", &d, &n) >= 1) {
356
 
                    str += n;
357
223
                    if (d < -100.0)
358
224
                        d = -100.0;
359
225
                    else if (d < 0.0)
363
229
                    else if (d > 1000.0)
364
230
                        d = 1000.0;
365
231
                    info->coef = d;
 
232
                    str += n;
366
233
                    coef = 1;
367
234
                }
368
235
                break;
 
236
            case 'C':
 
237
                if (type == fSERV_Dns)
 
238
                    break;
 
239
                if (!mime && MIME_ParseContentTypeEx(str + 1, &mime_t,
 
240
                                                     &mime_s, &mime_e)) {
 
241
                    info->mime_t = mime_t;
 
242
                    info->mime_s = mime_s;
 
243
                    info->mime_e = mime_e;
 
244
                    mime = 1;
 
245
                    while (*str && !isspace((unsigned char)(*str)))
 
246
                        str++;
 
247
                }
 
248
                break;
369
249
            case 'L':
370
250
                if (!locl && sscanf(str, "=%3s%n", s, &n) >= 1) {
371
251
                    if (strcasecmp(s, "YES") == 0) {
372
 
                        info->locl = 1/*true*/;
373
 
                        str += n;
374
 
                        locl = 1;
375
 
                    } else if (strcasecmp(s, "NO") == 0) {
376
 
                        info->locl = 0/*false*/;
377
 
                        str += n;
378
 
                        locl = 1;
379
 
                    }
 
252
                        info->locl |=  0x01/*true in low nibble*/;
 
253
                        str += n;
 
254
                        locl = 1;
 
255
                    } else if (strcasecmp(s, "NO") == 0) {
 
256
                        info->locl &= ~0x0F/*false in low nibble*/;
 
257
                        str += n;
 
258
                        locl = 1;
 
259
                    }
 
260
                }
 
261
                break;
 
262
            case 'P':
 
263
                if (type == fSERV_Dns)
 
264
                    break;
 
265
                if (!priv && sscanf(str, "=%3s%n", s, &n) >= 1) {
 
266
                    if (strcasecmp(s, "YES") == 0) {
 
267
                        info->locl |=  0x10;/*true in high nibble*/
 
268
                        str += n;
 
269
                        priv = 1;
 
270
                    } else if (strcasecmp(s, "NO") == 0) {
 
271
                        info->locl &= ~0xF0;/*false in high nibble*/
 
272
                        str += n;
 
273
                        priv = 1;
 
274
                    }
 
275
                }
 
276
                break;
 
277
            case 'Q':
 
278
                if (type == fSERV_Firewall || !info->host || quorum)
 
279
                    break;
 
280
                if (sscanf(str,"=%3s%n",s,&n) >= 1 && strcasecmp(s, "YES")==0){
 
281
                    info->quorum = (unsigned short)(-1);
 
282
                    str += n;
 
283
                    quorum = 1;
 
284
                } else if (sscanf(str, "=%hu%n", &h, &n) >= 1) {
 
285
                    info->quorum = h;
 
286
                    str += n;
 
287
                    quorum = 1;
 
288
                }
 
289
                break;
 
290
            case 'R':
 
291
                if (!rate && sscanf(str, "=%lf%n", &d, &n) >= 1) {
 
292
                    if (fabs(d) < 0.001)
 
293
                        d = 0.0;
 
294
                    else if (fabs(d) > 100000.0)
 
295
                        d = (d < 0.0 ? -1.0 : 1.0)*100000.0;
 
296
                    info->rate = d;
 
297
                    str += n;
 
298
                    rate = 1;
380
299
                }
381
300
                break;
382
301
            case 'S':
384
303
                    break;
385
304
                if (!sful && sscanf(str, "=%3s%n", s, &n) >= 1) {
386
305
                    if (strcasecmp(s, "YES") == 0) {
 
306
                        if (type == fSERV_Dns)
 
307
                            break; /*check only here for compatibility*/
387
308
                        info->sful = 1/*true */;
388
309
                        str += n;
389
310
                        sful = 1;
394
315
                    }
395
316
                }
396
317
                break;
 
318
            case 'T':
 
319
                if (!time && sscanf(str, "=%lu%n", &t, &n) >= 1) {
 
320
                    info->time = (TNCBI_Time) t;
 
321
                    str += n;
 
322
                    time = 1;
 
323
                }
 
324
                break;
397
325
            }
398
326
        } else {
399
327
            size_t i;
400
328
            for (i = 0; i < sizeof(k_FlagTag)/sizeof(k_FlagTag[0]); i++) {
401
 
                n = strlen(k_FlagTag[i]);
402
 
                if (strncasecmp(str, k_FlagTag[i], n) == 0)
 
329
                size_t n = strlen(k_FlagTag[i]);
 
330
                if (strncasecmp(str, k_FlagTag[i], n) == 0) {
 
331
                    info->flag = (ESERV_Flags) i;
 
332
                    str += n;
403
333
                    break;
404
 
            }
405
 
            if (i < sizeof(k_FlagTag)/sizeof(k_FlagTag[0])) {
406
 
                info->flag = (ESERV_Flags) i;
407
 
                str += n;
 
334
                }
408
335
            }
409
336
        }
410
337
        if (*str && !isspace((unsigned char)(*str)))
498
425
        info->host         = host;
499
426
        info->port         = port;
500
427
        info->sful         = 0;
501
 
        info->locl         = s_LocalServerDefault;
502
 
        info->flag         = SERV_DEFAULT_FLAG;
 
428
        info->locl         = s_LocalServerDefault & 0x0F;
503
429
        info->time         = 0;
504
430
        info->coef         = 0.0;
505
431
        info->rate         = 0.0;
506
432
        info->mime_t       = SERV_MIME_TYPE_UNDEFINED;
507
433
        info->mime_s       = SERV_MIME_SUBTYPE_UNDEFINED;
 
434
        info->mime_e       = eENCOD_None;
 
435
        info->flag         = SERV_DEFAULT_FLAG;
 
436
        memset(&info->reserved, 0, sizeof(info->reserved));
 
437
        info->quorum       = 0;
508
438
        info->u.ncbid.args = (TNCBI_Size) sizeof(info->u.ncbid);
509
439
        if (strcmp(args, "''") == 0) /* special case */
510
440
            args = 0;
519
449
 *  STANDALONE::   constructor and virtual functions
520
450
 */
521
451
 
 
452
/*ARGSUSED*/
522
453
static char* s_Standalone_Write(size_t reserve, const USERV_Info* u_info)
523
454
{
524
455
    char* str = (char*) malloc(reserve + 1);
529
460
}
530
461
 
531
462
 
 
463
/*ARGSUSED*/
532
464
static SSERV_Info* s_Standalone_Read(const char** str)
533
465
{
534
466
    return SERV_CreateStandaloneInfo(0, 0);
541
473
}
542
474
 
543
475
 
 
476
/*ARGSUSED*/
544
477
static int/*bool*/ s_Standalone_Equal(const USERV_Info* u1,
545
478
                                      const USERV_Info* u2)
546
479
{
559
492
        info->host   = host;
560
493
        info->port   = port;
561
494
        info->sful   = 0;
562
 
        info->locl   = s_LocalServerDefault;
563
 
        info->flag   = SERV_DEFAULT_FLAG;
 
495
        info->locl   = s_LocalServerDefault & 0x0F;
564
496
        info->time   = 0;
565
497
        info->coef   = 0.0;
566
498
        info->rate   = 0.0;
567
499
        info->mime_t = SERV_MIME_TYPE_UNDEFINED;
568
500
        info->mime_s = SERV_MIME_SUBTYPE_UNDEFINED;
 
501
        info->mime_e = eENCOD_None;
 
502
        info->flag   = SERV_DEFAULT_FLAG;
 
503
        memset(&info->reserved, 0, sizeof(info->reserved));
 
504
        info->quorum = 0;
 
505
        memset(&info->u.standalone, 0, sizeof(info->u.standalone));
569
506
    }
570
507
    return info;
571
508
}
668
605
        info->host        = host;
669
606
        info->port        = port;
670
607
        info->sful        = 0;
671
 
        info->locl        = s_LocalServerDefault;
672
 
        info->flag        = SERV_DEFAULT_FLAG;
 
608
        info->locl        = s_LocalServerDefault & 0x0F;
673
609
        info->time        = 0;
674
610
        info->coef        = 0.0;
675
611
        info->rate        = 0.0;
676
612
        info->mime_t      = SERV_MIME_TYPE_UNDEFINED;
677
613
        info->mime_s      = SERV_MIME_SUBTYPE_UNDEFINED;
 
614
        info->mime_e      = eENCOD_None;
 
615
        info->flag        = SERV_DEFAULT_FLAG;
 
616
        memset(&info->reserved, 0, sizeof(info->reserved));
 
617
        info->quorum      = 0;
678
618
        info->u.http.path = (TNCBI_Size) sizeof(info->u.http);
679
619
        info->u.http.args = (TNCBI_Size) (info->u.http.path +
680
620
                                          strlen(path ? path : "")+1);
705
645
static SSERV_Info* s_Firewall_Read(const char** str)
706
646
{
707
647
    ESERV_Type type;
708
 
    if (!(*str = SERV_ReadType(*str, &type)))
709
 
        return 0;
 
648
    const char* s;
 
649
    if (!(s = SERV_ReadType(*str, &type)))
 
650
        type = (ESERV_Type) fSERV_Any;
 
651
    else
 
652
        *str = s;
710
653
    return SERV_CreateFirewallInfo(0, 0, type);
711
654
}
712
655
 
733
676
        info->host   = host;
734
677
        info->port   = port;
735
678
        info->sful   = 0;
736
 
        info->locl   = s_LocalServerDefault;
737
 
        info->flag   = SERV_DEFAULT_FLAG;
 
679
        info->locl   = s_LocalServerDefault & 0x0F;
738
680
        info->time   = 0;
739
681
        info->coef   = 0.0;
740
682
        info->rate   = 0.0;
741
683
        info->mime_t = SERV_MIME_TYPE_UNDEFINED;
742
684
        info->mime_s = SERV_MIME_SUBTYPE_UNDEFINED;
 
685
        info->mime_e = eENCOD_None;
 
686
        info->flag   = SERV_DEFAULT_FLAG;
 
687
        memset(&info->reserved, 0, sizeof(info->reserved));
 
688
        info->quorum = 0;
743
689
        info->u.firewall.type = type;
744
690
    }
745
691
    return info;
748
694
 
749
695
 
750
696
/*****************************************************************************
 
697
 *  DNS::   constructor and virtual functions
 
698
 */
 
699
 
 
700
/*ARGSUSED*/
 
701
static char* s_Dns_Write(size_t reserve, const USERV_Info* u_info)
 
702
{
 
703
    char* str = (char*) malloc(reserve + 1);
 
704
 
 
705
    if (str)
 
706
        str[reserve] = '\0';
 
707
    return str;
 
708
}
 
709
 
 
710
 
 
711
/*ARGSUSED*/
 
712
static SSERV_Info* s_Dns_Read(const char** str)
 
713
{
 
714
    return SERV_CreateDnsInfo(0);
 
715
}
 
716
 
 
717
 
 
718
static size_t s_Dns_SizeOf(const USERV_Info* u)
 
719
{
 
720
    return sizeof(u->dns);
 
721
}
 
722
 
 
723
 
 
724
/*ARGSUSED*/
 
725
static int/*bool*/ s_Dns_Equal(const USERV_Info* u1, const USERV_Info* u2)
 
726
{
 
727
    return 1;
 
728
}
 
729
 
 
730
 
 
731
SSERV_Info* SERV_CreateDnsInfo(unsigned int host)
 
732
{
 
733
    SSERV_Info* info = (SSERV_Info*) malloc(sizeof(SSERV_Info));
 
734
 
 
735
    if (info) {
 
736
        info->type   = fSERV_Dns;
 
737
        info->host   = host;
 
738
        info->port   = 0;
 
739
        info->sful   = 0;
 
740
        info->locl   = s_LocalServerDefault & 0x0F;
 
741
        info->time   = 0;
 
742
        info->coef   = 0.0;
 
743
        info->rate   = 0.0;
 
744
        info->mime_t = SERV_MIME_TYPE_UNDEFINED;
 
745
        info->mime_s = SERV_MIME_SUBTYPE_UNDEFINED;
 
746
        info->mime_e = eENCOD_None;
 
747
        info->flag   = SERV_DEFAULT_FLAG;
 
748
        memset(&info->reserved, 0, sizeof(info->reserved));
 
749
        info->quorum = 0;
 
750
        memset(&info->u.dns.pad, 0, sizeof(info->u.dns.pad));
 
751
    }
 
752
    return info;
 
753
}
 
754
 
 
755
 
 
756
/*****************************************************************************
751
757
 *  Attributes for the different server types::  Implementation
752
758
 */
753
759
 
757
763
static const char kHTTP_POST [] = "HTTP_POST";
758
764
static const char kHTTP      [] = "HTTP";
759
765
static const char kFIREWALL  [] = "FIREWALL";
 
766
static const char kDNS       [] = "DNS";
760
767
 
761
768
 
762
769
/* Note: be aware of "prefixness" of tag constants and order of
792
799
    { fSERV_Firewall,
793
800
      kFIREWALL, sizeof(kFIREWALL) - 1,
794
801
      {s_Firewall_Write,    s_Firewall_Read,
795
 
       s_Firewall_SizeOf,   s_Firewall_Equal} }
 
802
       s_Firewall_SizeOf,   s_Firewall_Equal} },
 
803
 
 
804
    { fSERV_Dns,
 
805
      kDNS, sizeof(kDNS) - 1,
 
806
      {s_Dns_Write,         s_Dns_Read,
 
807
       s_Dns_SizeOf,        s_Dns_Equal} }
796
808
};
797
809
 
798
810
 
821
833
    }
822
834
    return 0;
823
835
}
 
836
 
 
837
 
 
838
/*
 
839
 * --------------------------------------------------------------------------
 
840
 * $Log: ncbi_server_info.c,v $
 
841
 * Revision 6.53  2003/09/02 21:21:42  lavr
 
842
 * Cleanup included headers
 
843
 *
 
844
 * Revision 6.52  2003/06/26 15:21:43  lavr
 
845
 * Use server's default locality for fSERV_Dns infos
 
846
 *
 
847
 * Revision 6.51  2003/06/16 15:58:50  lavr
 
848
 * Minor code format changes
 
849
 *
 
850
 * Revision 6.50  2003/05/31 05:16:28  lavr
 
851
 * Add ARGSUSED where args are meant to be unused
 
852
 *
 
853
 * Revision 6.49  2003/04/25 15:21:05  lavr
 
854
 * Explicit cast to avoid int->enum warning
 
855
 *
 
856
 * Revision 6.48  2003/03/13 19:08:26  lavr
 
857
 * Allow missing type in Firewall server info specification
 
858
 *
 
859
 * Revision 6.47  2003/03/07 22:21:31  lavr
 
860
 * Heed 'uninitted use' (false) warning by moving lines around
 
861
 *
 
862
 * Revision 6.46  2003/02/28 14:48:38  lavr
 
863
 * String type match for size_t and int in few expressions
 
864
 *
 
865
 * Revision 6.45  2002/11/01 20:15:36  lavr
 
866
 * Do not allow FIREWALL server specs to have Q flag
 
867
 *
 
868
 * Revision 6.44  2002/10/28 20:15:06  lavr
 
869
 * -<connect/ncbi_server_info.h> ("ncbi_server_infop.h" should suffice)
 
870
 *
 
871
 * Revision 6.43  2002/10/28 15:46:21  lavr
 
872
 * Use "ncbi_ansi_ext.h" privately
 
873
 *
 
874
 * Revision 6.42  2002/10/21 19:19:23  lavr
 
875
 * 2(was:3)-digit precision if R is exactly 0.01
 
876
 *
 
877
 * Revision 6.41  2002/09/24 15:05:23  lavr
 
878
 * Increase precision in SERV_Write() when R is small
 
879
 *
 
880
 * Revision 6.40  2002/09/17 15:39:33  lavr
 
881
 * SSERV_Info::quorum moved past the reserved area
 
882
 *
 
883
 * Revision 6.39  2002/09/04 15:09:47  lavr
 
884
 * Handle quorum field in SSERV_Info::, log moved to end
 
885
 *
 
886
 * Revision 6.38  2002/05/06 19:16:16  lavr
 
887
 * +#include <stdio.h>
 
888
 *
 
889
 * Revision 6.37  2002/03/22 19:52:18  lavr
 
890
 * Do not include <stdio.h>: included from ncbi_util.h or ncbi_priv.h
 
891
 *
 
892
 * Revision 6.36  2002/03/19 22:13:58  lavr
 
893
 * Do not use home-made ANSI-extensions if the platform provides
 
894
 *
 
895
 * Revision 6.35  2002/03/11 21:59:00  lavr
 
896
 * Support for changes in ncbi_server_info.h: DNS server type added as
 
897
 * well as support for MIME encoding in server specifications
 
898
 *
 
899
 * Revision 6.34  2001/12/06 16:07:44  lavr
 
900
 * More accurate string length estimation in SERV_WriteInfo()
 
901
 *
 
902
 * Revision 6.33  2001/11/25 22:12:00  lavr
 
903
 * Replaced g_SERV_LocalServerDefault -> SERV_SetLocalServerDefault()
 
904
 *
 
905
 * Revision 6.32  2001/11/16 20:25:53  lavr
 
906
 * +g_SERV_LocalServerDefault as a private global parameter
 
907
 *
 
908
 * Revision 6.31  2001/09/24 20:43:58  lavr
 
909
 * TSERV_Flags reverted to 'int'; expilict cast added in comparison
 
910
 *
 
911
 * Revision 6.30  2001/09/10 21:17:10  lavr
 
912
 * Support written for FIREWALL server type
 
913
 *
 
914
 * Revision 6.29  2001/06/19 19:12:01  lavr
 
915
 * Type change: size_t -> TNCBI_Size; time_t -> TNCBI_Time
 
916
 *
 
917
 * Revision 6.28  2001/06/05 14:11:29  lavr
 
918
 * SERV_MIME_UNDEFINED split into 2 (typed) constants:
 
919
 * SERV_MIME_TYPE_UNDEFINED and SERV_MIME_SUBTYPE_UNDEFINED
 
920
 *
 
921
 * Revision 6.27  2001/06/04 17:01:06  lavr
 
922
 * MIME type/subtype added to server descriptor
 
923
 *
 
924
 * Revision 6.26  2001/05/03 16:35:46  lavr
 
925
 * Local bonus coefficient modified: meaning of negative value changed
 
926
 *
 
927
 * Revision 6.25  2001/04/24 21:38:21  lavr
 
928
 * Additions to code to support new locality and bonus attributes of servers.
 
929
 *
 
930
 * Revision 6.24  2001/03/26 18:39:38  lavr
 
931
 * Casting to (unsigned char) instead of (int) for ctype char.class macros
 
932
 *
 
933
 * Revision 6.23  2001/03/06 23:53:07  lavr
 
934
 * SERV_ReadInfo can now consume either hostname or IP address
 
935
 *
 
936
 * Revision 6.22  2001/03/05 23:10:11  lavr
 
937
 * SERV_WriteInfo & SERV_ReadInfo both take only one argument now
 
938
 *
 
939
 * Revision 6.21  2001/03/02 20:09:14  lavr
 
940
 * Typo fixed
 
941
 *
 
942
 * Revision 6.20  2001/03/01 18:48:19  lavr
 
943
 * NCBID allowed to have '' (special case) as an empty argument
 
944
 *
 
945
 * Revision 6.19  2001/01/03 22:34:44  lavr
 
946
 * MAX_IP_ADDRESS_LEN -> MAX_IP_ADDR_LEN (as everywhere else)
 
947
 *
 
948
 * Revision 6.18  2000/12/29 17:59:38  lavr
 
949
 * Reading and writing of SERV_Info now use SOCK_* utility functions
 
950
 * SOCK_gethostaddr and SOCK_ntoa. More clean code for reading.
 
951
 *
 
952
 * Revision 6.17  2000/12/06 22:19:02  lavr
 
953
 * Binary host addresses are now explicitly stated to be in network byte
 
954
 * order, whereas binary port addresses now use native (host) representation
 
955
 *
 
956
 * Revision 6.16  2000/12/04 17:34:19  beloslyu
 
957
 * the order of include files is important, especially on other Unixes!
 
958
 * Look the man on inet_ntoa
 
959
 *
 
960
 * Revision 6.15  2000/10/20 17:13:30  lavr
 
961
 * Service descriptor parse bug fixed
 
962
 * Return empty string on unknown type (instead of abort) in 'SERV_TypeStr'
 
963
 *
 
964
 * Revision 6.14  2000/10/05 21:31:23  lavr
 
965
 * Standalone connection marked "stateful" by default
 
966
 *
 
967
 * Revision 6.13  2000/06/05 20:21:20  lavr
 
968
 * Eliminated gcc warning: "subscript has type `char'" in calls to
 
969
 * classification macros (<ctype.h>) by explicit casting to unsigned chars
 
970
 *
 
971
 * Revision 6.12  2000/05/31 23:12:22  lavr
 
972
 * First try to assemble things together to get working service mapper
 
973
 *
 
974
 * Revision 6.11  2000/05/24 16:45:15  lavr
 
975
 * Introduced replacement for inet_ntoa: my_ntoa
 
976
 *
 
977
 * Revision 6.10  2000/05/23 21:05:33  lavr
 
978
 * Memory leaks fixed (appeared after server-info structure rearrangement)
 
979
 *
 
980
 * Revision 6.9  2000/05/23 19:02:49  lavr
 
981
 * Server-info now includes rate; verbal representation changed
 
982
 *
 
983
 * Revision 6.8  2000/05/22 16:53:11  lavr
 
984
 * Rename service_info -> server_info everywhere (including
 
985
 * file names) as the latter name is more relevant
 
986
 *
 
987
 * Revision 6.7  2000/05/18 14:12:43  lavr
 
988
 * Cosmetic change
 
989
 *
 
990
 * Revision 6.6  2000/05/17 16:15:13  lavr
 
991
 * NCBI_* (for ANSI ext) undone - now "ncbi_ansi_ext.h" does good prototyping
 
992
 *
 
993
 * Revision 6.5  2000/05/17 14:22:32  lavr
 
994
 * Small cosmetic changes
 
995
 *
 
996
 * Revision 6.4  2000/05/16 15:09:02  lavr
 
997
 * Added explicit type casting to get "very smart" compilers happy
 
998
 *
 
999
 * Revision 6.3  2000/05/15 19:06:09  lavr
 
1000
 * Use home-made ANSI extensions (NCBI_***)
 
1001
 *
 
1002
 * Revision 6.2  2000/05/12 21:42:59  lavr
 
1003
 * Cleaned up for the C++ compilation, etc.
 
1004
 *
 
1005
 * Revision 6.1  2000/05/12 18:36:26  lavr
 
1006
 * First working revision
 
1007
 *
 
1008
 * ==========================================================================
 
1009
 */