By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,567 Members | 1,592 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,567 IT Pros & Developers. It's quick & easy.

Need some implementation ideea

P: n/a
Hi there!

This is the problem:
I have a server that accept connections.
Each request span a thread that handle it.
I need a way to store the client IP's.
If the requests made from a single IP pass a number, say 10, I want to drop
that request without processing.
All that request have a timeout so after say 10 sec elapsed, the requests
number made fom a single IP is decreased by one, so the client cant make
more that 10 requests in 10 secs.

Can someone help me?

Thanks, Crirus
Nov 20 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Hi Crirus,

Because of the relatively long time spans involved compared to the life of
a request and response, and because this is a service supplying all requests,
my thinking tends towards a separate process which acts as a 'permission
broker'. Also, that way you only need one timer to handle everything.

I'd have a list of 'decrement' events which would be managed using the
timer. An event on the list would consist of the time when it is to occur, an
action and the IP. Although such a class is highly generalisable, in your case
there will only be one type of event. The timer interval would be set to the
next event. On the tick it would decrement the count of the associated IP and
set the next interval accordingly.

Each request coming in to the server would pass the IP to this broker
which would check against its internal list. If over the threshold - "no
permission". If under - "ok, off you go".

Each "off you go" would bump up the count for that IP and add a decrement
event to the end of the list, due to go off in so many seconds.

Regards,
Fergus
Nov 20 '05 #2

P: n/a
Cor
Hi Crirus,

Can you not use "modified" that nice example Armin did send in this week?

\\\ by Armin
Private m_Thread As MyThread
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
m_Thread = New MyThread
AddHandler m_Thread.Progress, AddressOf OnProgress
AddHandler m_Thread.Done, AddressOf OnDone
m_Thread.Start()
End Sub
Public Delegate Sub ProgressDelegate(ByVal Progress As Integer)
Private Sub OnProgress(ByVal Progress As Integer)
If Me.InvokeRequired Then
Me.Invoke(New ProgressDelegate( _
AddressOf OnProgress _
), New Object() {Progress})
Else
Me.Button1.Text = Progress.ToString
End If
End Sub
Private Sub OnDone()
m_Thread = Nothing
End Sub
///
\\\
Class MyThread
Public Event Progress(ByVal Progress As Integer)
Public Event Done()
Private m_Thread As Thread
Public Sub Start()
m_Thread = New Thread(AddressOf ThreadStart)
m_Thread.Start()
End Sub
Private Sub ThreadStart()
Dim i As Integer
For i = 1 To 100
Thread.Sleep(100)
RaiseEvent Progress(i)
Next
RaiseEvent Done()
End Sub
End Class
///

Cor
Nov 20 '05 #3

