This shows you the differences between two versions of the page.
— | sdk:scripts:udp-msg-server [2015/05/05 15:04] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== SDK Script udp-msg-server.are ====== | ||
+ | <code c udp-msg-server.are> | ||
+ | /* DESC: This script will run an UDP server which is able to receive messages and forward them as SMS/E-Mail. | ||
+ | * Copyright (C) 2012 NetModule AG, Switzerland | ||
+ | */ | ||
+ | |||
+ | void usage() | ||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | if (argc < 2) { | ||
+ | usage(); | ||
+ | } | ||
+ | |||
+ | PORT = (int) argv[1]; | ||
+ | EMAILADDR = trim((string) argv[2]); | ||
+ | NUMBER = trim((string) argv[3]); | ||
+ | |||
+ | /* check arguments */ | ||
+ | if (strlen(EMAILADDR) > 0) { | ||
+ | pos = strchr(EMAILADDR, | ||
+ | if (is_void(pos)) { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | } | ||
+ | if (strlen(NUMBER) > 0) { | ||
+ | pos = strchr(NUMBER, | ||
+ | if (is_void(pos) || pos != 0) { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | /* open UDP socket */ | ||
+ | sock = socket(AF_INET, | ||
+ | if (sock < 0) { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* bind socket to 1st LAN interface */ | ||
+ | addr = nb_ifc_address(" | ||
+ | ret = bind(sock, PORT, addr); | ||
+ | if(ret == -1) { | ||
+ | printf(" | ||
+ | close(sock); | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | while (1) { | ||
+ | msg = recvfrom(sock); | ||
+ | if (left(msg, 4) == " | ||
+ | printf(" | ||
+ | break; | ||
+ | } else if (msg != "" | ||
+ | printf(" | ||
+ | |||
+ | /* send E-mail */ | ||
+ | if (strlen(EMAILADDR) > 0) { | ||
+ | if (nb_email_send(EMAILADDR, | ||
+ | printf(" | ||
+ | } else { | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | /* send SMS */ | ||
+ | if (strlen(NUMBER) > 0) { | ||
+ | id = nb_sms_send(NUMBER, | ||
+ | if (id) { | ||
+ | printf(" | ||
+ | } else { | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | close(sock); | ||
+ | exit(0); | ||
+ | |||
+ | </ | ||