user3329166
user3329166

Reputation: 126

Variable is being changed for some reason

This post is follow up to Call to the 'userConnect()' method skips

Correction to my last post, It is actually entering userConnect() method but some how SETUP_ERROR flag is being changed. As you can see in my userConnect() the places I changed SETUP_ERROR, I clearly printed what the error could be.

Now with the 'cout <<' in 'getMyIPAddress()' method, the SETUP_ERROR is 0 and enters into IF loop in userConnect() and works perfect. When I remove it, Somehow SETUP_ERROR is being changed to 1 and printing "SETUP_ERROR".

Cannot figure out why SETUP_ERROR being changed to 1, I know 'cout' has nothing to do with this but what could it be. Beats me.

getMyIPAdress():

void getMyIPAddress (char* command, char* port) {
    struct hostent *he;
    struct in_addr ipv4addr;
    char dnsIP[] = "8.8.8.8";                                   // Google DNS IP
    char dnsPort[] = "53" ;                                     // Google DNS TCP Port
    // Converting IP to struct in_addr type     
    inet_pton(AF_INET, dnsIP, &ipv4addr);
    // getting host details from IP address
    he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
    cout << "bazinga!" << endl;
    // Connecting to google DNS to get IP address of this process
    userConnect (he->h_name, dnsPort, port, command);
}

userConnect():

void userConnect ( char* sIP, char* sPort, string my_port, char* command )
{
    int numbytes;
    char buf[MAXDATASIZE];
    char rbuf[MAXDATASIZE];
    int rv;
    char s[INET6_ADDRSTRLEN];
    struct addrinfo *servinfo,*p,hints;
    int sockfd;
    int SETUP_ERROR;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    // Getting address related information 
    // flagging invalid ip addresses 
    if ((rv = getaddrinfo(sIP, sPort, &hints, &servinfo)) != 0) {
        cout << "getaddrinfo: " << endl << gai_strerror(rv) << endl;
        cout << " Invalid IP Address! " << endl;
        SETUP_ERROR = 1;
    }else
    {
        int count;
        for(p = servinfo; p != NULL; p = p->ai_next) {
            if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
                perror("client: socket");
                continue;
            }

            if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
                close(sockfd);
                perror("client: connect");
                continue;
            }

            break;
        }
        // flagging invalid port numbers
        if (p == NULL) {
            cout << stderr << "client: failed to connect" << endl;
            cout  << "Invalid Port Number! " << endl;
            SETUP_ERROR = 1;
        }
    }
    if (SETUP_ERROR != 1) {

        inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);       // gettin printable ip
        freeaddrinfo(servinfo);                                                                 // don't need anymore
        int len;
        // Functionalities based on input commands
        if (strcmp(command,"REGISTER")==0) {
            // client adding server as default first connection
            conn_head = insert(conn_head , server_IP, sPort, sockfd-OFFSET );
            servfd = sockfd;
            len = my_port.length();
            // sending client's listening port number to server 
            if (send (sockfd, &len, sizeof( len ), 0) == -1)
                perror("send packet length");
            if (send(sockfd, my_port.c_str(), len, 0) == -1)
                perror("send");
            // adding socket fd to master list 
            addFdToMaster(sockfd);
        }else if(strcmp(command, "MYIP")==0) {
            /* Next 10 line code snippet is taken from a blog online! Cant find it anymore to post reference ! :) */
            //get local socket info::
            struct sockaddr_in local_addr;
                socklen_t addr_len = sizeof(local_addr);
            if (getsockname(sockfd, (struct sockaddr*)&local_addr, &addr_len) < 0) {
                perror("getsockname");
            }
            /* get peer ip addr */
            char my_ip[INET_ADDRSTRLEN];
            if (inet_ntop(local_addr.sin_family, &(local_addr.sin_addr), myip, sizeof(myip)) == NULL) {
                perror("inet_ntop");
            }
            else
                cout << "HOST IP Address:: " << myip << endl;
        }
    }else
        cout << "SETUP_ERROR" << endl;
}

Upvotes: 0

Views: 108

Answers (1)

M.M
M.M

Reputation: 141618

You never initialize SETUP_ERROR, nor set it to 0. The test if (SETUP_ERROR != 1) is testing an uninitialized variable, in the cases where you seem to be expecting it to be 0.

To explain what you're seeing, your compiler is placing SETUP_ERROR on the stack, and since there's no initialization, it is just assuming a value from whatever bytes are in the memory location that the stack grows into. These could be affected by what you were doing in the other function.

Upvotes: 2

Related Questions