UNIX BILLING SYSTEM-开源

  • p2_961788
    了解作者
  • 20.8KB
    文件大小
  • gz
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-01 09:54
    上传日期
UNIX计费系统,用于网络服务器,voip等
accountd_src_0.0.4.tar.gz
  • accountd_src_0.0.4
  • src
  • ac_exit.h
    856B
  • itype.c
    4.7KB
  • collect.h
    2KB
  • ac_exit.c
    2.1KB
  • client
  • libs.linux
    0B
  • libs.solaris
    15B
  • account.c
    3.4KB
  • Makefile
    406B
  • libs.FreeBSD
    0B
  • libs.linux
    10B
  • main.mk
    148B
  • libs.solaris
    24B
  • collect.c
    14.8KB
  • Makefile
    724B
  • libs.FreeBSD
    9B
  • sock.h
    494B
  • sock.c
    3KB
  • accountd.c
    7.1KB
  • itype.h
    119B
  • accountd.h
    1.4KB
  • build_pgsql.sh
    1004B
  • doc
  • russian
  • quickstart
    3.2KB
  • FAQ
    7.8KB
  • pg_hba.conf.sample
    959B
  • bug.template
    951B
  • english
  • bug.template
    1008B
  • COPYRIGHT
    1016B
  • build_mysql.sh
    1.1KB
  • Makefile
    1.5KB
