--- /dev/null
+/*
+ File name : morep_valid.c
+ Projet : MERLIN
+ Date of creation : 2025/03/28
+ Version : 1.0
+ Copyright : Thales SIX
+ Author : Laurent Mazet <laurent.mazet@thalesgroup.com>
+
+ Description : Raw Ethernet packet valid program for MOREP library
+
+ History :
+ - initial version
+*/
+
+/* depend: */
+/* cflags: */
+/* linker: morep.o */
+/* winlnk: morep.o */
+
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "morep.h"
+#include "verbose.h"
+
+char *progname = NULL;
+
+int stop = 0;
+
+DECLARE_VERBOSE_LEVEL (morep, INFO);
+
+#define BUFMAX 4096
+
+void sig_handler (int sig)
+{
+ switch (sig) {
+ case SIGINT:
+ //stop = 1;
+ exit (0);
+ break;
+ case SIGTERM:
+ exit (0);
+ break;
+ }
+}
+
+typedef struct {
+ int morep;
+ int mode;
+ char *etype;
+} comm_t;
+
+#define MAXCOMMS 32
+
+int main (int argc, char **argv)
+{
+ char *filename = NULL;
+ char *url = NULL;
+ int mode = -1;
+ int nbcomms = 0;
+ comm_t comm_list[MAXCOMMS] = {0};
+
+ /* get basename */
+ char *pt = progname = argv[0];
+ while (*pt) {
+ if ((*pt == '/') || (*pt == '\\')) {
+ progname = pt + 1;
+ }
+ pt++;
+ }
+
+ /* process argument */
+ while (argc-- > 1) {
+ char *arg = *(++argv);
+ if (arg[0] != '-') {
+ filename = arg;
+ continue;
+ }
+ char c = arg[1];
+ switch (c) {
+ case 'r':
+ arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+ if (arg == NULL) {
+ VERBOSE (morep, ERROR, PRINTF ("%s: receiver url not specified\n", progname));
+ return 1;
+ }
+ url = arg;
+ mode = 1;
+ break;
+ case 't':
+ arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+ if (arg == NULL) {
+ VERBOSE (morep, ERROR, PRINTF ("%s: transmitter url not specified\n", progname));
+ return 1;
+ }
+ url = arg;
+ mode = 1;
+ break;
+ case 'v':
+ arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+ if (arg == NULL) {
+ VERBOSE (morep, ERROR, PRINTF ("%s: verbose level not specified\n", progname));
+ return 1;
+ }
+ CHANGE_VERBOSE_LEVEL (morep, atoi (arg));
+ break;
+ case 'h':
+ default:
+ printf ("usage: %s [-h] [-r url] [-t url] [-v level]\n", progname);
+ return (c != 'h');
+ }
+
+ /* init communication channel */
+ if (mode != -1) {
+ if (nbcomms < MAXCOMMS) {
+ int morep = MOREP_Connect (url);
+ if (morep >= 0) {
+ (comm_list + nbcomms)->morep = morep;
+ (comm_list + nbcomms)->mode = mode;
+ (comm_list + nbcomms)->etype = strrchr (url, '/') + 1;
+ nbcomms++;
+ } else {
+ VERBOSE (morep, ERROR, PRINTF ("can't connect on url '%s'\n", url));
+ return -1;
+ }
+ } else {
+ VERBOSE (morep, ERROR, PRINTF ("no more communication channel avaliable (%d)\n", nbcomms));
+ return -1;
+ }
+ mode = -1;
+ }
+ }
+
+ /* checks */
+ if (nbcomms == 0) {
+ VERBOSE (morep, ERROR, PRINTF ("no communication channel\n"));
+ return -1;
+ }
+
+ /* read file */
+ FILE *fid = stdin;
+ if (filename != NULL) {
+ fid = fopen (filename, "r");
+ if (fid == NULL) {
+ VERBOSE (morep, ERROR, PRINTF ("can't open file '%s' for reading\n", filename));
+ return -1;
+ }
+ }
+
+ /* signals */
+ signal(SIGINT, sig_handler);
+ signal(SIGTERM, sig_handler);
+
+ /* main loop */
+ int rc = 0;
+ while (!feof (fid)) {
+
+ /* read line */
+ char line[BUFMAX] = {0};
+ int i = 0;
+ do {
+ fread (line + i, 1, 1, fid);
+ if ((line[i] == '\n') || (line[i] == '\r')) {
+ line[i] = 0;
+ }
+ } while ((line[i] != 0) && (++i < (int)sizeof (line) - 1));
+
+ /* clean line */
+ char *pt = line;
+ while ((*pt == ' ') || (*pt == '\t')) {
+ if (*pt == '\0') {
+ break;
+ }
+ pt++;
+ }
+ if ((*pt == '\0') || (*pt == '#')) {
+ continue;
+ }
+
+ /* analyse line */
+
+
+ }
+
+ /* cleaning */
+ if (fid != stdin) {
+ fclose (fid);
+ }
+ while (nbcomms) {
+ MOREP_Close (--nbcomms);
+ }
+
+ return rc;
+}
+
+/* test: morep_valid.exe -h | grep usage */
+
+/* vim: set ts=4 sw=4 et: */