PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/netmonitor-0.4b/src/net_lib.c

#
C | 261 lines | 220 code | 29 blank | 12 comment | 89 complexity | 0f5153677cbd36780787d3f92b9f239e MD5 | raw file
Possible License(s): GPL-2.0
  1. /***********************************************************************
  2. *net_lib.c : Implements the function definitions of net_lib.h, which
  3. * are for use with netmon.c
  4. *Author: Dwayne Hoy
  5. *
  6. *Date: Friday, September 09 2005
  7. *
  8. *Version: 0.5
  9. *
  10. *Modified: Sunday, October 09 2005
  11. **********************************************************************/
  12. #include "net_lib.h"
  13. void display(net_t *Ndata, int size ,double time){
  14. int i;
  15. long double tx; //Stores the tx total
  16. long double rx; //Stores the rx total
  17. long double rx_trans; //Stores the rx rate
  18. long double tx_trans; //Stores the tx rate
  19. char tx_unit[4]; //Unit to display tx in
  20. char rx_unit[4]; //Unit to display rx in
  21. char rx_trans_unit[4]; //Unit to display rx rate in
  22. char tx_trans_unit[4]; //Unit to display tx rate in
  23. if(!DEBUG)
  24. clearscreen();
  25. /***********Prints title header*********/
  26. fprintf(stdout, "%s %16s %10s %14s %10s\n", "Device", "Downloaded",
  27. "Rate" ,"Uploaded", "Rate");
  28. if(!DIS_COMPACT) /*Seperator lines*/
  29. fprintf(stdout, "\n");
  30. for(i = 0; i < size; i++){
  31. tx = to_unit(tx_unit, get_txbtotal(&Ndata[i]),
  32. get_txgtotal(&Ndata[i]), 1);
  33. rx = to_unit(rx_unit, get_rxbtotal(&Ndata[i]),
  34. get_rxgtotal(&Ndata[i]), 1);
  35. tx_trans = get_txRate(&Ndata[i], time);
  36. tx_trans = to_unit(tx_trans_unit, tx_trans, 0, 0);
  37. rx_trans = get_rxRate(&Ndata[i], time);
  38. rx_trans = to_unit(rx_trans_unit, rx_trans, 0, 0);
  39. fprintf(stdout, "%4s %15.2Lf %2s %9.2Lf %2s/s %8.2Lf %2s %9.2Lf %2s/s\n",
  40. Ndata[i].device , rx, rx_unit, rx_trans, rx_trans_unit, tx,
  41. tx_unit, tx_trans, tx_trans_unit);
  42. if(!DIS_COMPACT) /*Seperator lines*/
  43. fprintf(stdout,"\n");
  44. }
  45. }
  46. void clearscreen(){
  47. char clear[3];
  48. clear[0] = 033; clear[1] = 'c'; clear[2]='\0';
  49. fprintf(stdout, "%s",clear);
  50. }
  51. long double to_unit(char *unit, long double bval, long double gval,
  52. int mode){
  53. long double tval;
  54. if((FORCE_UNIT == 3 && mode) || (FORCE_R_UNIT == 3 && !mode)){
  55. strcpy(unit, "KB");
  56. tval = gval * 1048576;
  57. tval += (bval / 1024);
  58. return tval;
  59. }
  60. else if((FORCE_UNIT == 2 && mode)|| (FORCE_R_UNIT == 2 && !mode)){
  61. strcpy(unit, "MB");
  62. tval = gval * 1024;
  63. tval += (bval / 1048576);
  64. return tval;
  65. }
  66. else if((FORCE_UNIT == 1 && mode)||(FORCE_R_UNIT == 1 && !mode)){
  67. strcpy(unit, "GB");
  68. tval = gval;
  69. tval += (bval / (long double)1073741824);
  70. return tval;
  71. }
  72. else if(bval < 1024 && gval == 0){
  73. strcpy(unit, "B");
  74. return bval;
  75. }
  76. else if(bval < 1048576 && gval == 0){
  77. strcpy(unit, "KB");
  78. tval = (bval / 1024);
  79. return tval;
  80. }
  81. else if(bval < 1073741824 && gval == 0){
  82. strcpy(unit, "MB");
  83. tval = (bval / 1048576);
  84. return tval;
  85. }
  86. else{
  87. strcpy(unit, "GB");
  88. tval = gval;
  89. tval += (bval / (long double)1073741824);
  90. return (tval);
  91. }
  92. }
  93. int checkfile(char *path){
  94. struct stat attrib;
  95. if((lstat(path, &attrib)) != 0){
  96. if(DEBUG)
  97. fprintf(stderr, "checkfile: Error: Unable to get file statistics\n");
  98. return -1;
  99. }
  100. if(S_ISLNK(attrib.st_mode)){
  101. if(DEBUG)
  102. fprintf(stderr, "checkfile: Error: File is a symbolic link\n");
  103. return -2;
  104. }
  105. if(DEBUG)
  106. fprintf(stderr, "checkfile: file security check has passed\n");
  107. return 0;
  108. }
  109. int ver_check(FILE *dev_file){
  110. char buff[80];
  111. int i = 0;
  112. for(i = 0; i < 2; i++){
  113. if((read(fileno(dev_file), buff, sizeof(buff))) < 0){
  114. if(DEBUG)
  115. fprintf(stderr, "ver_check: Error: Unable to read dev file\n");
  116. return -1;
  117. }
  118. }
  119. if((strstr(buff ,"compressed")) == NULL){
  120. if(DEBUG)
  121. fprintf(stderr, "ver_check: Error: Incorrect dev version\n");
  122. return -2;
  123. }
  124. if(DEBUG)
  125. fprintf(stderr, "ver_check: dev version is correct\n");
  126. return 0;
  127. }
  128. void remove_newline(char* string){
  129. int i = 0;
  130. for(i = 0; i < strlen(string); i++){
  131. if(string[i] == '\n'){
  132. string[i] = '\0';
  133. if(DEBUG)
  134. fprintf(stderr, "remove_newline: Successfully removed a new line\n");
  135. return;
  136. }
  137. }
  138. }
  139. int write_to_log(net_t *Ndata, char* path, int size){
  140. FILE *log;
  141. char temp_string[300];
  142. int i;
  143. if(((log = fopen(path, "w")) == NULL))
  144. return -1;
  145. if(DEBUG)
  146. fprintf(stderr, "write_to_log: Successfully opened file for writting\n");
  147. if(!fputs("<0.5>\n", log)){
  148. fprintf(stderr, "write_to_log: Error writting to log\n");
  149. return -1;
  150. }
  151. for(i = 0; i < size; i++){
  152. if((snprintf(temp_string, 300, "[%s]\n%Lu\n%Lu\n%Lu\n%Lu\n",
  153. Ndata[i].device,
  154. get_rxbtotal(&Ndata[i]),
  155. get_rxgtotal(&Ndata[i]),
  156. get_txbtotal(&Ndata[i]),
  157. get_txgtotal(&Ndata[i]))) < 0 ){
  158. fprintf(stderr, "write_to_log: Error creating string");
  159. return -1;
  160. }
  161. if(DEBUG)
  162. fprintf(stderr, "write_to_log: Successfully created string\n");
  163. if(!fputs(temp_string, log)){
  164. fprintf(stderr, "write_to_log: Error writting to log\n");
  165. return -1;
  166. }
  167. if(DEBUG)
  168. fprintf(stderr, "write_to_log: Successfully wrote to log\n");
  169. }
  170. fclose(log);
  171. return 0;
  172. }
  173. int parse_args(int argc, char *argv[]){
  174. int i;
  175. if(argc == 1)
  176. return 0;
  177. for(i = 1; i < argc; i++){
  178. if(strcmp(argv[i], "--config") == 0)
  179. return 1;
  180. else if(strcmp(argv[i], "--help") == 0){
  181. display_help();
  182. return -1;
  183. }
  184. else if(strcmp(argv[i], "--compact") == 0){
  185. DIS_COMPACT = 1;
  186. }
  187. else if(strcmp (argv[i], "--forceTGB") == 0){
  188. FORCE_UNIT = 1;
  189. }
  190. else if(strcmp (argv[i], "--forceTMB") == 0){
  191. FORCE_UNIT = 2;
  192. }
  193. else if(strcmp (argv[i], "--forceTKB") == 0){
  194. FORCE_UNIT = 3;
  195. }
  196. else if(strcmp (argv[i], "--forceRGB") == 0){
  197. FORCE_R_UNIT = 1;
  198. }
  199. else if(strcmp (argv[i], "--forceRMB") == 0){
  200. FORCE_R_UNIT = 2;
  201. }
  202. else if(strcmp (argv[i], "--forceRKB") == 0){
  203. FORCE_R_UNIT = 3;
  204. }
  205. else{
  206. fprintf(stderr, "Invalid argument please use --help flag for more details\n");
  207. return -1;
  208. }
  209. }
  210. return 0;
  211. }
  212. void display_help(){
  213. fprintf(stdout, "\nNetmon version 0.5\n");
  214. fprintf(stdout, "Usage [--help] [--options]....\n\n");
  215. fprintf(stdout, "The following command lines are availible\n");
  216. fprintf(stdout, "GENERAL\n");
  217. fprintf(stdout, "--help -> Displays this text\n");
  218. fprintf(stdout, "--config -> Runs the configuration wizard\n");
  219. fprintf(stdout, "--compact -> Removes spare lines from the display\n");
  220. fprintf(stdout, "Note: The --help and --config flags cannot be used with\n");
  221. fprintf(stdout, " any other arguments\n\n");
  222. fprintf(stdout, "FORCE UNITS\n");
  223. fprintf(stdout, "--forceTKB -> Forces Totals to Kilobytes\n");
  224. fprintf(stdout, "--forceTMB -> Forces Totals to Megabytes\n");
  225. fprintf(stdout, "--forceTGB -> Forces Totals to Gigabytes\n");
  226. fprintf(stdout, "--forceRKB -> Forces Data Rate to Kilobytes\n");
  227. fprintf(stdout, "--forceRMB -> Forces Data Rate to Megabytes\n");
  228. fprintf(stdout, "--forceRGB -> Forces Data Rate to Gigabytes\n");
  229. fprintf(stdout, "Note: Using too small of a data unit could lead to\n");
  230. fprintf(stdout, " a distorted display, in some cases causing the\n");
  231. fprintf(stdout, " display to become unreadable\n\n");
  232. }