-1

I'm having trouble to use bind method on Windows (C language). Well. My code is supposed to do the following things:

  1. get 2 addresses from the user: One must be an ip address from the local machine and another a remote address ( a google address for example).
  2. The application i'm writing will bind to the local address
  3. The application will connect to the second address.

I got some code from the Internet. You can see all the sources i had consulted on the comments.

This application will only accepts a string from the client (which specifies the local
address which the application will bind), then, the app will connect to a remote service
google, youtube or whatever. The main purpose that this app serves is to know if, when
 binded to a local address, the operative system (in this case Windows Based system) gives priority to:
- SA/DA rule
- Forwarding table




SOURCES:

http://stackoverflow.com/questions/2065495/using-a-specific-network-interface-for-a-socket-in-windows
http://stackoverflow.com/questions/2605182/when-binding-a-client-tcp-socket-to-a-specific-local-port-with-winsock-so-reuse
http://www.delta-search.com/?q=error+on+binding+to+local+address%2Cwindows&s=web&as=0&rlz=0&babsrc=HP_ss
http://social.msdn.microsoft.com/Forums/zh/vcgeneral/thread/763a00ab-0e7f-44c6-9794-840fc6cb2e07
http://www.delta-search.com/?q=add+ws2_32.lib+visual+studio+2010&babsrc=HP_ss&s=web&rlz=0&as=3&ac=0%2C331
http://stackoverflow.com/questions/5220309/why-am-i-getting-linker-errors-for-ws2-32-dll-in-my-c-program
http://msdn.microsoft.com/en-US/windows/desktop/aa904949
http://msdn.microsoft.com/en-us/library/windows/desktop/ms737550(v=vs.85).aspx



#pragma once
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <Ws2tcpip.h>
#include <stdio.h>


void Abort(char *msg);

void main(int argc,char*argv[]){

    int ret, fd;
    struct sockaddr_in sa_dst;
    struct sockaddr_in sa_loc;

    if(argc < 3)
        Abort("Syntax: SourceIpAddress(to bind) DestinationIpAddress(to connect)");


    fd = socket(AF_INET, SOCK_STREAM, 0);

    // Local
    memset(&sa_loc, 0, sizeof(struct sockaddr_in));
    sa_loc.sin_family = AF_INET;
    sa_loc.sin_port = htons(0);
    sa_loc.sin_addr.s_addr = inet_addr(argv[1]);

    if((ret = bind(fd, (struct sockaddr *)&sa_loc, sizeof(struct sockaddr))) < 0)
        Abort("Binding to local address");


    // Remote
    memset(&sa_dst, 0, sizeof(struct sockaddr_in));
    sa_dst.sin_family = AF_INET;
    sa_dst.sin_port = htons(80);
    sa_dst.sin_addr.s_addr = inet_addr(argv[2]); // google :)

    if((ret = connect(fd, (struct sockaddr *)&sa_dst, sizeof(struct sockaddr))) < 0)
        Abort("Connect to remote address");

    printf("\n\nConnection Successfully made!!\n\n");    }

void Abort(char *msg){
fprintf(stderr,"\n\n<ERROR>: <%s>\n",msg);
perror("\n\nExiting...");
exit(EXIT_FAILURE);       }

The problem is that bind returns -1. I've tested already.

Can anyone help me on this?

Thanks in advance

Pedro Neves
  • 364
  • 1
  • 8
  • 26

1 Answers1

1

When a Winsock function fails, you can call WSAGetLastError() to find out why.

In this case, you are not calling WSAStartup(), so socket() fails with a WSANOTINITIALISED error that you are ignoring, so you end up passing an invalid socket to bind().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770