1 /* $Id: socket.c,v 1.1 2005/12/12 18:14:22 nobunaga Exp $
3 Copyright (C) 2002 Marc Kirchner <kirchner@stud.fh-heilbronn.de>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 @name int pkt_socket_open(struct pkt_socket *sck, int type)
25 @desc Opens a packet socket. This is necessary to be able to send any packages.
26 @param sck pointer to a pkt_socket structure
27 @param type The type of the socket. Possible values are PKT_RAW (to open a raw socket. IP, TCP/UDP and application header have to be forged before sending) , PKT_STREAM, PKT_DGRAM. STREAM and DGRAM are supported by this function, but other function do not care.
31 pkt_socket_open(struct pkt_socket *sck, int type)
40 /* get mem for sockaddr structure */
41 if ((sck->sckad = (struct sockaddr_in *) malloc (sizeof(struct sockaddr_in))) == NULL) {
44 sck->sckad_len = sizeof(struct sockaddr_in);
45 memset(sck->sckad, 0, sck->sckad_len);
47 /* except for raw sockets, proto is set to 0 */
48 /* (Stevens, UNP 2nd ed., 1998) */
57 if ((sck->rawfd = socket(AF_INET, type, proto)) == -1) {
60 if (type == PKT_RAW) {
61 if ((ret = setsockopt(sck->rawfd, IPPROTO_IP, IP_HDRINCL, (const void *) &iphdrincl, sizeof(iphdrincl))) < 0) {
71 @name int pkt_socket_close(struct pkt_socket *sck)
72 @desc Closes a packet socket and frees used memory.
73 @param sck pointer to a pkt_socket structure
77 pkt_socket_close(struct pkt_socket *sck)
89 @name int pkt_socket_prepare(struct pkt_socket *sck, char *daddr)
90 @desc this function is necessary to enable the kernel to determine the correct outgoing interface
91 @param sck pointer to a pkt_socket structure
92 @param daddr dotted-decimal destination IP address
96 pkt_socket_prepare(struct pkt_socket *sck, char *daddr)
99 * this is necessary for the kernel to determine outgoing
108 /* set up sockaddr struct */
109 memset(sck->sckad, 0, sck->sckad_len);
110 if ((ret = inet_pton(AF_INET, daddr, &(sck->sckad->sin_addr))) == 0) {
113 sck->sckad->sin_family = AF_INET;
116 * i dont think we need to set the dest port...
123 @name int pkt_socket_setopt(struct pkt_socket *sck, int level, int optname, void *optval, socklen_t optlen)
124 @desc this is basically a wrapper function for setsockopt(2)
125 @param sck pointer to a pkt_socket structure
126 @param level level the socket option will apply to (should be SOL_SOCKET)
127 @param optname option name, see sys/socket.h for values
128 @param optval new option value (for boolean options: 0=false)
129 @param optlen option value size (this is a value-result parameter!)
133 pkt_socket_setopt(struct pkt_socket *sck, int level,
134 int optname, void *optval, socklen_t optlen)
138 return (setsockopt(sck->rawfd, level, optname, optval, optlen));
144 @name int pkt_send(struct packet *pkt, struct pkt_socket *sck)
145 @desc sends the packet pkt unsing the socket sck
146 @param pkt pointer to a packet structure
147 @param sck pointer to a pkt_socket structure
151 pkt_send(struct packet *pkt, struct pkt_socket *sck)
158 /* no sendto-flags support */
159 if ((ret = sendto(sck->rawfd, pkt->pkt, pkt->pkt_size, 0, (struct sockaddr *) sck->sckad, sck->sckad_len)) == -1) {