2 * Network Packet Utility Routines
4 * by Kern Sibbald, July MMII
10 Copyright (C) 2002-2003 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 * Receive a message from the other end. Each message consists of
34 * two packets. The first is a header that contains the size
35 * of the data that follows in the second packet.
36 * Returns number of bytes read
37 * Returns 0 on end of file
38 * Returns -1 on hard end of file (i.e. network connection close)
42 bnet_recv_pkt(BSOCK *bsock, BPKT *pkt, int *version)
48 unser_begin(bsock->msg, 0);
49 unser_uint16(lversion);
50 *version = (int)lversion;
53 for ( ; pkt->type != BP_EOF; pkt++) {
56 ser_string((char *)pkt->id);
61 ser_int8(*(int8_t *)pkt->value);
64 ser_int32(*(int32_t *)pkt->value);
68 ser_unit32(*(uint32_t *)pkt->value);
71 ser_int64(*(int64_t *)pkt->value);
76 ser_uint64(*(uint64_t *)pkt->value);
81 ser_string((char *)pkt->value);
84 ser_uint32(*(uint32_t *)pkt->len);
85 ser_bytes((char *)pkt->value, pkt->len);
88 Emsg1(M_ABORT, 0, _("Unknown BPKT type: %d\n"), pkt->type);
91 unser_end(bsock->msg, 0);
96 * Send a message over the network. The send consists of
97 * two network packets. The first is sends a 32 bit integer containing
98 * the length of the data packet which follows.
100 * Returns: 0 on failure
104 bnet_send_pkt(BSOCK *bsock, BPKT *pkt, int version)
108 ser_begin(bsock->msg, 0);
111 for ( ; pkt->type != BP_EOF; pkt++) {
114 ser_string((char *)pkt->id);
119 ser_int8(*(int8_t *)pkt->value);
122 ser_int32(*(int32_t *)pkt->value);
126 ser_unit32(*(uint32_t *)pkt->value);
129 ser_int64(*(int64_t *)pkt->value);
134 ser_uint64(*(uint64_t *)pkt->value);
139 ser_string((char *)pkt->value);
142 ser_uint32(*(uint32_t *)pkt->len);
143 ser_bytes((char *)pkt->value, pkt->len);
146 Emsg1(M_ABORT, 0, _("Unknown BPKT type: %d\n"), pkt->type);
149 ser_end(bsock->msg, 0);