]>
Commit | Line | Data |
---|---|---|
85330553 | 1 | /* $Id$ |
2 | * | |
3 | * socket layer for update_server | |
4 | * | |
5 | * Copyright (C) 1997-1998 by the Massachusetts Institute of Technology | |
6 | * For copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
8 | */ | |
9 | ||
10 | #include <mit-copyright.h> | |
11 | #include <moira.h> | |
12 | ||
36f6a0ef | 13 | #include <sys/types.h> |
85330553 | 14 | #include <sys/uio.h> |
15 | ||
16 | #include <errno.h> | |
17 | #include <stdlib.h> | |
18 | #include <unistd.h> | |
19 | ||
20 | RCSID("$Header$"); | |
21 | ||
22 | #define putlong(cp, l) { cp[0] = l >> 24; cp[1] = l >> 16; cp[2] = l >> 8; cp[3] = l; } | |
23 | #define getlong(cp, l) l = ((cp[0] * 256 + cp[1]) * 256 + cp[2]) * 256 + cp[3] | |
24 | ||
25 | extern void fail(int conn, int err, char *msg); | |
26 | ||
27 | int send_int(int conn, long data) | |
28 | { | |
29 | char buf[8]; | |
30 | ||
31 | putlong(buf, 4); | |
32 | putlong((buf + 4), data); | |
33 | if (write(conn, buf, 8) == 8) | |
34 | return 0; | |
35 | else | |
36 | { | |
37 | fail(conn, errno, "sending integer"); | |
38 | return errno; | |
39 | } | |
40 | } | |
41 | ||
42 | int recv_int(int conn, long *data) | |
43 | { | |
44 | char buf[8]; | |
45 | ||
46 | if (read(conn, buf, 8) != 8) | |
47 | { | |
48 | fail(conn, errno, "reading integer"); | |
49 | return errno; | |
50 | } | |
51 | getlong((buf + 4), *data); | |
52 | return 0; | |
53 | } | |
54 | ||
55 | ||
56 | int send_string(int conn, char *buf, size_t len) | |
57 | { | |
58 | char fulllen[4], stringlen[4]; | |
59 | struct iovec iov[3]; | |
60 | ||
61 | putlong(fulllen, (len + 4)); | |
62 | putlong(stringlen, len); | |
63 | iov[0].iov_base = fulllen; | |
64 | iov[0].iov_len = 4; | |
65 | iov[1].iov_base = stringlen; | |
66 | iov[1].iov_len = 4; | |
67 | iov[2].iov_base = buf; | |
68 | iov[2].iov_len = len; | |
69 | ||
70 | if (writev(conn, iov, 3) == -1) | |
71 | { | |
72 | fail(conn, errno, "sending string"); | |
73 | return errno; | |
74 | } | |
75 | else | |
76 | return 0; | |
77 | } | |
78 | ||
79 | int recv_string(int conn, char **buf, size_t *len) | |
80 | { | |
81 | char tmp[4]; | |
82 | ||
83 | if (read(conn, tmp, 4) != 4) | |
84 | { | |
85 | fail(conn, errno, "reading string"); | |
86 | return errno; | |
87 | } | |
88 | if (read(conn, tmp, 4) != 4) | |
89 | { | |
90 | fail(conn, errno, "reading string"); | |
91 | return errno; | |
92 | } | |
93 | getlong(tmp, *len); | |
94 | ||
95 | *buf = malloc(*len); | |
96 | if (!*buf) | |
97 | { | |
98 | fail(conn, ENOMEM, "reading string"); | |
99 | return ENOMEM; | |
100 | } | |
101 | if (read(conn, *buf, *len) != *len) | |
102 | { | |
103 | free(buf); | |
104 | fail(conn, errno, "reading string"); | |
105 | return errno; | |
106 | } | |
107 | ||
108 | return 0; | |
109 | } |