Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi friends,
i have some doubts regarding the configuring the ethernet ports( by writing a C code)...say, if i want to configure the serial ports for some baudrate and parity or stopbits from a c code i wil be doing like this...
Code:
struct termios port_parameters,port_read_parameters;
//INSIDE THE FUNCTION I WILL BE DOING ALL THESE.....SAY AFTER //OPENING A PARTICULAR COMPORT DEVICE...fd = open ("/dev/ttyS0", //O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY);
switch ( speed)
{
case B_9600:
port_parameters.c_cflag = B9600;
break;
case B_19200:
port_parameters.c_cflag = B19200;
break;
case B_38400:
port_parameters.c_cflag = B38400;
break;
case B_57600:
port_parameters.c_cflag = B57600;
break;
case B_115200:
port_parameters.c_cflag = B115200;
break;
case B_NONE:
default:
// we may have to log an error message to log server LogAddEntry() ??
port_parameters.c_cflag = B9600;
break;
}
switch ( no_bits)
{
case D_7:
port_parameters.c_cflag = port_parameters.c_cflag | CS7;
break;
case D_8:
case D_NONE:
default:
port_parameters.c_cflag = port_parameters.c_cflag | CS8;
break;
}
switch ( parity)
{
case 1:
port_parameters.c_cflag = port_parameters.c_cflag | PARENB;
// port_parameters.c_iflag = ICRNL;
break;
case -1:
port_parameters.c_cflag = port_parameters.c_cflag | PARENB | PARODD;
// port_parameters.c_iflag = ICRNL;
break;
case 0:
default:
// port_parameters.c_iflag = IGNPAR | ICRNL;
port_parameters.c_iflag = IGNPAR;
// port_parameters.c_iflag &= ~ICRNL;
break;
}
port_parameters.c_iflag &= ~ICRNL;
if ( stop_bits == 2)
port_parameters.c_cflag = port_parameters.c_cflag | CSTOPB;
port_parameters.c_cflag = port_parameters.c_cflag | CLOCAL | CREAD;
port_parameters.c_oflag = 0;
port_parameters.c_lflag = 0; /*ICANON;*/
port_parameters.c_cc[VMIN]=1;
port_parameters.c_cc[VTIME]=0;
/* clean port */
tcflush(fd, TCIFLUSH);
fcntl(fd, F_SETFL, FASYNC);
//after doing all these things i will be doing like this...
/* activate the settings port */
if (tcsetattr(fd,TCSANOW,&port_parameters) <0)
{
printf("Setting error \n");
//LogMessage(COM_SET_PARM_ERROR,(UCHAR8) com_port);
CloseDevice(fd, com_port);
return ERROR;
}
/* clean I & O device */
tcflush(fd,TCIOFLUSH); ///check the neccesity
if (tcgetattr (fd,& port_read_parameters) < 0)
{
printf("error to read old setting \n");
//LogMessage(COM_READ_PARM_ERROR,(UCHAR8) com_port);
return ERROR;
}
//finally if i want to read those things i wil be doing like this..
printf("new c_iflag = %x \n", port_read_parameters.c_iflag);
printf("new c_oflag = %x \n", port_read_parameters.c_oflag);
printf("new c_cflag = %x \n", port_read_parameters.c_cflag);
printf("new c_lflag = %x \n", port_read_parameters.c_lflag);
//all of which is defined in termios.h
similarly if i want to configure the subnet mask default gateway and other things(i.e. for an ethernet port..)..through a C code how to achieve that....please help me in achieving this..or some other good ideas..
thanks...
In the above program i have just given the important things what i am doing.. many variables and simple things may not have inclucded in the code.....
There is way, way too much involved to explain it all through forums like this one. Google if you want. But the very best info is available if you're willing to shell out a few bucks for a book. The book is UNIX Network Programming, Volume I, by W. Richard Stevens of happy memory:
thanks for reply wjevans_7d1@yahoo.co....
i will go by ur words i will go step by step...and i don have a credit card to purchase that book and also that is not available in many book shops here....anyway i will first try to read the IP address of my system.....i googled and altered a code... (socket.c)... please please tell me what i am doing is correct or not( to read the IP address) in this code ?? or the code here doesnt mean anything ??? or what i am thinking is not correct ??? (to read the IP address)
Code:
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include<stdio.h>
#include <net/if.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
//int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp)
int main()
{
int fd;
struct ifreq ifr;
struct sockaddr_in *our_ip;
int *addr;
memset(&ifr, 0, sizeof(struct ifreq));
if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0)
{
ifr.ifr_addr.sa_family = AF_INET;
//strcpy(ifr.ifr_name, interface);
strcpy(ifr.ifr_name, "simple_test");
if (addr)
{
printf(" 1) i am correct till here \n ");
if (ioctl(fd, SIOCGIFADDR, &ifr) == 0)
{
printf(" 2) i am correct till here \n ");
our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
printf(" 3) i am correct till here \n ");
*addr = our_ip->sin_addr.s_addr;
printf("%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr));
}
else
{
perror("SIOCGIFADDR failed, is the interface up and configured?");
return -1;
}
}
}
}
Most bookstores that I've patronized will be happy to order a book for you if they don't have it in stock. That way you can pay with cash or a check. No credit card needed.
I really, really recommend that you try doing that. You will get going much more quickly that way. This stuff is not rocket science, but it is more complicated than a conversation in linuxquestions can teach without involving a LOT of time.
Once you're really going on this, if you have specific questions, do come back here with them.
In the meantime, what happens when you run this program?
thanks for reply....
ya i will surely purchase that book then...
and in the code above it is not at all entering the ioctl if statement....
so, please gimme some piece of code to get the IP of the system and other details....
In linuxquestions and other such forums, the etiquette is that the asker of a question does as much of the work as possible, so that the answerer can devote his or her time answering as many questions as possible, or perhaps spending some of his or her life away from that forum or even computers in general.
So "please gimme some piece of code" is, unfortunately, inconvenient.
Tell ya what, though. Though I'm not willing to write your code for you, I am willing to spend a bit of time looking at your code. It's worthwhile, perhaps, for you to work on that code while waiting for your book to arrive.
When did the bookseller say it would arrive?
Anyway, you say that the code never executes the ioctl() call. As I glance at it, I see that the surrounding block of code is introduced with this:
Code:
if (addr)
... and, while addr is declared, it is never assigned a value before you test its value.
In your efforts to debug your code, you would do well to start with that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.