LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2007, 12:48 AM   #1
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Rep: Reputation: 32
code to configure ethernet ports....


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.....

Last edited by culin; 03-29-2007 at 12:51 AM.
 
Old 03-29-2007, 07:20 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
You'll be using ioctl(). Do this:

Code:
man 2 ioctl
That alone won't get you very far, but you need that as background. The next step is to explore the various ioctl calls you might be doing:

Code:
SIOCGIFCONF
SIOCGIFADDR
SIOCSIFADDR
SIOCGIFFLAGS
SIOCSIFFLAGS
SIOCGIFDSTADDR
SIOCSIFDSTADDR
SIOCGIFBRDADDR
SIOCSIFBRDADDR
SIOCGIFNETMASK
SIOCSIFNETMASK
SIOCGIFMETRIC
SIOCSIFMETRIC
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:

http://www.aw-bc.com/catalog/academi...411551,00.html

Hope this helps.
 
Old 03-29-2007, 09:27 AM   #3
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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;
                        }
                }
        }
}
THANKS AGAIN.....
 
Old 03-30-2007, 08:05 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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?
 
Old 04-02-2007, 12:35 AM   #5
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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....
 
Old 04-03-2007, 07:20 AM   #6
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.

Good luck.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c code for accessing ethernet ports gaurav jindal Linux - Networking 2 01-18-2007 08:20 PM
Ethernet ports not working on new system aaronvegh Linux - Networking 8 06-30-2006 06:18 AM
ARP responses on all ethernet ports - ignoring ip address slackjames Linux - Networking 3 05-12-2006 12:24 AM
How do I configure ports? M O L8ingN2dust Linux - Security 1 11-01-2005 02:17 PM
Fedora, e1000 driver, 6 ethernet ports pim42 Linux - Hardware 0 06-03-2004 05:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 11:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration