LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2014, 07:43 AM   #1
chelem
LQ Newbie
 
Registered: Oct 2014
Posts: 4

Rep: Reputation: Disabled
Get processes being used by a specific port using C


Hey all,

I'm creating a very small personal firewall (nothing serious) and I want to implement a "killswitch" which will close all ports. The problem arises when I cannot close ports because some processes are using it. Is there a way (using C, not a 3rd party tool) to detect which processes are using which port so I can kill them?

Thanks
 
Old 11-03-2014, 12:51 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by chelem View Post
I'm creating a very small personal firewall
According to your other thread you're not because you're using the wrong tool for the right task.
 
Old 11-03-2014, 02:32 AM   #3
chelem
LQ Newbie
 
Registered: Oct 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
According to your other thread you're not because you're using the wrong tool for the right task.

*That* app is different and not related to this one. Yes I'm creating two apps simultaneously.
 
Old 11-03-2014, 03:05 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,454

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
You could look at the source of lsof and see what it does when you do "lsof -i".
Code:
apt-get source lsof
 
Old 11-04-2014, 01:28 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
OK, then do use iptables to block access as it trumps killing processes (you can test this yourself) and to kill existing TCP connections see the code for 'cutter' and 'tcpkill'.


Quote:
Originally Posted by chelem View Post
The problem arises when I cannot close ports because some processes are using it. Is there a way (using C, not a 3rd party tool) to detect which processes are using which port so I can kill them?
See the code for 'netstat -l' on how to populate a struct with entries you see in /proc/net/* listing existing connections?
 
  


Reply

Tags
firewall, port, process, sockets


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to route specific users/processes via OpenVPN nuvista175 Linux - Networking 2 02-22-2013 02:30 AM
Is there a cmd that will kill all processes that are accessing a specific filename bret Linux - General 2 12-17-2009 10:30 AM
whether segment's are specific to each processes Kesavaraj Linux - General 1 08-04-2008 08:42 AM
Starting processes as a specific user? Loonyjuice Linux - Newbie 6 01-04-2007 02:17 AM
How do I see what processes are touching specific devices ravalox Linux - Software 1 02-28-2004 08:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 05:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration