]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/lib/ZSendPkt.c
r4266@bucket (orig r256): kcr | 2008-01-20 22:39:30 -0500
[1ts-debian.git] / zephyr / lib / ZSendPkt.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains source for the ZSendPacket function.
3  *
4  *      Created by:     Robert French
5  *
6  *      $Id$
7  *
8  *      Copyright (c) 1987,1991 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #ifndef lint
14 static const char rcsid_ZSendPacket_c[] =
15     "$Zephyr: /mit/zephyr/src/lib/RCS/ZSendPacket.c,v 1.29 91/03/21 11:57:08 raeburn Exp $";
16 #endif
17
18 #include <internal.h>
19 #include <sys/socket.h>
20
21 static int wait_for_hmack(ZNotice_t *, void *);
22
23 Code_t
24 ZSendPacket(char *packet,
25             int len,
26             int waitforack)
27 {
28     Code_t retval;
29     struct sockaddr_in dest;
30     ZNotice_t notice, acknotice;
31         
32     if (!packet || len < 0)
33         return (ZERR_ILLVAL);
34
35     if (len > Z_MAXPKTLEN)
36         return (ZERR_PKTLEN);
37     
38     if (ZGetFD() < 0)
39         if ((retval = ZOpenPort((u_short *)0)) != ZERR_NONE)
40             return (retval);
41
42     dest = ZGetDestAddr();
43         
44     if (sendto(ZGetFD(), packet, len, 0, (struct sockaddr *)&dest,
45                sizeof(dest)) < 0)
46         return (errno);
47
48     if (!waitforack)
49         return (ZERR_NONE);
50
51     if ((retval = ZParseNotice(packet, len, &notice)) != ZERR_NONE)
52         return (retval);
53     
54     retval = Z_WaitForNotice(&acknotice, wait_for_hmack, &notice.z_uid,
55                              HM_TIMEOUT);
56     if (retval == ETIMEDOUT)
57       return ZERR_HMDEAD;
58     if (retval == ZERR_NONE)
59       ZFreeNotice (&acknotice);
60     return retval;
61 }
62
63 static int
64 wait_for_hmack(ZNotice_t *notice,
65                void *uid)
66 {
67     return (notice->z_kind == HMACK && ZCompareUID(&notice->z_uid, (ZUnique_Id_t *)uid));
68 }