3 char const *cvs_id_control
= "$Id: control.c,v 1.4 2004-12-16 08:49:53 bodea Exp $";
9 int pack_control(char *data
, int len
, uint8_t type
, int argc
, char *argv
[])
11 struct nsctl_packet pkt
;
12 struct nsctl_args arg
;
14 int sz
= (p
- (char *) &pkt
);
16 if (len
> sizeof(pkt
))
20 argc
= 0xff; // paranoia
22 pkt
.magic
= ntohs(NSCTL_MAGIC
);
31 if (s
> sizeof(arg
.value
))
32 s
= sizeof(arg
.value
); // silently truncate
38 return -1; // overflow
41 memcpy(arg
.value
, a
, arg
.len
);
49 * terminate: this is both a sanity check and additionally
50 * ensures that there's a spare byte in the packet to null
51 * terminate the last argument when unpacking (see unpack_control)
53 if (sz
+ sizeof(arg
.len
) > len
)
54 return -1; // overflow
57 memcpy(p
, &arg
.len
, sizeof(arg
.len
));
59 sz
+= sizeof(arg
.len
);
60 memcpy(data
, &pkt
, sz
);
65 int unpack_control(struct nsctl
*control
, char *data
, int len
)
67 struct nsctl_packet pkt
;
69 int sz
= (p
- (char *) &pkt
);
73 return NSCTL_ERR_SHORT
;
75 if (len
> sizeof(pkt
))
76 return NSCTL_ERR_LONG
;
78 memcpy(&pkt
, data
, len
);
79 if (ntohs(pkt
.magic
) != NSCTL_MAGIC
)
80 return NSCTL_ERR_MAGIC
;
85 case NSCTL_REQ_UNLOAD
:
87 case NSCTL_REQ_CONTROL
:
90 control
->type
= pkt
.type
;
94 return NSCTL_ERR_TYPE
;
97 control
->argc
= pkt
.argc
;
98 for (i
= 0; i
<= control
->argc
; i
++)
103 return NSCTL_ERR_SHORT
;
106 *p
++ = 0; // null terminate previous arg
109 if (i
< control
->argc
)
112 return NSCTL_ERR_SHORT
;
114 control
->argv
[i
] = p
;
120 /* check for terminator */
122 return NSCTL_ERR_SHORT
;
127 return NSCTL_ERR_LONG
; // trailing cr*p
129 return control
->type
;
132 void dump_control(struct nsctl
*control
, FILE *stream
)
134 char *type
= "*unknown*";
139 switch (control
->type
)
141 case NSCTL_REQ_LOAD
: type
= "NSCTL_REQ_LOAD"; break;
142 case NSCTL_REQ_UNLOAD
: type
= "NSCTL_REQ_UNLOAD"; break;
143 case NSCTL_REQ_HELP
: type
= "NSCTL_REQ_HELP"; break;
144 case NSCTL_REQ_CONTROL
: type
= "NSCTL_REQ_CONTROL"; break;
145 case NSCTL_RES_OK
: type
= "NSCTL_RES_OK"; break;
146 case NSCTL_RES_ERR
: type
= "NSCTL_RES_ERR"; break;
149 fprintf(stream
, "Control packet:\n");
150 fprintf(stream
, " Type: %d (%s)\n", (int) control
->type
, type
);
151 fprintf(stream
, " Args: %d", (int) control
->argc
);
155 fprintf(stream
, " (\"");
156 for (i
= 0; i
< control
->argc
; i
++)
157 fprintf(stream
, "%s%s", i
? "\", \"" : "", control
->argv
[i
]);
159 fprintf(stream
, "\")");
162 fprintf(stream
, "\n\n");