Exemple #1
0
void *(gn_vec_grow)(size_t *vec, size_t nr, size_t elt_size)
{
   size_t need = vec[0] + nr;
   if (need < nr)
      gn_fatal("integer overflow");

   size_t alloc = vec[1];
   if (alloc >= need)
      return vec + 2;

   if (vec == gn_vec_void) {
      if (need > (SIZE_MAX - sizeof gn_vec_void) / elt_size)
         gn_fatal("integer overflow");
      vec = gn_malloc(sizeof gn_vec_void + need * elt_size);
      vec[0] = vec[1] = 0;
      return vec + 2;
   }

   alloc += (alloc >> 1);
   if (alloc < need)
      alloc = need;
   if (alloc > (SIZE_MAX - sizeof gn_vec_void) / elt_size)
      gn_fatal("integer overflow");
   vec = gn_realloc(vec, sizeof gn_vec_void + alloc * elt_size);
   vec[1] = alloc;
   return vec + 2;
}
Exemple #2
0
void l3_proc(gn_switch_t *sw, packet_in_info_t *packet_in_info, UINT4 gw_ip)
{
    UINT4 j = 0;
    ether_t *ether = (ether_t *)packet_in_info->data;

    if(ether->proto == htons(ETHER_IP))
    {
        l3_send_buf_t *new_packet = (l3_send_buf_t *)gn_malloc(sizeof(l3_send_buf_t));
        new_packet->dst_ip = ((ip_t *)ether)->dest;
        new_packet->buffer_id = packet_in_info->buffer_id;
        new_packet->pkt_len = packet_in_info->data_len;
        new_packet->pkt_data = (UINT1 *)gn_malloc(new_packet->pkt_len);
        if(new_packet->pkt_data)
        {
            memcpy(new_packet->pkt_data, ether, new_packet->pkt_len);
        }
        new_packet->sw = sw;
        new_packet->time_smp = g_cur_sys_time.tv_sec;
        queue_in_packet(new_packet);
    }
    else if(ether->proto == htons(ETHER_ARP))
    {
        arp_t *arp = (arp_t *)packet_in_info->data;

        //发送缓存的首包并检查租户
        l3_forward_first_packet(sw, arp->sendip, arp->sendmac, packet_in_info->inport);
    }

    l3_install_flow_local(sw, gw_ip, packet_in_info->inport, ether);

    for(j = 0; j < g_server.max_switch; j++)
    {
        if(0 > sw->index)
        {
            return;
        }

        if (g_short_weight[j][sw->index] == 0)
        {
            continue;
        }

        l3_flowmod_chain(gw_ip, j, sw->index, packet_in_info->inport, ether);
    }
}
Exemple #3
0
static int answer_to_connection(void *cls, struct MHD_Connection *connection,
        const INT1 *url, const char *method, const INT1 *version,
        const INT1 *upload_data, size_t *upload_data_size, void **con_cls)
{
    INT4 ret = 0;
    UINT4 idx = 0;

    if (NULL == *con_cls)
    {
        struct connection_info *conn_info = (struct connection_info *)gn_malloc(sizeof(struct connection_info));
        if(NULL == conn_info)
        {
            return MHD_NO;
        }

        conn_info->connection_time = g_cur_sys_time.tv_sec;
        *con_cls = (void *)conn_info;
        return MHD_YES;
    }

    if (0 == strcmp(method, "GET"))
    {
        LOG_PROC("INFO", "Restful[%s]: [%s]", method, url);
        g_rest_reply = proc_rest_msg(method, url, upload_data);
        //todo ÅпÕ
        ret = send_page(connection, g_rest_reply, MHD_HTTP_OK);     //»Ø¸´ÏûÏ¢
        memset((char *) upload_data, 0x0, *upload_data_size);    //Çå¿Õ»º³åÇø
        *upload_data_size = 0;
        upload_data = NULL;

        free(g_rest_reply);
        return ret;
    }

    if (0 == strcmp(method, "POST") || 0 == strcmp(method, "DELETE")
            || 0 == strcmp(method, "PUT") || 0 == strcmp(method, "PATCH")
            || 0 == strcmp(method, "HEAD"))
    {
        struct connection_info *conn_info = *con_cls;
        conn_info->connection_type = HTTP_POST;

        if (*upload_data_size)
        {
            if(is_json(upload_data, *upload_data_size))
            {
                INT1 *tmp = (char*) upload_data;
                for (idx = *upload_data_size; idx > 0; --idx)
                {
                    if ('}' == tmp[idx])
                    {
                        tmp[idx + 1] = '\0';
                        break;
                    }
                }

                LOG_PROC("INFO", "Restful[%s]: [%s] %s\n", method, url, upload_data);
                g_rest_reply = proc_rest_msg(method, url, upload_data);

                memset((char *) upload_data, 0x0, *upload_data_size);    //Çå¿Õ»º³åÇø
                *upload_data_size = 0;
            }
            else
            {
                if(conn_info->connection_time - g_cur_sys_time.tv_sec > HTTP_TIMEOUT)
                {
                    memset((char *) upload_data, 0x0, *upload_data_size);    //Çå¿Õ»º³åÇø
                    *upload_data_size = 0;
                    return MHD_NO;
                }
            }

            return MHD_YES;
        }
        else
        {
            ret = send_page(connection, g_rest_reply, MHD_HTTP_OK);
            memset((char *) upload_data, 0x0, *upload_data_size);    //Çå¿Õ»º³åÇø
            *upload_data_size = 0;
            upload_data = NULL;

            gn_free((void **)&g_rest_reply);
            return ret;
        }
    }

    return ret;
}
Exemple #4
0
//read all the configuration from the ".ini" file
ini_file_t* read_ini(const char *path)
{
    char *pos = NULL;
    char buf[1024] = {0};
    ini_file_t *doc = NULL;
    ini_selection_t *sec = NULL;
    ini_item_t *item = NULL;
    ini_comment_t *comm_list = NULL;
    ini_comment_t *comm = NULL;

    FILE *fp = fopen(path, "r");
    if (NULL == fp)
    {
        return NULL;
    }

    doc = (ini_file_t*)gn_malloc(sizeof(ini_file_t));
    if (NULL == doc)
    {
        return NULL;
    }

    doc->fd = fp;
    while(fgets(buf, 1023, fp))
    {
        for(pos = buf; '\0' != *pos; pos++)
        {
           if(('\n' == *pos) || ('\r' == *pos))
           {
               *pos = '\0';
           }
        }

        switch(buf[0])
        {
            case '\n':
            {
                break;
            }
            case '\0':
            {
                break;
            }
            case '#':
            {
                comm = (ini_comment_t *)gn_malloc(sizeof(ini_comment_t));
                if(NULL == comm)
                {
                    goto EXCEPTION_EXIT;
                }

                comm->desc = strdup(buf);
                ADD_ELEMENT_TO_LIST(comm_list, ini_comment_t, comm);
                break;
            }
            case ';':
            {
                comm = (ini_comment_t *)gn_malloc(sizeof(ini_comment_t));
                if(NULL == comm)
                {
                    goto EXCEPTION_EXIT;
                }

                comm->desc = strdup(buf);
                ADD_ELEMENT_TO_LIST(comm_list, ini_comment_t, comm);
                break;
            }
            case '[':
            {
                sec = (ini_selection_t*)gn_malloc(sizeof(ini_selection_t));
                if(NULL == sec)
                {
                    goto EXCEPTION_EXIT;
                }

                sec->selection = strdup(buf);
                sec->comments = comm_list;
                comm_list = NULL;

                ADD_ELEMENT_TO_LIST(doc->selections, ini_selection_t, sec);
                break;
            }
            default:
            {
                if(NULL == sec)
                {
                    goto EXCEPTION_EXIT;
                }

                item = (ini_item_t *)gn_malloc(sizeof(ini_item_t));
                if(NULL == item)
                {
                    goto EXCEPTION_EXIT;
                }

                pos = buf;
                do
                {
                    if('\0' == *pos++)
                    {
                        goto EXCEPTION_EXIT;
                    }
                }while('=' != *pos);

                *pos = '\0';
                pos++;

                item->name = strdup(buf);
                item->value = strdup(pos);
                item->comments = comm_list;
                comm_list = NULL;

                ADD_ELEMENT_TO_LIST(sec->ini_items, ini_item_t, item);
                break;
            }
        }
    }

    return doc;

EXCEPTION_EXIT:
    close_ini(&doc);
    return NULL;
}
Exemple #5
0
// set the value of the configure item
int set_value(const ini_file_t *ini_file, const char *selection, const char *item, char* save_value)
{
    ini_selection_t *sec_tmp = NULL;
    ini_item_t *item_tmp = NULL;
    ini_selection_t *sec_new = NULL;
    ini_file_t *doc = (ini_file_t *)ini_file;
    ini_item_t *item_new = NULL;

    if((NULL == ini_file) || (NULL == selection) || (NULL == item))
    {
        return 0;
    }

    // judge the selection exists
    if (ini_file->selections) {
    	sec_tmp = ini_file->selections;
    	while (sec_tmp) {
    		if(0 == strcmp(selection, sec_tmp->selection))
			{
				break;
			}
			sec_tmp = sec_tmp->next;
    	}
    }

    if (NULL == sec_tmp) {
    	// add selections to list
    	sec_new = (ini_selection_t*)gn_malloc(sizeof(ini_selection_t));
		if(NULL == sec_new)
		{
			return 0;
		}

		sec_new->selection = strdup(selection);
		ADD_ELEMENT_TO_LIST(doc->selections, ini_selection_t, sec_new);
    }

    if(ini_file->selections)
    {
        sec_tmp = ini_file->selections;
        while(sec_tmp)
        {
            if(0 == strcmp(selection, sec_tmp->selection))
            {
                item_tmp = sec_tmp->ini_items;
                while(item_tmp)
                {
                    if(0 == strcmp(item, item_tmp->name))
                    {
                        strcpy(item_tmp->value, save_value);
                        return 1;
                    }

                    item_tmp = item_tmp->next;
                }

                // add item
                if (NULL == item_tmp) {

                	item_new = (ini_item_t *)gn_malloc(sizeof(ini_item_t));
					if(NULL == item_new)
					{
						return 0;
					}

					item_new->name = strdup(item);
					item_new->value = strdup(save_value);
					item_new->comments = NULL;

					ADD_ELEMENT_TO_LIST(sec_tmp->ini_items, ini_item_t, item_new);
                }
            }

            sec_tmp = sec_tmp->next;
        }
    }

    return 0;
}