LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2013, 04:52 PM   #1
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Question Connecting socket using bogus address -- No problem! But why?


I cannot understand why an attempt to connect to a bogus address using port 22 fails, yet if I change the port to 80 it succeeds. I would expect both cases to fail.

Here's some code that demonstrates the issue:
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>

int main()
{
    const char* address = "bogus";
    //const char* port = "22";
    const char* port = "80";

    struct sockaddr_in sin;
    struct addrinfo    hints;
    struct addrinfo*   host_info = 0;

    memset(&sin, 0, sizeof(sin));
    memset(&hints, 0, sizeof(hints));

    hints.ai_family  = AF_INET;
    hints.ai_flags   = 0;   // superfluous
    hints.ai_addrlen = sizeof(sin);

    assert(getaddrinfo(address, port, &hints, &host_info) == 0);
    assert(host_info != 0);

    memcpy(&sin, host_info->ai_addr, host_info->ai_addrlen);

    int sock = 0;

    assert((sock = socket(AF_INET, SOCK_STREAM, 0)) > 0);

    assert(connect(sock, (struct sockaddr*) &sin, (socklen_t) sizeof(sin)) == 0);

    printf("Success!\n");

    return 0;
}
With port 80 in use, I successfully connect and see the printed message of "Success!". If I change the port to 22, then the assert(), when attempting to connect, is triggered.

Can anyone help explain this odd behavior?

Last edited by dwhitney67; 03-04-2013 at 04:55 PM.
 
Old 03-04-2013, 07:21 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by dwhitney67 View Post
If I change the port to 22, then the assert(), when attempting to connect, is triggered.
But the assert around getaddrinfo() is not triggered? Maybe try printing the host info that is returned by getaddrinfo() then.
 
Old 03-04-2013, 08:26 PM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Original Poster
Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by ntubski View Post
But the assert around getaddrinfo() is not triggered? Maybe try printing the host info that is returned by getaddrinfo() then.
ntubski,

Thanks for the pointer.

I printed out the IP address and came up with an address of 199.101.28.20. Using my web browser, I found it to be http://search.dnsassist.verizon.net. This explains why using port 80 I was able to connect.
Code:
    ...
    struct sockaddr_in* sin2 = (struct sockaddr_in*)host_info->ai_addr;
    printf("%s\n", inet_ntoa(sin2->sin_addr));
    ...
So apparently my system fails over to that IP address (of my ISP) when I supply any bogus server name and port 80. It would be nice if I could configure my system to avoid such a thing. Any ideas on how to do this?

Last edited by dwhitney67; 03-05-2013 at 04:58 AM.
 
Old 03-04-2013, 10:11 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'd expect that's probably the default of most ISPs; the pkt goes as far as it can (sort of).
If you want a test addr, stick to localhost range 127.0.0.0/8 or possibly something in your normal lan addr range eg 192.168.0.0/16.
This RFC lists the 'special' ranges; https://tools.ietf.org/html/rfc3330
 
Old 03-05-2013, 09:34 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
According to http://log.psi.cc/2008/03/02/how-to-...ns-assistance/ and http://domnit.org/verizon/ you may be able to opt out of this "feature".
 
Old 03-06-2013, 08:11 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Original Poster
Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by ntubski View Post
According to http://log.psi.cc/2008/03/02/how-to-...ns-assistance/ and http://domnit.org/verizon/ you may be able to opt out of this "feature".
Again, thanks! The second link you provided was clearer to understand than the first.

The issue is now resolved.
 
  


Reply

Tags
connect, socket



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
Connecting using Socket NetProgrammer Programming 8 09-06-2012 09:05 AM
Python IP Address socket problem with bind xskycamefalling Programming 5 02-14-2010 09:57 PM
Creating a bogus DNS domain inside private address range network zogness Linux - Networking 13 10-16-2009 01:34 PM
problem in connecting Multiple clients to socket server in linux M007 Linux - Server 1 10-31-2007 11:52 PM
Connecting socket with non-local ip address igoshen Linux - Networking 3 04-23-2007 05:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 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