~ubuntu-branches/ubuntu/precise/netatalk/precise

« back to all changes in this revision

Viewing changes to libatalk/util/atalk_addr.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Rittau
  • Date: 2004-01-19 12:43:49 UTC
  • Revision ID: james.westby@ubuntu.com-20040119124349-es563jbp0hk0ae51
Tags: upstream-1.6.4
ImportĀ upstreamĀ versionĀ 1.6.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifdef HAVE_CONFIG_H
 
2
#include "config.h"
 
3
#endif
 
4
 
 
5
#include <sys/types.h>
 
6
#include <netatalk/at.h>
 
7
#include <netatalk/endian.h>
 
8
#include <atalk/util.h>
 
9
#include <ctype.h>
 
10
 
 
11
/* 
 
12
 * Check whether "cp" is a valid ascii representation
 
13
 * of an AppleTalk address and convert to a binary address.
 
14
 * Examples of accepted forms are (in decimal, net of 4321,
 
15
 * node of 65):
 
16
 *
 
17
 *      4321.65
 
18
 *      0x10E1.41
 
19
 *      16.225.65
 
20
 *      0x10.E1.41
 
21
 *
 
22
 * If hex is used, and the first digit is one of A-F, the leading
 
23
 * 0x is redundant. Returns 1 if the address is valid, 0 if not.
 
24
 *
 
25
 * Unlike Internet addresses, AppleTalk addresses can have leading
 
26
 * 0's. This means that we can't support octal addressing.
 
27
 */
 
28
 
 
29
int atalk_aton( cp, addr )
 
30
    char                *cp;
 
31
    struct at_addr      *addr;
 
32
{
 
33
    u_int32_t val, base, n;
 
34
    char c;
 
35
 
 
36
    val = 0; base = 10;
 
37
    if ( *cp == '0' && ( *++cp == 'x' || *cp == 'X' )) {
 
38
        base = 16, cp++;
 
39
    }
 
40
    if ( !isdigit( *cp ) && isxdigit( *cp )) {
 
41
        base = 16;
 
42
    }
 
43
 
 
44
    for ( n = 0;; n++ ) {
 
45
        while (( c = *cp ) != '\0') {
 
46
            if ( isascii( c ) && isdigit( c )) {
 
47
                val = (val * base) + (c - '0');
 
48
                cp++;
 
49
                continue;
 
50
            }
 
51
 
 
52
            if ( base == 16 && isascii( c ) && isxdigit( c )) {
 
53
                val = ( val << 4 ) + ( c + 10 - ( islower( c ) ? 'a' : 'A' ));
 
54
                cp++;
 
55
                continue;
 
56
            }
 
57
            break;
 
58
        }
 
59
 
 
60
        if ( c != '.' && c != '\0' ) {
 
61
            return( 0 );
 
62
        }
 
63
 
 
64
        switch ( n ) {
 
65
        case 0:
 
66
            if ( addr ) {
 
67
                if ( val > 65535 ) {
 
68
                    return( 0 );
 
69
                }
 
70
                addr->s_net = val;
 
71
            }
 
72
            if ( *cp++ ) {
 
73
                val = 0;
 
74
            } else {
 
75
                break;
 
76
            }
 
77
            continue;
 
78
 
 
79
        case 2:
 
80
            if ( addr ) {
 
81
                if ( addr->s_net > 255 ) {
 
82
                    return( 0 );
 
83
                }
 
84
                addr->s_net <<= 8;
 
85
                addr->s_net += addr->s_node;
 
86
            }
 
87
            /*FALLTHROUGH*/
 
88
 
 
89
        case 1:
 
90
            if ( addr ) {
 
91
                if ( val > 255 ) {
 
92
                    return( 0 );
 
93
                }
 
94
                addr->s_node = val;
 
95
            }
 
96
            if ( *cp++ ) {
 
97
                val = 0;
 
98
            } else {
 
99
                break;
 
100
            }
 
101
            continue;
 
102
 
 
103
        default:
 
104
            return( 0 );
 
105
        }
 
106
        break;
 
107
    }
 
108
 
 
109
    if ( n < 1 ) {
 
110
        return( 0 );
 
111
    }
 
112
    if ( addr ) {
 
113
        addr->s_net = htons( addr->s_net );
 
114
    }
 
115
    return (1);
 
116
}