1#include <stdio.h>
2#include <signal.h>     /* for singal handle */
3#include "phkey.h"
4#include "phruncall.h"
5#include "phupdate.h"
6#ifndef WIN32
7#include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */
8#include <netdb.h>
9#include <unistd.h>     /* for close() */
10#include "log.h"
11
12static void create_pidfile()
13{
14    FILE *pidfile;
15    char pidfilename[128];
16    sprintf(pidfilename, "%s", "/var/run/phddns.pid");
17
18    if ((pidfile = fopen(pidfilename, "w")) != NULL) {
19		fprintf(pidfile, "%d\n", getpid());
20		(void) fclose(pidfile);
21    } else {
22		printf("Failed to create pid file %s: %m", pidfilename);
23		pidfilename[0] = 0;
24    }
25}
26#endif
27
28PHGlobal global;
29PH_parameter parameter;
30static void my_handleSIG (int sig)
31{
32	if (sig == SIGINT)
33	{
34		printf ("signal = SIGINT\n");
35		phddns_stop(&global);
36		exit(0);
37	}
38	if (sig == SIGTERM)
39	{
40		printf ("signal = SIGTERM\n");
41		phddns_stop(&global);
42	}
43	signal (sig, my_handleSIG);
44}
45
46//������������
47static void myOnStatusChanged(PHGlobal* global, int status, long data)
48{
49	printf("myOnStatusChanged %s", convert_status_code(status));
50	if (status == okKeepAliveRecved)
51	{
52		printf(", IP: %d", data);
53	}
54	if (status == okDomainsRegistered)
55	{
56		printf(", UserType: %d", data);
57	}
58	printf("\n");
59}
60
61//������������
62static void myOnDomainRegistered(PHGlobal* global, char *domain)
63{
64	printf("myOnDomainRegistered %s\n", domain);
65}
66
67//��������XML��������
68static void myOnUserInfo(PHGlobal* global, char *userInfo, int len)
69{
70	printf("myOnUserInfo %s\n", userInfo);
71}
72
73//��������XML��������
74static void myOnAccountDomainInfo(PHGlobal* global, char *domainInfo, int len)
75{
76	printf("myOnAccountDomainInfo %s\n", domainInfo);
77}
78
79int main(int argc, char *argv[])
80{
81	void (*ohandler) (int);
82#ifdef WIN32
83	WORD VersionRequested;		// passed to WSAStartup
84	WSADATA  WsaData;			// receives data from WSAStartup
85	int error;
86
87	VersionRequested = MAKEWORD(2, 0);
88
89	//start Winsock 2
90	error = WSAStartup(VersionRequested, &WsaData);
91#endif
92
93	ohandler = signal (SIGINT, my_handleSIG);
94	if (ohandler != SIG_DFL) {
95		printf ("previous signal handler for SIGINT is not a default handler\n");
96		signal (SIGINT, ohandler);
97	}
98
99	init_global(&global);
100	init_parameter(&parameter);
101	if( checkparameter(argc,argv,&global,&parameter) != 0 ) {
102//		printf ("Parameter has an unexpected error,please read the help above.Thank you for your patient!\n");
103		return 0;
104	}
105
106	if( parameter.bDaemon == 1 ) {
107#ifndef WIN32
108		printf("phddns started as daemon!\n");
109		if(0 != daemon(0,0))
110		{
111			printf("daemon(0,0) failed\n");
112			return -1;
113		}
114#else
115		printf("phddns started daemon failed\n");
116#endif
117	}
118
119	log_open( parameter.logfile, 1);	//empty file will cause we printf to stdout
120	//create_pidfile();
121
122	global.cbOnStatusChanged = myOnStatusChanged;
123	global.cbOnDomainRegistered = myOnDomainRegistered;
124	global.cbOnUserInfo = myOnUserInfo;
125	global.cbOnAccountDomainInfo = myOnAccountDomainInfo;
126
127	set_default_callback(&global);
128	#ifdef OFFICIAL_CLIENT
129	#include "official/official_key.c"
130	#else
131
132	global.clientinfo = PH_EMBED_CLIENT_INFO;
133	global.challengekey = PH_EMBED_CLIENT_KEY;
134	#endif
135
136	for (;;)
137	{
138		int next = phddns_step(&global);
139		sleep(next);
140	}
141	phddns_stop(&global);
142	return 0;
143}
144