Skip to contentSkip to conversation

Creating a routing program - the basics

DISCUSSION Mateus Interciso 3 participants 6 messages Oct 23, 2007 – Nov 2, 2007

lore
Share & tools
lore
#0 lore raw

Hello, I'm currently on university, and for my graduation paper, I'll be implementing a routing algorythm, by hand, in C.
The routing algorythm itself is not easy, but one of the starting parts of it, is to find a way of discovering the MAC that it is currently connected to, maybe is bether if I draw, since I'm not really that good in english:

[PC with n NICs]-----(n cables)---->[n PCs]

I have full control of the first PC, of course, since is the one I'm implementing the router, so I do know the MAC of it, but how to discover the MAC on the other end of the n lines, so I can start sending packets? Anyone could help me impĺementing this exact thing? I think I got the theory right, but I have no clue how to start programing.

Thanks a lot.

Mateus

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#1 lore raw
Quoted 7 lines · Mateus Interciso

Hello, I'm currently on university, and for my graduation paper, I'll be implementing a routing algorythm, by hand, in C.
The routing algorythm itself is not easy, but one of the starting parts of it, is to find a way of discovering the MAC that it is currently connected to, maybe is bether if I draw, since I'm not really that good in english:

Quoted 5 lines

[PC with n NICs]-----(n cables)---->[n PCs]

I have full control of the first PC, of course, since is the one I'm implementing the router, so I do know the MAC of it, but how to discover the MAC on the other end of the n lines, so I can start sending packets?

Probably this is a starting point: (not tested)

___ BEGIN SOURCE ___

#include <iostream>
#include <stdio.h>
#include <netdb.h>
#include <netinet/if_ether.h>

int read_mac(void) {
	int sock, sockfd, n, cnt;
	char buffer[2048];
	unsigned char *iphead, *ethhead;
	struct ether_addr ether;

	if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0) {
		perror("socket");
		exit(1);
	}

	while (1) {
		if ((n = recvfrom(sock, buffer, 2048, 0, NULL, NULL)) == -1) {
			perror("recvfrom");
			close(sock);
			exit(1);
		}
	
		ethhead = (unsigned char *)buffer;
		
		if (ethhead != NULL) {
			iphead = (unsigned char *)(buffer + 14); // Skip Ethernet header			
			printf("Peer MAC: "
				"%02x:%02x:%02x:%02x:%02x:%02x\n",
				ethhead[0], ethhead[1], ethhead[2],
				ethhead[3], ethhead[4], ethhead[5]);
			printf("Source MAC: "
				"%02x:%02x:%02x:%02x:%02x:%02x\n",
				ethhead[6], ethhead[7], ethhead[8],
				ethhead[9], ethhead[10], ethhead[11]);

			if (*iphead == 0x45) { // Check for IPv4, no options present
				printf("Peer IP: %d.%d.%d.%d\n",
					iphead[12], iphead[13],
					iphead[14], iphead[15]);
				printf("Source IP: %d.%d.%d.%d\n",
					iphead[16], iphead[17],
					iphead[18], iphead[19]);
				printf("Protocol (UDP = 11): %02x Hex\n", iphead[9]);
			}
		}
	}
	
	return 0;
}
int main(int argc, char **argv) {
    read_mac();
    return EXIT_SUCCESS;
}

___ END SOURCE ___

Honestly, you should make yourself acquainted with low-level network programming. Otherwise you'll be asking a lot of theses questions in the future. Anyway, hope this helps.

\Steve

--

Steve Grägert
DigitalEther.de
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 23 lines · Steve Graegert
Quoted 6 lines · Mateus Interciso

Hello, I'm currently on university, and for my graduation paper, I'll be implementing a routing algorythm, by hand, in C. The routing algorythm itself is not easy, but one of the starting parts of it, is to find a way of discovering the MAC that it is currently connected to, maybe is bether if I draw, since I'm not really that good in english:

Quoted 6 lines

[PC with n NICs]-----(n cables)---->[n PCs]

I have full control of the first PC, of course, since is the one I'm implementing the router, so I do know the MAC of it, but how to discover the MAC on the other end of the n lines, so I can start sending packets?

Probably this is a starting point: (not tested)

___ BEGIN SOURCE ___

#include <iostream>
#include <stdio.h>
#include <netdb.h>
#include <netinet/if_ether.h>

