inet_ntoa to strings

This is a discussion on inet_ntoa to strings within the Networking/Device Communication forums, part of the General Programming Boards category; I am creating a UDP server and it is working but, i would like to do some ip comparisons and ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    inet_ntoa to strings

    I am creating a UDP server and it is working but, i would like to do some ip comparisons and break them down t thestring representive
    say for instance i want to check for a specific ip 192.168.1.1 and if that address send a different message back than my normal
    this is what i have so far
    o1, o2, o3 & o4 are the string representives of each octet of the ip
    when compiling i get passing argument 1 of sscanf makes pointer from integer without cast
    i want to rid that, but it seems as if the strings are present

    for storing the ip in a string i tried to use
    Code:
    sscanf("%s",inet_ntoa(addr.sin_addr),uip);
    full code so far
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <sys/socket.h>
    #include <resolv.h>
    #include <string.h>
    
    #define DEFAULT_UDP_PORT	1234
    
    int main(int count, char *strings[])
    {	int sd;
    //	int i;		//use for counter
    	int fport;  	//from port
    	int lastport = 0;	//set last port for compare to from port
    	int uport;  	//upper byte of from port
    	int lport;  	//lower byte of from port
    	int sbytes = 20; //# of bytes to send back
    	int users;	//number of users on irc server
    	int port=DEFAULT_UDP_PORT;
    	struct sockaddr_in addr;
    	char buffer[32];	//receive buffer
    	char sbuffer[32];	//send buffer
    	char temp[32]={0x0};	//results of netstat
    	
    	char o1[5], o2[5], o3[5], o4[5];  //set string rep of each octet
    	int io1, io2, io3, io4;		//set int value for each octet
    
    	
    	if ( count != 2 )
    		printf("usage: %s <port>\n...Using UDP default port (%d).\n", strings[0], port);
    	else
    		port = atoi(strings[1]);
    	sd = socket(PF_INET, SOCK_DGRAM, 0);
    	bzero(&addr, sizeof(addr));
    	addr.sin_family = AF_INET;
    	addr.sin_port = htons(port);
    	addr.sin_addr.s_addr = INADDR_ANY;
    	if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
    		perror("bind");
    	while (1)
    	{	int bytes, addr_len=sizeof(addr);
    
    		bytes = recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr*)&addr, &addr_len);
    
    		//sscanf(inet_ntoa(addr.sin_addr), "%hu.%hu.%hu.%hu", &ip[0],&ip[1],&ip[2],&ip[3]);
    		sscanf(inet_ntoa(addr.sin_addr), "%4[^.].%4[^.].%4[^.].%4[^.]", o1, o2, o3, o4);
    
    		//set from port to port received from
    		fport = ntohs(addr.sin_port);
    
    		//set upper byte of port	
    		uport = fport / 256;
    
    		//set lower byte of port
    		lport = fport - (uport * 256);
    	
    		//int of each octet
    		io1 = atoi(o1);
    		io2 = atoi(o2);
    		io3 = atoi(o3);
    		io4 = atoi(o4);
    		sbuffer[0] = 'h';
    		sbuffer[1] = 'i';
    		sbuffer[2] = ' ';
    		sbuffer[3] = 'b';
    		sbuffer[4] = 'y';
    		sbuffer[5] = 'e';
                                 //test different ip here and change message
                                 sendto(sd,sbuffer,sbytes,0, (struc sockaddr*)&addr, sizeof(addr));
    }
    close(sd);
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You need to rearrange the arguments to sscanf. The first one is the string to read from, the second is the "template", the remainder are variables to hold data.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    4
    even when i rearrange it to
    Code:
    sscanf(inet_ntoa(addr.sin_addr),"%s",uip);
    i still get warning passing argument 1 of sscaanf makes pointer from integer without cast
    the second sscanf is working becaise io1 thru io4 have the values i expected, but also gives me same warning, with this new sscanf above ill get a segment fault

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "pointer from integer without cast" almost always mean "you didn't include the header file" (in this case <arpa/inet.h>).

  5. #5
    Registered User
    Join Date
    Feb 2009
    Location
    India, Gujarat
    Posts
    22
    Quote Originally Posted by TomChesley View Post
    I am creating a UDP server and it is working but, i would like to do some ip comparisons and break them down t thestring representive
    say for instance i want to check for a specific ip 192.168.1.1 and if that address send a different message back than my normal
    this is what i have so far
    o1, o2, o3 & o4 are the string representives of each octet of the ip
    when compiling i get passing argument 1 of sscanf makes pointer from integer without cast
    i want to rid that, but it seems as if the strings are present

    for storing the ip in a string i tried to use
    Code:
    sscanf("%s",inet_ntoa(addr.sin_addr),uip);
    full code so far
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <sys/socket.h>
    #include <resolv.h>
    #include <string.h>
    
    #define DEFAULT_UDP_PORT	1234
    
    int main(int count, char *strings[])
    {	int sd;
    //	int i;		//use for counter
    	int fport;  	//from port
    	int lastport = 0;	//set last port for compare to from port
    	int uport;  	//upper byte of from port
    	int lport;  	//lower byte of from port
    	int sbytes = 20; //# of bytes to send back
    	int users;	//number of users on irc server
    	int port=DEFAULT_UDP_PORT;
    	struct sockaddr_in addr;
    	char buffer[32];	//receive buffer
    	char sbuffer[32];	//send buffer
    	char temp[32]={0x0};	//results of netstat
    	
    	char o1[5], o2[5], o3[5], o4[5];  //set string rep of each octet
    	int io1, io2, io3, io4;		//set int value for each octet
    
    	
    	if ( count != 2 )
    		printf("usage: %s <port>\n...Using UDP default port (%d).\n", strings[0], port);
    	else
    		port = atoi(strings[1]);
    	sd = socket(PF_INET, SOCK_DGRAM, 0);
    	bzero(&addr, sizeof(addr));
    	addr.sin_family = AF_INET;
    	addr.sin_port = htons(port);
    	addr.sin_addr.s_addr = INADDR_ANY;
    	if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
    		perror("bind");
    	while (1)
    	{	int bytes, addr_len=sizeof(addr);
    
    		bytes = recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr*)&addr, &addr_len);
    
    		//sscanf(inet_ntoa(addr.sin_addr), "%hu.%hu.%hu.%hu", &ip[0],&ip[1],&ip[2],&ip[3]);
    		sscanf(inet_ntoa(addr.sin_addr), "%4[^.].%4[^.].%4[^.].%4[^.]", o1, o2, o3, o4);
    
    		//set from port to port received from
    		fport = ntohs(addr.sin_port);
    
    		//set upper byte of port	
    		uport = fport / 256;
    
    		//set lower byte of port
    		lport = fport - (uport * 256);
    	
    		//int of each octet
    		io1 = atoi(o1);
    		io2 = atoi(o2);
    		io3 = atoi(o3);
    		io4 = atoi(o4);
    		sbuffer[0] = 'h';
    		sbuffer[1] = 'i';
    		sbuffer[2] = ' ';
    		sbuffer[3] = 'b';
    		sbuffer[4] = 'y';
    		sbuffer[5] = 'e';
                                 //test different ip here and change message
                                 sendto(sd,sbuffer,sbytes,0, (struc sockaddr*)&addr, sizeof(addr));
    }
    close(sd);
    }
    -------------------------------------------------------------------------------------------------------------------


    just make a typecasting of first argument of sscanf
    like : " sscanf((const char*)inet_ntoa(addr.sin_addr), "%4[^.].%4[^.].%4[^.].%4[^.]", o1, o2, o3, o4);"

    it's work

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Ups - misread o1 and io1 vars
    Last edited by vart; 02-24-2009 at 10:52 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    I won't repeat myself, or others - http://www.tek-tips.com/viewthread.c...1532610&page=1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    4
    thanx the <arpa/inet.h> did the trick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21