Imported Upstream version 3.3.3
[debian/amanda] / common-src / packet.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1999 University of Maryland at College Park
4  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of U.M. not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  U.M. makes no representations about the
14  * suitability of this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  *
17  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Authors: the Amanda Development Team.  Its members are listed in a
25  * file named AUTHORS, in the root directory of this distribution.
26  */
27 /*
28  * $Id: packet.c,v 1.8 2006/05/25 01:47:12 johnfranks Exp $
29  *
30  * Routines for modifying the amanda protocol packet type
31  */
32 #include "amanda.h"
33 #include "arglist.h"
34 #include "packet.h"
35
36 /*
37  * Table of packet types and their printable forms
38  */
39 static const struct {
40     const char name[5];
41     pktype_t type;
42 } pktypes[] = {
43     { "REQ", P_REQ },
44     { "REP", P_REP },
45     { "PREP", P_PREP },
46     { "ACK", P_ACK },
47     { "NAK", P_NAK }
48 };
49 #define NPKTYPES        (int)(SIZEOF(pktypes) / SIZEOF(pktypes[0]))
50
51 /*
52  * Initialize a packet
53  */
54 void pkt_init_empty(
55     pkt_t *pkt,
56     pktype_t type)
57 {
58     assert(pkt != NULL);
59     assert(strcmp(pkt_type2str(type), "BOGUS") != 0);
60
61     pkt->type = type;
62     pkt->packet_size = 1000;
63     pkt->body = alloc(pkt->packet_size);
64     pkt->body[0] = '\0';
65     pkt->size = strlen(pkt->body);
66 }
67
68 printf_arglist_function2(void pkt_init, pkt_t *, pkt, pktype_t, type,
69     const char *, fmt)
70 {
71     va_list     argp;
72     int         len;
73
74     assert(pkt != NULL);
75     assert(strcmp(pkt_type2str(type), "BOGUS") != 0);
76     if(fmt == NULL)
77         fmt = "";
78
79     pkt->type = type;
80     pkt->packet_size = 1000;
81     pkt->body = alloc(pkt->packet_size);
82     while(1) {
83         arglist_start(argp, fmt);
84         len = g_vsnprintf(pkt->body, pkt->packet_size, fmt, argp);
85         arglist_end(argp);
86         if (len > -1 && len < (int)(pkt->packet_size - 1))
87             break;
88         pkt->packet_size *= 2;
89         amfree(pkt->body);
90         pkt->body = alloc(pkt->packet_size);
91     }
92     pkt->size = strlen(pkt->body);
93 }
94
95 /*
96  * Append data to a packet
97  */
98 printf_arglist_function1(void pkt_cat, pkt_t *, pkt, const char *, fmt)
99 {
100     size_t      len;
101     int         lenX;
102     va_list     argp;
103     char *      pktbody;
104
105     assert(pkt != NULL);
106     assert(fmt != NULL);
107
108     len = strlen(pkt->body);
109
110     while(1) {
111         arglist_start(argp, fmt);
112         lenX = g_vsnprintf(pkt->body + len, pkt->packet_size - len, fmt,argp);
113         arglist_end(argp);
114         if (lenX > -1 && lenX < (int)(pkt->packet_size - len - 1))
115             break;
116         pkt->packet_size *= 2;
117         pktbody = alloc(pkt->packet_size);
118         strncpy(pktbody, pkt->body, len);
119         pktbody[len] = '\0';
120         amfree(pkt->body);
121         pkt->body = pktbody;
122     }
123     pkt->size = strlen(pkt->body);
124 }
125
126 /*
127  * Converts a string into a packet type
128  */
129 pktype_t
130 pkt_str2type(
131     const char *typestr)
132 {
133     int i;
134
135     assert(typestr != NULL);
136
137     for (i = 0; i < (int)NPKTYPES; i++)
138         if (strcmp(typestr, pktypes[i].name) == 0)
139             return (pktypes[i].type);
140     return ((pktype_t)-1);
141 }
142
143 /*
144  * Converts a packet type into a string
145  */
146 const char *
147 pkt_type2str(
148     pktype_t    type)
149 {
150     int i;
151
152     for (i = 0; i < (int)NPKTYPES; i++)
153         if (pktypes[i].type == type)
154             return (pktypes[i].name);
155     return ("BOGUS");
156 }