Quoted 13 lines
int read_mac(void) {
	int sock, sockfd, n, cnt;
	char buffer[2048];
	unsigned char *iphead, *ethhead;
	struct ether_addr ether;

	if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0) {
		perror("socket");
		exit(1);
	}

	while (1) {
		if ((n = recvfrom(sock, buffer, 2048, 0, NULL, NULL)) == 

-1) {

Quoted 9 lines
			perror("recvfrom");
			close(sock);
			exit(1);
		}
	
		ethhead = (unsigned char *)buffer;
		
		if (ethhead != NULL) {
			iphead = (unsigned char *)(buffer + 14); // Skip 

Ethernet header

Quoted 10 lines
			printf("Peer MAC: "
				"%02x:%02x:%02x:%02x:%02x:%02x\n",
				ethhead[0], ethhead[1], ethhead[2],
				ethhead[3], ethhead[4], ethhead[5]);
			printf("Source MAC: "
				"%02x:%02x:%02x:%02x:%02x:%02x\n",
				ethhead[6], ethhead[7], ethhead[8],
				ethhead[9], ethhead[10], ethhead[11]);

			if (*iphead == 0x45) { // Check for IPv4, no 

options present

Quoted 7 lines
				printf("Peer IP: %d.%d.%d.%d\n",
					iphead[12], iphead[13],
					iphead[14], iphead[15]);
				printf("Source IP: %d.%d.%d.%d\n",
					iphead[16], iphead[17],
					iphead[18], iphead[19]);
				printf("Protocol (UDP = 11): %02x Hex\n", 

iphead[9]);

Quoted 23 lines
			}
		}
	}
	
	return 0;
}
int main(int argc, char **argv) {
    read_mac();
    return EXIT_SUCCESS;
}

___ END SOURCE ___

Honestly, you should make yourself acquainted with low-level network programming. Otherwise you'll be asking a lot of theses questions in the future. Anyway, hope this helps.

\Steve

--

Quoted 7 lines

Steve Grägert
DigitalEther.de
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html

Thank you, this will help.
Do you have any book/site where I can take a deep look into how to do that stuff?

MAteus

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 99 lines · Mateus Interciso
Quoted 23 lines · Steve Graegert
Quoted 6 lines · Mateus Interciso

Hello, I'm currently on university, and for my graduation paper, I'll be implementing a routing algorythm, by hand, in C. The routing algorythm itself is not easy, but one of the starting parts of it, is to find a way of discovering the MAC that it is currently connected to, maybe is bether if I draw, since I'm not really that good in english:

Quoted 6 lines

[PC with n NICs]-----(n cables)---->[n PCs]

I have full control of the first PC, of course, since is the one I'm implementing the router, so I do know the MAC of it, but how to discover the MAC on the other end of the n lines, so I can start sending packets?

Probably this is a starting point: (not tested)

___ BEGIN SOURCE ___

#include <iostream>
#include <stdio.h>
#include <netdb.h>
#include <netinet/if_ether.h>

Quoted 13 lines
int read_mac(void) {
      int sock, sockfd, n, cnt;
      char buffer[2048];
      unsigned char *iphead, *ethhead;
      struct ether_addr ether;

      if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0) {
              perror("socket");
              exit(1);
      }

      while (1) {
              if ((n = recvfrom(sock, buffer, 2048, 0, NULL, NULL)) ==

-1) {

Quoted 9 lines
                      perror("recvfrom");
                      close(sock);
                      exit(1);
              }

              ethhead = (unsigned char *)buffer;

              if (ethhead != NULL) {
                      iphead = (unsigned char *)(buffer + 14); // Skip

Ethernet header

Quoted 10 lines
                      printf("Peer MAC: "
                              "%02x:%02x:%02x:%02x:%02x:%02x\n",
                              ethhead[0], ethhead[1], ethhead[2],
                              ethhead[3], ethhead[4], ethhead[5]);
                      printf("Source MAC: "
                              "%02x:%02x:%02x:%02x:%02x:%02x\n",
                              ethhead[6], ethhead[7], ethhead[8],
                              ethhead[9], ethhead[10], ethhead[11]);

                      if (*iphead == 0x45) { // Check for IPv4, no

options present

Quoted 7 lines
                              printf("Peer IP: %d.%d.%d.%d\n",
                                      iphead[12], iphead[13],
                                      iphead[14], iphead[15]);
                              printf("Source IP: %d.%d.%d.%d\n",
                                      iphead[16], iphead[17],
                                      iphead[18], iphead[19]);
                              printf("Protocol (UDP = 11): %02x Hex\n",

iphead[9]);

Quoted 23 lines
                      }
              }
      }

      return 0;
}
int main(int argc, char **argv) {
    read_mac();
    return EXIT_SUCCESS;
}

