?
Solved

How to send a link-layer packet in C?

Posted on 2003-03-25
6
Medium Priority
?
383 Views
Last Modified: 2010-04-15
Hi,
I am using Linux gcc. How can I write a program that can send a data-link layer packet (MAC layer)?
Is socket programing capable?

Thanks for advising me.

Sarick
0
Comment
Question by:iamminga
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Expert Comment

by:MHuang
ID: 8206576
You can certainly do socket programming using C in Linux.  This is one site I found useful:

http://www.linuxsocket.org/
0
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8208503
Obviously u can do it ..

http://www.maththinking.com/boat/computerbooks.html

try this for a small ref book on socket programming in C
0
 
LVL 1

Accepted Solution

by:
cjs2895 earned 200 total points
ID: 8215983
The procedure for sending and writing link layer packets is different for every operating system, and frequently changes between major releases of the operating systems, and I've even seen cases where direct support for specific devices was required on the part of the usermode program. Many OS vendors are very closed lipped about sending raw packets. In short, it can be a mess.

Therefore I would like to recommend the following two  libraries to you. They are both open source, and actively maintained. If you write your program against these then you can send and receive raw packets from just about any OS and be fairly shielded from everything I described above.

To send raw packets check out libnet at:
http://libnet.sourceforge.net/

To receive raw packets check out libpcap at:
http://www.tcpdump.org/

Both libraries come with example programs. I recall that libnet had a really nice tutorial on how to use it. I believe libpcap has some short example programs that were easy to understand. Most open source sniffers are based one these two libraries.

Good luck!
0
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8216413
//CODE FOR SERVER PROGRAM
//8.2. server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>CS 640 Fall 1994 BSD Socket Reference Page 12
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
main(argc, argv)
int argc;
char *argv[];
{
struct servent *servp;
struct sockaddr_in server, remote;
int request_sock, new_sock;
int nfound, fd, maxfd, bytesread, addrlen;
fd_set rmask, mask;
static struct timeval timeout = { 0, 500000 }; /* one half second */
char buf[BUFSIZ];
if (argc != 2) {
(void) fprintf(stderr,"usage: %s service\n",argv[0]);
exit(1);
}
if ((request_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket");
exit(1);
}
if (isdigit(argv[1][0])) {
static struct servent s;
servp = &s;
s.s_port = htons((u_short)atoi(argv[1]));
} else if ((servp = getservbyname(argv[1], "tcp")) == 0) {
fprintf(stderr,"%s: unknown service\n");
exit(1);
}
memset((void *) &server, sizeof server);
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = servp->s_port;
if (bind(request_sock, (struct sockaddr *)&server, sizeof server) < 0) {
perror("bind");
exit(1);
}
if (listen(request_sock, SOMAXCONN) < 0) {
perror("listen");
exit(1);
}
FD_ZERO(&mask);
FD_SET(request_sock, &mask);
maxfd = request_sock;
for (;;) {
rmask = mask;
nfound = select(maxfd+1, &rmask, (fd_set *)0, (fd_set *)0, &timeout);
if (nfound < 0) {
if (errno == EINTR) {
printf("interrupted system call\n");
continue;
}
/* something is very wrong! */
perror("select");
exit(1);
}
if (nfound == 0) {
/* timeout */
printf("."); fflush(stdout);
continue;
}
if (FD_ISSET(request_sock, &rmask)) {
/* a new connection is available on the connetion socket */
addrlen = sizeof(remote);
new_sock = accept(request_sock,
(struct sockaddr *)&remote, &addrlen);
if (new_sock < 0) {
perror("accept");
exit(1);
}
printf("connection from host %s, port %d, socket %d\n",
inet_ntoa(remote.sin_addr), ntohs(remote.sin_port),
new_sock);
FD_SET(new_sock, &mask);
if (new_sock > maxfd)
maxfd = new_sock;
FD_CLR(request_sock, &rmask);
}
for (fd=0; fd <= maxfd ; fd++) {
/* look for other sockets that have data available */
if (FD_ISSET(fd, &rmask)) {
/* process the data */
bytesread = read(fd, buf, sizeof buf - 1);
if (bytesread<0) {
perror("read");
/* fall through */
}
if (bytesread<=0) {
printf("server: end of file on %d\n",fd);
FD_CLR(fd, &mask);
if (close(fd)) perror("close");
continue;
}
buf[bytesread] = \0;
printf("%s: %d bytes from %d: %s\n",
argv[0], bytesread, fd, buf);
/* echo it back */
if (write(fd, buf, bytesread)!=bytesread)
perror("echo");
}
}
}
} /* main - server.c */



//CODE FOR CLIENT PROGRAM


//8.1. client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <errno.h>
#include <ctype.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
main(argc, argv)
int argc;
char *argv[];
{
struct hostent *hostp;
struct servent *servp;
struct sockaddr_in server;
int sock;
static struct timeval timeout = { 5, 0 }; /* five seconds */
fd_set rmask, xmask, mask;
char buf[BUFSIZ];
int nfound, bytesread;
if (argc != 3) {
(void) fprintf(stderr,"usage: %s service host\n",argv[0]);
exit(1);
}
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket");
exit(1);
}
if (isdigit(argv[1][0])) {
static struct servent s;
servp = &s;
s.s_port = htons((u_short)atoi(argv[1]));
} else if ((servp = getservbyname(argv[1], "tcp")) == 0) {
fprintf(stderr,"%s: unknown service\n",argv[1]);
exit(1);
}
if ((hostp = gethostbyname(argv[2])) == 0) {
fprintf(stderr,"%s: unknown host\n",argv[2]);
exit(1);
}
memset((void *) &server, 0, sizeof server);
server.sin_family = AF_INET;
memcpy((void *) &server.sin_addr, hostp->h_addr, hostp->h_length);
server.sin_port = servp->s_port;
if (connect(sock, (struct sockaddr *)&server, sizeof server) < 0) {
(void) close(sock);
perror("connect");
exit(1);
}
FD_ZERO(&mask);
FD_SET(sock, &mask);
FD_SET(fileno(stdin), &mask);
for (;;) {
rmask = mask;
nfound = select(FD_SETSIZE, &rmask, (fd_set *)0, (fd_set *)0, &timeout);
if (nfound < 0) {
if (errno == EINTR) {
printf("interrupted system call\n");
continue;
}
/* something is very wrong! */
perror("select");
exit(1);
}
if (nfound == 0) {
/* timer expired */
printf("Please type something!\n");
continue;
}
if (FD_ISSET(fileno(stdin), &rmask)) {
/* data from keyboard */
if (!fgets(buf, sizeof buf, stdin)) {
if (ferror(stdin)) {
perror("stdin");
exit(1);
}
exit(0);
}
if (write(sock, buf, strlen(buf)) < 0) {
perror("write");
exit(1);
}
}
if (FD_ISSET(sock,&rmask)) {
/* data from network */
bytesread = read(sock, buf, sizeof buf);
buf[bytesread] = \0;
printf("%s: got %d bytes: %s\n", argv[0], bytesread, buf);
}
}
} /* main - client.c */


0
 
LVL 20

Expert Comment

by:jmcg
ID: 10190937
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by cjs2895.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question