| /*------------------------------------------------------------ | |
| (C) Copyright [2006-2008] Marvell International Ltd. | |
| All Rights Reserved | |
| ------------------------------------------------------------*/ | |
| #include <sys/types.h> | |
| #include <sys/socket.h> | |
| #include <sys/ioctl.h> | |
| #include <sys/un.h> | |
| #include <linux/tcp.h> | |
| #include <fcntl.h> | |
| #include <errno.h> | |
| #include <netinet/in.h> | |
| #include <include/log.h> | |
| #include "bipsocket.h" | |
| #define FALSE 0 | |
| #define TRUE 1 | |
| #define DPRINTF(fmt, args ...) do{RERRMSG("BIP-"fmt, ##args);}while(0) | |
| void MTIL_CloseSocket(IN OUT SOCKET_DESC * sock, char *path) | |
| { | |
| if (*sock != -1) { | |
| close(*sock); | |
| } | |
| *sock = -1; | |
| if (path && path[0]) { | |
| unlink(path); | |
| } | |
| } | |
| unsigned char MTIL_Accept(IN SOCKET_DESC mainSock, OUT SOCKET_DESC * sessionSock) | |
| { | |
| struct sockaddr_in addr; | |
| int len; | |
| len = sizeof(addr); | |
| memset(&addr, 0, len); | |
| *sessionSock = accept(mainSock, (struct sockaddr *)&addr, (socklen_t *) & len); | |
| if (*sessionSock == SOCKET_ERROR) { | |
| return FALSE; | |
| } | |
| return TRUE; | |
| } | |
| unsigned char MTIL_Listen(IN SOCKET_DESC mainSock, IN int backlog) | |
| { | |
| if (listen(mainSock, backlog) == SOCKET_ERROR) | |
| return FALSE; | |
| return TRUE; | |
| } | |
| unsigned char MTIL_Connect(IN SOCKET_DESC sock, struct sockaddr * addrTo, IN int addrlen) | |
| { | |
| if ((connect(sock, addrTo, addrlen)) == 0) | |
| return TRUE; | |
| return FALSE; | |
| } | |
| unsigned char MTIL_RecvSock(SOCKET_DESC sock, char *rcvBuf, int *bufSize, long waitSec, struct sockaddr * from, int from_len) | |
| { | |
| int rcvBytes; | |
| (void)waitSec; | |
| if (sock == -1) | |
| return FALSE; | |
| if (from) { | |
| if ((rcvBytes = recvfrom(sock, rcvBuf, *bufSize, 0, from, (socklen_t *) & from_len)) <= 0) { | |
| sleep(2); | |
| return FALSE; | |
| } | |
| } else { | |
| if ((rcvBytes = recv(sock, rcvBuf, *bufSize, 0)) <= 0) { | |
| if (errno != 0) | |
| sleep(2); | |
| return FALSE; | |
| } | |
| } | |
| DPRINTF("%s recv %d bytes", __FUNCTION__, rcvBytes); | |
| *bufSize = rcvBytes; | |
| return TRUE; | |
| } | |
| unsigned char MTIL_SendSock(SOCKET_DESC sock, char *sndBuf, int sndSize, struct sockaddr * to) | |
| { | |
| int sent = 0; | |
| int lento = 0; | |
| if (to && to->sa_family == AF_UNIX) | |
| lento = sizeof(struct sockaddr_un); | |
| else if (to && to->sa_family == AF_INET6) | |
| lento = sizeof(struct sockaddr_in6); | |
| else | |
| lento = sizeof(struct sockaddr_in); | |
| DPRINTF("%s send %d bytes", __FUNCTION__, sndSize); | |
| while (sndSize) { | |
| if (to) | |
| sent = sendto(sock, sndBuf + sent, sndSize, 0, to, lento); | |
| else | |
| sent = send(sock, sndBuf + sent, sndSize, 0); | |
| if (sent < 0) { | |
| if (errno == EINTR) | |
| continue; | |
| return FALSE; | |
| } else { | |
| if (sent == 0) { | |
| } | |
| } | |
| sndSize -= sent; | |
| } | |
| return TRUE; | |
| } | |
| int MTIL_GetLastSocketError() | |
| { | |
| #if !defined BIONIC | |
| extern int errno; | |
| #endif | |
| return errno; | |
| } |