]> arthur.barton.de Git - netatalk.git/blob - libatalk/atp/atp_open.c
c43c62e2327a400e35bdfd4c3c596557654034f3
[netatalk.git] / libatalk / atp / atp_open.c
1 /*
2  * Copyright (c) 1990,1991 Regents of The University of Michigan.
3  * All Rights Reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software and
6  * its documentation for any purpose and without fee is hereby granted,
7  * provided that the above copyright notice appears in all copies and
8  * that both that copyright notice and this permission notice appear
9  * in supporting documentation, and that the name of The University
10  * of Michigan not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. This software is supplied as is without expressed or
13  * implied warranties of any kind.
14  *
15  *      Research Systems Unix Group
16  *      The University of Michigan
17  *      c/o Mike Clark
18  *      535 W. William Street
19  *      Ann Arbor, Michigan
20  *      +1-313-763-0525
21  *      netatalk@itd.umich.edu
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <sys/param.h>
33 #include <sys/socket.h>
34
35 #include <netatalk/at.h>
36 #include <netatalk/endian.h>
37
38 #include <atalk/netddp.h>
39 #include <atalk/ddp.h>
40 #include <atalk/atp.h>
41
42 #include "atp_internals.h"
43
44 ATP atp_open(port, saddr)
45     u_int8_t port;
46     const struct at_addr *saddr;
47 {
48     struct sockaddr_at  addr;
49     int                 s;
50     ATP                 atp;
51     struct timeval      tv;
52     int                 pid;
53
54 #ifdef DEBUG
55     printf( "<%d> atp_open\n", getpid());
56 #endif
57
58     memset(&addr, 0, sizeof(addr));
59     addr.sat_port = port;
60     if (saddr) 
61       memcpy(&addr.sat_addr, saddr, sizeof(struct at_addr));
62     if ((s = netddp_open(&addr, NULL)) < 0)
63         return NULL;
64
65     if (( atp = (ATP) atp_alloc_buf()) == NULL ) {
66         netddp_close(s);
67         return NULL;
68     }
69
70     /* initialize the atp handle */
71     memset(atp, 0, sizeof( struct atp_handle ));
72     memcpy(&atp->atph_saddr, &addr, sizeof(addr));
73
74     atp->atph_socket = s;
75     atp->atph_reqto = -1;
76     gettimeofday( &tv, (struct timezone *) 0 );
77     pid = getpid();
78     atp->atph_tid = tv.tv_sec ^ ((( pid << 8 ) & 0xff00 ) | ( pid >> 8 ));
79
80 #ifdef EBUG
81 srandom( tv.tv_sec );
82 #endif
83
84     return atp;
85 }