The Assimilation Project  based on Assimilation version 1.1.5.1460994010
netioudp.c
Go to the documentation of this file.
1 
24 #include <memory.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #ifdef _MSC_VER
28 # include <winsock2.h>
29 #else
30 # include <unistd.h>
31 # include <sys/socket.h>
32 # include <netinet/in.h>
33 #endif
34 #include <address_family_numbers.h>
35 #include <proj_classes.h>
36 #include <netioudp.h>
37 #include <frameset.h>
38 
39 
47 
48 #undef TESTIPV4ONLY
49 
51 NetIOudp*
52 netioudp_new(gsize objsize
54  , PacketDecoder* decoder)
55 {
56  NetIO* iret;
57  NetIOudp* ret;
58  gint sockfd;
59 
60  if (objsize < sizeof(NetIOudp)) {
61  objsize = sizeof(NetIOudp);
62  }
63  iret = netio_new(objsize, config, decoder);
64  proj_class_register_subclassed(iret, "NetIOudp");
65  ret = CASTTOCLASS(NetIOudp, iret);
66 #ifdef TESTIPV4ONLY
67  sockfd = -1;
68 #else
69  sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
70 #endif
71  if (sockfd < 0) {
72  sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
73  if (sockfd < 0) {
74  g_critical("%s.%d: socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) failed: %s [%d]"
75  , __FUNCTION__, __LINE__, g_strerror(errno), errno);
76  UNREF(iret);
77  return NULL;
78  }
79  g_warning("%s.%d: IPv6 is disabled.", __FUNCTION__, __LINE__);
80  iret->v4only = TRUE;
81  }
82 #ifdef WIN32
83  {
84  u_long iMode=1;
85  ioctlsocket(sockfd, FIONBIO, &iMode);
86  }
87  ret->baseclass.giosock = g_io_channel_win32_new_socket(sockfd);
88 #else
89  ret->baseclass.giosock = g_io_channel_unix_new(sockfd);
90 #endif
91  g_io_channel_set_close_on_unref(ret->baseclass.giosock, TRUE);
92  return ret;
93 }
94 
IETF/IANA Address family assignments.
gboolean v4only
True if giosock is an IPv4 socket.
Definition: netio.h:62
Implements UDP network I/O (NetIOudp) class.
NetIOudp * netioudp_new(gsize objsize, ConfigContext *config, PacketDecoder *decoder)
Construct new UDP NetIO object (and its socket, etc)
Definition: netioudp.c:52
Implements minimal client-oriented Frame and Frameset capabilities.
#define __FUNCTION__
NetIOudp is a NetIO class subclass specialized to UDP connections.
Definition: netioudp.h:40
gpointer proj_class_register_subclassed(gpointer object, const char *static_subclassname)
Log the creation of a subclassed object from a superclassed object.
Definition: proj_classes.c:192
struct _NetIOudp NetIOudp
Definition: netioudp.h:35
Defines interfaces a project Class system for class hierarchies in 'C'.
NetIO baseclass
Base class (NetIO) object.
Definition: netioudp.h:41
NetIO * netio_new(gsize objsize, ConfigContext *config, PacketDecoder *decoder)
NetIO constructor.
Definition: netio.c:488
GIOChannel * giosock
Glib GIOChannel for this socket.
Definition: netio.h:61
#define CASTTOCLASS(Cclass, obj)
Safely cast 'obj' to C-class 'class' - verifying that it was registerd as being of type class ...
Definition: proj_classes.h:66
#define UNREF(obj)
Definition: assimobj.h:35
This is a basic NetIO class abstract class for doing network I/O.
Definition: netio.h:58