___ END SOURCE ___

Honestly, you should make yourself acquainted with low-level network programming. Otherwise you'll be asking a lot of theses questions in the future. Anyway, hope this helps.

\Steve

--

Quoted 7 lines

Steve Grägert
DigitalEther.de
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html

Quoted 3 lines

Thank you, this will help.
Do you have any book/site where I can take a deep look into how to do that stuff?

I can't make any recommendations beyond the well-known programming books like Steven's UNP and O'Reilly's "Understanding the Linux Kernel". Additionally, you may find Sams Publishing 's "Linux(R) Socket Programming" (0672319357) useful.

\Steve

--

Steve Grägert
DigitalEther.de
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 118 lines · Steve Graegert
Quoted 104 lines · Mateus Interciso
Quoted 24 lines · Steve Graegert
Quoted 7 lines · Mateus Interciso

Hello, I'm currently on university, and for my graduation paper, I'll be implementing a routing algorythm, by hand, in C. The routing algorythm itself is not easy, but one of the starting parts of it, is to find a way of discovering the MAC that it is currently connected to, maybe is bether if I draw, since I'm not really that good in english:

Quoted 6 lines

[PC with n NICs]-----(n cables)---->[n PCs]

I have full control of the first PC, of course, since is the one I'm implementing the router, so I do know the MAC of it, but how to discover the MAC on the other end of the n lines, so I can start sending packets?

Probably this is a starting point: (not tested)

___ BEGIN SOURCE ___

#include <iostream>
#include <stdio.h>
#include <netdb.h>
#include <netinet/if_ether.h>

Quoted 15 lines
int read_mac(void) {
      int sock, sockfd, n, cnt;
      char buffer[2048];
      unsigned char *iphead, *ethhead;
      struct ether_addr ether;

      if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0)
      {
              perror("socket");
              exit(1);
      }

      while (1) {
              if ((n = recvfrom(sock, buffer, 2048, 0, NULL, NULL))
              ==

-1) {

Quoted 10 lines
                      perror("recvfrom");
                      close(sock);
                      exit(1);
              }

              ethhead = (unsigned char *)buffer;

              if (ethhead != NULL) {
                      iphead = (unsigned char *)(buffer + 14); //
                      Skip

Ethernet header

Quoted 10 lines
                      printf("Peer MAC: "
                              "%02x:%02x:%02x:%02x:%02x:%02x\n",
                              ethhead[0], ethhead[1], ethhead[2],
                              ethhead[3], ethhead[4], ethhead[5]);
                      printf("Source MAC: "
                              "%02x:%02x:%02x:%02x:%02x:%02x\n",
                              ethhead[6], ethhead[7], ethhead[8],
                              ethhead[9], ethhead[10], ethhead[11]);

                      if (*iphead == 0x45) { // Check for IPv4, no

options present

Quoted 8 lines
                              printf("Peer IP: %d.%d.%d.%d\n",
                                      iphead[12], iphead[13],
                                      iphead[14], iphead[15]);
                              printf("Source IP: %d.%d.%d.%d\n",
                                      iphead[16], iphead[17],
                                      iphead[18], iphead[19]);
                              printf("Protocol (UDP = 11): %02x
                              Hex\n",

iphead[9]);

Quoted 23 lines
                      }
              }
      }

      return 0;
}
int main(int argc, char **argv) {
    read_mac();
    return EXIT_SUCCESS;
}

___ END SOURCE ___

Honestly, you should make yourself acquainted with low-level network programming. Otherwise you'll be asking a lot of theses questions in the future. Anyway, hope this helps.

\Steve

--

Quoted 7 lines

Steve Grägert
DigitalEther.de
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html

Quoted 3 lines

Thank you, this will help.
Do you have any book/site where I can take a deep look into how to do that stuff?

I can't make any recommendations beyond the well-known programming books like Steven's UNP and O'Reilly's "Understanding the Linux Kernel". Additionally, you may find Sams Publishing 's "Linux(R) Socket Programming" (0672319357) useful.

\Steve

--

Quoted 7 lines

Steve Grägert
DigitalEther.de
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html

Thank you, I'll take a look at those books :D

