odhcp6c: let odhcp6c_add_state return a success/failure indication
authorHans Dedecker <dedeckeh@gmail.com>
Sat, 13 Jan 2018 18:09:53 +0000 (19:09 +0100)
committerHans Dedecker <dedeckeh@gmail.com>
Sat, 13 Jan 2018 20:19:09 +0000 (21:19 +0100)
This will allow callers of odhcp6_add_state parsing a command line option :
generate a syslog error in case of failure
bail out with return an error code

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
src/odhcp6c.c
src/odhcp6c.h

index 666af5c398493f07f97d6d775bbd65755c869171..e590d6b0a35b70ee38c6242b5a578881f9aa82e0 100644 (file)
@@ -103,10 +103,13 @@ int main(_unused int argc, char* const argv[])
 
                case 'V':
                        l = script_unhexlify(buf, sizeof(buf), optarg);
-                       if (!l)
+                       if (l) {
+                               if (odhcp6c_add_state(STATE_VENDORCLASS, buf, l)) {
+                                       syslog(LOG_ERR, "Failed to set vendor-class option");
+                                       return 1;
+                               }
+                       } else
                                help = true;
-
-                       odhcp6c_add_state(STATE_VENDORCLASS, buf, l);
                        break;
 
                case 'P':
@@ -134,7 +137,10 @@ int main(_unused int argc, char* const argv[])
                        else
                                prefix.iaid = htonl(++ia_pd_iaid_index);
 
-                       odhcp6c_add_state(STATE_IA_PD_INIT, &prefix, sizeof(prefix));
+                       if (odhcp6c_add_state(STATE_IA_PD_INIT, &prefix, sizeof(prefix))) {
+                               syslog(LOG_ERR, "Failed to set request IPv6-Prefix");
+                               return 1;
+                       }
                        break;
 
                case 'F':
@@ -149,7 +155,10 @@ int main(_unused int argc, char* const argv[])
                                buf[1] = DHCPV6_OPT_CLIENTID;
                                buf[2] = 0;
                                buf[3] = l;
-                               odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
+                               if (odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4)) {
+                                       syslog(LOG_ERR, "Failed to override client-ID");
+                                       return 1;
+                               }
                        } else
                                help = true;
                        break;
@@ -168,7 +177,10 @@ int main(_unused int argc, char* const argv[])
                                else if (optpos[0])
                                        optarg = &optpos[1];
 
-                               odhcp6c_add_state(STATE_ORO, &opttype, 2);
+                               if (odhcp6c_add_state(STATE_ORO, &opttype, 2)) {
+                                       syslog(LOG_ERR, "Failed to add requested option");
+                                       return 1;
+                               }
                        }
                        break;
 
@@ -178,8 +190,11 @@ int main(_unused int argc, char* const argv[])
 
                case 'u':
                        optlen = htons(strlen(optarg));
-                       odhcp6c_add_state(STATE_USERCLASS, &optlen, 2);
-                       odhcp6c_add_state(STATE_USERCLASS, optarg, strlen(optarg));
+                       if (odhcp6c_add_state(STATE_USERCLASS, &optlen, 2) ||
+                                       odhcp6c_add_state(STATE_USERCLASS, optarg, strlen(optarg))) {
+                               syslog(LOG_ERR, "Failed to set user-class option");
+                               return 1;
+                       }
                        break;
 
                case 's':
@@ -523,12 +538,16 @@ void odhcp6c_clear_state(enum odhcp6c_state state)
        state_len[state] = 0;
 }
 
-void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
+int odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
 {
        uint8_t *n = odhcp6c_resize_state(state, len);
 
-       if (n)
-               memcpy(n, data, len);
+       if (!n)
+               return -1;
+
+       memcpy(n, data, len);
+
+       return 0;
 }
 
 int odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len)
@@ -621,8 +640,8 @@ bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new,
                        x->t1 = new->t1;
                        x->t2 = new->t2;
                        x->iaid = new->iaid;
-               } else
-                       odhcp6c_add_state(state, new, odhcp6c_entry_size(new));
+               } else if (odhcp6c_add_state(state, new, odhcp6c_entry_size(new)))
+                       return false;
        } else if (x)
                odhcp6c_remove_state(state, ((uint8_t*)x) - start, odhcp6c_entry_size(x));
 
index 1d9bd3e6e554921d374cd54d228b0807579e7d0d..10abc5ce2a25969cdce5b0f2d6895162d636ada4 100644 (file)
@@ -348,7 +348,7 @@ bool odhcp6c_addr_in_scope(const struct in6_addr *addr);
 
 // State manipulation
 void odhcp6c_clear_state(enum odhcp6c_state state);
-void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len);
+int odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len);
 void odhcp6c_append_state(enum odhcp6c_state state, const void *data, size_t len);
 int odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len);
 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len);