Changeset 1778 for trunk/src/common/sendpacket.c
- Timestamp:
- 03/31/07 21:05:16 (22 months ago)
- Files:
-
- 1 modified
-
trunk/src/common/sendpacket.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/common/sendpacket.c
r1773 r1778 81 81 #include <errno.h> 82 82 #include <stdarg.h> 83 #include <stdio.h> 83 84 #include <sys/types.h> 84 85 #include <sys/time.h> … … 129 130 static sendpacket_t *sendpacket_open_libnet(const char *, char *) __attribute__((unused)); 130 131 static struct tcpr_ether_addr *sendpacket_get_hwaddr_libnet(sendpacket_t *) __attribute__((unused)); 131 132 132 #endif /* HAVE_LIBNET */ 133 133 … … 175 175 sendpacket_seterr(sp, "Error with pf send(): %s", strerror(errno)); 176 176 } 177 177 178 178 #elif defined HAVE_BPF 179 179 retcode = write(sp->handle.fd, (void *)data, len); … … 184 184 sendpacket_seterr(sp, "Error with bpf write(): %s", strerror(errno)); 185 185 } 186 186 187 187 #elif defined HAVE_LIBNET 188 188 retcode = libnet_adv_write_link(sp->handle.lnet, (u_int8_t*)data, (u_int32_t)len); … … 194 194 } 195 195 196 #elif defined HAVE_PCAP_INJECT 196 197 /* 197 198 * pcap methods don't seem to support ENOBUFS, so we just straight fail 198 199 * is there a better way??? 199 200 */ 200 #elif defined HAVE_PCAP_INJECT 201 if ((retcode = pcap_inject(sp->handle.pcap, (void*)data, len)) < 0) 202 sendpacket_seterr(sp, "Error with pcap_inject(): %s", pcap_geterr(sp->handle.pcap)); 201 retcode = pcap_inject(sp->handle.pcap, (void*)data, len); 202 if (retcode < 0 && errno == ENOBUFS && !didsig) { 203 sp->retry ++; 204 goto TRY_SEND_AGAIN; 205 } else if (retcode < 0) { 206 sendpacket_seterr(sp, "Error with pcap_inject(packet " COUNTER_SPEC "): %s (%d)", 207 sp->sent + 1, pcap_geterr(sp->handle.pcap), errno); 208 } 203 209 204 210 #elif defined HAVE_PCAP_SENDPACKET 205 if ((retcode = pcap_sendpacket(sp->handle.pcap, data, (int)len)) < 0) 206 sendpacket_seterr(sp, "Error with pcap_sendpacket(): %s", pcap_geterr(sp->handle.pcap)); 207 208 /* 209 * pcap_sendpacket returns 0 on success, not the packet length! 210 * hence, as a special case, update the counters here and return len 211 */ 212 sp->bytes_sent += len; 213 sp->sent ++; 214 return len; 211 retcode = pcap_sendpacket(sp->handle.pcap, data, (int)len); 212 if (retcode < 0 && errno == ENOBUFS && !didsig) { 213 sp->retry ++; 214 goto TRY_SEND_AGAIN; 215 } else if (retcode < 0) { 216 sendpacket_seterr(sp, "Error with pcap_sendpacket(packet " COUNTER_SPEC "): %s", 217 sp->sent + 1, pcap_geterr(sp->handle.pcap)); 218 } else { 219 /* 220 * pcap_sendpacket returns 0 on success, not the packet length! 221 * hence, we have to fix retcode to be more standard on success 222 */ 223 retcode = len; 224 } 225 215 226 #endif 216 227 … … 259 270 260 271 memset(buf, 0, sizeof(buf)); 261 sprintf(buf, "Statistics for network device: %s\n", sp->device); 262 sprintf(buf, "Attempted packets: " COUNTER_SPEC "\n", sp->attempt); 263 sprintf(buf, "Successful packets: " COUNTER_SPEC "\n", sp->sent); 264 sprintf(buf, "Failed packets: " COUNTER_SPEC "\n", sp->failed); 265 sprintf(buf, "Retried packets: " COUNTER_SPEC "\n", sp->retry); 272 sprintf(buf, "Statistics for network device: %s\n" 273 "\tAttempted packets: " COUNTER_SPEC "\n" 274 "\tSuccessful packets: " COUNTER_SPEC "\n" 275 "\tFailed packets: " COUNTER_SPEC "\n" 276 "\tRetried packets: " COUNTER_SPEC "\n", 277 sp->device, sp->attempt, sp->sent, sp->failed, sp->retry); 266 278 return(buf); 267 279 } … … 337 349 pcap_t *pcap; 338 350 sendpacket_t *sp; 339 351 /* 352 u_int spoof_eth_src = 1; 353 int fd; 354 */ 340 355 assert(device); 341 356 assert(errbuf); 357 358 dbg(1, "sendpacket: using Libpcap"); 342 359 343 360 /* open_pcap_live automatically fills out our errbuf for us */ … … 348 365 strlcpy(sp->device, device, sizeof(sp->device)); 349 366 sp->handle.pcap = pcap; 367 /* 368 fd = pcap_get_selectable_fd(pcap); 369 if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) { 370 errx(1, "Unable to enable source MAC spoof support: %s", strerror(errno)); 371 } 372 */ 350 373 return sp; 351 374 } … … 369 392 assert(device); 370 393 assert(errbuf); 394 395 dbg(1, "sendpacket: using Libnet"); 371 396 372 397 if ((lnet = libnet_init(LIBNET_LINK_ADV, device, errbuf)) == NULL) … … 411 436 assert(errbuf); 412 437 438 dbg(1, "sendpacket: using PF_PACKET"); 439 413 440 /* open our socket */ 414 441 if ((mysocket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) { … … 568 595 memset(&ifr, '\0', sizeof(struct ifreq)); 569 596 597 dbg(1, "sendpacket: using BPF"); 570 598 /* open socket */ 571 599 mysocket = -1; … … 720 748 { 721 749 int dlt; 722 #if defined HAVE_PF_PACKET || defined HAVE_LIBNET 750 #if defined HAVE_BPF 751 int rcode; 752 753 if ((rcode = ioctl(sp->handle.fd, BIOCGDLT, &dlt)) < 0) { 754 warnx("Unable to get DLT value for BPF device (%s): %s", sp->device, strerror(errno)); 755 return(-1); 756 } 757 #elif defined HAVE_PF_PACKET || defined HAVE_LIBNET 723 758 /* use libpcap to get dlt */ 724 759 pcap_t *pcap; … … 730 765 dlt = pcap_datalink(pcap); 731 766 pcap_close(pcap); 732 #elif defined HAVE_BPF 733 int rcode; 734 735 if ((rcode = ioctl(sp->handle.fd, BIOCGDLT, &dlt)) < 0) { 736 warnx("Unable to get DLT value for BPF device (%s): %s", sp->device, strerror(errno)); 737 return(-1); 738 } 739 #else /* HAVE_PCAP_SENDPACKET / HAVE_PCAP_INJECT */ 767 #elif defined HAVE_PCAP_SENDPACKET || defined HAVE_PCAP_INJECT 740 768 dlt = pcap_datalink(sp->handle.pcap); 741 769 #endif 742 770 return dlt; 743 771 } 744 745