Mateus

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 128 lines · Mateus Interciso
Quoted 118 lines · Steve Graegert
Quoted 104 lines · Mateus Interciso
Quoted 24 lines · Steve Graegert
Quoted 7 lines · Mateus Interciso

Hello, I'm currently on university, and for my graduation paper, I'll be implementing a routing algorythm, by hand, in C. The routing algorythm itself is not easy, but one of the starting parts of it, is to find a way of discovering the MAC that it is currently connected to, maybe is bether if I draw, since I'm not really that good in english:

Quoted 6 lines

[PC with n NICs]-----(n cables)---->[n PCs]

I have full control of the first PC, of course, since is the one I'm implementing the router, so I do know the MAC of it, but how to discover the MAC on the other end of the n lines, so I can start sending packets?

Probably this is a starting point: (not tested)

___ BEGIN SOURCE ___

#include <iostream>
#include <stdio.h>
#include <netdb.h>
#include <netinet/if_ether.h>

Quoted 15 lines
int read_mac(void) {
      int sock, sockfd, n, cnt;
      char buffer[2048];
      unsigned char *iphead, *ethhead;
      struct ether_addr ether;

      if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0)
      {
              perror("socket");
              exit(1);
      }

      while (1) {
              if ((n = recvfrom(sock, buffer, 2048, 0, NULL, NULL))
              ==

-1) {

Quoted 10 lines
                      perror("recvfrom");
                      close(sock);
                      exit(1);
              }

              ethhead = (unsigned char *)buffer;

              if (ethhead != NULL) {
                      iphead = (unsigned char *)(buffer + 14); //
                      Skip

Ethernet header

Quoted 10 lines
                      printf("Peer MAC: "
                              "%02x:%02x:%02x:%02x:%02x:%02x\n",
                              ethhead[0], ethhead[1], ethhead[2],
                              ethhead[3], ethhead[4], ethhead[5]);
                      printf("Source MAC: "
                              "%02x:%02x:%02x:%02x:%02x:%02x\n",
                              ethhead[6], ethhead[7], ethhead[8],
                              ethhead[9], ethhead[10], ethhead[11]);

                      if (*iphead == 0x45) { // Check for IPv4, no

options present

Quoted 8 lines
                              printf("Peer IP: %d.%d.%d.%d\n",
                                      iphead[12], iphead[13],
                                      iphead[14], iphead[15]);
                              printf("Source IP: %d.%d.%d.%d\n",
                                      iphead[16], iphead[17],
                                      iphead[18], iphead[19]);
                              printf("Protocol (UDP = 11): %02x
                              Hex\n",

iphead[9]);

Quoted 23 lines
                      }
              }
      }

      return 0;
}
int main(int argc, char **argv) {
    read_mac();
    return EXIT_SUCCESS;
}

___ END SOURCE ___

Honestly, you should make yourself acquainted with low-level network programming. Otherwise you'll be asking a lot of theses questions in the future. Anyway, hope this helps.

\Steve

--

Quoted 7 lines

Steve Grägert
DigitalEther.de
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html

Quoted 3 lines

Thank you, this will help.
Do you have any book/site where I can take a deep look into how to do that stuff?

I can't make any recommendations beyond the well-known programming books like Steven's UNP and O'Reilly's "Understanding the Linux Kernel". Additionally, you may find Sams Publishing 's "Linux(R) Socket Programming" (0672319357) useful.

\Steve

--

Quoted 7 lines

Steve Grägert
DigitalEther.de
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html

Quoted 3 lines

Thank you, I'll take a look at those books :D

Mateus

Hi,

I would most certainly in the first place go for the following books by Richard Stevens:

# TCP/IP Illustrated, Volume 1: The Protocols, Addison-Wesley, 1994.
# TCP/IP Illustrated, Volume 2: The Implementation, Addison-Wesley, 1995.
# TCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols, Addison-Wesley, 1996. # UNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTI, Prentice Hall, 1998. # UNIX Network Programming, Volume 2, Second Edition: Interprocess Communications, Prentice Hall, 1999.

for a complete list of Richard Steven's books:
http://www.kohala.com/start/#books
or amazon...

Than the source-code of libpcap [tcpdump] is always very helpful.

Maybe also another suggestion;
Effective TCP/IP programming by Snader [addison wesley]

But the R.Stevens books are definitely the way to go if starting out with C topics like you've describe above.

GoodLuck.

J.

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html