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);
}