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

« back to all changes in this revision

Viewing changes to libatalk/nbp/nbp_rgstr.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
/*
 
2
 * $Id: nbp_rgstr.c,v 1.4 2002/01/17 06:12:02 srittau Exp $
 
3
 *
 
4
 * Copyright (c) 1990,1993 Regents of The University of Michigan.
 
5
 * All Rights Reserved. See COPYRIGHT.
 
6
 */
 
7
 
 
8
#ifdef HAVE_CONFIG_H
 
9
#include "config.h"
 
10
#endif /* HAVE_CONFIG_H */
 
11
 
 
12
#include <string.h>
 
13
#include <errno.h>
 
14
#include <signal.h>
 
15
 
 
16
#include <sys/types.h>
 
17
#include <sys/param.h>
 
18
#include <sys/socket.h>
 
19
#include <sys/time.h>
 
20
 
 
21
#include <netatalk/at.h>
 
22
#include <netatalk/endian.h>
 
23
#include <atalk/nbp.h>
 
24
#include <atalk/ddp.h>
 
25
#include <atalk/netddp.h>
 
26
 
 
27
#ifdef HAVE_NETDB_H
 
28
#include <netdb.h>
 
29
#endif /* HAVE_NETDB_H */
 
30
#include  "nbp_conf.h"
 
31
 
 
32
/* FIXME/SOCKLEN_T: socklen_t is a unix98 feature. */
 
33
#ifndef SOCKLEN_T
 
34
#define SOCKLEN_T unsigned int
 
35
#endif /* ! SOCKLEN_T */
 
36
 
 
37
int nbp_rgstr( sat, obj, type, zone )
 
38
    struct sockaddr_at  *sat;
 
39
    const char          *obj, *type, *zone;
 
40
{
 
41
    struct sockaddr_at  to;
 
42
    struct nbpnve       nn;
 
43
    struct nbphdr       nh;
 
44
    struct nbptuple     nt;
 
45
    struct timeval      timeout;
 
46
    fd_set              readfd;
 
47
    struct servent      *se;
 
48
    char                *data;
 
49
    int                 s, cc;
 
50
    SOCKLEN_T           namelen;
 
51
 
 
52
    if ( nbp_lookup( obj, type, zone, &nn, 1, &sat->sat_addr ) > 0 ) {
 
53
        errno = EADDRINUSE;
 
54
        return( -1 );
 
55
    }
 
56
 
 
57
    memset(&to, 0, sizeof(to));
 
58
    if ((s = netddp_open(&to, NULL)) < 0)
 
59
        return -1;
 
60
 
 
61
    data = nbp_send;
 
62
    *data++ = DDPTYPE_NBP;
 
63
    nh.nh_op = NBPOP_RGSTR;
 
64
    nh.nh_cnt = 1;
 
65
    nh.nh_id = ++nbp_id;
 
66
    memcpy( data, &nh, SZ_NBPHDR );
 
67
    data += SZ_NBPHDR;
 
68
 
 
69
    memset(&nt, 0, sizeof(nt));
 
70
    nt.nt_net = sat->sat_addr.s_net;
 
71
    nt.nt_node = sat->sat_addr.s_node;
 
72
    nt.nt_port = sat->sat_port;
 
73
    memcpy( data, &nt, SZ_NBPTUPLE);
 
74
    data += SZ_NBPTUPLE;
 
75
 
 
76
    if ( obj ) {
 
77
        if (( cc = strlen( obj )) > NBPSTRLEN ) return( -1 );
 
78
        *data++ = cc;
 
79
        memcpy( data, obj, cc );
 
80
        data += cc;
 
81
    } else {
 
82
        *data++ = 0;
 
83
    }
 
84
 
 
85
    if ( type ) {
 
86
        if (( cc = strlen( type )) > NBPSTRLEN ) return( -1 );
 
87
        *data++ = cc;
 
88
        memcpy( data, type, cc );
 
89
        data += cc;
 
90
    } else {
 
91
        *data++ = 0;
 
92
    }
 
93
 
 
94
    if ( zone ) {
 
95
        if (( cc = strlen( zone )) > NBPSTRLEN ) return( -1 );
 
96
        *data++ = cc;
 
97
        memcpy( data, zone, cc );
 
98
        data += cc;
 
99
    } else {
 
100
        *data++ = 1;
 
101
        *data++ = '*'; /* default zone */
 
102
    }
 
103
 
 
104
    
 
105
    if ( nbp_port == 0 ) {
 
106
        if (( se = getservbyname( "nbp", "ddp" )) == NULL ) {
 
107
            nbp_port = 2;
 
108
        } else {
 
109
            nbp_port = ntohs( se->s_port );
 
110
        }
 
111
    }
 
112
    to.sat_port = nbp_port;
 
113
 
 
114
    if ( netddp_sendto( s, nbp_send, data - nbp_send, 0, 
 
115
                        (struct sockaddr *)&to,
 
116
                        sizeof( struct sockaddr_at )) < 0 ) {
 
117
        goto register_err;
 
118
    }
 
119
 
 
120
    FD_ZERO( &readfd );
 
121
    FD_SET( s, &readfd );
 
122
    timeout.tv_sec = 2;
 
123
    timeout.tv_usec = 0;
 
124
    if (( cc = select( s + 1, &readfd, 0, 0, &timeout )) < 0 ) {
 
125
        goto register_err;
 
126
    }
 
127
    if ( cc == 0 ) {
 
128
        errno = ETIMEDOUT;
 
129
        goto register_err;
 
130
    }
 
131
 
 
132
    namelen = sizeof( struct sockaddr_at );
 
133
    if (( cc = netddp_recvfrom( s, nbp_recv, sizeof( nbp_recv ), 0,
 
134
                        (struct sockaddr *)&to, &namelen )) < 0 ) {
 
135
        goto register_err;
 
136
    }
 
137
 
 
138
    netddp_close( s );
 
139
 
 
140
    data = nbp_recv;
 
141
    if ( *data++ != DDPTYPE_NBP ) {
 
142
        return( -1 );
 
143
    }
 
144
    memcpy( &nh, data, SZ_NBPHDR );
 
145
    if ( nh.nh_op != NBPOP_OK ) {
 
146
        return -1;
 
147
    }
 
148
    return( 0 );
 
149
 
 
150
register_err:
 
151
    netddp_close(s);
 
152
    return -1;
 
153
}