ReactOS  0.4.14-dev-77-gd9e7c48
SSendtoByName.c
Go to the documentation of this file.
1 #include "syshdrs.h"
2 
3 #ifndef NO_SIGNALS
4 extern volatile Sjmp_buf gNetTimeoutJmp;
5 extern volatile Sjmp_buf gPipeJmp;
6 #endif
7 
8 #ifndef NO_SIGNALS
9 
10 int
11 SSendtoByName(int sfd, const char *const buf, size_t size, int fl, const char *const toAddrStr, int tlen)
12 {
13  int nwrote, tleft, result;
14  vsio_sigproc_t sigalrm, sigpipe;
15  time_t done, now;
16  struct sockaddr_in toAddr;
17 
18  if ((result = AddrStrToAddr(toAddrStr, &toAddr, -1)) < 0) {
19  return (result);
20  }
21 
22  if (SSetjmp(gNetTimeoutJmp) != 0) {
23  alarm(0);
24  (void) SSignal(SIGALRM, (sio_sigproc_t) sigalrm);
25  (void) SSignal(SIGPIPE, (sio_sigproc_t) sigpipe);
26  errno = ETIMEDOUT;
27  return (kTimeoutErr);
28  }
29 
30  if (SSetjmp(gPipeJmp) != 0) {
31  alarm(0);
32  (void) SSignal(SIGALRM, (sio_sigproc_t) sigalrm);
33  (void) SSignal(SIGPIPE, (sio_sigproc_t) sigpipe);
34  errno = EPIPE;
35  return (kBrokenPipeErr);
36  }
37 
38  sigalrm = (vsio_sigproc_t) SSignal(SIGALRM, SIOHandler);
40 
41  time(&now);
42  done = now + tlen;
43  tleft = (int) (done - now);
44  forever {
45  (void) alarm((unsigned int) tleft);
46  nwrote = sendto(sfd, buf, size, fl,
47  (struct sockaddr *) &toAddr,
48  (int) sizeof(struct sockaddr_in));
49  (void) alarm(0);
50  if (nwrote >= 0)
51  break;
52  if (errno != EINTR)
53  break; /* Fatal error. */
54  errno = 0;
55  time(&now);
56  tleft = (int) (done - now);
57  if (tleft < 1) {
58  nwrote = kTimeoutErr;
59  errno = ETIMEDOUT;
60  break;
61  }
62  }
63 
64  (void) SSignal(SIGALRM, (sio_sigproc_t) sigalrm);
65  (void) SSignal(SIGPIPE, (sio_sigproc_t) sigpipe);
66 
67  return (nwrote);
68 } /* SSendtoByName */
69 
70 #else
71 
72 int
73 SSendtoByName(int sfd, const char *const buf, size_t size, int fl, const char *const toAddrStr, int tlen)
74 {
75  int nwrote, tleft;
76  time_t done, now;
77  fd_set ss;
78  struct timeval tv;
79  int result;
80  struct sockaddr_in toAddr;
81 
82  if ((result = AddrStrToAddr(toAddrStr, &toAddr, -1)) < 0) {
83  return (result);
84  }
85 
86  time(&now);
87  done = now + tlen;
88  nwrote = 0;
89  forever {
90  forever {
91  if (now >= done) {
92  errno = ETIMEDOUT;
93  return (kTimeoutErr);
94  }
95  tleft = (int) (done - now);
96  errno = 0;
97  FD_ZERO(&ss);
98  FD_SET(sfd, &ss);
99  tv.tv_sec = tleft;
100  tv.tv_usec = 0;
102  if (result == 1) {
103  /* ready */
104  break;
105  } else if (result == 0) {
106  /* timeout */
107  errno = ETIMEDOUT;
108  return (kTimeoutErr);
109  } else if (errno != EINTR) {
110  return (-1);
111  }
112  time(&now);
113  }
114 
115  nwrote = sendto(sfd, buf, size, fl,
116  (struct sockaddr *) &toAddr,
117  (int) sizeof(struct sockaddr_in));
118 
119  if (nwrote >= 0)
120  break;
121  if (errno != EINTR)
122  break; /* Fatal error. */
123  }
124 
125  return (nwrote);
126 } /* SSendto */
127 
128 #endif
129 
130 
131 
132 int
133 SendtoByName(int sfd, const char *const buf, size_t size, const char *const toAddrStr)
134 {
135  int result;
136  struct sockaddr_in toAddr;
137 
138  if ((result = AddrStrToAddr(toAddrStr, &toAddr, -1)) < 0) {
139  return (result);
140  }
141 
142  do {
143  result = sendto(sfd, buf, size, 0,
144  (struct sockaddr *) &toAddr,
145  (int) sizeof(struct sockaddr_in));
146  } while ((result < 0) && (errno == EINTR));
147 
148  return (result);
149 } /* SendtoByName */
Definition: winsock.h:66
#define EPIPE
Definition: acclib.h:91
#define SELECT_TYPE_ARG234
Definition: wincfg.h:4
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
volatile sio_sigproc_t vsio_sigproc_t
Definition: sio.h:124
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define EINTR
Definition: acclib.h:80
unsigned long tv_sec
Definition: linux.h:1738
int errno
__u16 time
Definition: mkdosfs.c:366
#define FD_ZERO(set)
Definition: winsock.h:96
#define FD_SET(fd, set)
Definition: winsock.h:89
int AddrStrToAddr(const char *const, struct sockaddr_in *const, const int)
Definition: StrAddr.c:54
int SSendtoByName(int sfd, const char *const buf, size_t size, int fl, const char *const toAddrStr, int tlen)
Definition: SSendtoByName.c:73
#define Sjmp_buf
Definition: sio.h:41
_In_ FLONG fl
Definition: winddi.h:1279
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
time_t now
Definition: finger.c:65
smooth NULL
Definition: ftsmooth.c:416
unsigned long tv_usec
Definition: linux.h:1739
int SendtoByName(int sfd, const char *const buf, size_t size, const char *const toAddrStr)
#define kTimeoutErr
Definition: sio.h:58
GLsizeiptr size
Definition: glext.h:5919
#define SIGPIPE
Definition: signal.h:35
#define SSetjmp(a)
Definition: sio.h:39
#define SELECT_TYPE_ARG5
Definition: wincfg.h:7
#define forever
Definition: ncftp.h:73
#define kBrokenPipeErr
Definition: sio.h:59
__kernel_time_t time_t
Definition: linux.h:252
void(*)(int) SSignal(int signum, void(*handler)(int))
Definition: sio.h:237
INT WSAAPI sendto(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags, IN CONST struct sockaddr *to, IN INT tolen)
Definition: send.c:82
#define ETIMEDOUT
Definition: errno.h:121
#define ss
Definition: i386-dis.c:432
GLuint64EXT * result
Definition: glext.h:11304
void(* sio_sigproc_t)(int)
Definition: sio.h:123
void SIOHandler(int)
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31