Avatar of vasureddym
vasureddym

asked on 

help me in accessing LAN using C programming

Hi all!
i'm trying to access LAN using C programming.
till now i could access serial port, but i'm not sure how to acess LAN.
could someone help me in this regard!!!!

thanks in advance.

vasu
C

Avatar of undefined
Last Comment
vasureddym
Avatar of PCT
PCT

If you what to send data through your Ethernet card you can use Sockets.
You will have to include some header probably socket.h for unix or winsock.h for windows into your code.

Don't forget to include the required libraries before compiling.


Avatar of stefan73
stefan73
Flag of Germany image

Hi vasureddym,
Depends on what you mean by "access LAN". Low-level access of network card?
Or do you want to access a particular server via SMB?
Do you want to access some TCP/IP service, like FTP?

Cheers,
Stefan
Avatar of sunnycoder
sunnycoder
Flag of India image

ok heres what you need to do

open a socket:


fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
if (fd < 0) {
sprintf(ebuf, "socket: %s", strerror(errno));
goto failed;
}



/* check device type */

memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0 ) {
sprintf(ebuf, "SIOCGIFHWADDR: %s", strerror(errno));
goto failed;
}

if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
sprintf(ebuf, "unsupported physical layer type 0x%x (ethernet
only)",
ifr.ifr_hwaddr.sa_family);
goto failed;
}


memcpy(ethaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);


if you need promisc mode, don't forget to restore device flags.


ifr.ifr_flags |= IFF_PROMISC;
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0 ) {
sprintf(ebuf, "SIOCSIFFLAGS: %s", strerror(errno));
goto failed;
}
atexit(restore_ifr);


/****/


void restore_ifr(void)
{
int fd;


fd = socket(PF_INET, SOCK_PACKET, htons(0x0003));
if (fd < 0)
fprintf(stderr, "linux socket: %s", strerror(errno));
else if (ioctl(fd, SIOCSIFFLAGS, &saved_ifr) < 0)
fprintf(stderr, "linux SIOCSIFFLAGS: %s", strerror(errno));
}


To receive/send stuff, use recvmsg/sendmsg,
look into the concepts of the structs:
msghdr, ethhdr, iovec, and sockaddr_pkt to build/decode packages.

for more info http:20660168.html
Avatar of vasureddym
vasureddym

ASKER

Hi all!

Thanks PCT for your Participation.

Hi Stefan73, all I want is to learn accessing/transferring files on LAN using protocols like TCP/IP.
I tried the following simple program. It, is not making use of any protocols.
thanks for your participation.

Hi sunnycoder, i'm beginner to Network programming in C. terms like 'sockets' are new to me,. can you suggest me something..
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of vasureddym
vasureddym

ASKER

thanks sunny!
i found that link very useful!
i didn't go through it completely. right now i'm  in another project, so i have to keep this topic aside for few days.
however, it is not a good idea to keep this Question for so long time.
i want to close this for the time being.
anyways, thanks
-vasureddym
C
C

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, so it has found lasting use in applications that had formerly been coded in assembly language, including operating systems as well as various application software for computers ranging from supercomputers to embedded systems. It is distinct from C++ (which has its roots in C) and C#, and many later languages have borrowed directly or indirectly from C.

23K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo