/********************************************************************************* * Copyright: (C) 2018 LingYun IoT Studio * All rights reserved. * * Filename: ddns_client.c * Description: This is a ddns client code to report public IP address to * server, it just need to send the domain name, and server * can get its public IP address. * * Version: 1.0.0(1/1/2019) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "2019-1-1 01:38:08 PM" * ********************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include "iniparser.h" #define MAX_HOSTS 3 #define PROG_VERSION "v1.0.0" int socket_send_domain(char *serverip, int port, char *msg); static void program_usage(char *progname) { printf("Usage: %s [OPTION]...\n", progname); printf(" %s is LingYun studio DDNS client program to report public IP address\n", progname); printf("\nMandatory arguments to long options are mandatory for short options too:\n"); printf(" -c[conf ] Specify configure file\n"); printf(" -h[help ] Display this help information\n"); printf(" -v[version ] Display the program version\n"); printf("\n%s version %s\n", progname, PROG_VERSION); return; } int main(int argc, char **argv) { char *conf_file = "./ddns.conf"; char *progname=NULL; int opt; dictionary *ini; char *str; int val; char *domain; char *serverip; int port; int i; char key[64]; struct option long_options[] = { {"conf", required_argument, NULL, 'c'}, {"version", no_argument, NULL, 'v'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; progname = (char *)basename(argv[0]); /* Parser the command line parameters */ while ((opt = getopt_long(argc, argv, "c:dvh", long_options, NULL)) != -1) { switch (opt) { case 'c': /* Set configure file */ conf_file = optarg; break; case 'v': /* Get software version */ printf("%s version %s\n", progname, PROG_VERSION); return 0; case 'h': /* Get help information */ program_usage(progname); return 0; default: break; } } ini = iniparser_load(conf_file); if( !ini ) { printf("ERROR: cannot parse file: '%s'\n", conf_file); return -1; } domain=iniparser_getstring(ini, "common:domain", NULL); if( !domain ) { printf("ERROR: cannot parse domain in '%s'\n", conf_file); return -1; } for(i=1; i Start send domain '%s' to [%s:%d]\n", domain, serverip, port); socket_send_domain(serverip, port, domain); printf("\n"); } iniparser_freedict(ini); } int socket_send_domain(char *serverip, int port, char *msg) { int conn_fd = -1; int rv = -1; struct sockaddr_in serv_addr; struct sockaddr_in addr; struct timeval timeo = {6, 0}; socklen_t len = sizeof(timeo); conn_fd = socket(AF_INET, SOCK_STREAM, 0); if(conn_fd < 0) { printf("ERROR: create socket failure: %s\n", strerror(errno)); return -1; } memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(port); inet_aton( serverip, &serv_addr.sin_addr ); setsockopt(conn_fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, len); if( connect(conn_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { printf("ERROR: connect to server [%s:%d] failure: %s\n", serverip, port, strerror(errno)); goto cleanup; } if( write(conn_fd, msg, strlen(msg)) < 0 ) { printf("ERROR: Write data to server [%s:%d] failure: %s\n", serverip, port, strerror(errno)); goto cleanup; } printf("ok\n"); sleep(1); cleanup: close(conn_fd); }