#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "j_socket.h"
//type
typedef enum{
IP138,
ITLEARNER
}WEBTYPE;
typedef struct __root_field__
{
char city[64];
char provider[32];
}root_location;
typedef struct __ip138_field__
{
root_location root;
}ip138_field;
typedef struct __itlearner_field__
{
root_location root;
char nation[32];
}itlearner_field;
typedef int (*search_handle) (char*, void*);
typedef struct __SET_WEB__
{
WEBTYPE type;
search_handle site;
}SET_WEB;
//function
static int jry_search_ip138(char* ip, void* obj);
static int jry_search_itlearner(char* ip, void* obj);
//Variable
static SET_WEB __g_func_web[]={\
{IP138,jry_search_ip138},
{ITLEARNER, jry_search_itlearner},
};
int jry_copy_webdata(char *src, char *start_key, char *end_key, char *dest)
{
char *start=NULL;
char *end=NULL;
do
{
if(!src || !start_key || !end_key || !dest)return 0;
start=strstr(src, start_key);
start=start+strlen(start_key);
if(!start)break;
end=strstr(start, end_key);
if(!end)break;
if(!start || !end)return 0;
dest[end-start]=0;
strncpy(dest, start, end-start);
} while (0);
return 1;
}
static int jry_search_ip138(char* ip, void* obj)
{
char *buf=NULL;
size_t size=0;
int iret=0;
ip138_field *field=(ip138_field*)obj;
char url[128]={0};
do
{
if(!ip || !obj)return 0;
sprintf(url, "%s%s%s%s", "http://www.ip138.com/","ips.asp?", "ip=", ip);
if(SUCCESS!=jry_open_url(url, &buf, &size)){iret=0; break;}
if(!buf)return 0;
if(!jry_copy_webdata(buf, ">本站主数据:", "<", field->root.city)){iret=0;break;}
if(!jry_copy_webdata(buf, ">参考数据一:", "<", field->root.provider)){iret=0;break;}
} while (0);
if(buf){free(buf);buf=NULL;}
return 1;
}
static int jry_search_itlearner(char *ip, void* obj)
{
char *buf=NULL;
size_t size=0;
int iret=0;
itlearner_field *field=(itlearner_field*)obj;
char url[128]={0};
do
{
if(!ip || !obj)return 0;
sprintf(url, "%s%s%s%s", "http://ip.itlearner.com/","?", "ip=", ip);
if(SUCCESS!=jry_open_url(url, &buf, &size)){iret=0; break;}
if(!buf)return 0;
if(!jry_copy_webdata(buf, ">所在区域:", "<", field->root.city)){iret=0;break;}
if(!jry_copy_webdata(buf, ">国家:", "<", field->nation)){iret=0;break;}
} while (0);
if(buf){free(buf);buf=NULL;}
return 1;
}
int jry_search_handle(char* ip, WEBTYPE web, void* obj)
{
return __g_func_web[web].site(ip, obj);
}
void test()
{
char ip[32]={0};
__itlearner_field__ obj={0};
//printf("%s\n", jry_get_localip(ip));
if(!JRY_WSASTARUP())
{
printf("网络初始化失败\n");
return ;
}
jry_search_handle(jry_get_localip(ip), ITLEARNER, &obj);
printf("%s\t%s\t%s\n", obj.root.city, obj.root.provider, obj.nation);
}
int main(int arg, char **argv)
{
test();
return 0;
}