From e2927aac55a6e9bda36af9d1d96bcb93c9ba8f18 Mon Sep 17 00:00:00 2001 From: Taeho Oh Date: Mon, 4 Jan 2016 21:19:58 +0900 Subject: [PATCH] Add cmd arg parser. --- Makefile | 2 ++ omvs_main.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 12259b0..5680393 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ DEBUG = y +OMVS_VERSION = 0.1 TARGET = omvs OBJS = \ @@ -9,6 +10,7 @@ RM = rm -rf PKGS = gstreamer-1.0 CFLAGS = -I. `pkg-config --cflags $(PKGS)` `pcap-config --cflags` CFLAGS += -std=gnu99 -W -Wall -Wno-unused-result -pedantic +CFLAGS += -DOMVS_VERSION=\"$(OMVS_VERSION)\" LDFLAGS = `pkg-config --libs $(PKGS)` `pcap-config --libs` ifeq ($(DEBUG),y) diff --git a/omvs_main.c b/omvs_main.c index 9b3f6fc..6a07d24 100644 --- a/omvs_main.c +++ b/omvs_main.c @@ -1,9 +1,77 @@ #include #include -int main(int argc, char **argv) { - (void)argc; - (void)argv; +static gchar *_omvs_net_dev; +static gchar *_omvs_output = "omvs_out.txt"; +static gint _omvs_jobs = 1; +static gint _omvs_sleep = 1000; +static gint _omvs_timeout = 10000; - return 0; +static GOptionEntry _omvs_entries[] = +{ + { "interface", 'i', 0, G_OPTION_ARG_STRING, &_omvs_net_dev, + "Network device interface", "dev" }, + { "jobs", 'j', 0, G_OPTION_ARG_INT, &_omvs_jobs, + "Number of jobs", "jobs" }, + { "output", 'o', 0, G_OPTION_ARG_FILENAME, &_omvs_output, + "Output filename", "filename" }, + { "sleep", 's', 0, G_OPTION_ARG_INT, &_omvs_sleep, + "Sleep time(milliseconds) between scans", "ms" }, + { "timeout", 't', 0, G_OPTION_ARG_INT, &_omvs_timeout, + "Timeout time(milliseconds) in each scan", "ms" }, + { NULL } +}; + +typedef struct _OMVSPort { + gint port; + gint type; +} OMVSPort; + +typedef struct _OMVSIPAddr { + guint32 addr; + OMVSPort *ports; + gint num_ports; +} OMVSIPAddr; + +typedef struct _OMVSScanner { + OMVSIPAddr *ipaddrs; + gint num_ipaddrs; + gint scan_ipaddr_idx; + gint scan_port_idx; +} OMVSScanner; + +int main(int argc, char *argv[]) { + GError *error = NULL; + GOptionContext *context; + int ret = 0; + + context = g_option_context_new(""); + g_option_context_set_summary(context, + "omvs scans to find free video streaming multicast ip addresses." + ); + g_option_context_set_description(context, + "Oh! Multicast Video Scanner " OMVS_VERSION " is" + " Written by Taeho Oh .\n" + "http://ohhara.sarang.net"); + g_option_context_add_main_entries(context, _omvs_entries, NULL); + g_option_context_add_group(context, gst_init_get_option_group()); + if (!g_option_context_parse(context, &argc, &argv, &error)) { + g_print("option parsing failed: %s\n", error->message); + ret = -1; + goto finish_return; + } + + if (argc < 2) { + gchar *help_msg; + help_msg = g_option_context_get_help(context, TRUE, NULL); + g_print("%s", help_msg); + g_free(help_msg); + ret = -2; + goto finish_return; + } + +finish_return: + g_option_context_free(context); + + return ret; }