内容介绍
#include <sys/types.h> #include <sys/stat.h> #include <sys/socket.h> #include <sys/uio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <signal.h> #include <string.h> #include <pthread.h> #ifdef USE_PGSQL #include <time.h> #endif #include <netinet/in.h> #include <arpa/inet.h rel='nofollow' onclick='return false;'> #include "accountd.h" #include "collect.h" #include "ac_exit.h" static int db_errors = 0; static pthread_mutex_t file_mute = PTHREAD_MUTEX_INITIALIZER; struct ac_rule *rules = NULL; /* accounting rules */ pthread_mutex_t rule_mute = PTHREAD_MUTEX_INITIALIZER; /* rules mutex */ long tlimit = 1048576; /* traffic limit in bytes */ pthread_attr_t det_attr; /* attributes for detached thread */ char db_name[32]; void recovery_errdb() { struct { in_addr_t ip; int port; long in; long out; } retry; struct ac_rule *ac; int db_fd; (void)pthread_mutex_lock(&file_mute); if ((db_fd = open(db_name, O_RDONLY, S_IRUSR | S_IWUSR)) == -1) { (void)pthread_mutex_unlock(&file_mute); return; } (void)pthread_mutex_lock(&rule_mute); while (read(db_fd, &retry, sizeof(retry)) == sizeof(retry)) for (ac = rules; ac; ac = ac->next) if (ac->rl.ip == retry.ip) { ac->rl.in += retry.in; ac->rl.out += retry.out; if (retry.port) { struct pts_data *pts; for (pts = ac->rl.pts; pts; pts = pts->next) if (pts->pd.p == retry.port) { pts->pd.in += retry.in; pts->pd.out += retry.out; } } } (void)pthread_mutex_unlock(&rule_mute); (void)close(db_fd); (void)unlink(db_name); (void)pthread_mutex_unlock(&file_mute); } /* * 普嗡蒙� 踪谫琢旁友 邢 由俏撂� 粤释乓� * 诹猩淤琢旁 耘苏萆� 牧挝倥 � 铝谡 牧挝偃, * 拊下� 哨屡至载 邢耘依 晌葡彝撩缮 幸� KILL`� || CRASH`� etc. * 紫佑粤瘟滋勺晾杂� 牧挝倥 哨 滔窍� ... */ void checkpoint(int const signum) { struct { struct sigaction sa_new; struct sigaction sa_old; } actions; struct ac_rule *ac; struct pts_data *pts; int ep = 0; (void)memset(&actions, 0, sizeof(actions)); /* 瘟 滓磐� 踪邢涛盼裳 下伊孪赞伤� 汕蜗疑艺磐 邢陶夼挝偈 由俏撂 */ actions.sa_new.sa_handler = SIG_IGN; (void)sigaction(signum, &(actions.sa_new), &(actions.sa_old)); /* 匈粤磐友 诹猩恿载 子� 愚旁奚松 � 铝谡 牧挝偃 */ (void)pthread_mutex_lock(&rule_mute); for (ac = rules; ac; ac = ac->next) if (ac->rl.in || ac->rl.out) { if (check_rule(&(ac->rl))) { fsend_rule(&(rules->rl)); ep = 1; } ac->rl.in = ac->rl.out = 0; for (pts = ac->rl.pts; pts; pts = pts->next) pts->pd.in = pts->pd.out = 0; } (void)pthread_mutex_unlock(&rule_mute); /* * 踪邢涛雅� 紫佑粤蜗滋盼膳 磷烈墒蜗窍 屏侍�, * 庞躺 诹猩迂 � 铝谡 牧挝偃 幸羡塘 沼信畚� */ if (!ep) recovery_errdb(); /* 紫谧伊萘磐 幸拍倌蛰墒 下伊孪赞伤 由俏撂� */ (void)sigaction(signum, &(actions.sa_old), NULL); } /* * 庞躺 闻紫谕现蜗 诹猩恿载 牧挝倥 � 砸疗粕伺 � 铝谡 牧挝偃, * 诹猩淤琢磐 � 屏侍, 牧挝倥 抡恼� 紫佑粤瘟滋勺猎赜� 幸� 釉烈耘 幸锨伊屯� � 1 * 伊� � 蘖� ... */ void fsend_rule(const struct t_data *ch) { int db_fd; const int none_port = 0; struct pts_data *pts; (void)pthread_mutex_lock(&file_mute); db_fd = open(db_name, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR); if (db_fd == -1) { warning("STORE ERROR: %d X %ld %ld\n", ch->ip, ch->in, ch->out); for (pts = ch->pts; pts; pts = pts->next) warning("STORE ERROR: %d %d %ld %ld\n", ch->ip, pts->pd.p, pts->pd.in, pts->pd.out); (void)pthread_mutex_unlock(&file_mute); return; } /* * 诹猩淤琢磐 牧挝倥 � 屏侍 � 葡彝猎� : * [ip addr][port][in][out] */ if (ch->in || ch->out) { (void)write(db_fd, &(ch->ip), sizeof(ch->ip)); /* send ip address */ (void)write(db_fd, &none_port, sizeof(none_port)); /* none port */ (void)write(db_fd, &(ch->in), sizeof(ch->in) * 2); /* in/out */ } for (pts = ch->pts; pts; pts = pts->next) if (pts->pd.in || pts->pd.out) { (void)write(db_fd, &(ch->ip), sizeof(ch->ip)); /* ip address */ (void)write(db_fd, &(pts->pd), sizeof(pts->pd)); /* port, in/out */ } (void)close(db_fd); (void)pthread_mutex_unlock(&file_mute); } int check_rule(const struct t_data *ch) { int ip_id; char query[256]; struct pts_data *pts; #ifdef USE_PGSQL PGresult *res; char *str_id; #endif #ifdef USE_MYSQL MYSQL_RES *res; MYSQL_ROW row; int ch_m = #else (void) #endif snprintf(query, sizeof(query), "SELECT ip_id FROM ip_addr_%s WHERE ip_addr = '%s' LIMIT 1", dev, inet_ntoa(*(struct in_addr *)&(ch->ip))); /* * 邢陶蘖磐 赡盼陨粕肆韵� ip 聊遗恿 哨 铝谫 牧挝偃, * 子� 粤绿擅� 幼掩僮晾杂� 邢 茉贤� 赡盼陨粕肆韵艺 */ (void)pthread_mutex_lock(&db_mute); #ifdef USE_PGSQL res = PQexec(db_con, query); if (PQresultStatus(res) != PGRES_TUPLES_OK) { warning("PostgreSQL %s (can't get ip ident for ip %s)\n", PQerrorMessage(db_con), inet_ntoa(*(struct in_addr *)&(ch->ip))); (void)pthread_mutex_unlock(&db_mute); PQclear(res); return (1); } (void)pthread_mutex_unlock(&db_mute); str_id = PQgetvalue(res, 0, 0); if (!str_id || (ip_id = (int)strtol(str_id, (char **)NULL, 10)) <= 0) { warning("ERROR: can't get ip ident for ip %s\n", inet_ntoa(*(struct in_addr *)&(ch->ip))); PQclear(res); return (1); } PQclear(res); #endif #ifdef USE_MYSQL if (mysql_real_query(&db_con, query, ch_m)) { mde: warning("MySQL ERROR %d: %s\n", mysql_errno(&db_con), mysql_error(&db_con)); (void)pthread_mutex_unlock(&db_mute); return (1); } if (!(res = mysql_store_result(&db_con))) { (void)pthread_mutex_unlock(&db_mute); mre: warning("ERROR: can't get ip ident for ip %s\n", inet_ntoa(*(struct in_addr *)&(ch->ip))); return (1); } (void)pthread_mutex_unlock(&db_mute); row = mysql_fetch_row(res); if (!row || !row[0] || (ip_id = (int)strtol(row[0], (char **)NULL, 10)) <= 0) { mysql_free_result(res); goto mre; } mysql_free_result(res); #endif /* * 赏叛 赡盼陨粕肆韵� ip 聊遗恿, 下蜗滋雅�/南铝滋雅� * 牧挝倥 � 优耘紫� 砸疗伤� � 铝谂/� 牧挝偃 */ #ifdef USE_MYSQL ch_m = snprintf(query, sizeof(query), "UPDATE etraf_%s SET bin = bin + %ld, bout = bout + %ld " "WHERE ip_id = %d AND day = CURDATE() AND hour = HOUR(CURTIME())", dev, ch->in, ch->out, ip_id); #endif #ifdef USE_PGSQL { struct timeval tv; struct tm *ptm; (void)gettimeofday(&tv, NULL); ptm = localtime(&(tv.tv_sec)); (void)snprintf(query, sizeof(query), "UPDATE etraf_%s SET bin = bin + %ld, bout = bout + %ld " "WHERE ip_id = %d AND day = date(now()) AND hour = %d", dev, ch->in, ch->out, ip_id, ptm->tm_hour); (void)pthread_mutex_lock(&db_mute); res = PQexec(db_con, query); if (PQresultStatus(res) != PGRES_COMMAND_OK) { pFe: PQclear(res); warning("PostgreSQL %s", PQerrorMessage(db_con)); (void)pthread_mutex_unlock(&db_mute); return (1); } if (!strcmp(PQcmdTuples(res), "0")) { PQclear(res); (void)snprintf(query, sizeof(query), "INSERT INTO etraf_%s VALUES " "(%d, date(now()), %d, %ld, %ld)", dev, ip_id, ptm->tm_hour, ch->in, ch->out); res = PQexec(db_con, query); if (PQresultStatus(res) != PGRES_COMMAND_OK) goto pFe; } PQclear(res); for (pts = ch->pts; pts; pts = pts->next) { if (!pts->pd.in && !pts->pd.out) continue; (void)snprintf(query, sizeof(query), "UPDATE etraf_pts_%s SET bin = bin + %ld, " "bout = bout + %ld WHERE ip_id = %d AND port = %d " "AND day = date(now()) AND hour = %d", dev, pts->pd.in, pts->pd.out, ip_id, pts->pd.p, ptm->tm_hour); res = PQexec(db_con, query); if (PQresultStatus(res) != PGRES_COMMAND_OK) { pgerT: PQclear(res); warning("can't update traffic on port %d for ip %s: " "PostgreSQL %s", pts->pd.p, inet_ntoa(*(struct in_addr *)&(ch->ip)), PQerrorMessage(db_con)); continue; } if (!strcmp(PQcmdTuples(res), "0")) { PQclear(res); (void)snprintf(query, sizeof(query), "INSERT INTO etraf_pts_%s VALUES " "(%d, date(now()), %d, %d, %ld, %ld)", dev, ip_id, ptm->tm_hour, pts->pd.p, pts->pd.
评论
    相关推荐
    • TCP迭代服务器程序(Unix网络编程)
      Unix网络编程例子, 最简单的网络程序例子。
    • Unix Echo服务器和客户端(C语言)
      有两个版本(C语言,内有完整的说明文档): 1.实现客户端发一句,服务器返回一个 2.客户端一次连接可以发送多句(每句就是一个package),服务器返回最初的5句(不足5句,就返回所有的)
    • DomainSocket:客户端-使用Unix域套接字的服务器通信
      服务器-使用UNIX域套接字的客户端通信 功能性 DomainSocket类提供Unix域套接字的抽象。 您可以创建服务器套接字和客户端套接字进行通信。 有一个Daemon类和Sender类(服务器和客户端)作为用法示例。 例子 您可以...
    • UNIX网络编程 第2版pdg
      论述了基本编程内容后,即转入高级套接口编程的相 关主题,包括IPv4与IPv6的互操作性、UNIX域协议、非阻塞I/O、路由套接口、广播、多播、线程、原始 套接口、数据链路访问等,对于客户—服务器程序的各种设计方法也...
    • putty【连接远程Unix服务器
      这个应该都知道吧,用于在Windows下连接远程Unix服务器,也可以自己用puttyGen自己生成证书。与你共享!
    • unix网络编程.rar
      socket编程相关,很适合学习网络编程开发、服务器开发 socket编程相关,很适合学习网络编程开发、服务器开发 socket编程相关,很适合学习网络编程开发、服务器开发
    • unixbench.7z
      unixbench 测试服务器CPU性能的工具,非常不错的!可以下载下来进行使用!很简单的! 可以下载
    • 主流服务器UNIX操作系统用户帐号的设置.
      主流服务器UNIX操作系统用户帐号的设置
    • unix-connect-开源
      Unix Connect可以以多种方式使用。 开发它是为了能够将Unix系统用作Z-NETZ中的服务器。 网关功能是一个副作用。 网关例程尤其是相对可移植的