2 "$Id: newsocket.c 9428 2008-07-08 06:31:53Z mwedel $";
53 #define llevDebug LOG_DEBUG
54 #define llevError LOG_ERROR
63 const unsigned char *pos=buf;
69 amt=write(fd, pos, len);
70 }
while ((amt<0) && ((errno==EINTR) || (errno=EAGAIN)));
72 amt=send(fd, pos, len, 0);
73 }
while ((amt<0) && (WSAGetLastError()==EINTR));
77 LOG(
llevError,
"write_socket",
"New socket (fd=%d) write failed: %s.\n", fd, strerror(errno));
81 LOG(
llevError,
"write_socket",
"Write_To_Socket: No data written out.\n");
104 sl->
buf[sl->
len++] = (data>>8)&0xff;
105 sl->
buf[sl->
len++] = data & 0xff;
111 sl->
buf[sl->
len++] = (data>>24)&0xff;
112 sl->
buf[sl->
len++] = (data>>16)&0xff;
113 sl->
buf[sl->
len++] = (data>>8)&0xff;
114 sl->
buf[sl->
len++] = data & 0xff;
119 int len = strlen(str);
123 memcpy(sl->
buf + sl->
len, str, len);
129 sl->
buf[-2] = sl->
len / 256;
130 sl->
buf[-1] = sl->
len % 256;
145 return ((data[0]<<24) + (data[1]<<16) + (data[2]<<8) + data[3]);
152 return (((sint64)data[0]<<56) + ((sint64)data[1]<<48) +
153 ((sint64)data[2]<<40) + ((sint64)data[3]<<32) +
154 ((sint64)data[4]<<24) + ((sint64)data[5]<<16) + ((sint64)data[6]<<8) + (sint64)data[7]);
156 return (((uint64)data[0]<<56) + ((uint64)data[1]<<48) +
157 ((uint64)data[2]<<40) + ((uint64)data[3]<<32) +
158 ((uint64)data[4]<<24) + (data[5]<<16) + (data[6]<<8) + data[7]);
163 return ((data[0]<<8)+data[1]);
175 int stat,toread,readsome=0;
181 stat=read(fd, sl->
buf + sl->
len, 2-sl->
len);
182 }
while ((stat==-1) && (errno==EINTR));
184 stat=recv(fd, sl->
buf + sl->
len, 2-sl->
len, 0);
185 }
while ((stat==-1) && (WSAGetLastError()==EINTR));
192 if (errno!=EAGAIN && errno!=EWOULDBLOCK) {
194 if (WSAGetLastError()!=EAGAIN && WSAGetLastError()!=WSAEWOULDBLOCK) {
196 perror(
"ReadPacket got an error.");
197 LOG(
llevDebug,
"SockList_ReadPacket",
"ReadPacket got error %d, returning -1",errno);
202 if (stat==0)
return -1;
208 if (stat<2)
return 0;
214 toread = 2+(sl->
buf[0] << 8) + sl->
buf[1] - sl->
len;
215 if ((toread + sl->
len) > len) {
216 LOG(
llevError,
"SockList_ReadPacket",
"Want to read more bytes than will fit in buffer.\n");
223 stat = read(fd, sl->
buf+ sl->
len, toread);
224 }
while ((stat<0) && (errno==EINTR));
226 stat = recv(fd, sl->
buf+ sl->
len, toread, 0);
227 }
while ((stat<0) && (WSAGetLastError()==EINTR));
231 if (errno!=EAGAIN && errno!=EWOULDBLOCK) {
233 if (WSAGetLastError()!=EAGAIN && WSAGetLastError()!=WSAEWOULDBLOCK) {
235 perror(
"ReadPacket got an error.");
236 LOG(
llevDebug,
"SockList_ReadPacket",
"ReadPacket got error %d, returning 0",errno);
240 if (stat==0)
return -1;
247 if (toread==0)
return 1;
249 LOG(
llevError,
"SockList_ReadPacket",
"SockList_ReadPacket: Read more bytes than desired.");
267 sl.
len += vsprintf((
char*)sl.
buf + sl.
len, str, args);
void SockList_Init(SockList *sl, uint8 *buf)
static int write_socket(int fd, const unsigned char *buf, int len)
void script_monitor_str(const char *command)
short GetShort_String(const unsigned char *data)
char GetChar_String(const unsigned char *data)
int SockList_Send(SockList *sl, int fd)
void SockList_AddInt(SockList *sl, uint32 data)
void LOG(LogLevel level, const char *origin, const char *format,...)
int SockList_ReadPacket(int fd, SockList *sl, int len)
int cs_print_string(int fd, const char *str,...)
void SockList_AddString(SockList *sl, const char *str)
void SockList_AddChar(SockList *sl, char c)
void SockList_AddShort(SockList *sl, uint16 data)
const char *const rcsid_common_newsocket_c
sint64 GetInt64_String(const unsigned char *data)
int GetInt_String(const unsigned char *data)