Revert "sq set_socket_option"
authorStefan Metzmacher <metze@samba.org>
Tue, 30 Jun 2020 14:52:07 +0000 (16:52 +0200)
committerStefan Metzmacher <metze@samba.org>
Mon, 5 Oct 2020 12:12:40 +0000 (14:12 +0200)
This reverts commit acff662f487465ae06464f6f2e3a00235ac03f55.

lib/util/util_net.c
lib/util/util_net.h

index 7d3785ffae9e636405cd358ba25a994dd1a84965..c45dd8746f1c011dc4bd06d12c08b5a15dd4a848 100644 (file)
@@ -1005,20 +1005,17 @@ int get_socket_option(int fd, const char *option)
        int i;
 
        for (i=0; socket_options[i].name != NULL; i++) {
-               const smb_socket_option *opt = &socket_options[i];
+               const smb_socket_option *p = &socket_options[i];
                int value = 0;
                socklen_t vlen = sizeof(value);
                int ret;
 
-               if (!strequal(opt->name, option)) {
+               if (!strequal(p->name, option)) {
                        continue;
                }
 
-               ret = getsockopt(fd,
-                                opt->level,
-                                opt->option,
-                                (void *)&value,
-                                &vlen);
+               ret = getsockopt(fd, p->level, p->option,
+                                (void *)&value, &vlen);
                if (ret == -1) {
                        return -1;
                }
@@ -1029,88 +1026,69 @@ int get_socket_option(int fd, const char *option)
        return -1;
 }
 
-void set_socket_option(int fd, const char *option)
-{
-       TALLOC_CTX *frame = talloc_stackframe();
-       char *name = NULL;
-       int value = 1;
-       char *p = NULL;
-       bool got_value = false;
-       int i;
-
-       name = talloc_strdup(frame, option);
-       if (name == NULL) {
-               DEBUG(0,("talloc_strdup(%s) - failed\n", option));
-               TALLOC_FREE(frame);
-               return;
-       }
+/****************************************************************************
+ Set user socket options.
+****************************************************************************/
 
-       if ((p = strchr_m(name, '='))) {
-               *p = 0;
-               value = atoi(p+1);
-               got_value = true;
-       }
+void set_socket_options(int fd, const char *options)
+{
+       TALLOC_CTX *ctx = talloc_new(NULL);
+       char *tok;
+
+       while (next_token_talloc(ctx, &options, &tok," \t,")) {
+               int ret=0,i;
+               int value = 1;
+               char *p;
+               bool got_value = false;
+
+               if ((p = strchr_m(tok,'='))) {
+                       *p = 0;
+                       value = atoi(p+1);
+                       got_value = true;
+               }
 
-       for (i=0; socket_options[i].name != NULL; i++) {
-               const smb_socket_option *opt = &socket_options[i];
-               int ret;
+               for (i=0;socket_options[i].name;i++)
+                       if (strequal(socket_options[i].name,tok))
+                               break;
 
-               if (!strequal(opt->name, name)) {
+               if (!socket_options[i].name) {
+                       DEBUG(0,("Unknown socket option %s\n",tok));
                        continue;
                }
 
-               switch (opt->opttype) {
+               switch (socket_options[i].opttype) {
                case OPT_BOOL:
                case OPT_INT:
+                       ret = setsockopt(fd,socket_options[i].level,
+                                       socket_options[i].option,
+                                       (char *)&value,sizeof(int));
                        break;
+
                case OPT_ON:
-                       if (got_value) {
+                       if (got_value)
                                DEBUG(0,("syntax error - %s "
-                                       "does not take a value\n",
-                                       option));
-                       }
+                                       "does not take a value\n",tok));
 
-                       value = opt->value;
+                       {
+                               int on = socket_options[i].value;
+                               ret = setsockopt(fd,socket_options[i].level,
+                                       socket_options[i].option,
+                                       (char *)&on,sizeof(int));
+                       }
                        break;
-               default:
-                       continue;
                }
 
-               ret = setsockopt(fd,
-                                opt->level,
-                                opt->option,
-                                (char *)&value,
-                                sizeof(int));
                if (ret != 0) {
                        /* be aware that some systems like Solaris return
                         * EINVAL to a setsockopt() call when the client
                         * sent a RST previously - no need to worry */
                        DEBUG(2,("Failed to set socket option %s (Error %s)\n",
-                               option, strerror(errno) ));
+                               tok, strerror(errno) ));
                }
-               TALLOC_FREE(frame);
-               return;
-       }
-
-       DEBUG(0,("Unknown socket option %s\n", option));
-       TALLOC_FREE(frame);
-}
-
-/****************************************************************************
- Set user socket options.
-****************************************************************************/
-
-void set_socket_options(int fd, const char *options)
-{
-       TALLOC_CTX *frame = talloc_stackframe();
-       char *tok = NULL;
-
-       while (next_token_talloc(frame, &options, &tok," \t,")) {
-               set_socket_option(fd, tok);
        }
 
+       TALLOC_FREE(ctx);
        print_socket_options(fd);
-       TALLOC_FREE(frame);
 }
 
 #define GET_SOCK_OPT(_fd, _name) \
@@ -1170,7 +1148,7 @@ void get_socket_options_keepalive(int sock_fd,
 #define SET_SOCK_OPT(_fd, _name, _value) do { \
        char _val[100]; \
        snprintf(_val, sizeof(_val) - 1, #_name "=%d", _value); \
-       set_socket_option(_fd, _val); \
+       set_socket_options(_fd, _val); \
 } while(0)
 
 void set_socket_options_keepalive(int sock_fd,
index fd8620e272edbcc081bb7dae10968aef764f0439..b3354d0e91d3b898b800fa520e34b1309b567e73 100644 (file)
@@ -122,7 +122,6 @@ char *print_canonical_sockaddr(TALLOC_CTX *ctx,
                        const struct sockaddr_storage *pss);
 
 int get_socket_option(int fd, const char *option);
-void set_socket_option(int fd, const char *option);
 void set_socket_options(int fd, const char *options);
 void print_socket_options(int fd);
 void get_socket_options_keepalive(int sock_fd,