/* 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("usage: udpserver.are <port> <email-addr> <sms-number>\n"); 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("ERROR: Invalid E-Mail address\n"); exit(1); } } if (strlen(NUMBER) > 0) { pos = strchr(NUMBER, "+"); if (is_void(pos) || pos != 0) { printf("ERROR: Invalid SMS number\n"); exit(1); } } printf("Going send E-mail to '%s', SMS to '%s'\n", EMAILADDR, NUMBER); /* open UDP socket */ sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock < 0) { printf("ERROR: Unable to open socket\n"); exit(1); } /* bind socket to 1st LAN interface */ addr = nb_ifc_address("LAN1"); ret = bind(sock, PORT, addr); if(ret == -1) { printf("ERROR: Unable to bind to port %d on LAN1\n", PORT); close(sock); exit(1); } printf("Listening for messages on port %d\n", PORT); while (1) { msg = recvfrom(sock); if (left(msg, 4) == "quit") { printf("Received quit, terminating ...\n"); break; } else if (msg != "") { printf("Received message '%s'\n", msg); /* send E-mail */ if (strlen(EMAILADDR) > 0) { if (nb_email_send(EMAILADDR, "message received", msg) == 0) { printf("Forwarded message via E-mail\n"); } else { printf("ERROR: Unable to send E-mail\n"); } } /* send SMS */ if (strlen(NUMBER) > 0) { id = nb_sms_send(NUMBER, msg); if (id) { printf("Forwarded message via SMS\n"); } else { printf("ERROR: Unable to send SMS\n"); } } } } close(sock); exit(0);