1 // -*- c++ -*-
2 //------------------------------------------------------------------------
3 //                              ConUDPSocket.cpp
4 //------------------------------------------------------------------------------
5 //  Copyright (C) 1999  Vladislav Grinchenko
6 //
7 //  This library is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU Library General Public
9 //  License as published by the Free Software Foundation; either
10 //  version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------
12 
13 #ifndef WIN32
14 
15 #include "assa/ConUDPSocket.h"
16 #include "assa/UNIXAddress.h"
17 #include "assa/INETAddress.h"
18 
19 using namespace ASSA;
20 
21 bool
22 ConUDPSocket::
connect(const Address & peer_address_)23 connect (const Address& peer_address_)
24 {
25 	char self[] = "ConUDPSocket::connect"; trace(self);
26 
27 	if ( ::connect (getHandler(),peer_address_.getAddress(),
28 					peer_address_.getLength()) < 0 ) {
29 		setstate (Socket::failbit);
30 		return false;
31 	}
32 	return true;
33 }
34 
35 void
36 ConUDPSocket::
unconnect()37 unconnect()
38 {
39 	// Ignore errors here. On some systems connect() might return
40 	// EAFNOSUPPORT error, on some might not, but it is OK.
41 	//
42 	char self[] = "ConUDPSocket::unconnect"; trace(self);
43 
44 	if ( getDomain() == AF_INET ) {
45 		INETAddress addr;
46 		SA_IN* addrp = (SA_IN*) addr.getAddress();
47 
48 		addrp->sin_family = AF_UNSPEC;
49 		(void) connect(addr);
50 	}
51 	else {			// AF_LOCAL
52 		// I haven't tested whether it works at all.
53 
54 		UNIXAddress addr("");
55 		SA_UN* addrp = (SA_UN*) addr.getAddress();
56 
57 		addrp->sun_family = AF_UNSPEC;
58 		(void) connect(addr);
59 	}
60 }
61 
62 int
63 ConUDPSocket::
read(char * packet_,const unsigned int size_)64 read(char* packet_, const unsigned int size_)
65 {
66 	int len;
67 	len = ::read(getHandler(), packet_, size_);
68 
69 	if (len == -1) {
70 		setstate (Socket::failbit);
71 	}
72 	else if ( len == 0 ) {
73 		setstate (Socket::failbit | Socket::eofbit);
74 	}
75 	return len;
76 }
77 
78 int
79 ConUDPSocket::
write(const char * packet_,const unsigned int size_)80 write (const char* packet_, const unsigned int size_)
81 {
82 	return  ::write(getHandler(), (const void*) packet_, size_);
83 }
84 
85 #endif // WIN32
86