]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bnet_pkt.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / lib / bnet_pkt.c
1 /*
2  * Network Packet Utility Routines
3  *
4  *  by Kern Sibbald, July MMII
5  *
6  *
7  *   Version $Id$
8  */
9 /*
10    Copyright (C) 2002-2004 Kern Sibbald and John Walker
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29 #ifdef implemented
30
31 #include "bacula.h"
32
33 /*
34  * Receive a message from the other end. Each message consists of
35  * two packets. The first is a header that contains the size
36  * of the data that follows in the second packet.
37  * Returns number of bytes read
38  * Returns 0 on end of file
39  * Returns -1 on hard end of file (i.e. network connection close)
40  * Returns -2 on error
41  */
42 int32_t
43 bnet_recv_pkt(BSOCK *bsock, BPKT *pkt, int *version)
44 {
45    unser_declare;
46    short lversion;
47    int type;
48
49    unser_begin(bsock->msg, 0);
50    unser_uint16(lversion);
51    *version = (int)lversion;
52
53
54    for ( ; pkt->type != BP_EOF; pkt++) {
55       if (pkt->id) {
56          ser_int8(BP_ID);
57          ser_string((char *)pkt->id);
58       }
59       ser_int8(pkt->type);
60       switch (pkt->type) {
61       case BP_CHAR:
62          ser_int8(*(int8_t *)pkt->value);
63          break;
64       case BP_INT32:
65          ser_int32(*(int32_t *)pkt->value);
66          break;
67       case BP_UINT32:
68          break;
69          ser_unit32(*(uint32_t *)pkt->value);
70          break;
71       case BP_INT64:
72          ser_int64(*(int64_t *)pkt->value);
73          break;
74       case BP_BTIME:
75       case BP_UTIME:
76       case BP_UINT64:
77          ser_uint64(*(uint64_t *)pkt->value);
78          break;
79       case BP_POOL:
80       case BP_STRING:
81       case BP_NAME:
82          ser_string((char *)pkt->value);
83          break;
84       case BP_BYTES:
85          ser_uint32(*(uint32_t *)pkt->len);
86          ser_bytes((char *)pkt->value, pkt->len);
87          break;
88       default:
89          Emsg1(M_ABORT, 0, _("Unknown BPKT type: %d\n"), pkt->type);
90       }
91    }
92    unser_end(bsock->msg, 0);
93
94 }
95
96 /*
97  * Send a message over the network. The send consists of
98  * two network packets. The first is sends a 32 bit integer containing
99  * the length of the data packet which follows.
100  *
101  * Returns: 0 on failure
102  *          1 on success
103  */
104 int
105 bnet_send_pkt(BSOCK *bsock, BPKT *pkt, int version)
106 {
107    ser_declare;
108
109    ser_begin(bsock->msg, 0);
110    ser_uint16(version);
111
112    for ( ; pkt->type != BP_EOF; pkt++) {
113       if (pkt->id) {
114          ser_int8(BP_ID);
115          ser_string((char *)pkt->id);
116       }
117       ser_int8(pkt->type);
118       switch (pkt->type) {
119       case BP_CHAR:
120          ser_int8(*(int8_t *)pkt->value);
121          break;
122       case BP_INT32:
123          ser_int32(*(int32_t *)pkt->value);
124          break;
125       case BP_UINT32:
126          break;
127          ser_unit32(*(uint32_t *)pkt->value);
128          break;
129       case BP_INT64:
130          ser_int64(*(int64_t *)pkt->value);
131          break;
132       case BP_BTIME:
133       case BP_UTIME:
134       case BP_UINT64:
135          ser_uint64(*(uint64_t *)pkt->value);
136          break;
137       case BP_POOL:
138       case BP_STRING:
139       case BP_NAME:
140          ser_string((char *)pkt->value);
141          break;
142       case BP_BYTES:
143          ser_uint32(*(uint32_t *)pkt->len);
144          ser_bytes((char *)pkt->value, pkt->len);
145          break;
146       default:
147          Emsg1(M_ABORT, 0, _("Unknown BPKT type: %d\n"), pkt->type);
148       }
149    }
150    ser_end(bsock->msg, 0);
151 }
152
153 #endif