#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include "cpmsocket.h"
Include dependency graph for cpmsocket.c:
Go to the source code of this file.
Functions | |
PMSocket * | pmsocket_open (const char *address, uint16_t port, transport trans) |
PMSocket * | pmsocket_connect (const char *address, uint16_t port) |
PMSocket * | pmsocket_accept (PMSocket *pmSocket, int queue) |
int | pmsocket_close (PMSocket *pmSocket) |
int | pmsocket_send (const PMSocket *pmSocket, const byte *data, uint32_t length) |
PMSocket * | pmsocket_recv (const PMSocket *pmSocket, byte *data, uint32_t length, PMSocket *sender) |
|
Definition at line 65 of file cpmsocket.c. References PMSocket::addr, PMSocket::socketFD, TCP, and PMSocket::trans.
00066 { 00067 PMSocket *client = (PMSocket *) malloc(sizeof(PMSocket)); 00068 uint32_t addrLen; 00069 00070 listen(pmSocket->socketFD, queue); 00071 client->trans = TCP; 00072 client->socketFD = accept(pmSocket->socketFD, (struct sockaddr *) &(pmSocket->addr), &addrLen); 00073 00074 return client; 00075 } |
|
Definition at line 77 of file cpmsocket.c. References PMSOCKET_ERROR, and PMSocket::socketFD.
00078 { 00079 if (pmSocket->socketFD != PMSOCKET_ERROR) 00080 close(pmSocket->socketFD); 00081 00082 free(pmSocket); 00083 pmSocket = NULL; 00084 00085 return 0; 00086 } |
|
Definition at line 39 of file cpmsocket.c. References PMSocket::addr, PMSOCKET_ERROR, PMSocket::socketFD, TCP, and PMSocket::trans.
00040 { 00041 PMSocket *pmSocket = (PMSocket *) malloc(sizeof(PMSocket)); 00042 struct hostent *hostEnt; 00043 uint32_t addrLen; 00044 00045 if ((hostEnt = gethostbyname(address)) == NULL) 00046 return NULL; 00047 00048 if ((pmSocket->socketFD = socket(PF_INET, SOCK_STREAM, 0)) == PMSOCKET_ERROR) 00049 return NULL; 00050 00051 pmSocket->addr.sin_family = PF_INET; 00052 pmSocket->addr.sin_port = htons(port); 00053 memcpy(&(pmSocket->addr.sin_addr.s_addr), hostEnt->h_addr_list[0], 00054 hostEnt->h_length); 00055 pmSocket->trans = TCP; 00056 00057 addrLen = sizeof(pmSocket->addr); 00058 if (connect(pmSocket->socketFD, (struct sockaddr *) &(pmSocket->addr), addrLen) 00059 == PMSOCKET_ERROR) 00060 return NULL; 00061 00062 return pmSocket; 00063 } |
|
Definition at line 7 of file cpmsocket.c. References PMSocket::addr, PMSOCKET_ERROR, PMSocket::socketFD, TCP, PMSocket::trans, and UDP.
00008 { 00009 PMSocket *pmSocket = (PMSocket *) malloc(sizeof(PMSocket)); 00010 struct hostent *hostEnt; 00011 uint32_t addrLen; 00012 00013 if ((hostEnt = gethostbyname(address)) == NULL) 00014 return NULL; 00015 00016 if (trans == TCP) { 00017 if ((pmSocket->socketFD = socket(PF_INET, SOCK_STREAM, 0)) == PMSOCKET_ERROR) 00018 return NULL; 00019 } 00020 else if (trans == UDP) { 00021 if ((pmSocket->socketFD = socket(PF_INET, SOCK_DGRAM, 0)) == PMSOCKET_ERROR) 00022 return NULL; 00023 } 00024 00025 pmSocket->addr.sin_family = PF_INET; 00026 pmSocket->addr.sin_port = htons(port); 00027 memcpy(&(pmSocket->addr.sin_addr.s_addr), hostEnt->h_addr_list[0], 00028 hostEnt->h_length); 00029 pmSocket->trans = trans; 00030 00031 addrLen = sizeof(pmSocket->addr); 00032 if (bind(pmSocket->socketFD, (struct sockaddr *) &(pmSocket->addr), addrLen) 00033 == PMSOCKET_ERROR) 00034 return NULL; 00035 00036 return pmSocket; 00037 } |
|
Definition at line 110 of file cpmsocket.c. References PMSocket::addr, PMSOCKET_ERROR, PMSocket::socketFD, TCP, PMSocket::trans, and UDP.
00111 { 00112 uint32_t received; 00113 PMSocket *pms = (sender == NULL) ? (PMSocket *) malloc(sizeof(PMSocket)) : sender; 00114 00115 if (pmSocket->trans == TCP) { 00116 do { 00117 received = recv(pmSocket->socketFD, data, length, 0); 00118 length -= received; 00119 } while (received != PMSOCKET_ERROR && length > 0); 00120 if (sender != NULL) { 00121 free(pms); 00122 pms = NULL; 00123 } 00124 00125 } 00126 else if (pmSocket->trans == UDP) { 00127 uint32_t addrLen; 00128 do { 00129 received = recvfrom(pmSocket->socketFD, data, length, 0, 00130 (struct sockaddr *) &(pms->addr), 00131 (socklen_t *) &addrLen); 00132 length -= received; 00133 } while (received != PMSOCKET_ERROR && length > 0); 00134 00135 if (sender != NULL) 00136 pms = NULL; 00137 } 00138 00139 return pms; 00140 } |
|
Definition at line 88 of file cpmsocket.c. References PMSocket::addr, PMSOCKET_ERROR, PMSocket::socketFD, TCP, PMSocket::trans, and UDP.
00089 { 00090 uint32_t sent; 00091 00092 if (pmSocket->trans == TCP) { 00093 do { 00094 sent = send(pmSocket->socketFD, data, length, 0); 00095 length -= sent; 00096 } while (sent != PMSOCKET_ERROR && length > 0); 00097 } 00098 else if (pmSocket->trans == UDP) { 00099 do { 00100 sent = sendto(pmSocket->socketFD, data, length, 0, 00101 (struct sockaddr *) &(pmSocket->addr), 00102 (socklen_t) sizeof(pmSocket->addr)); 00103 length -= sent; 00104 } while (sent != PMSOCKET_ERROR && length > 0); 00105 } 00106 00107 return 0; 00108 } |