Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

ip-win32.c

Go to the documentation of this file.
00001 /*
00002  * ip-win32.c
00003  *
00004  * Copyright (c) 2002 Dug Song <dugsong@monkey.org>
00005  *
00006  * $Id: ip-win32.c,v 1.5 2005/01/25 21:30:40 dugsong Exp $
00007  */
00008 
00009 #include "config.h"
00010 
00011 #include <ws2tcpip.h>
00012 
00013 #include <errno.h>
00014 #include <stdlib.h>
00015 
00016 #include "dnet.h"
00017 
00018 struct ip_handle {
00019         WSADATA                 wsdata;
00020         SOCKET                  fd;
00021         struct sockaddr_in      sin;
00022 };
00023 
00024 ip_t *
00025 ip_open(void)
00026 {
00027         BOOL on;
00028         ip_t *ip;
00029 
00030         if ((ip = calloc(1, sizeof(*ip))) != NULL) {
00031                 if (WSAStartup(MAKEWORD(2, 2), &ip->wsdata) != 0) {
00032                         free(ip);
00033                         return (NULL);
00034                 }
00035                 if ((ip->fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) ==
00036                     INVALID_SOCKET)
00037                         return (ip_close(ip));
00038                 
00039                 on = TRUE;
00040                 if (setsockopt(ip->fd, IPPROTO_IP, IP_HDRINCL,
00041                         (const char *)&on, sizeof(on)) == SOCKET_ERROR) {
00042                         SetLastError(ERROR_NETWORK_ACCESS_DENIED);
00043                         return (ip_close(ip));
00044                 }
00045                 ip->sin.sin_family = AF_INET;
00046                 ip->sin.sin_port = htons(666);
00047         }
00048         return (ip);
00049 }
00050 
00051 ssize_t
00052 ip_send(ip_t *ip, const void *buf, size_t len)
00053 {
00054         struct ip_hdr *hdr = (struct ip_hdr *)buf;
00055         
00056         ip->sin.sin_addr.s_addr = hdr->ip_src;
00057         
00058         if ((len = sendto(ip->fd, (const char *)buf, len, 0,
00059             (struct sockaddr *)&ip->sin, sizeof(ip->sin))) != SOCKET_ERROR)
00060                 return (len);
00061         
00062         return (-1);
00063 }
00064 
00065 ip_t *
00066 ip_close(ip_t *ip)
00067 {
00068         if (ip != NULL) {
00069                 WSACleanup();
00070                 if (ip->fd != INVALID_SOCKET)
00071                         closesocket(ip->fd);
00072                 free(ip);
00073         }
00074         return (NULL);
00075 }

Generated on Sun May 14 14:51:12 2006 by  doxygen 1.4.2