]> arthur.barton.de Git - netatalk.git/blob - libatalk/pap/pap_open.c
Removed duplicate -I cruft from automake files
[netatalk.git] / libatalk / pap / pap_open.c
1 /* moved over from bin/pap/pap.c */
2
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6
7 static struct {
8   PAP pap;
9   int tickle;
10   pid_t pid;
11 } client;
12
13 static void tickle_handler()
14 {
15   if (client.tickle++ < 4)
16     pap_tickle(client.pap, client.pap->pap_connid, &client.pap->pap_sat);
17   else {
18     kill(client.pid, SIGTERM);
19     syslog(LOG_ERR, "pap_alarm: connection timed out.");
20     exit(1);
21   }
22 }
23
24
25 PAP pap_open(PAP pap, struct nbpnve *nn, u_int8_t quantum, int flags)
26 {
27     struct atp_block atpb;
28     struct timeval stv, tv;
29     u_int16_t waiting;
30     pid_t pid;
31
32     if (!pap->inited) {
33       pap->pap_connid = getpid() & 0xff;
34       pap->cmdbuf[ 0 ] = pap->pap_connid;
35       pap->cmdbuf[ 1 ] = PAP_OPEN;
36       pap->cmdbuf[ 2 ] = pap->cmdbuf[ 3 ] = 0;
37       pap->cmdbuf[ 4 ] = atp_sockaddr( pap->pap_atp )->sat_port;
38       pap->cmdbuf[ 5 ] = quantum;       /* flow quantum */
39       
40       if ( gettimeofday( &stv, NULL ) < 0 ) {
41         perror( "gettimeofday" );
42         return NULL;
43       }
44       
45       for (;;) {
46         if ( flags & PAPFLAG_CUTS ) {
47           waiting = 0xffff;
48         } else {
49           if ( gettimeofday( &tv, NULL ) < 0 ) {
50             perror( "gettimeofday" );
51             return NULL;
52           }
53           waiting = htons( tv.tv_sec - stv.tv_sec );
54         }
55         
56         memcpy(pap->cmdbuf + 6, &waiting, sizeof( waiting ));
57         atpb.atp_saddr = &nn->nn_sat;
58         atpb.atp_sreqdata = pap->cmdbuf;
59         atpb.atp_sreqdlen = 8;          /* bytes in OpenConn request */
60         atpb.atp_sreqto = 2;            /* retry timer */
61         atpb.atp_sreqtries = 5;         /* retry count */
62         if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
63           perror( "atp_sreq" );
64           return NULL;
65         }
66         
67         iov.iov_base = pap->data;
68         iov.iov_len = sizeof( rbuf );
69         atpb.atp_rresiov = &iov;
70         atpb.atp_rresiovcnt = 1;
71         if ( atp_rresp( atp, &atpb ) < 0 ) {
72           perror( "atp_rresp" );
73           if ( connattempts-- <= 0 ) {
74             fprintf( stderr, "Can't connect!\n" );
75             return NULL;
76           }
77           continue;
78         }
79         
80         /* sanity */
81         if ( iov.iov_len < 8 || pap->data[ 0 ] != pap->pap_connid ||
82              pap->data[ 1 ] != PAP_OPENREPLY ) {
83           fprintf( stderr, "Bad response!\n" );
84           continue;     /* This is weird, since TIDs must match... */
85         }
86         
87         if ( isatty( 1 )) {
88           printf( "%.*s\n", iov.iov_len - 9, iov.iov_base + 9 );
89         }
90         updatestatus( iov.iov_base + 9, iov.iov_len - 9 );
91         
92         bcopy( &rbuf[ 6 ], &result, sizeof( result ));
93         if ( result != 0 ) {
94           sleep( 2 );
95         } else {
96           memcpy(&pap->pap_sat, &nn.nn_sat, sizeof( struct sockaddr_at ));
97           pap->pap_sat.sat_port = rbuf[ 4 ];
98           pap->pap_quantum = rbuf[ 5 ];
99           break;
100         }
101       }
102       
103       if ( isatty( 1 )) {
104         printf( "Connected to %.*s:%.*s@%.*s.\n",
105                 nn.nn_objlen, nn.nn_obj,
106                 nn.nn_typelen, nn.nn_type,
107                 nn.nn_zonelen, nn.nn_zone );
108       }
109
110     /* open a second atp connection */
111     if (( atp = atp_open( 0 )) == NULL ) {
112         perror( "atp_open" );
113         return NULL;
114     }
115
116     client.pap = pap;
117     client.pid = pid;
118     pap->inited = 1;
119     }
120
121     /* wait around for tickles */
122     
123 }