LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 11-06-2017, 04:08 AM   #1
OnkelWolfram
LQ Newbie
 
Registered: Nov 2017
Posts: 2

Rep: Reputation: Disabled
Local ports in connect() syscall from socket.c ?


Hey there,

i am trying to modify the connect() syscall from the socket.c kernel file, so i can get information about the source port connected to a foreign address' port. I assume that the information I need must either be stored in "struct sockaddr __user* uservaddr" which the syscall takes as parameter or in "struct socket *sock" inside the syscall, but I am only able to find the destination port (e.g. port 80 when using wget).

But where can I access the variable storing the local port? As far as I know, a local port must be defined somewhere at this point.

Thanks in advance for your help!
 
Old 11-06-2017, 01:25 PM   #2
sixerjman
Member
 
Registered: Sep 2004
Distribution: Debian Testing / Unstable
Posts: 175
Blog Entries: 1

Rep: Reputation: 31
Smile Good question!

That's a pretty 'meaty' question for Linux system programmers to field. I will drill
down in the code and see what I can see (and understand). What OS and kernel version
are you running? I run Fedora Release 26 in production and the kernel version is
4.10.13. I have some 4.12 kernels on my Debian machines. I will check back later
with my findings and get an update on your platform.
 
Old 11-06-2017, 02:03 PM   #3
sixerjman
Member
 
Registered: Sep 2004
Distribution: Debian Testing / Unstable
Posts: 175
Blog Entries: 1

Rep: Reputation: 31
Google search birthed a couple of good links:

How to get ip address from sock structure in c?
http://tinyurl.com/y7e93d8h

Getting the source address of an incoming socket connection
http://tinyurl.com/y98rqlrh
 
Old 11-07-2017, 05:26 AM   #4
OnkelWolfram
LQ Newbie
 
Registered: Nov 2017
Posts: 2

Original Poster
Rep: Reputation: Disabled
hey sixerjman,

thank you for your reply. I am modifying an Openwrt embedded Linux with Kernel v3.18.

I want to clearify two things: First of all, I am not looking for the source address, but for the source port! The second thing is that both of your links refer to user space functions.

I did some research with an old book about kernel development and i found out that the socket.c file mainly deals with the OSI application layer and on this level, source and destination ports of sockets are not defined yet, so i have to go deeper to transport layer (tcp.c and udp.c in the Linux kernel). I am not sure if this is right, but i will do some further evaluation. I hope this will be the solution.

Nevertheless, if someone still has a way to get source ports from the connect() syscall, feel free to contribute!
 
Old 11-07-2017, 04:29 PM   #5
sixerjman
Member
 
Registered: Sep 2004
Distribution: Debian Testing / Unstable
Posts: 175
Blog Entries: 1

Rep: Reputation: 31
Ah, yes! My apologies, I missed the detail about extracting the port number in the connect() call from your original post in my haste to reply. Thank you for the clarification about the Linux version and the openwrt platform. I will once again drill down and see what I can see. Thank you for the fascinating problem.
 
Old 11-08-2017, 11:16 AM   #6
sixerjman
Member
 
Registered: Sep 2004
Distribution: Debian Testing / Unstable
Posts: 175
Blog Entries: 1

Rep: Reputation: 31
Good article detailing connect() source port allocation

This article 'Bind before Connect' sheds light on the internals of source port allocation. Hopefully this may help targeting where to modify code to report the source code: https://idea.popcount.org/2014-04-03...efore-connect/
 
  


Reply


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
[SOLVED] can't connect to local mysql server through socket.................. cheeliong Linux - Newbie 3 08-10-2011 03:34 AM
kernel hang on 'connect' to local socket ta0kira Programming 2 12-16-2008 04:47 AM
Frustration: Can't connect to local MySQL server through socket '/usr/local/mysql-5.0 nidala Linux - Newbie 1 09-28-2008 12:20 AM
Can't connect to local MySQL server through socket tommytomato Linux - Newbie 6 05-25-2004 10:16 AM
Can't connect to local MySQL server through socket . . . patpawlowski Programming 2 01-23-2004 04:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 08:24 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration