• dhcp


    #include "slot_module_update.h"

    uint32_t server_ips[MAX_SERVER_IPS] = {0};

    void str_toupper(char* str,int str_size)
    {
    //uint8_t *str_p = (uint8_t*)str;
    int i = 0;
    for( ;i < str_size; i++)
    {
    //dbg("from %c",str[i]);
    str[i] = toupper(str[i]);
    //dbg("to %c",str[i]);
    //str_p[i] = toupper(str_p[i]);
    }
    }

    int file_exist(char *filename)
    {
    return (access(filename,0) == 0);
    }

    int update_dhcpd_conf_file(packet,allocate_ip,filename)
    struct packet packet;
    char* allocate_ip;
    char* filename;
    {
    if(!file_exists(filename)){
    return FILE_PARSER_NOT_EXIST;
    }
    FILE *fp;
    char* buf[MAXDATASIZE];
    int line_num = 0;
    int valid_ip_line_num;
    struct in_addr ip_addr;

    FILE *new_fp;
    if((new_fp = fopen("tmep_conf","w")) == NULL)
    return FILE_PARSER_READ_ERROR;

    if((fp = fopen(filename,"r")) == NULL)
    return FILE_PARSER_READ_ERROR;
    while(fgets(buf,MAXDATASIZE,fp)){
    if(strstr()){
    }
    else
    {
    fprintf(new_fp,"%s",buf);
    }
    memset(buf,0,MAXDATASIZE);
    }
    return 0;
    }


    int socket_get_slot_module_id(packet,slot_id,module_id,server_ip)
    struct packet *packet;    
    uint8_t *slot_id;
    uint8_t *module_id;
    uint32_t server_ip;
    {   
    int sockfd, numbytes;    
    char buf[MAXDATASIZE] = {0};    
    struct sockaddr_in their_addr; // connector's address information   
    int sendbytes = 0;
    char mac_addr[17];
    int get_slot_module_id_flag = 0;

        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {    
       perror("socket");    
       return SLOT_MODULE_SOCKET_ERROR;
    }    

        their_addr.sin_family = AF_INET; /* host byte order */    
    their_addr.sin_port = htons(SERVERPORT); /* short, network byte order */    
    their_addr.sin_addr.s_addr = htonl(server_ip);
    // bzero(&(their_addr.sin_zero),0); /* zero the rest of the struct */

    dbg("ready to connect\n");
        if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1){
        perror("connect");
    close(sockfd);
        return SLOT_MODULE_CONNECT_ERROR;
    }
    //get mac addr from packet
    memcpy(mac_addr,packet->haddr->hbuf,MAC_ADDR_LENGTH);

    //str_toupper(mac_addr,MAC_ADDR_LENGTH);
    strcat(buf,GET_INFO);
    memcpy(buf+GET_INFO_LENGTH,mac_addr,MAC_ADDR_LENGTH);

    if((sendbytes = send(sockfd,buf,GET_INFO_LENGTH+MAC_ADDR_LENGTH,0)) == -1){
    perror("send");
    close(sockfd);
    return SLOT_MODULE_SEND_ERROR;
    }
    dbg("sentbytes ok!\n");

    while(1){
    memset(buf,0,MAXDATASIZE);
    if ((numbytes = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
    perror("recv");
    close(sockfd);
    return SLOT_MODULE_RECV_ERROR;
    }
    else if(numbytes == 1 && buf[0] == 0){
    printf("can not find the mac\n");
    break;
    }
    else if(numbytes == 2){
    dbg("slot is %d and module is %d\n",buf[0],buf[1]);
    *slot_id = buf[0];
    *module_id = buf[1];
    get_slot_module_id_flag = 1;
    break;
    }
    else continue;
    }
    close(sockfd);

    if(get_slot_module_id_flag == 1){
    return 0;
    }
    else
    return SLOT_MODULE_NOT_EXISTS_ERROR;

    }    



    int get_slot_num_and_ips(slot_num,filename)
    char *filename;
    int *slot_num;
    {
    if(!file_exist(filename)){
    *slot_num = 0;
    return FILE_PARSER_NOT_EXIST;
    }
    FILE *fp;
    char buf[MAXDATASIZE] = {0};
    int line_num = 0;
    int valid_ip_line_num = 0;
    struct in_addr ip_addr;

    if((fp = fopen(filename,"r")) == NULL)
    return FILE_PARSER_READ_ERROR;
    while(fgets(buf,MAXDATASIZE,fp)){
    if(buf[0] == 0x10){
    buf[0] = 0;
    continue;
    }
    else{
    line_num++;
    int i = 0;
    while(buf[i] != 0x10)
    i++;
    buf[i] = 0;
    if(inet_aton(buf,&ip_addr) != 0){
    dbg("buf:%s\n",buf);
    server_ips[valid_ip_line_num] = ntohl(ip_addr.s_addr);
    memset(buf,0,MAXDATASIZE);
    valid_ip_line_num++;
    continue;
    }
    else{
    dbg("ip_addr in wrong format!\n");
    continue;
    }
    }
    }
    if(line_num == 0){
    dbg("file empty!\n");
    *slot_num = 0;
    fclose(fp);
    return FILE_PARSER_EMPTY_ERROR;
    }
    else{
    *slot_num = valid_ip_line_num;
    dbg("slot_num is :%d and serverips0 is %d:\n",*slot_num,server_ips[0]);
    fclose(fp);
    return 0;
    }
    }

    int socket_get_slot_module_update(packet)
    struct packet *packet;
    {
    int k = 0;

    for(k =0;k<16;k++)
    dbg("%02x ",packet->raw->chaddr[k]);
    dbg("\n");
    for(k =0;k<17;k++)
    dbg("%02x ",packet->interface->hw_address.hbuf[k]);
    dbg("\n");

    uint8_t slot_id = 0;
    uint8_t module_id = 0;
    int get_slot_module_result = -1;
    int update_dhcp_conf_result = -1;
    char allocate_ip[MAX_IP_ADDR_LENGTH] = {0};
    int slot_num = 0;
    int i;
    int get_slots_flag = 0;
    int get_slot_num_and_ips_result = 0;


    if(get_slot_num_and_ips_result = get_slot_num_and_ips(&slot_num,SLOT_MODULE_SERVER_IPS) != 0)
    return get_slot_num_and_ips_result;
    for(i=0; i<slot_num; i++)
    {
    if((get_slot_module_result = socket_get_slot_module_id(packet,&slot_id,&module_id,server_ips[i])) > 0){
    dbg("Get slot and module in slot%d failed.Error code is:%d\n",i,get_slot_module_result);
    continue;
    }
    else{
    get_slots_flag = 1;
    break;
    }
    }
    if(get_slots_flag == 1 ){
    if(slot_id != 0 && module_id != 0){
    sprintf(allocate_ip,"192.168.%ud.%ud",slot_id,module_id);
    dbg("get slot and module success!allocate ip is %s\n",allocate_ip);
    if(update_dhcp_conf_result = (update_dhcpd_conf_file(packet,allocate_ip,path_dhcpd_conf)) != 0){
    dbg("Update conf file error\n");
    return update_dhcp_conf_result;
    }
    }
    }
    else{
    dbg("All slots scaned.Not found\n");
    return SLOT_MODULE_NOT_EXISTS_ALL_ERROR;
    }
    }

  • 相关阅读:
    网络七层协议
    Sizeof与Strlen的区别与联系
    Java面试宝典(说说&和&&的区别)
    Java se基础(类的属性及关键字)
    了解java中垃圾回收机制
    MYSQL常用函数(系统信息函数)
    MYSQL常用函数(类型转化函数)
    MYSQL常用函数(格式化函数)
    MYSQL常用函数(控制流函数)
    【CF-1362】B. Johnny and His Hobbies
  • 原文地址:https://www.cnblogs.com/zhihaowang/p/10128557.html
Copyright © 2020-2023  润新知