[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
Communications Programming Concepts

Receiving Packets Over Ethernet Example Program

#include <stdio.h>
#include <sys/ndd_var.h>
#include <sys/kinfo.h>
/*
 * Get the MAC address of the ethernet adapter we're using...
 */
getaddr(char *device, char *addr) 
{
          int size;
          struct kinfo_ndd *nddp;
          void *end;
          int found = 0;
          size = getkerninfo(KINFO_NDD, 0, 0, 0);
          if (size == 0) {
                    fprintf(stderr, "No ndds.\n");
                    exit(0);
          }
          
          if (size < 0) {
                    perror("getkerninfo 1");
                    exit(1);
          }
          nddp = (struct kinfo_ndd *)malloc(size);
          
          if (!nddp) {
                    perror("malloc");
                    exit(1);
          }
          if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
                    perror("getkerninfo 2");
                    exit(2);
          }
          end = (void *)nddp + size;
          while (((void *)nddp < end) && !found) {
                    if (!strcmp(nddp->ndd_alias, device) ||
                              !strcmp(nddp->ndd_name, device)) {
                              found++;
                              bcopy(nddp->ndd_addr, addr, 6);
                    } else
                              nddp++;
          }
          return (found);
}
/*
 * Hex print function...
 */
pit(str, buf, len)
u_char *str;
u_char *buf;
int len;
{
          int i;
          printf("%s", str);
          for (i=0; i<len; i++)
                    printf("%2.2X", buf[i]);
          printf("\n");
          fflush(stdout);
}
/*
 * Ethernet packet format...
 */
typedef struct {
          unsigned char          dst[6];
          unsigned char          src[6];
          unsigned short         ethertype;
          unsigned char          data[1500];
} xmit;
main(int argc, char *argv[]) {
          char *device;
          u_int ethertype;
          xmit buf;
          int s;
          struct sockaddr_ndd_8022 sa;
          int cc;
          if (argc != 3) {
                    printf("Usage: %s <ifname> ethertype\n", argv[0]);
                    printf("EG:    %s ent0 0x600\n", argv[0]);
                    exit(1);
          }
          device = argv[1];
          sscanf(argv[2], "%x", &ethertype);
          printf("Ethertype: %x\n", ethertype);
          s = socket(AF_NDD, SOCK_DGRAM, NDD_PROT_ETHER);
          if (s < 0) {
                    perror("socket");
                    exit(1);
          }
          sa.sndd_8022_family = AF_NDD;
          sa.sndd_8022_len = sizeof(sa);
          sa.sndd_8022_filtertype = NS_ETHERTYPE;
          sa.sndd_8022_ethertype = (u_short)ethertype;
          sa.sndd_8022_filterlen = sizeof(struct ns_8022);
          bcopy(device, sa.sndd_8022_nddname, sizeof(sa.sndd_8022_nddname));
          if (bind(s, (struct sockaddr *)&sa, sizeof(sa))) {
                    perror("bind");
                    exit(2);
          }
        if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
                perror("connect");
                exit(3);
        }
        do {
                if ((cc = read(s, &buf, sizeof(buf))) < 0) {
                        perror("write");
                        exit(4);
                }
                    if (cc) {
                              printf("Read %d bytes:\n", cc);
                              pit("\tsrc = ", buf.src, 6);
                              pit("\tdst = ", buf.dst, 6);
                              pit("\ttype = ", &(buf.ethertype), 2);
                              printf("\tdata string: %s\n", buf.data);
                    }
                         } while (cc > 0);
          close(s);
}

[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]