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