libpurple/protocols/zephyr/ZSendPkt.c

changeset 40643
1c9bdf8d3e85
parent 40166
811f82db29dd
--- a/libpurple/protocols/zephyr/ZSendPkt.c	Fri Dec 11 01:48:49 2020 -0600
+++ b/libpurple/protocols/zephyr/ZSendPkt.c	Fri Dec 11 04:12:45 2020 -0600
@@ -8,6 +8,8 @@
  *	"mit-copyright.h".
  */
 
+#include <purple.h>
+
 #include "internal.h"
 #ifdef WIN32
 #include <winsock.h>
@@ -20,39 +22,50 @@
 Code_t
 ZSendPacket(char *packet, int len, int waitforack)
 {
-    Code_t retval;
-    struct sockaddr_in dest;
-    ZNotice_t notice, acknotice;
+	GError *error = NULL;
+	Code_t retval;
+	ZNotice_t notice, acknotice;
 
-    if (!packet || len < 0)
-	return (ZERR_ILLVAL);
+	if (packet == NULL || len < 0) {
+		return ZERR_ILLVAL;
+	}
 
-    if (len > Z_MAXPKTLEN)
-	return (ZERR_PKTLEN);
+	if (len > Z_MAXPKTLEN) {
+		return ZERR_PKTLEN;
+	}
 
-    if (ZGetFD() < 0)
-	if ((retval = ZOpenPort((unsigned short *)0)) != ZERR_NONE)
-	    return (retval);
-
-    dest = ZGetDestAddr();
+	if (ZGetSocket() == NULL) {
+		retval = ZOpenPort(NULL);
+		if (retval != ZERR_NONE) {
+			return retval;
+		}
+	}
 
-    if (sendto(ZGetFD(), packet, len, 0, (struct sockaddr *)&dest,
-	       sizeof(dest)) < 0)
-	return (errno);
+	if (g_socket_send_to(ZGetSocket(), ZGetDestAddr(), packet, len, NULL,
+	                     &error) < 0) {
+		purple_debug_error("zephyr", "Failed to send packet of size %d: %s",
+		                   len, error->message);
+		g_error_free(error);
+		return ZERR_INTERNAL;
+	}
 
-    if (!waitforack)
-	return (ZERR_NONE);
+	if (!waitforack) {
+		return ZERR_NONE;
+	}
 
-    if ((retval = ZParseNotice(packet, len, &notice)) != ZERR_NONE)
-	return (retval);
+	if ((retval = ZParseNotice(packet, len, &notice)) != ZERR_NONE) {
+		return retval;
+	}
 
-    retval = Z_WaitForNotice (&acknotice, wait_for_hmack, &notice.z_uid,
-			      HM_TIMEOUT);
-    if (retval == ETIMEDOUT)
-      return ZERR_HMDEAD;
-    if (retval == ZERR_NONE)
-      ZFreeNotice (&acknotice);
-    return retval;
+	retval = Z_WaitForNotice(&acknotice, wait_for_hmack, &notice.z_uid,
+	                         HM_TIMEOUT);
+	if (retval == ETIMEDOUT) {
+		return ZERR_HMDEAD;
+	}
+	if (retval == ZERR_NONE) {
+		ZFreeNotice(&acknotice);
+	}
+	return retval;
 }
 
 static int wait_for_hmack(ZNotice_t *notice, void *uid)

mercurial