Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

How to send a link-layer packet in C?

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
iamminga
Asked:
iamminga
1 Solution
 
MHuangCommented:
You can certainly do socket programming using C in Linux.  This is one site I found useful:

http://www.linuxsocket.org/
0
 
sarda_rameshCommented:
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
 
cjs2895Commented:
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
 
sarda_rameshCommented:
//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
 
jmcgOwnerCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now