]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-system: Add ctdb_parse_connections() function
authorAmitay Isaacs <amitay@gmail.com>
Fri, 11 Mar 2016 00:44:12 +0000 (11:44 +1100)
committerAmitay Isaacs <amitay@samba.org>
Fri, 1 Apr 2016 02:42:11 +0000 (04:42 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/common/system.h
ctdb/common/system_util.c

index 1229a7ed88460dac3c4ef6895f68d61f76b42b09..604b66d9ba787be9b3237fce402d3376df8fd96f 100644 (file)
@@ -20,6 +20,8 @@
 #ifndef __CTDB_SYSTEM_H__
 #define __CTDB_SYSTEM_H__
 
+#include <talloc.h>
+
 /* From system_common.c */
 
 uint32_t uint16_checksum(uint16_t *data, size_t n);
@@ -64,4 +66,7 @@ ssize_t sys_write(int fd, const void *buf, size_t count);
 
 void ctdb_wait_for_process_to_exit(pid_t pid);
 
+int ctdb_parse_connections(FILE *fp, TALLOC_CTX *mem_ctx,
+                          int *num_conn, struct ctdb_connection **out);
+
 #endif /* __CTDB_SYSTEM_H__ */
index f47d5867b1f563d1eebffded8f07c6a17ad17ffa..4d56fd3d8532665f2ad03e0bc81a5a0232c49f2f 100644 (file)
@@ -23,6 +23,7 @@
 #include "system/shmem.h"
 #include "system/network.h"
 
+#include <talloc.h>
 #include <libgen.h>
 
 #include "lib/util/debug.h"
@@ -427,3 +428,57 @@ void ctdb_wait_for_process_to_exit(pid_t pid)
                sleep(5);
        }
 }
+
+int ctdb_parse_connections(FILE *fp, TALLOC_CTX *mem_ctx,
+                          int *num_conn, struct ctdb_connection **out)
+{
+       struct ctdb_connection *conn = NULL;
+       char line[128], src[128], dst[128]; /* long enough for IPv6 */
+       int line_num, ret;
+       int num = 0, max = 0;
+
+       line_num = 0;
+       while (! feof(fp)) {
+               if (fgets(line, sizeof(line), fp) == NULL) {
+                       break;
+               }
+               line_num += 1;
+
+               /* Skip empty lines */
+               if (line[0] == '\n') {
+                       continue;
+               }
+
+               ret = sscanf(line, "%s %s\n", src, dst);
+               if (ret != 2) {
+                       DEBUG(DEBUG_ERR, ("Bad line [%d]: %s\n",
+                                         line_num, line));
+                       return EINVAL;
+               }
+
+               if (num >= max) {
+                       max += 1024;
+                       conn = talloc_realloc(mem_ctx, conn,
+                                             struct ctdb_connection, max);
+                       if (conn == NULL) {
+                               return ENOMEM;
+                       }
+               }
+
+               if (! parse_ip_port(src, &conn[num].src)) {
+                       DEBUG(DEBUG_ERR, ("Invalid IP address %s\n", src));
+                       return EINVAL;
+               }
+
+               if (! parse_ip_port(dst, &conn[num].dst)) {
+                       DEBUG(DEBUG_ERR, ("Invalid IP address %s\n", dst));
+                       return EINVAL;
+               }
+
+               num += 1;
+       }
+
+       *num_conn = num;
+       *out = conn;
+       return 0;
+}