ADD aaa-12 增加配置管理—本地Portal服务器配置

RCA:
SOL:
修改人:chenling
检视人:
This commit is contained in:
ChenLing 2019-07-02 15:04:25 +08:00
parent d1527f709b
commit 0ee53aa325
2 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,42 @@
#ifndef LOCALPORTAL_H_
#define LOCALPORTAL_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include "../../../../common/rpc/rpc_common.h"
/*配置消息 */
typedef struct {
uint32_t ip;
int port;
}localportal_configure_t;
/*全局变量初始化 失败为1 成功为0*/
int Init(localportal_configure_t *localportal);
/*检查IP地址是否有效端口号是否被占用 */
int _valid_ipv4_port(const char *str, int port);
/*判断配置本地Portal服务器的IP地址是否有效端口号是否被占用 */
ret_code portalserver_config_chk(uint source, uint config_type,
pointer input, int input_len,
pointer output, int *output_len);
/*系统管理模块将数据内容IP地址、端口号发送给web server */
int portalserver_config_proc(uint source, uint config_type,
pointer input, int input_len,
pointer output, int *output_len);
#endif

View File

@ -0,0 +1,133 @@
#include "../../../../common/rpc/rpc.h"
#include "../include/parsefile.h"
#include "../include/configm.h"
#include "../../../netlink_uapi/libnetlinku.h"
#include "../include/localportal.h"
#include <cjson/cJSON.h>
#include "../../../../../Common/s2j/s2j.h"
#include "../../../../../Common/commuapinl.h"
/*全局变量,存放本地Portal服务器的IP地址和端口号 */
localportal_configure_t *localportal;
/*全局变量初始化 失败为1 成功为0*/
int Init(localportal_configure_t *localportal)
{
localportal = (localportal_configure_t *)malloc(sizeof * localportal);
if (NULL == localportal)
{
return 1;
}
return 0;
}
/*检查IP地址是否有效端口号是否被占用 */
int _valid_ipv4_port(const char *str, int port)
{
int ret;
int fd;
int i;
volatile int local_errno;
struct sockaddr_in addr;
fd = socket(AF_INET,SOCK_STREAM,0); //初始化socket
if(fd ==-1) //检查是否正常初始化socket
{
return -1;
}
errno = 0;
local_errno = errno;
ret = inet_pton(AF_INET, str ,&addr.sin_addr);
printf("the value of ret is:%d\n",ret);
if(ret > 0)
{
fprintf(stderr, "\"%s\" is a vaild IPv4 address\n", str);
addr.sin_family = AF_INET; //地址结构的协议簇
addr.sin_port=htons(port); //地址结构的端口地址,网络字节序
printf("the value of str:%s\n", str);
i = (bind(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr)));
printf("the value of i:%d\n", i);
if( i < 0)
{
printf("port %d has been used. \n", port);
close(fd);
return -1;
}
printf("port %d is ok. \n", port);
close(fd);
return 0;
}
else if (ret < 0)
{
fprintf(stderr, "EAFNOSUPPORT: %s\n", strerror(local_errno));
return -1;
}
else
{
fprintf(stderr, "\"%s\" is not a vaild IPv4 address\n", str);
return -1;
}
}
/*判断配置本地Portal服务器的IP地址是否有效端口号是否被占用 */
ret_code portalserver_config_chk(uint source, uint config_type,
pointer input, int input_len,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
localportal_configure_t *struct_portal;
struct_portal = (localportal_configure_t *)input;
if(input_len < sizeof(localportal_configure_t) )
{
ret = RET_INPUTERR;
}
char str[32];
inet_ntop(AF_INET, (void *)&struct_portal->ip, str, 32);
char *ip_addr = str;
if( (_valid_ipv4_port(ip_addr, struct_portal->port)) < 0 )
{
ret = RET_ERR;
}
ASSERT_RET(ret);
return RET_OK;
}
/*系统管理模块将数据内容IP地址、端口号发送给web server */
int portalserver_config_proc(uint source, uint config_type,
pointer input, int input_len,
pointer output, int *output_len)
{
ret_code ret = RET_OK;
int code;
localportal_configure_t *struct_portal;
struct_portal = (localportal_configure_t *)input;
char str[32];
inet_ntop(AF_INET, (void *)&struct_portal->ip, str, 32);
char *ip_addr = str;
rpc_log_info("portalserver configure: ip: %s port: %d\n",
struct_portal->ip, struct_portal->port);
/*将配置信息发送到web server */
/*把本地Portal server的配置信息存入全局变量 */
localportal = struct_portal;
return 0;
}