Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…/protolib

fixed some sprintf() issues
  • Loading branch information
bebopagogo committed Jun 4, 2024
2 parents ba029d0 + f530eba commit bd45fb9
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 25 deletions.
1 change: 1 addition & 0 deletions examples/pipeExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ void PipeExample::OnServerEvent(ProtoSocket& /*theSocket*/,
unsigned int len = 8191;
if (server_pipe.Recv(buffer, len))
{
buffer[len] = '\0';
if (len)
PLOG(PL_ERROR, "pipeExample: recvd \"%s\"\n", buffer);
}
Expand Down
4 changes: 1 addition & 3 deletions examples/protoPipeSend.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
# Uncomment this one to send as bytes (example/test)
#msg = text.encode('ascii')

x = 12

pipe.Send(x)
pipe.Send(msg)

print("Sent message '%s'" % text)
5 changes: 3 additions & 2 deletions makefiles/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,9 @@ stree: $(STREE_OBJ) libprotokit.a

clean:
rm -f *.o $(COMMON)/*.o $(MANET)/*.o $(NS)/*.o ../src/*/*.o ../examples/*.o \
*.a *.$(SYSTEM_SOEXT) ../lib/*.a ../lib/*.../bin/* $(SYSTEM_SOEXT) \
arposer averageExample base64Example detourExample graphExample graphRider graphXMLExample jsonExample lfsrExample msg2MsgExample msgExample netExample pcmd pipe2SockExample pipeExample protoCapExample protoApp protoExample protoFileExample queueExample riposer serialExample simpleTcpExample sock2PipeExample threadExample timerTest ting vifExample vifLan gr ../bin/*
*.a *.$(SYSTEM_SOEXT) ../lib/*.a ../lib/* ../bin/* $(SYSTEM_SOEXT) \
arposer averageExample base64Example detourExample graphExample graphRider graphXMLExample jsonExample lfsrExample msg2MsgExample msgExample netExample pcmd pipe2SockExample pipeExample protoCapExample protoApp protoExample protoFileExample queueExample riposer serialExample simpleTcpExample sock2PipeExample threadExample timerTest ting vifExample vifLan gr
rm -rf ../build/* ../protokit.egg-info


# DO NOT DELETE THIS LINE -- mkdep uses it.
Expand Down
3 changes: 3 additions & 0 deletions src/common/protoNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ unsigned int ProtoNet::GetInterfaceAddressMask(unsigned int ifIndex, const Proto
return 0;
}
#else

// TBD - actually this appears to have been implemented and these #if/#else are OBE
// with them blocked within a larger #ifndef WIN32 ... so this code could be cleaned up.
PLOG(PL_ERROR,"ProtoNet::GetInterfaceAddressMask() error: function not implemented for WIN32\n");
return 0;
#endif
Expand Down
1 change: 1 addition & 0 deletions src/common/protoSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,7 @@ bool ProtoSocket::Accept(ProtoSocket* newSocket)
bool ProtoSocket::Send(const char* buffer,
unsigned int& numBytes)
{
//TRACE("ProtoSocket::Send() ...\n");
if (IsConnected())
{
#ifdef WIN32
Expand Down
2 changes: 2 additions & 0 deletions src/linux/linuxDetour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,9 @@ int LinuxDetour::NfqCallback(nfq_q_handle* nfqQueue,
// A change to the nfq_get_payload() prototype seemed to kick in around Linux header files
// version 3.6? (This will probably need to be fine tuned for the right version threshold.)

#ifndef LINUX_VERSION_MAJOR
#define LINUX_VERSION_MAJOR (LINUX_VERSION_CODE/65536)
#endif // !LINUX_VERSION_MAJOR
#define LINUX_VERSION_MINOR ((LINUX_VERSION_CODE - (LINUX_VERSION_MAJOR*65536)) / 256)

#if ((LINUX_VERSION_MAJOR > 3) || ((LINUX_VERSION_MAJOR == 3) && (LINUX_VERSION_MINOR > 5)))
Expand Down
40 changes: 26 additions & 14 deletions src/python/protopipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ extern "C" {
}
}

static PyObject* Pipe_GetName(Pipe *self) {

static PyObject* Pipe_GetName(Pipe *self)
{
#if PY_MAJOR_VERSION >= 3
PyObject *rv = PyUnicode_FromString(self->thisptr->GetName()); //Human readable?
#else
Expand All @@ -83,7 +83,8 @@ extern "C" {
return rv;
}

static PyObject* Pipe_Connect(Pipe *self, PyObject *args) {
static PyObject* Pipe_Connect(Pipe *self, PyObject *args)
{
const char * name;

if (!PyArg_ParseTuple(args, "s", &name))
Expand Down Expand Up @@ -143,9 +144,10 @@ extern "C" {
}
if (PyObject_TypeCheck(obj, &PyUnicode_Type))
{
//PySys_WriteStdout("sending string ...\n");
int size;
if (!PyArg_ParseTuple(args, "s#", &buffer, &size))
//PySys_WriteStderr("sending string ...\n");
Py_ssize_t size;

if (NULL == (buffer = PyUnicode_AsUTF8AndSize(obj, &size)))
{
PyErr_SetString(ProtoError, "Invalid string.");
return NULL;
Expand All @@ -154,7 +156,7 @@ extern "C" {
}
else if (PyObject_TypeCheck(obj, &PyBytes_Type))
{
//PySys_WriteStdout("sending bytes ...\n");
//PySys_WriteStderr("sending bytes ...\n");
Py_ssize_t size = PyBytes_Size(obj);
if (0 == size)
{
Expand All @@ -171,7 +173,7 @@ extern "C" {
}
else if (PyObject_TypeCheck(obj, &PyByteArray_Type))
{
//PySys_WriteStdout("sending bytearray ...\n");
//PySys_WriteStderr("sending bytearray ...\n");
Py_ssize_t size = PyByteArray_Size(obj);
if (0 == size)
{
Expand All @@ -191,7 +193,7 @@ extern "C" {
PyErr_SetString(ProtoError, "Invalid argument type (must be str, bytes, or bytearray).");
return NULL;
}
//PySys_WriteStdout("sending message size %u\n", size_u);

if (!self->thisptr->Send(buffer, size_u)) {
PyErr_SetString(ProtoError, "Could not send buffer.");
return NULL;
Expand All @@ -200,25 +202,35 @@ extern "C" {
}

static PyObject* Pipe_Recv(Pipe *self, PyObject *args) {
char *buffer;
//char *buffer;
Py_ssize_t size;
bool result = false;

if (!PyArg_ParseTuple(args, "I", &size))
return NULL;
unsigned int size_u = size;

buffer = new char[size];

// Had to use "malloc() here instead of "new" operator,
// but not really sure why
char* buffer = (char*)malloc(size_u);

if (NULL == buffer)
{
PyErr_SetString(ProtoError, "Could not allocate receive buffer.");
return NULL;
}

// Release the GIL since this can block...
Py_BEGIN_ALLOW_THREADS
result = self->thisptr->Recv(buffer, size_u);
Py_END_ALLOW_THREADS

//PySys_WriteStdout("received message size %u\n", size_u);

if (!result) {
if (!result)
{
PyErr_SetString(ProtoError, "Could not recv.");
free(buffer);
return NULL;
}

Expand All @@ -228,7 +240,7 @@ extern "C" {
#else
PyObject *rv = PyString_FromStringAndSize(buffer, size_u); //Human readable?
#endif
delete[] buffer;
free(buffer);
return rv;
}

Expand Down
11 changes: 5 additions & 6 deletions src/python/protospace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ extern "C" {
// Init space with number of dimensions inferrred from list length
unsigned int numDimensions = PyList_Size(pList);

double* originOrdinates = new double[numDimensions];
double* originOrdinates = (double*)malloc(numDimensions);
if (NULL == originOrdinates)
{
PyErr_SetString(ProtoError, "new origin ordinates error");
Expand All @@ -343,18 +343,18 @@ extern "C" {
}
else
{
delete[] originOrdinates;
free(originOrdinates);
PyErr_SetString(ProtoError, "Space ordinates must be integers or doubles");
return -1;
}
}
if (!self->thisptr->Init(originOrdinates))
{
delete[] originOrdinates;
free(originOrdinates);
PyErr_SetString(ProtoError, "ProtoSpace::Iterator::Init() error");
return -1;
}
delete[] originOrdinates;
free(originOrdinates);
// SpaceIterator maintains reference to Space until destroyed
Py_INCREF(pSpace);
self->py_space = pSpace;
Expand Down Expand Up @@ -424,7 +424,7 @@ extern "C" {
0, /* tp_dictoffset */
(initproc)SpaceIterator_init, /* tp_init */
0, /* tp_alloc */
Space_new, /* tp_new */
SpaceIterator_new, /* tp_new */
};

static PyObject* Space_iterate(Space* self, PyObject* args)
Expand Down Expand Up @@ -499,5 +499,4 @@ extern "C" {
return (PyObject*)iterator;
} // end Space_iterate()


} // end extern "C"
1 change: 1 addition & 0 deletions src/win32/win32Net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,7 @@ bool ProtoNet::AddInterfaceAddress(unsigned int ifaceIndex, const ProtoAddress&
PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() no matching interface found for ifaceIndex %s", ifaceIndex);
return false;
};

bool ProtoNet::AddInterfaceAddress(const char* ifaceName, const ProtoAddress& ifaceAddr, unsigned int maskLen, bool dhcp_enabled)
{
/*
Expand Down

0 comments on commit bd45fb9

Please # to comment.