Search |
Scout Module APISocket APIThe safe raw socket API uses the standard Linux socket API with some minor semantic differences. Just as in standard Linux, first the socket must be created with the socket system call. To create a safe raw socket, the domain of the call must be set to PF_INET, the type to SOCK_RAW, and the protocol can be one of the following:
sock = socket(PF_INET, SOCK_RAW, IPPROTO_TCP); Once the socket is created, it is necessary to bind it to a particular local port of the specified protocol (or identifier in the case of an ICMP socket). The standard Linux bind system call is used. To bind the socket created above to local TCP port 9090:
struct sockaddr_in sin;
memset(& sin, 0, sizeof(sin));
sin.sin_port = htons(9090);
bind(sock, (struct sockaddr *)& sin, sizeof(sin));
After the socket has been bound to a local port, it is ready to be used to send and receive data. The usual sendto, sendmsg, recv, recvfrom, recvmsg and select calls can be used (note that the send call is not supported for raw sockets). Packets received on a safe raw socket include the IP and TCP/UDP/ICMP headers, but not the link layer protocol. By default, packets sent on a raw socket include the TCP/UDP/ICMP header but not the IP header. To send packets that include the IP header, the IP_HDRINCL socket option must be set on the socket. This call will succeed only after a successful bind:
int tmp = 1;
setsockopt(sock, 0, IP_HDRINCL, & tmp, sizeof(tmp));
To close a safe raw socket, use the usual close system call. |
PlanetLab loginAnnouncements
|