基本信息
源码名称:探测网络中在线主机个数
源码大小:0.11M
文件格式:.rar
开发语言:C/C++
更新时间:2016-05-08
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "Ip_func.h"
#include "Ip_detect.h"


//分配空间,如果分配失败则等待5秒继续分配,
//连续分配5次失败后退出,分配成功则返回地址;
void *emalloc(size_t size)
{
    void *ptr;
    /*
     * Just for our personal safety, we increase the
     * size by one
     */
    if((int)size < 0)
    {
        fprintf(stderr, "[%d] Won't allocate a pointer of size %u !\n", getpid(), size);
    }

    size   ;

    /*
    * If no memory can be allocated, then wait a little.
    * It's very likely that another process child will free
    * the size of memory we need. So we make 10 attempts,
    * and if nothing happens, then we exit
    */

    ptr = malloc(size);

    if(!ptr)
    {
        int i;

        for(i = 0; i < 5 && ptr == NULL; i  )
        {
            waitpid(0, NULL, WNOHANG);
            usleep(5000);
            ptr = malloc(size);
        }

        if( ptr == NULL )
        {
            fprintf(stderr, "[%d] Could not allocate a pointer of size %u !\n", getpid(), size);
            exit(1);
        }
    }
    bzero(ptr, size);

    return ptr;
}

char *estrdup(const char *str)
{
    char * buf;
    int len;

    if(!str)
    {
        return NULL;
    }

    len = strlen(str);

    buf = emalloc(len   1);
    memcpy(buf, str, len);
    buf[len] = '\0';

    return buf;
}

void efree(void *ptr)
{
    char **p = ptr;

    if(p && *p)
    {
    	free(*p);
    	*p=NULL;
	}
}


Ip_List *Single_ip(char *Str_ip)
{
    int is_valid;
    Ip_List *head = NULL;
    Ip_List *p = NULL;

    struct in_addr addr;
    is_valid = inet_pton(AF_INET, Str_ip, &addr);

    if(is_valid <= 0)
    {
        printf("IP invalid....\n");
        printf("\tToo know detail using the --help(-h) option\n");

        exit(0);
    }

    p = (Ip_List *)emalloc(sizeof(Ip_List));
    head = p;

    strcpy(p->Ip_arglist, Str_ip);

    return head;
}

Ip_List *Range_ip(char *Str_ip)
{
    char *IP_tail = NULL;
    char *IP_head = NULL;
    char *IP_p = NULL;
    char *buf = NULL;
    int is_valid;
    int count = 0;
    Ip_List *head = NULL;

    if(strchr(Str_ip, '-') == 0)
    {
        printf("IP invalid....\n");
        printf("\tToo know detail using the --help(-h) option\n");
        exit(0);
    }

    buf = estrdup(Str_ip);
    IP_tail = buf;

    while(count < 2)
    {
        if(!IP_tail)
        {
            break;
        }

        IP_head = IP_tail;
        IP_p = strchr(IP_tail, '-');

        if(IP_p)
        {
            *IP_p = '\0';
            IP_tail = IP_p   1;
        }

        struct in_addr addr;
        is_valid = inet_pton(AF_INET, IP_head, &addr);

        if(is_valid <= 0)
        {
            printf("IP invalid....\n");
            printf("\tToo know detail using the --help(-h) option\n");
            exit(0);
        }

        count   ;
    }

    head = Range_ip_Init(Str_ip);

    return head;
}

Ip_List *Range_ip_Init(char *Str_ip)
{
    char *buf = NULL;
    char tmp[32] = {0};
    //char tmp1[32] = {0};
    char str_tmp[16] = {0};

    Ip_List *head = NULL;
    Ip_List *p = NULL;
    Ip_List *q = NULL;

    int var1[5] = {0}, var2[5] = {0};

    if(!Str_ip)
    {
        return NULL;
    }
    buf = (char *)emalloc(80);
    memset(buf, '0', 80);

    strcpy(buf, Str_ip);

    sscanf(buf, "%d.%d.%d.%d-%d.%d.%d.%d", &var1[0], &var1[1], &var1[2], &var1[3],
           &var2[0], &var2[1], &var2[2], &var2[3]);
    sprintf(tmp, "%d.%d.%d.", var1[0], var1[1], var1[2]);
    //strcmp(tmp1, tmp);

    p = (Ip_List *)emalloc(sizeof(Ip_List));
    head = p;

    while(var2[3] >= var1[3])
    {
        q = p;

        sprintf(str_tmp, "%d", var1[3]);
        strcat(tmp, str_tmp);

        strcpy(p->Ip_arglist, tmp);

        var1[3]  ;

        if(var1[3] <= var2[3])
        {
            p = (Ip_List *)emalloc(sizeof(Ip_List));
            q->next = p;
        }
        else
        {
            break;
        }

        memset(tmp, '0', 32);
        sprintf(tmp, "%d.%d.%d.", var1[0], var1[1], var1[2]);
    }
    p->next = NULL;

    return head;
}

Ip_List *Mask_ip(char *Str_ip)
{
    Ip_List *head = NULL;
    //Ip_List *p = NULL;
    char *Ip_tail = NULL;
    char *Ip_head = NULL;
    char *Ip_p = NULL;
    int result = 0;

    Ip_tail = Str_ip;
    Ip_head = Ip_tail;

    Ip_p = strchr(Ip_tail, '/');
    *Ip_p = '\0';
    Ip_tail = Ip_p   1;

    if(!Ip_p)
    {
        printf("IP invalid....\n");
        printf("\tToo know detail using the --help(-h) option\n");
        exit(0);
    }

    struct in_addr addr;
    result = inet_pton(AF_INET, Ip_head, &addr);

    if(result <= 0)
    {
        printf("IP invalid....\n");
        printf("\tToo know detail using the --help(-h) option\n");
        exit(0);
    }

    Ip_head = Ip_tail;

    if(atoi(Ip_head) < 0 || atoi(Ip_head) > 32)
    {
        printf("IP invalid....\n");
        printf("\tToo know detail using the --help(-h) option\n");
        exit(0);
    }

    return head;
}

Ip_List *Is_ip_online(Ip_List *head)
{
    FILE *fp = NULL;
    Ip_List *p = NULL;
    char commond[64];
    char buffer[1024] = {0};
    p = head;

    while(p)
    {
        sprintf(commond, "ping %s -w 1", p->Ip_arglist);
        //sprintf(commond, "ping %s -w 1  1 > /dev/null 2 > /dev/null", p->Ip_arglist);
        fp = popen(commond, "r");

        while(!feof(fp))
        {
            fgets(buffer, sizeof(buffer) - 1, fp);

            if(!strstr(buffer, "0 received"))
            {
                p->flag = 1;
            }
            else
            {
                p->flag = 0;
                break;
            }
        }
        memset(commond, '0', 64);
        memset(buffer, '0', 1024);
        pclose(fp);

        p = p->next;
    }

    return head;
}

void display_help(char *Str_ip)
{
    printf("General options :\n");
    printf("\t-v : shows version number\n");
    printf("\t-h : shows this help\n");
    printf("\t-s : single ip <option> [ip]\n");
    printf("\t-r : range ip  <option> [ip1-ip2]\n");
    printf("\t-m : mask ip   <option> [ip1/mask]\n");
    exit(0);
}