Added pairsprintf()
authorAlan T. DeKok <aland@freeradius.org>
Tue, 30 Apr 2013 22:47:06 +0000 (18:47 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 30 Apr 2013 22:47:31 +0000 (18:47 -0400)
Which prints a format string to the vp_strvalue of a VALUE_PAIR

Makefile
src/include/libradius.h
src/lib/valuepair.c
src/main/process.c
src/main/soh.c
src/main/valuepair.c

index 4583b8b..73ec793 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -174,8 +174,9 @@ config.status: configure
 check-includes:
        scripts/min-includes.pl `find . -name "*.c" -print`
 
+.PHONY: TAGS
 TAGS:
-       etags `find src -type f -name '*.[ch]' -print`
+       etags `find src -type f -name '*.[ch]' -print` > $@
 
 #
 #  Make test certificates.
index 5a87b4d..0ed75fc 100644 (file)
@@ -430,6 +430,11 @@ VALUE_PAIR *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *vp);
 VALUE_PAIR     *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *vp, unsigned int attr, unsigned int vendor, int8_t tag);
 void           pairmemcpy(VALUE_PAIR *vp, uint8_t const * src, size_t len);
 void           pairstrcpy(VALUE_PAIR *vp, char const * src);
+void           pairsprintf(VALUE_PAIR *vp, char const * fmt, ...)
+#ifdef __GNUC__
+               __attribute__ ((format (printf, 2, 3)))
+#endif
+;
 void           pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from);
 void           pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
                          unsigned int attr, unsigned int vendor, int8_t tag);
index b047b3b..56ddb47 100644 (file)
@@ -2294,3 +2294,24 @@ void pairstrcpy(VALUE_PAIR *vp, char const *src)
        vp->vp_strvalue[size] = '\0';
        vp->length = size;
 }
+
+
+/** Print data into an "string" data type.
+ *
+ * @param[in,out] vp to update
+ * @param[in] fmt the format string
+ * @param[in] size of the data
+ */
+void pairsprintf(VALUE_PAIR *vp, char const *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       vsnprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), fmt, ap);
+       va_end(ap);
+
+       /*
+        *      vsnprintf returns random things on different platforms
+        */
+       vp->length = strlen(vp->vp_strvalue);
+}
index 5aef8be..0910927 100644 (file)
@@ -2252,9 +2252,7 @@ static int request_will_proxy(REQUEST *request)
         */
        vp = radius_paircreate(request, &request->proxy->vps,
                               PW_PROXY_STATE, 0);
