diff --git a/.gitignore b/.gitignore index ec2ac48..377591c 100644 --- a/.gitignore +++ b/.gitignore @@ -93,3 +93,4 @@ utils/mnetscan/mnetscan mail.out utils/mnettoss/mnettoss mnet/* +utils/mnetsubunsub/mnetsubunsub diff --git a/utils/mnetscan/mnetscan.c b/utils/mnetscan/mnetscan.c index e0cc9d8..17574c9 100644 --- a/utils/mnetscan/mnetscan.c +++ b/utils/mnetscan/mnetscan.c @@ -25,6 +25,7 @@ struct msg_t { uint32_t timedate; int oaddr; int daddr; + int type; char reply[36]; } __attribute__ ((packed)); @@ -286,7 +287,7 @@ int export_messages(int area) { JAM_DelSubPacket(jsp); msg.area = areas[area]->id; - + msg.type = 0; if (areas[area]->hub == mynode) { for (n = 0; n < areas[area]->link_count; n++) { if (imhub) { diff --git a/utils/mnetsubunsub/Makefile b/utils/mnetsubunsub/Makefile new file mode 100644 index 0000000..3a90fa2 --- /dev/null +++ b/utils/mnetsubunsub/Makefile @@ -0,0 +1,26 @@ +CC=cc +CFLAGS=-I/usr/local/include -I../../deps/ +DEPS = mnetsubunsub.c +JAMLIB = ../../deps/jamlib/jamlib.a +LIBUUID = ../../deps/libuuid/.libs/libuuid.a +OBJ = mnetsubunsub.o + +all: mnetsubunsub + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +$(JAMLIB): + cd ../../deps/jamlib && make -f Makefile.linux + +$(LIBUUID): + cd ../../deps/libuuuid && ./configure + cd ../../deps/libuuuid && $(MAKE) + +mnetsubunsub: $(OBJ) $(JAMLIB) $(LIBUUID) + $(CC) -o mnetsubunsub $^ $(CFLAGS) -L/usr/local/lib + +.PHONY: clean + +clean: + rm -f $(OBJ) mnetsubunsub diff --git a/utils/mnetsubunsub/mnetsubunsub.c b/utils/mnetsubunsub/mnetsubunsub.c new file mode 100644 index 0000000..104633a --- /dev/null +++ b/utils/mnetsubunsub/mnetsubunsub.c @@ -0,0 +1,353 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "libuuid/uuid.h" + +char *baseindir = NULL; +char *baseoutdir = NULL; + +char *config_file; + +int imhub = 0; + +struct msgarea_t { + int id; + int hub; + char *basedir; + int *links; + int link_count; +}; + +struct msg_t { + int area; + char from[32]; + char to[32]; + char subject[64]; + uint32_t timedate; + int oaddr; + int daddr; + int type; + char reply[36]; +} __attribute__ ((packed)); + +struct msgarea_t **areas; +int area_count; +int mynode = 0; +int hubnode = 0; + +int copy_file(char *src, char *dest) { + FILE *src_file; + FILE *dest_file; + + char c; + + src_file = fopen(src, "rb"); + if (!src_file) { + return -1; + } + dest_file = fopen(dest, "wb"); + if (!dest_file) { + fclose(src_file); + return -1; + } + + while(1) { + c = fgetc(src_file); + if (!feof(src_file)) { + fputc(c, dest_file); + } else { + break; + } + } + + fclose(src_file); + fclose(dest_file); + return 0; +} + +size_t trimwhitespace(char *out, size_t len, const char *str) { + if(len == 0) + return 0; + + const char *end; + size_t out_size; + + // Trim leading space + while(isspace((unsigned char)*str)) str++; + + if(*str == 0) { + *out = 0; + return 1; + } + + // Trim trailing space + end = str + strlen(str) - 1; + while(end > str && isspace((unsigned char)*end)) end--; + end++; + + // Set output size to minimum of trimmed string length and buffer size minus 1 + out_size = (end - str) < len-1 ? (end - str) : len-1; + + // Copy trimmed string and add null terminator + memcpy(out, str, out_size); + out[out_size] = 0; + + return out_size; +} + +int parse_config_file(char *filename) { + FILE *fptr; + char buffer[256]; + char bufferw[256]; + char *ptr; + struct msgarea_t *newarea; + + + area_count = 0; + + fptr = fopen(filename, "r"); + if (!fptr) { + return 0; + } + + fgets(buffer, 256, fptr); + while (!feof(fptr)) { + if (buffer[0] != ';') { + if (buffer[strlen(buffer) - 1] == '\n'){ + buffer[strlen(buffer) - 1] = '\0'; + if (strncasecmp(buffer, "IMHUB", 5) == 0) { + trimwhitespace(bufferw, 256, &buffer[6]); + if (strcasecmp(bufferw, "TRUE") == 0) { + imhub = 1; + } + } else if (strncasecmp(buffer, "UPLINK", 6) == 0) { + trimwhitespace(bufferw, 256, &buffer[7]); + hubnode = atoi(bufferw); + } else if (strncasecmp(buffer, "INDIR", 5) == 0) { + trimwhitespace(bufferw, 256, &buffer[6]); + baseindir = strdup(bufferw); + } else if (strncasecmp(buffer, "OUTDIR", 6) == 0) { + trimwhitespace(bufferw, 256, &buffer[7]); + baseoutdir = strdup(bufferw); + } else if (strncasecmp(buffer, "MYNODE", 6) == 0) { + trimwhitespace(bufferw, 256, &buffer[7]); + mynode = atoi(bufferw); + } else if (strncasecmp(buffer, "MSGAREA", 7) == 0) { + newarea = NULL; + ptr = strtok(&buffer[8], ","); + + if (ptr != NULL) { + newarea = (struct msgarea_t *)malloc(sizeof(struct msgarea_t)); + trimwhitespace(bufferw, 256, ptr); + newarea->id = atoi(bufferw); + newarea->link_count = 0; + ptr = strtok(NULL, ","); + + if (ptr != NULL) { + trimwhitespace(bufferw, 256, ptr); + newarea->hub = atoi(bufferw); + ptr = strtok(NULL, ","); + if (ptr != NULL) { + + trimwhitespace(bufferw, 256, ptr); + newarea->basedir = strdup(bufferw); + ptr = strtok(NULL, ","); + while (ptr != NULL) { + trimwhitespace(bufferw, 256, ptr); + if (newarea->link_count == 0) { + newarea->links = (int *)malloc(sizeof(int)); + } else { + newarea->links = (int *)realloc(newarea->links, sizeof(int) * (newarea->link_count + 1)); + } + newarea->links[newarea->link_count] = atoi(bufferw); + newarea->link_count++; + ptr = strtok(NULL, ","); + } + } + } + } + if (newarea != NULL) { + if (area_count == 0) { + areas = (struct msgarea_t **)malloc(sizeof(struct msgarea_t *)); + } else { + areas = (struct msgarea_t **)realloc(areas, sizeof(struct msgarea_t *) * (area_count + 1)); + } + areas[area_count] = newarea; + area_count++; + } + } + } else { + fclose(fptr); + return 0; + } + } + fgets(buffer, 256, fptr); + } + + fclose(fptr); + return 1; +} + + + +void remove_config_file_area(int areaid) { + char backup[PATH_MAX]; + char buffer[256]; + char bufferc[256]; + char bufferw[256]; + FILE *fptr1; + FILE *fptr2; + char *ptr; + int id; + int i; + + snprintf(backup, PATH_MAX, "%s.bak", config_file); + fptr1 = fopen(config_file, "r"); + fptr2 = fopen(backup, "w"); + + fgets(buffer, 256, fptr1); + while (!feof(fptr1)) { + if (strncasecmp(buffer, "MSGAREA", 7) == 0) { + strcpy(bufferc, buffer); + ptr = strtok(&bufferc[8], ","); + if (ptr != NULL) { + trimwhitespace(bufferw, 256, ptr); + id = atoi(bufferw); + if (id != areaid) { + fputs(buffer, fptr2); + } + } + } else { + fputs(buffer, fptr2); + } + fgets(buffer, 256, fptr1); + } + unlink(config_file); + rename(backup, config_file); +} + +int main(int argc, char **argv) { + char buffer[PATH_MAX]; + struct msg_t msg; + uuid_t myuuid; + char msgid[37]; + FILE *fptr; + struct stat st; + + if (argc != 6) { + fprintf(stderr, "Usage ./mnetsubunsub [Configfile] [ADD/REMOVE] [AREA#] [HOST#] [JamBase]"); + return -1; + } + + if (!parse_config_file(argv[1])) { + fprintf(stderr, "Error parsing config file: %s\n", argv[1]); + return -1; + } + + if (baseoutdir == NULL) { + fprintf(stderr, "OUTDIR must be defined\n"); + return -1; + } + + if (strcasecmp(argv[2], "ADD") == 0) { + // send add control message + memset(&msg, 0, sizeof(struct msg_t)); + msg.area = atoi(argv[3]); + sprintf(msg.from, "SYSTEM"); + sprintf(msg.to, "SYSTEM"); + sprintf(msg.subject, "ADD Message"); + msg.timedate = time(NULL); + msg.oaddr = mynode; + msg.daddr = atoi(argv[4]); + msg.type = 1; + if (imhub) { + snprintf(buffer, PATH_MAX, "%s/%d/", baseoutdir, msg.daddr); + } else { + snprintf(buffer, PATH_MAX, "%s/%d/", baseoutdir, hubnode); + } + if (stat(buffer, &st) != 0) { + if (mkdir(buffer, 0755) != 0) { + fprintf(stderr, "Error making directory %s\n", buffer); + return -1; + } + } + + memset(msgid, 0, 37); + uuid_generate(myuuid); + uuid_unparse_lower(myuuid, msgid); + + if (imhub) { + snprintf(buffer, PATH_MAX, "%s/%d/%d-%s.message", baseoutdir, msg.daddr, msg.daddr, msgid); + } else { + snprintf(buffer, PATH_MAX, "%s/%d/%d-%s.message", baseoutdir, hubnode, msg.daddr, msgid); + } + fptr = fopen(buffer, "wb"); + if (fptr == NULL) { + fprintf(stderr, "Error creating file %s\n", buffer); + return -1; + } + + fwrite(&msg, sizeof(struct msg_t), 1, fptr); + fwrite("ADD\r", 4, 1, fptr); + fclose(fptr); + + // add config line + fptr = fopen(argv[1], "a"); + fprintf(fptr, "MSGAREA, %d, %d, %s\n", msg.area, msg.daddr, argv[5]); + fclose(fptr); + + } else if (strcasecmp(argv[2], "REMOVE") == 0) { + // send remove control message + memset(&msg, 0, sizeof(struct msg_t)); + msg.area = atoi(argv[3]); + sprintf(msg.from, "SYSTEM"); + sprintf(msg.to, "SYSTEM"); + sprintf(msg.subject, "REMOVE Message"); + msg.timedate = time(NULL); + msg.oaddr = mynode; + msg.daddr = atoi(argv[4]); + msg.type = 1; + if (imhub) { + snprintf(buffer, PATH_MAX, "%s/%d/", baseoutdir, msg.daddr); + } else { + snprintf(buffer, PATH_MAX, "%s/%d/", baseoutdir, hubnode); + } + if (stat(buffer, &st) != 0) { + if (mkdir(buffer, 0755) != 0) { + fprintf(stderr, "Error making directory %s\n", buffer); + return -1; + } + } + + memset(msgid, 0, 37); + uuid_generate(myuuid); + uuid_unparse_lower(myuuid, msgid); + + if (imhub) { + snprintf(buffer, PATH_MAX, "%s/%d/%d-%s.message", baseoutdir, msg.daddr, msg.daddr, msgid); + } else { + snprintf(buffer, PATH_MAX, "%s/%d/%d-%s.message", baseoutdir, hubnode, msg.daddr, msgid); + } + fptr = fopen(buffer, "wb"); + if (fptr == NULL) { + fprintf(stderr, "Error creating file %s\n", buffer); + return -1; + } + + fwrite(&msg, sizeof(struct msg_t), 1, fptr); + fwrite("REMOVE\r", 7, 1, fptr); + fclose(fptr); + + // remove config line + remove_config_file_area(msg.area); + + } + return 0; +} \ No newline at end of file diff --git a/utils/mnettoss/mnettoss.c b/utils/mnettoss/mnettoss.c index 9910016..b53d9d2 100644 --- a/utils/mnettoss/mnettoss.c +++ b/utils/mnettoss/mnettoss.c @@ -10,6 +10,8 @@ char *baseindir = NULL; char *baseoutdir = NULL; +char *config_file; + int imhub = 0; struct msgarea_t { @@ -28,6 +30,7 @@ struct msg_t { uint32_t timedate; int oaddr; int daddr; + int type; char reply[36]; } __attribute__ ((packed)); @@ -258,6 +261,48 @@ int isdupe(struct msg_t *msg, char *uuid) { return 0; } +void update_config_file_area(int areaid) { + char backup[PATH_MAX]; + char buffer[256]; + char bufferc[256]; + char bufferw[256]; + FILE *fptr1; + FILE *fptr2; + char *ptr; + int id; + int i; + + snprintf(backup, "%s.bak", config_file); + fptr1 = fopen(config_file, "r"); + fptr2 = fopen(backup, "w"); + + fgets(buffer, 256, fptr1); + while (!feof(fptr1)) { + if (strncasecmp(buffer, "MSGAREA", 7) == 0) { + strcpy(bufferc, buffer); + ptr = strtok(&bufferc[8], ","); + if (ptr != NULL) { + trimwhitespace(bufferw, 256, ptr); + id = atoi(bufferw); + if (id == areaid) { + fprintf(fptr2, "MSGAREA, %d, %d, %s", areas[areaid]->id, areas[areaid]->hub, areas[areaid]->basedir); + for (i=0;ilink_count;i++) { + fprintf(fptr2, ", %d", areas[areaid]->links[i]); + } + fprintf(fptr2, "\n"); + } else { + fputs(buffer, fptr2); + } + } + } else { + fputs(buffer, fptr2); + } + fgets(buffer, 256, fptr1); + } + unlink(config_file); + rename(backup, config_file); +} + int import(char *filename) { FILE *fptr; char buffer[PATH_MAX]; @@ -269,6 +314,7 @@ int import(char *filename) { struct stat st; int z; int i; + int j; char *body; s_JamBase *jb; s_JamBaseHeader jbh; @@ -316,6 +362,52 @@ int import(char *filename) { } if (areas[areaid]->hub == mynode) { + if (msg.type == 1) { + if (strncasecmp(body, "ADD", 3) == 0) { + for (i=0;ilink_count;i++) { + if (areas[areaid]->links[i] == msg.oaddr) { + // already subscribed + return 1; + } + } + + if (areas[areaid]->link_count == 0) { + areas[areaid]->links = (int *)malloc(sizeof(int)); + } else { + areas[areaid]->links = (int *)realloc(areas[areaid]->links, sizeof(int) * (areas[areaid]->link_count + 1)); + } + + areas[areaid]->links[areas[areaid]->link_count] = msg.oaddr; + areas[areaid]->link_count++; + + // + update_config_file_area(areaid); + + } else if (strncasecmp(body, "REMOVE", 6) == 0) { + for (i=0;ilink_count;i++) { + if (areas[areaid]->links[i] == msg.oaddr) { + + if (areas[areaid]->link_count == 1) { + free(areas[areaid]->links); + areas[areaid]->link_count = 0; + } else { + for (j=i;jlink_count-1;j++) { + areas[areaid]->links[j] = areas[areaid]->links[j+1]; + } + + areas[areaid]->links = (int *)realloc(areas[areaid]->links, sizeof(int) * (areas[areaid]->link_count - 1)); + areas[areaid]->link_count--; + + + } + update_config_file_area(areaid); + return 1; + } + } + } + return 1; + } + for (i=0;ilink_count;i++) { if (areas[areaid]->links[i] == msg.oaddr) { continue; @@ -342,6 +434,7 @@ int import(char *filename) { } } + ret = isdupe(&msg, uuid); if (ret == -1) { free(body); @@ -456,6 +549,8 @@ int main(int argc, char **argv) { return -1; } + config_file = argv[1]; + if (baseoutdir == NULL) { fprintf(stderr, "OUTDIR must be defined\n"); return -1;