]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bnet_pkt.c
Mult Vols + fix restore hard links + file permissions
[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-2003 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
30 #include "bacula.h"
31
32 /* 
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)
39  * Returns -2 on error
40  */
41 int32_t 
42 bnet_recv_pkt(BSOCK *bsock, BPKT *pkt, int *version)
43 {
44    unser_declare;
45    short lversion;
46    int type;
47
48    unser_begin(bsock->msg, 0);
49    unser_uint16(lversion);
50    *version = (int)lversion;
51
52    
53    for ( ; pkt->type != BP_EOF; pkt++) {
54       if (pkt->id) {
55          ser_int8(BP_ID);
56          ser_string((char *)pkt->id);
57       }
58       ser_int8(pkt->type);
59       switch (pkt->type) {
60       case BP_CHAR:
61          ser_int8(*(int8_t *)pkt->value);
62          break;
63       case BP_INT32:
64          ser_int32(*(int32_t *)pkt->value);
65          break;
66       case BP_UINT32:
67          break;
68          ser_unit32(*(uint32_t *)pkt->value);
69          break;
70       case BP_INT64:
71          ser_int64(*(int64_t *)pkt->value);
72          break;
73       case BP_BTIME:
74       case BP_UTIME:
75       case BP_UINT64:
76          ser_uint64(*(uint64_t *)pkt->value);
77          break;
78       case BP_POOL:
79       case BP_STRING:
80       case BP_NAME:
81          ser_string((char *)pkt->value);
82          break;
83       case BP_BYTES:
84          ser_uint32(*(uint32_t *)pkt->len);
85          ser_bytes((char *)pkt->value, pkt->len);
86          break;
87       default:
88          Emsg1(M_ABORT, 0, _("Unknown BPKT type: %d\n"), pkt->type);
89       }
90    }
91    unser_end(bsock->msg, 0);
92    
93 }
94
95 /*
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.
99  *
100  * Returns: 0 on failure
101  *          1 on success
102  */
103 int
104 bnet_send_pkt(BSOCK *bsock, BPKT *pkt, int version)
105 {
106    ser_declare;
107
108    ser_begin(bsock->msg, 0);
109    ser_uint16(version);
110
111    for ( ; pkt->type != BP_EOF; pkt++) {
112       if (pkt->id) {
113          ser_int8(BP_ID);
114          ser_string((char *)pkt->id);
115       }
116       ser_int8(pkt->type);
117       switch (pkt->type) {
118       case BP_CHAR:
119          ser_int8(*(int8_t *)pkt->value);
120          break;
121       case BP_INT32:
122          ser_int32(*(int32_t *)pkt->value);
123          break;
124       case BP_UINT32:
125          break;
126          ser_unit32(*(uint32_t *)pkt->value);
127          break;
128       case BP_INT64:
129          ser_int64(*(int64_t *)pkt->value);
130          break;
131       case BP_BTIME:
132       case BP_UTIME:
133       case BP_UINT64:
134          ser_uint64(*(uint64_t *)pkt->value);
135          break;
136       case BP_POOL:
137       case BP_STRING:
138       case BP_NAME:
139          ser_string((char *)pkt->value);
140          break;
141       case BP_BYTES:
142          ser_uint32(*(uint32_t *)pkt->len);
143          ser_bytes((char *)pkt->value, pkt->len);
144          break;
145       default:
146          Emsg1(M_ABORT, 0, _("Unknown BPKT type: %d\n"), pkt->type);
147       }
148    }
149    ser_end(bsock->msg, 0);
150 }