-       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%d",
-                request->packet->id);
-       vp->length = strlen(vp->vp_strvalue);
+       pairsprintf(vp, "%d", request->packet->id);
 
        /*
         *      Should be done BEFORE inserting into proxy hash, as
@@ -2634,10 +2632,8 @@ static void ping_home_server(void *ctx)
        vp = pairmake(request->proxy, &request->proxy->vps,
                      "NAS-Identifier", "", T_OP_SET);
        if (vp) {
-               snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue),
-                        "Status Check %u. Are you alive?",
-                        home->num_sent_pings);
-               vp->length = strlen(vp->vp_strvalue);
+               pairsprintf(vp, "Status Check %u. Are you alive?",
+                           home->num_sent_pings);
        }
 
        request->proxy->src_ipaddr = home->src_ipaddr;
index 371a4e8..5335674 100644 (file)
@@ -532,31 +532,31 @@ int soh_verify(REQUEST *request, uint8_t const *data, unsigned int data_len) {
                                                        s = "security-updates";
                                                        switch (hcstatus) {
                                                                case 0xff0005:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s ok all-installed", s);
+                                                                       pairsprintf(vp, "%s ok all-installed", s);
                                                                        break;
                                                                case 0xff0006:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s warn some-missing", s);
+                                                                       pairsprintf(vp, "%s warn some-missing", s);
                                                                        break;
                                                                case 0xff0008:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s warn never-started", s);
+                                                                       pairsprintf(vp, "%s warn never-started", s);
                                                                        break;
                                                                case 0xc0ff000c:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s error no-wsus-srv", s);
+                                                                       pairsprintf(vp, "%s error no-wsus-srv", s);
                                                                        break;
                                                                case 0xc0ff000d:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s error no-wsus-clid", s);
+                                                                       pairsprintf(vp, "%s error no-wsus-clid", s);
                                                                        break;
                                                                case 0xc0ff000e:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s warn wsus-disabled", s);
+                                                                       pairsprintf(vp, "%s warn wsus-disabled", s);
                                                                        break;
                                                                case 0xc0ff000f:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s error comm-failure", s);
+                                                                       pairsprintf(vp, "%s error comm-failure", s);
                                                                        break;
                                                                case 0xc0ff0010:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s warn needs-reboot", s);
+                                                                       pairsprintf(vp, "%s warn needs-reboot", s);
                                                                        break;
                                                                default:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s error %08x", s, hcstatus);
+                                                                       pairsprintf(vp, "%s error %08x", s, hcstatus);
                                                                        break;
                                                        }
                                                        break;
@@ -566,28 +566,28 @@ int soh_verify(REQUEST *request, uint8_t const *data, unsigned int data_len) {
                                                        s = "auto-updates";
                                                        switch (hcstatus) {
                                                                case 1:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s warn disabled", s);
+                                                                       pairsprintf(vp, "%s warn disabled", s);
                                                                        break;
                                                                case 2:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s ok action=check-only", s);
+                                                                       pairsprintf(vp, "%s ok action=check-only", s);
                                                                        break;
                                                                case 3:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s ok action=download", s);
+                                                                       pairsprintf(vp, "%s ok action=download", s);
                                                                        break;
                                                                case 4:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s ok action=install", s);
+                                                                       pairsprintf(vp, "%s ok action=install", s);
                                                                        break;
                                                                case 5:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s warn unconfigured", s);
+                                                                       pairsprintf(vp, "%s warn unconfigured", s);
                                                                        break;
                                                                case 0xc0ff0003:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s warn service-down", s);
+                                                                       pairsprintf(vp, "%s warn service-down", s);
                                                                        break;
                                                                case 0xc0ff0018:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s warn never-started", s);
+                                                                       pairsprintf(vp, "%s warn never-started", s);
                                                                        break;
                                                                default:
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s error %08x", s, hcstatus);
+                                                                       pairsprintf(vp, "%s error %08x", s, hcstatus);
                                                                        break;
                                                        }
                                                        break;
@@ -604,12 +604,12 @@ int soh_verify(REQUEST *request, uint8_t const *data, unsigned int data_len) {
                                                                         */
                                                                        t = clientstatus2str(hcstatus);
                                                                        if (t) {
-                                                                               snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s error %s", s, t);
+                                                                               pairsprintf(vp, "%s error %s", s, t);
                                                                        } else {
-                                                                               snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s error %08x", s, hcstatus);
+                                                                               pairsprintf(vp, "%s error %08x", s, hcstatus);
                                                                        }
                                                                } else {
-                                                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue),
+                                                                       pairsprintf(vp,
                                                                                        "%s ok snoozed=%i microsoft=%i up2date=%i enabled=%i",
                                                                                        s,
                                                                                        hcstatus & 0x8 ? 1 : 0,
@@ -619,7 +619,7 @@ int soh_verify(REQUEST *request, uint8_t const *data, unsigned int data_len) {
                                                                                        );
                                                                }
                                                        } else {
-                                                               snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%i unknown %08x", curr_hc, hcstatus);
+                                                               pairsprintf(vp, "%i unknown %08x", curr_hc, hcstatus);
                                                        }
                                                        break;
                                        }
@@ -628,7 +628,7 @@ int soh_verify(REQUEST *request, uint8_t const *data, unsigned int data_len) {
                                        if (!vp) return 0;
 
                                        /* FIXME: what to do with the payload? */
-                                       snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%08x/%i ?", curr_shid, curr_shid_c);
+                                       pairsprintf(vp, "%08x/%i ?", curr_shid, curr_shid_c);
                                }
                                break;
 
@@ -639,7 +639,6 @@ int soh_verify(REQUEST *request, uint8_t const *data, unsigned int data_len) {
 
                data += tlv.tlv_len;
                data_len -= tlv.tlv_len;
-
        }
 
        return 0;
index 2bf519b..a0278b4 100644 (file)
@@ -1272,7 +1272,8 @@ void module_failure_msg(REQUEST *request, char const *fmt, ...)
                return;
        }
 
-       len = snprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), "%s: ", request->module);
+       pairsprintf(vp, "%s: ", request->module);
+       len = vp->length;
        
        vsnprintf(vp->vp_strvalue + len, sizeof(vp->vp_strvalue) - len, fmt, ap);
        pairadd(&request->packet->vps, vp);