reload.cpp
3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <lib_include.h>
#include <lib_file_ini.h>
enum E_RELOAD_CMD{
e_reload_cmd_min = 1,
e_reload_cmd_notice = 1,//发公告
e_reload_cmd_mail = 2,//发送邮件
e_reload_cmd_offline = 3,//T人下线
e_reload_cmd_max,
};
static struct sockaddr_in mcast_addr;
static int mcast_fd;
static const char *opt_string = "c:hv:";
static int connect_to_broadcaster(const char* mcast_ip, int mcast_port, const char* outgoing_ip, const char* outgoing_interface)
{
mcast_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (mcast_fd == -1) {
printf("Failed to Create `mcast_fd`: err=%d %s\n", errno, strerror(errno));
return -1;
}
memset(&mcast_addr, 0, sizeof(mcast_addr));
mcast_addr.sin_family = AF_INET;
inet_pton(AF_INET, mcast_ip, &(mcast_addr.sin_addr));
mcast_addr.sin_port = htons(mcast_port);
int on = 1;
setsockopt(mcast_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
in_addr_t ipaddr;
inet_pton(AF_INET, outgoing_ip, &ipaddr);
if (setsockopt(mcast_fd, IPPROTO_IP, IP_MULTICAST_IF, &ipaddr, sizeof ipaddr) == -1) {
printf("Failed to Set Outgoing Interface: err=%d %s %s\n",
errno, strerror(errno), outgoing_ip);
return -1;
}
if (bind(mcast_fd, (struct sockaddr*)&mcast_addr, sizeof mcast_addr) == -1) {
printf("Failed to Bind `mcast_fd`: err=%d %s\n", errno, strerror(errno));
return -1;
}
struct group_req req;
req.gr_interface = if_nametoindex(outgoing_interface);
memcpy(&req.gr_group, &mcast_addr, sizeof mcast_addr);
if (setsockopt(mcast_fd, IPPROTO_IP, MCAST_JOIN_GROUP, &req, sizeof req) == -1) {
printf("Failed to Join Mcast Grp: err=%d %s\n", errno, strerror(errno));
return -1;
}
return 0;
}
int send_to_broadcaster(std::string cmd, std::string val)
{
std::string send_data;
send_data += cmd;
if (!val.empty()){
send_data +="," + val;
}
return sendto(mcast_fd, send_data.c_str(), (int)send_data.size(), 0, (sockaddr*)&mcast_addr, sizeof(mcast_addr));
}
void display_usage()
{
printf("-c define the command\n");
printf(" 1, e_reload_cmd_notice = 1,发公告 \n");
printf(" 2, e_reload_cmd_mail = 2,//发送邮件 \n");
printf(" 3, e_reload_cmd_offline = 3,//T人下线 \n");
printf("-v command's value\n");
printf(" 1,xxxxxx\n");
printf(" 2,server_id[0:all],mail_id\n");
printf(" 3,server_id, uid\n");
printf("-h display this message\n");
}
int main(int argc, char* argv[])
{
el::lib_file_ini_t ini;
ini.load("./reload.ini");
std::string mcast_ip;
int mcast_port = 0;
std::string outgoing_ip;
std::string outgoing_interface;
if (SUCC != ini.get_val(mcast_ip, "mcast", "ip")){
printf("error[]");
return 0;
}
if (SUCC != ini.get_val(mcast_port, "mcast", "port")){
printf("error[]");
return 0;
}
if (SUCC != ini.get_val(outgoing_ip, "mcast", "outgoing_ip")){
printf("error[]");
return 0;
}
if (SUCC != ini.get_val(outgoing_interface, "mcast", "outgoing_interface")){
printf("error[]");
return 0;
}
//int i = 0;
std::string val;
std::string cmd;
int oc ;
while( (oc = getopt(argc, argv, opt_string)) != -1) {
switch( oc ) {
case 'c':{
cmd = optarg;
printf("cmd:%s\n",cmd.c_str());
}
break;
case 'v':{
val = optarg;
printf("value:%s\n",val.c_str());
}
break;
case 'h':
display_usage();
break;
default:
break;
}
}
uint32_t cmd_id = atoi(cmd.c_str());
if (cmd_id < e_reload_cmd_min || e_reload_cmd_max <= cmd_id){
printf("no reload cmd:%u", cmd_id);
return 0;
}
int ret = connect_to_broadcaster(mcast_ip.c_str(), mcast_port, outgoing_ip.c_str(), outgoing_interface.c_str());
if (0 != ret){
return 0;
}
ret = send_to_broadcaster(cmd, val);
printf("send ret:%d\n",ret);
return ret;
}