P: n/a
Hi Fergus
Hard to follow your ideea :(
What I was thinking by now is a lockSynk object, called Flood, that handle
the IP's.
Each request handler thread should add the IP to the Flood ip's list that
maintain a number of them in a dictionary (IP, number pair)
The add function should return true if IP is allowed to receipt data.
So, each thread wait for the others previous threads to return from lock and
then test it's own IP .

The question I have right now is how to decrement each IP's number added in
that with it's own timer in that class.

Anyway, can you repost your ideea another way to lem me follow it?

Thanks
Crirus

"Fergus Cooney" <fi****@post.com> wrote in message
news:Oe**************@TK2MSFTNGP10.phx.gbl...
Hi Crirus,

Because of the relatively long time spans involved compared to the life of a request and response, and because this is a service supplying all requests, my thinking tends towards a separate process which acts as a 'permission
broker'. Also, that way you only need one timer to handle everything.

I'd have a list of 'decrement' events which would be managed using the
timer. An event on the list would consist of the time when it is to occur, an action and the IP. Although such a class is highly generalisable, in your case there will only be one type of event. The timer interval would be set to the next event. On the tick it would decrement the count of the associated IP and set the next interval accordingly.

Each request coming in to the server would pass the IP to this broker
which would check against its internal list. If over the threshold - "no
permission". If under - "ok, off you go".

Each "off you go" would bump up the count for that IP and add a decrement event to the end of the list, due to go off in so many seconds.

Regards,
Fergus

Nov 20 '05 #4

P: n/a
So, what is this sample code?
some kind of a timer?
"Cor" <no*@non.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
Hi Crirus,

Can you not use "modified" that nice example Armin did send in this week?

\\\ by Armin
Private m_Thread As MyThread
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
m_Thread = New MyThread
AddHandler m_Thread.Progress, AddressOf OnProgress
AddHandler m_Thread.Done, AddressOf OnDone
m_Thread.Start()
End Sub
Public Delegate Sub ProgressDelegate(ByVal Progress As Integer)
Private Sub OnProgress(ByVal Progress As Integer)
If Me.InvokeRequired Then
Me.Invoke(New ProgressDelegate( _
AddressOf OnProgress _
), New Object() {Progress})
Else
Me.Button1.Text = Progress.ToString
End If
End Sub
Private Sub OnDone()
m_Thread = Nothing
End Sub
///
\\\
Class MyThread
Public Event Progress(ByVal Progress As Integer)
Public Event Done()
Private m_Thread As Thread
Public Sub Start()
m_Thread = New Thread(AddressOf ThreadStart)
m_Thread.Start()
End Sub
Private Sub ThreadStart()
Dim i As Integer
For i = 1 To 100
Thread.Sleep(100)
RaiseEvent Progress(i)
Next
RaiseEvent Done()
End Sub
End Class
///

Cor

Nov 20 '05 #5

P: n/a
I post below some code

Private Function isBanned(ByVal ip As String) As Boolean
SyncLock myFlood
Return myFlood.isFlooding(ip)
End SyncLock
End Function

'this function is the callback for a receiv thread
Private Sub AcceptCallback(ByVal ar As IAsyncResult)
Try
allDone.Set() ' Signal the listen thread to continue.
Dim listener As Socket = CType(ar.AsyncState, Socket) 'Get the
socket that handles the client request.
Dim handler As Socket = listener.EndAccept(ar) 'Mark the end of
accept and create the new socket for client

Dim clientIP As IPEndPoint = CType(handler.RemoteEndPoint,
IPEndPoint) 'adresa clientului not used here
If isBanned(clientIP.Address.ToString) Then
handler.Shutdown(SocketShutdown.Both)
handler.Close()
Exit Sub
End If
........
end sub

Public Class Flood
Private Const maxConnectionAllowed = 10
Private myIps() As DictionaryEntry
Public Function isFlooding(ByVal ip As String)
For i As Integer = 0 To UBound(myIps)
If myIps(i).Key = ip Then
myIps(i).Value += 1
If myIps(i).Value > maxConnectionAllowed Then
Return True
Else
Return False
End If
End If
Next
End Function
End Class
Nov 20 '05 #6

P: n/a
Cor
Hi Crirus
So, what is this sample code?
some kind of a timer?


Yes and I thougth when you make your steps for 1 in a second, you are very
close to your goal. But it is not some kind of a timer, it is a timer,
which can keep the other processes going very well and you can make it in a
way I think you need..

But it is just an idea, I am at the moment typical not doing the things you
are doing now

Cor
Nov 20 '05 #7

P: n/a
OK, thanks, but my problem is not to set a timer...
Is deeper, in the way that I need to know what to do with that timer.
I need a IP manager that keep track of the IP that meke request to my server
and only allow a certain number of them to be processed

In fact, is a flood protection I want
"Cor" <no*@non.com> wrote in message
news:el**************@TK2MSFTNGP10.phx.gbl...
Hi Crirus
So, what is this sample code?
some kind of a timer?
Yes and I thougth when you make your steps for 1 in a second, you are very
close to your goal. But it is not some kind of a timer, it is a timer,
which can keep the other processes going very well and you can make it in

a way I think you need..

But it is just an idea, I am at the moment typical not doing the things you are doing now

Cor

Nov 20 '05 #8

P: n/a
Hi Fergus,
After second read, seems that we go on the same route...
please, detail only the decremental events part :)
"Crirus" <Cr****@datagroup.ro> wrote in message
news:ul**************@TK2MSFTNGP09.phx.gbl...
Hi Fergus
Hard to follow your ideea :(
What I was thinking by now is a lockSynk object, called Flood, that handle
the IP's.
Each request handler thread should add the IP to the Flood ip's list that
maintain a number of them in a dictionary (IP, number pair)
The add function should return true if IP is allowed to receipt data.
So, each thread wait for the others previous threads to return from lock and then test it's own IP .

The question I have right now is how to decrement each IP's number added in that with it's own timer in that class.

Anyway, can you repost your ideea another way to lem me follow it?

Thanks
Crirus

"Fergus Cooney" <fi****@post.com> wrote in message
news:Oe**************@TK2MSFTNGP10.phx.gbl...
Hi Crirus,

Because of the relatively long time spans involved compared to the life of
a request and response, and because this is a service supplying all

requests,
my thinking tends towards a separate process which acts as a 'permission
broker'. Also, that way you only need one timer to handle everything.

I'd have a list of 'decrement' events which would be managed using the timer. An event on the list would consist of the time when it is to occur, an
action and the IP. Although such a class is highly generalisable, in
your case
there will only be one type of event. The timer interval would be set to the
next event. On the tick it would decrement the count of the associated

IP and
set the next interval accordingly.

Each request coming in to the server would pass the IP to this

broker which would check against its internal list. If over the threshold - "no
permission". If under - "ok, off you go".

Each "off you go" would bump up the count for that IP and add a

decrement
event to the end of the list, due to go off in so many seconds.

Regards,
Fergus


Nov 20 '05 #9

P: n/a
Cor
Hi Crirus

In fact, is a flood protection I want


That was exactly what I was understanding.

And with saving all IP's I thought you can get messed up with a terrible
unmanaged collection, so I thought maybe there is another approach.

But if you have your solution, take that first.

You did ask for idea's I thought

Cor
Nov 20 '05 #10

P: n/a
Yes, but I dont know how to use that exemple you sent :)
"Cor" <no*@non.com> wrote in message
news:ON**************@TK2MSFTNGP09.phx.gbl...
Hi Crirus

