Changeset cf90d9f4e60f515ae835451b841ce4df92ca1a34
- Timestamp:
- 11/03/08 02:43:11 (4 years ago)
- Children:
- f51abe393259f4b3a94cea5d277c2dd5865696ee
- Parents:
- b1bb912de56607e397dd65f43cce29f66f79a172
- git-author:
- Neutron Soutmun <neo.neutron@…> (09/12/08 18:56:30)
- git-committer:
- Neutron Soutmun <neo.neutron@…> (11/03/08 02:43:11)
- Files:
-
- 9 added
- 10 modified
-
AUTHORS (modified) (1 diff)
-
ChangeLog (modified) (1 diff)
-
src/Makefile.am (modified) (1 diff)
-
src/rahunasd.c (modified) (10 diffs)
-
src/rahunasd.h (modified) (4 diffs)
-
src/rh-config.h (added)
-
src/rh-ipset.c (modified) (3 diffs)
-
src/rh-ipset.h (modified) (2 diffs)
-
src/rh-task-dbset.c (added)
-
src/rh-task-dbset.h (added)
-
src/rh-task-ipset.c (added)
-
src/rh-task-ipset.h (added)
-
src/rh-task-memset.c (added)
-
src/rh-task-memset.h (added)
-
src/rh-task.c (added)
-
src/rh-task.h (added)
-
src/rh-utils.c (modified) (1 diff)
-
src/rh-utils.h (modified) (1 diff)
-
src/rh-xmlrpc-server.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
AUTHORS
r4d5414b rcf90d9f 1 1 Neutron Soutmun <neo.neutron@gmail.com> 2 Suriya Soutmun <darksolar@gmail.com> -
ChangeLog
rb1bb912 rcf90d9f 1 2008-09-12 Neutron Soutmun <neo.neutron@gmail.com> 2 3 * +src/rh-task-*.{h,c}: 4 - Add the tasks concept of the workers. eg. memset, ipset, 5 dbset and etc. 6 - Each tasks provide the function to handle each events, 7 eg. init, start and stop service, start and stop session. 8 * src/rahunas.{h,c}, src/rh-ipset.{h,c}, src/rh-utils.{h,c}, 9 src/rh-xmlrpc-server.c: 10 Change respectively to new tasks concept and do code beautifier. 11 * +src/rh-config.h: Move the configuration definitions from rahunasd.h. 12 * src/Makefile.am: Change respectively to properly build the tasks concept. 13 * AUTHORS: Add Suriya Soutmun to the author team. 14 1 15 2008-09-03 Neutron Soutmun <neo.neutron@gmail.com> 2 16 -
src/Makefile.am
r2d4b30a rcf90d9f 10 10 rh-xmlrpc-cmd.c \ 11 11 rh-ipset.c \ 12 rh-utils.c 12 rh-utils.c \ 13 rh-task.c \ 14 rh-task-ipset.c \ 15 rh-task-memset.c 13 16 14 17 rahunasd_LDADD = \ -
src/rahunasd.c
rb1bb912 rcf90d9f 15 15 #include <errno.h> 16 16 #include <unistd.h> 17 #include <time.h>18 17 #include <signal.h> 19 18 #include <syslog.h> … … 24 23 #include "rh-ipset.h" 25 24 #include "rh-utils.h" 25 #include "rh-task.h" 26 26 27 27 /* Abstract functions */ 28 28 int logmsg(int priority, const char *msg, ...); 29 29 int getline(int fd, char *buf, size_t size); 30 int finish(); 31 int ipset_flush(); 32 33 struct rahunas_map* rh_init_map(); 34 int rh_init_members(struct rahunas_map *map); 35 36 int walk_through_set (int (*callback)(void *)); 30 37 31 size_t expired_check(void *data); 38 size_t nas_reboot(void *data);39 32 40 33 /* Declaration */ … … 163 156 case SIGKILL: 164 157 if (pid == 0) { 165 walk_through_set(&nas_reboot);166 158 rh_exit(); 167 159 exit(EXIT_SUCCESS); … … 174 166 break; 175 167 } 176 }177 178 int ipset_flush()179 {180 syslog(LOG_NOTICE, "Flushing IPSET...");181 set_flush(SET_NAME);182 }183 184 int finish()185 {186 char *exitmsg = "Exit Gracefully";187 struct rahunas_member *members = NULL;188 int i;189 int end;190 191 if (map) {192 if (map->members) {193 members = map->members;194 end = map->size;195 } else {196 end = 0;197 }198 199 for (i=0; i < end; i++) {200 rh_free_member(&members[i]);201 }202 203 rh_free(&(map->members));204 rh_free(&map);205 }206 207 rh_free(&rahunas_set);208 syslog(LOG_INFO, exitmsg);209 return 0;210 168 } 211 169 … … 233 191 } 234 192 235 struct rahunas_map* rh_init_map() {236 struct rahunas_map *map = NULL;237 238 map = (struct rahunas_map*)(rh_malloc(sizeof(struct rahunas_map)));239 240 map->members = NULL;241 map->size = 0;242 243 return map;244 }245 246 int rh_init_members (struct rahunas_map* map)247 {248 struct rahunas_member *members = NULL;249 int size;250 251 if (!map)252 return (-1);253 254 size = map->size == 0 ? MAX_MEMBERS : map->size;255 256 members =257 (struct rahunas_member*)(rh_malloc(sizeof(struct rahunas_member)*size));258 259 memset(members, 0, sizeof(struct rahunas_member)*size);260 261 map->members = members;262 263 return 0;264 }265 266 193 gboolean polling(gpointer data) { 267 194 struct rahunas_map *map = (struct rahunas_map *)data; … … 278 205 struct ip_set_rahunas *table = NULL; 279 206 struct rahunas_member *members = map->members; 207 struct task_req req; 280 208 unsigned int i; 281 209 char *ip = NULL; 282 ip_set_ip_t current_ip;283 210 int res = 0; 284 211 … … 289 216 290 217 for (i = 0; i < map->size; i++) { 291 if (test_bit(IPSET_RAHUNAS_ISSET, 292 (void *)&table[i].flags)) { 293 if ((time(NULL) - table[i].timestamp) > IDLE_THRESHOLD) { 218 if (test_bit(IPSET_RAHUNAS_ISSET, (void *)&table[i].flags) && 219 (time(NULL) - table[i].timestamp) > IDLE_THRESHOLD) { 294 220 DP("Found IP: %s expired", idtoip(map, i)); 295 current_ip = ntohl(map->first_ip) + i; 296 res = set_adtip_nb(rahunas_set, ¤t_ip, &table[i].ethernet, 297 IP_SET_OP_DEL_IP); 298 DP("set_adtip_nb() res=%d errno=%d", res, errno); 299 300 if (res == 0) { 301 send_xmlrpc_stopacct(map, i, RH_RADIUS_TERM_IDLE_TIMEOUT); 302 303 if (!members[i].username) 304 members[i].username = termstring; 305 306 if (!members[i].session_id) 307 members[i].session_id = termstring; 308 309 logmsg(RH_LOG_NORMAL, "Session Idle-Timeout, User: %s, IP: %s, " 310 "Session ID: %s, MAC: %s", 311 members[i].username, 312 idtoip(map, i), 313 members[i].session_id, 314 mac_tostring(members[i].mac_address)); 315 316 rh_free_member(&members[i]); 317 } 318 } 221 req.id = i; 222 memcpy(req.mac_address, &table[i].ethernet, ETH_ALEN); 223 req.req_opt = RH_RADIUS_TERM_IDLE_TIMEOUT; 224 send_xmlrpc_stopacct(map, i, RH_RADIUS_TERM_IDLE_TIMEOUT); 225 res = rh_task_stopsess(map, &req); 319 226 } 320 227 } 321 228 } 322 229 323 size_t nas_reboot(void *data)324 {325 struct ip_set_list *setlist = (struct ip_set_list *) data;326 struct set *set = set_list[setlist->index];327 size_t offset;328 struct ip_set_rahunas *table = NULL;329 struct rahunas_member *members = map->members;330 unsigned int i;331 char *ip = NULL;332 ip_set_ip_t current_ip;333 int res = 0;334 335 offset = sizeof(struct ip_set_list) + setlist->header_size;336 table = (struct ip_set_rahunas *)(data + offset);337 338 DP("Map size %d", map->size);339 340 for (i = 0; i < map->size; i++) {341 if (test_bit(IPSET_RAHUNAS_ISSET, (void *)&table[i].flags)) {342 DP("Found IP: %s in set, try logout", idtoip(map, i));343 current_ip = ntohl(map->first_ip) + i;344 res = set_adtip_nb(rahunas_set, ¤t_ip, &table[i].ethernet,345 IP_SET_OP_DEL_IP);346 DP("set_adtip_nb() res=%d errno=%d", res, errno);347 348 if (res == 0) {349 send_xmlrpc_stopacct(map, i, RH_RADIUS_TERM_NAS_REBOOT);350 351 if (!members[i].username)352 members[i].username = termstring;353 354 if (!members[i].session_id)355 members[i].session_id = termstring;356 357 logmsg(RH_LOG_NORMAL, "Session Stop (NAS Reboot), User: %s, IP: %s, "358 "Session ID: %s, MAC: %s",359 members[i].username,360 idtoip(map, i),361 members[i].session_id,362 mac_tostring(members[i].mac_address));363 364 rh_free_member(&members[i]);365 }366 }367 }368 }369 370 int get_header_from_set ()371 {372 struct ip_set_req_rahunas_create *header = NULL;373 void *data = NULL;374 ip_set_id_t idx;375 socklen_t size, req_size;376 size_t offset;377 int res = 0;378 in_addr_t first_ip;379 in_addr_t last_ip;380 381 size = req_size = load_set_list(SET_NAME, &idx,382 IP_SET_OP_LIST_SIZE, CMD_LIST);383 384 DP("Get Set Size: %d", size);385 386 if (size) {387 data = rh_malloc(size);388 ((struct ip_set_req_list *) data)->op = IP_SET_OP_LIST;389 ((struct ip_set_req_list *) data)->index = idx;390 res = kernel_getfrom_handleerrno(data, &size);391 DP("get_lists getsockopt() res=%d errno=%d", res, errno);392 393 if (res != 0 || size != req_size) {394 free(data);395 return -EAGAIN;396 }397 size = 0;398 }399 400 offset = sizeof(struct ip_set_list);401 header = (struct ip_set_req_rahunas_create *) (data + offset);402 403 first_ip = htonl(header->from);404 last_ip = htonl(header->to);405 406 memcpy(&map->first_ip, &first_ip, sizeof(in_addr_t));407 memcpy(&map->last_ip, &last_ip, sizeof(in_addr_t));408 map->size = ntohl(map->last_ip) - ntohl(map->first_ip) + 1;409 410 logmsg(RH_LOG_NORMAL, "First IP: %s", ip_tostring(ntohl(map->first_ip)));411 logmsg(RH_LOG_NORMAL, "Last IP: %s", ip_tostring(ntohl(map->last_ip)));412 logmsg(RH_LOG_NORMAL, "Set Size: %lu", map->size);413 414 rh_free(&data);415 return res;416 }417 418 int walk_through_set (int (*callback)(void *))419 {420 void *data = NULL;421 ip_set_id_t idx;422 socklen_t size, req_size;423 int res = 0;424 425 size = req_size = load_set_list(SET_NAME, &idx,426 IP_SET_OP_LIST_SIZE, CMD_LIST);427 428 DP("Get Set Size: %d", size);429 430 if (size) {431 data = rh_malloc(size);432 ((struct ip_set_req_list *) data)->op = IP_SET_OP_LIST;433 ((struct ip_set_req_list *) data)->index = idx;434 res = kernel_getfrom_handleerrno(data, &size);435 DP("get_lists getsockopt() res=%d errno=%d", res, errno);436 437 if (res != 0 || size != req_size) {438 free(data);439 return -EAGAIN;440 }441 size = 0;442 }443 444 if (data != NULL)445 (*callback)(data);446 447 rh_free(&data);448 return res;449 }450 451 230 void rh_exit() 452 231 { 453 232 syslog(LOG_ALERT, "Child Exiting .."); 454 ipset_flush();455 finish();233 rh_task_stopservice(map); 234 rh_task_cleanup(); 456 235 rh_closelog(DEFAULT_LOG); 457 236 } … … 577 356 dup2(fd_log, STDERR_FILENO); 578 357 358 sprintf(version, "Starting %s - Version %s", PROGRAM, VERSION); 359 logmsg(RH_LOG_NORMAL, version); 360 syslog(LOG_INFO, version); 361 362 rh_task_init(); 363 579 364 gnet_init(); 580 365 main_loop = g_main_loop_new (NULL, FALSE); 581 366 582 sprintf(version, "Starting %s - Version %s", PROGRAM, VERSION);583 584 585 logmsg(RH_LOG_NORMAL, version);586 syslog(LOG_INFO, version);587 588 rahunas_set = set_adt_get(SET_NAME);589 DP("getsetname: %s", rahunas_set->name);590 DP("getsetid: %d", rahunas_set->id);591 DP("getsetindex: %d", rahunas_set->index);592 593 map = rh_init_map();594 get_header_from_set();595 rh_init_members(map);596 597 367 /* XML RPC Server */ 598 368 server = gnet_xmlrpc_server_new (addr, port); … … 600 370 if (!server) { 601 371 syslog(LOG_ERR, "Could not start XML-RPC server!"); 602 ipset_flush(); 603 finish(); 372 rh_task_stopservice(map); 604 373 exit (EXIT_FAILURE); 605 374 } … … 622 391 g_timeout_add_seconds (POLLING, polling, map); 623 392 393 rh_task_startservice(map); 394 624 395 g_main_loop_run(main_loop); 625 396 -
src/rahunasd.h
rb1bb912 rcf90d9f 8 8 #define __RAHUNASD_H 9 9 10 10 11 #include <stdio.h> 11 12 #include <sys/socket.h> … … 14 15 #include <time.h> 15 16 #include <linux/if_ether.h> 17 #include <errno.h> 18 19 #include "rh-config.h" 20 #include "rh-utils.h" 16 21 17 22 #define PROGRAM "RahuNASd" … … 19 24 #define MAX_MEMBERS 0x00FFFF 20 25 21 /* Configuration */ 22 #define DEFAULT_LOG "/var/log/rahunas/rahunas.log" 23 #define DEFAULT_PID "/var/run/rahunasd.pid" 24 25 #ifdef RH_DEBUG 26 # define IDLE_THRESHOLD 30 27 # define POLLING 15 28 #else 29 # define IDLE_THRESHOLD 600 30 # define POLLING 60 31 #endif 32 33 #define SET_NAME "rahunas_set" 34 #define XMLSERVICE_HOST "localhost" 35 #define XMLSERVICE_PORT 8888 36 #define XMLSERVICE_URL "/xmlrpc_service.php" 26 extern struct rahunas_map *map; 27 extern struct set *rahunas_set; 28 extern const char *termstring; 37 29 38 30 enum RH_LOG { … … 75 67 void rh_free_member (struct rahunas_member *member); 76 68 77 static c har *timemsg()69 static const char *timemsg() 78 70 { 79 71 static char tmsg[32] = ""; -
src/rh-ipset.c
r1268629 rcf90d9f 12 12 #include "rh-ipset.h" 13 13 #include "rh-utils.h" 14 15 extern struct set **set_list;16 extern ip_set_id_t max_sets;17 14 18 15 int kernel_getsocket(void) … … 256 253 } 257 254 258 259 255 void set_flush(const char *name) 260 256 { … … 350 346 return size; 351 347 } 348 349 int get_header_from_set (struct rahunas_map *map) 350 { 351 struct ip_set_req_rahunas_create *header = NULL; 352 void *data = NULL; 353 ip_set_id_t idx; 354 socklen_t size, req_size; 355 size_t offset; 356 int res = 0; 357 in_addr_t first_ip; 358 in_addr_t last_ip; 359 360 size = req_size = load_set_list(SET_NAME, &idx, 361 IP_SET_OP_LIST_SIZE, CMD_LIST); 362 363 DP("Get Set Size: %d", size); 364 365 if (size) { 366 data = rh_malloc(size); 367 ((struct ip_set_req_list *) data)->op = IP_SET_OP_LIST; 368 ((struct ip_set_req_list *) data)->index = idx; 369 res = kernel_getfrom_handleerrno(data, &size); 370 DP("get_lists getsockopt() res=%d errno=%d", res, errno); 371 372 if (res != 0 || size != req_size) { 373 free(data); 374 return -EAGAIN; 375 } 376 size = 0; 377 } 378 379 offset = sizeof(struct ip_set_list); 380 header = (struct ip_set_req_rahunas_create *) (data + offset); 381 382 first_ip = htonl(header->from); 383 last_ip = htonl(header->to); 384 385 memcpy(&map->first_ip, &first_ip, sizeof(in_addr_t)); 386 memcpy(&map->last_ip, &last_ip, sizeof(in_addr_t)); 387 map->size = ntohl(map->last_ip) - ntohl(map->first_ip) + 1; 388 389 logmsg(RH_LOG_NORMAL, "First IP: %s", ip_tostring(ntohl(map->first_ip))); 390 logmsg(RH_LOG_NORMAL, "Last IP: %s", ip_tostring(ntohl(map->last_ip))); 391 logmsg(RH_LOG_NORMAL, "Set Size: %lu", map->size); 392 393 rh_free(&data); 394 return res; 395 } 396 397 int walk_through_set (int (*callback)(void *)) 398 { 399 void *data = NULL; 400 ip_set_id_t idx; 401 socklen_t size, req_size; 402 int res = 0; 403 404 size = req_size = load_set_list(SET_NAME, &idx, 405 IP_SET_OP_LIST_SIZE, CMD_LIST); 406 407 DP("Get Set Size: %d", size); 408 409 if (size) { 410 data = rh_malloc(size); 411 ((struct ip_set_req_list *) data)->op = IP_SET_OP_LIST; 412 ((struct ip_set_req_list *) data)->index = idx; 413 res = kernel_getfrom_handleerrno(data, &size); 414 DP("get_lists getsockopt() res=%d errno=%d", res, errno); 415 416 if (res != 0 || size != req_size) { 417 free(data); 418 return -EAGAIN; 419 } 420 size = 0; 421 } 422 423 if (data != NULL) 424 (*callback)(data); 425 426 rh_free(&data); 427 return res; 428 } -
src/rh-ipset.h
r6f9f304 rcf90d9f 15 15 #define GETSOCK_TRIES 5 16 16 #define LIST_TRIES 5 17 18 extern struct set **set_list; 19 extern ip_set_id_t max_sets; 17 20 18 21 /* The view of an ipset in userspace */ … … 82 85 unsigned op, unsigned cmd); 83 86 87 int get_header_from_set (struct rahunas_map *map); 88 89 int walk_through_set (int (*callback)(void *)); 90 84 91 void parse_ip(const char *str, ip_set_ip_t *ip); 85 92 void parse_mac(const char *mac, unsigned char *ethernet); -
src/rh-utils.c
r1268629 rcf90d9f 7 7 #include <syslog.h> 8 8 #include <glib.h> 9 #include <time.h> 9 10 10 11 #include "rh-utils.h" -
src/rh-utils.h
r9c2bbec rcf90d9f 14 14 const char *rh_string_get_sep(const char *haystack, const char *sep, 15 15 unsigned short idx); 16 17 16 #endif // __RH_UTILS_H -
src/rh-xmlrpc-server.c
r5193352 rcf90d9f 2 2 * RahuNAS XML-RPC Server implementation 3 3 * Author: Neutron Soutmun <neo.neutron@gmail.com> 4 * Suriya Soutmun <darksolar@gmail.com> 4 5 * Date: 2008-08-07 5 6 */ … … 9 10 #include "rahunasd.h" 10 11 #include "rh-xmlrpc-server.h" 12 #include "rh-radius.h" 11 13 #include "rh-ipset.h" 12 14 #include "rh-utils.h" 13 14 extern struct set *rahunas_set; 15 #include "rh-task.h" 16 15 17 extern const char* termstring; 16 18 … … 24 26 struct rahunas_member *members = NULL; 25 27 unsigned char ethernet[ETH_ALEN] = {0,0,0,0,0,0}; 28 struct task_req req; 26 29 gchar *ip = NULL; 27 30 gchar *username = NULL; … … 29 32 gchar *mac_address = NULL; 30 33 uint32_t id; 31 int res = 0;32 34 33 35 if (!map) … … 58 60 } 59 61 60 res = set_adtip(rahunas_set, ip, mac_address, IP_SET_OP_ADD_IP); 61 if (res == 0) { 62 members[id].flags = 1; 63 if (members[id].username && members[id].username != termstring) 64 free(members[id].username); 65 66 if (members[id].session_id && members[id].username != termstring) 67 free(members[id].session_id); 68 69 members[id].username = strdup(username); 70 if (!members[id].username) 71 members[id].username = termstring; 72 73 members[id].session_id = strdup(session_id); 74 if (!members[id].session_id) 75 members[id].session_id = termstring; 76 77 time(&(members[id].session_start)); 78 79 parse_mac(mac_address, ðernet); 80 memcpy(&members[id].mac_address, ðernet, ETH_ALEN); 81 82 logmsg(RH_LOG_NORMAL, "Session Start, User: %s, IP: %s, " 83 "Session ID: %s, MAC: %s", 84 members[id].username, 85 idtoip(map, id), 86 members[id].session_id, 87 mac_tostring(members[id].mac_address)); 88 89 } else if (res == RH_IS_IN_SET) { 90 members[id].flags = 1; 91 } 62 req.id = id; 63 req.username = username; 64 req.session_id = session_id; 65 parse_mac(mac_address, ðernet); 66 memcpy(req.mac_address, ðernet, ETH_ALEN); 67 68 rh_task_startsess(map, &req); 92 69 93 70 *reply_string = g_strdup_printf("Greeting! Got: IP %s, User %s, ID %s", … … 116 93 struct rahunas_map *map = (struct rahunas_map *)user_data; 117 94 struct rahunas_member *members; 95 struct task_req req; 118 96 gchar *ip = NULL; 119 97 gchar *mac_address = NULL; … … 148 126 } 149 127 128 req.id = id; 129 parse_mac(mac_address, ðernet); 130 memcpy(req.mac_address, ðernet, ETH_ALEN); 131 req.req_opt = RH_RADIUS_TERM_USER_REQUEST; 132 150 133 if (members[id].flags) { 151 parse_mac(mac_address, ðernet);152 134 if (memcmp(ðernet, &members[id].mac_address, ETH_ALEN) == 0) { 153 res = set_adtip(rahunas_set, idtoip(map, id), mac_address, 154 IP_SET_OP_DEL_IP); 135 res = rh_task_stopsess(map, &req); 155 136 if (res == 0) { 156 if (!members[id].username)157 members[id].username = termstring;158 159 if (!members[id].session_id)160 members[id].session_id = termstring;161 162 logmsg(RH_LOG_NORMAL, "Session Stop, User: %s, IP: %s, "163 "Session ID: %s, MAC: %s",164 members[id].username,165 idtoip(map, id),166 members[id].session_id,167 mac_tostring(members[id].mac_address));168 169 rh_free_member(&members[id]);170 171 137 *reply_string = g_strdup_printf("Client IP %s was removed!", 172 138 idtoip(map, id));
