File: Stats.c

package info (click to toggle)
mysecureshell 2.00%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 888 kB
  • sloc: ansic: 7,421; sh: 700; perl: 264; makefile: 116
file content (85 lines) | stat: -rw-r--r-- 2,296 bytes parent folder | download | duplicates (4)
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
/*
MySecureShell permit to add restriction to modified sftp-server
when using MySecureShell as shell.
Copyright (C) 2007-2014 MySecureShell Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation (version 2)

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "../config.h"
#include <stdlib.h>
#include <time.h>
#include "Buffer.h"
#include "SftpWho.h"
#include "Stats.h"

tStats		*StatsNew()
{
  tStats	*stats;

  stats = calloc(1, sizeof(*stats));
  return (stats);
}

void    StatsDelete(tStats *stats)
{
  free(stats);
}


void    StatsUpdate(tStats *stats)
{
  t_sftpwho	*who = SftWhoGetAllStructs();

  if (who != NULL)
    {
      u_int32_t	download = 0, upload = 0;
      u_int16_t	users = 0;
      int	i;
      
      for (i = 0; i < SFTPWHO_MAXCLIENT; i++)
	{
	  if ((who[i].status & SFTPWHO_STATUS_MASK) != SFTPWHO_EMPTY)
	    {
	      users++;
	      download += who[i].download_current;
	      upload += who[i].upload_current;
	    }
	}
      stats->users[stats->writePos] = users;
      stats->download[stats->writePos] = download;
      stats->upload[stats->writePos] = upload;
      stats->writePos = (stats->writePos + 1) % STATS_SECONDES;
    }
}

void    StatsSend(tStats *stats, u_int32_t lastRefresh, tBuffer *b)
{
  u_int32_t	currentTime, showTime;
  int		firstPos, i;

  currentTime = (u_int32_t )time(NULL);
  showTime = currentTime - lastRefresh;
  if (showTime >= STATS_SECONDES)
    showTime = STATS_SECONDES - 1;
  firstPos = (stats->writePos - (int )showTime + STATS_SECONDES) % STATS_SECONDES;
  BufferPutInt32(b, showTime);
  for (i = firstPos; i != stats->writePos; )
    {
      BufferPutInt16(b, stats->users[i]);
      BufferPutInt32(b, stats->download[i]);
      BufferPutInt32(b, stats->upload[i]);
      i = (i + 1) % STATS_SECONDES;
    }
}