In fact, is a flood protection I want


That was exactly what I was understanding.

And with saving all IP's I thought you can get messed up with a terrible
unmanaged collection, so I thought maybe there is another approach.

But if you have your solution, take that first.

You did ask for idea's I thought

Cor

Nov 20 '05 #11

P: n/a
Cor
Hi Fergus,
I knew this, just as I said to Scorpion yesterday.
I thougth you was to bed man.
:-))))))
Cor
Nov 20 '05 #12

P: n/a
Cor
Hi Fergus,

I saw the other thread, and before I started on the image I thought look
what Fergus did made.

I can see from the signature from your writting that it is from you, but I
think it is better not sending vb files. Some will think that it are
virusses and then we have the whole discussion again.

I know why you did it and the hurry you are in now and it was of course an
accidant that can happen.

I thought I was the best to tell you and not someone like Herfried or Armin
and I hope those keep there mouth.

Cor
Nov 20 '05 #13

P: n/a
Hi Cor,

LOL. I should be. I just wanted to finish that event queueing sample for
Crirus.

Thanks for talking to Eva for me. I don't have any time at all now (Zzzz),
and I'm not sure I'd be able to do it anyway. I'll have a quick go tonight.

Regards,
Fergus
Nov 20 '05 #14

P: n/a
Hi Cor,

'Twas no accident. And now I'm puzzled. What's wrong with vb files -
they're just text? Open one up and it whips you into VS.

Regards,
Fergus
Nov 20 '05 #15

P: n/a
Cor
Hi Fergus,
You are right, I was thinking about vbs
Sorry,
Cor
Nov 20 '05 #16

P: n/a
Hi Fergus
Thanks for time spended and the great sample

Crirus

"Fergus Cooney" <fi****@post.com> wrote in message
news:uE*************@TK2MSFTNGP11.phx.gbl...
Hi Crirus,

To prove the idea I had to do it all. ;-)

I think this should show you what I meant. It's practically ready to run - see the Module at the bottom.

You'll need to test it much better than I have. There may be problems with the SyncLocks as there's the potential for recursion.

Regards,
Fergus

Nov 20 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.