APUE Learning Example Source Code
guowenxue
2020-04-13 e24ac85cc8d281a3ee271fd631e9478868dca743
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*********************************************************************************
 *      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 <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "2019-1-1 01:38:08 PM"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <getopt.h>
#include <libgen.h>
 
#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              *id;
    char              *serverip;
    int                port;
    int                i;
    char               key[64];
    char               msg[512];
 
    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; 
    }   
 
    id=iniparser_getstring(ini, "common:id", NULL);
    domain=iniparser_getstring(ini, "common:domain", NULL);
 
    if( !domain )
    {
        printf("ERROR: cannot parse domain/id in '%s'\n", conf_file); 
        return -1; 
    }
 
    memset(msg, 0, sizeof(msg));
    snprintf(msg, sizeof(msg), "[%s]: '%s'", id, domain);
 
    for(i=0; i<MAX_HOSTS; i++)
    {
        memset(key, 0, sizeof(key));
        snprintf(key, sizeof(key), "host%d:ip", i);
 
        serverip = iniparser_getstring(ini, key, NULL);
 
        snprintf(key, sizeof(key), "host%d:port", i);
        port = iniparser_getint(ini, key, 0);
 
        if( !serverip || !port )
        {
            continue;
        }
 
        printf("==> Start send domain '%s' to [%s:%d]\n", domain, serverip, port);
        socket_send_domain(serverip, port, msg);
        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);
}