| 1 | #include "EXTERN.h" |
| 2 | #include "perl.h" |
| 3 | #include "XSUB.h" |
| 4 | |
| 5 | #include "ppport.h" |
| 6 | |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/socket.h> |
| 9 | |
| 10 | typedef PerlIO* InOutStream; |
| 11 | typedef int SysRet; |
| 12 | |
| 13 | #ifndef PerlIO |
| 14 | #define PerlIO_fileno(f) fileno(f) |
| 15 | #endif |
| 16 | |
| 17 | static Size_t aligned_cmsghdr_sz = 0; |
| 18 | |
| 19 | struct Socket__MsgHdr { |
| 20 | struct msghdr m; |
| 21 | struct iovec io; |
| 22 | }; |
| 23 | |
| 24 | static void |
| 25 | smhobj_2msghdr(SV *obj, struct Socket__MsgHdr *mh) |
| 26 | { |
| 27 | HV* hash; |
| 28 | SV ** svp; |
| 29 | STRLEN dlen; |
| 30 | |
| 31 | if (!obj || !sv_isa(obj, "Socket::MsgHdr")) |
| 32 | croak("parameter not of type Socket::MsgHdr"); |
| 33 | |
| 34 | hash = (HV*) SvRV(obj); |
| 35 | |
| 36 | Zero(mh, 1, struct Socket__MsgHdr); |
| 37 | |
| 38 | mh->m.msg_iov = &mh->io; |
| 39 | mh->m.msg_iovlen = 1; |
| 40 | |
| 41 | /* Set any values supplied by the user, but translate |
| 42 | * empty strings to explicit NULLs (for FreeBSD's sake). |
| 43 | */ |
| 44 | if ((svp = hv_fetch(hash, "name", 4, FALSE)) && SvOK(*svp)) { |
| 45 | mh->m.msg_name = SvPV_force(*svp, dlen); |
| 46 | mh->m.msg_namelen = dlen; |
| 47 | if (0 == dlen) mh->m.msg_name = NULL; |
| 48 | } |
| 49 | |
| 50 | if ((svp = hv_fetch(hash, "buf", 3, FALSE)) && SvOK(*svp)) { |
| 51 | mh->io.iov_base = SvPV_force(*svp, dlen); |
| 52 | mh->io.iov_len = dlen; |
| 53 | if (0 == dlen) mh->io.iov_base = NULL; |
| 54 | } |
| 55 | |
| 56 | if ((svp = hv_fetch(hash, "control", 7, FALSE)) && SvOK(*svp)) { |
| 57 | mh->m.msg_control = SvPV_force(*svp, dlen); |
| 58 | mh->m.msg_controllen = dlen; |
| 59 | if (0 == dlen) mh->m.msg_control = NULL; |
| 60 | } |
| 61 | |
| 62 | if ((svp = hv_fetch(hash, "flags", 5, FALSE)) && SvOK(*svp)) { |
| 63 | mh->m.msg_flags = SvIV(*svp); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | MODULE = Socket::MsgHdr PACKAGE = Socket::MsgHdr PREFIX = smh_ |
| 68 | |
| 69 | SV * |
| 70 | smh_pack_cmsghdr(...) |
| 71 | PROTOTYPE: $$$;@ |
| 72 | PREINIT: |
| 73 | STRLEN len; |
| 74 | STRLEN space; |
| 75 | I32 i; |
| 76 | struct cmsghdr *cm; |
| 77 | CODE: |
| 78 | space = 0; |
| 79 | for (i=0; i<items; i+=3) { |
| 80 | len = sv_len(ST(i+2)); |
| 81 | space += CMSG_SPACE(len); |
| 82 | } |
| 83 | RETVAL = newSV( space ); |
| 84 | SvPOK_on(RETVAL); |
| 85 | SvCUR_set(RETVAL, space); |
| 86 | |
| 87 | cm = (struct cmsghdr *)SvPVX(RETVAL); |
| 88 | |
| 89 | for (i=0; i<items; i+=3) { |
| 90 | len = sv_len(ST(i+2)); |
| 91 | cm->cmsg_len = CMSG_LEN(len); |
| 92 | cm->cmsg_level = SvIV( ST(i) ); |
| 93 | cm->cmsg_type = SvIV( ST(i+1) ); |
| 94 | Copy(SvPVX(ST(i+2)), CMSG_DATA(cm), len, U8); |
| 95 | cm = (struct cmsghdr *)((U8 *)cm + CMSG_SPACE( len )); |
| 96 | } |
| 97 | OUTPUT: |
| 98 | RETVAL |
| 99 | |
| 100 | void |
| 101 | smh_unpack_cmsghdr(cmsv) |
| 102 | SV* cmsv; |
| 103 | INIT: |
| 104 | struct msghdr dummy; |
| 105 | struct cmsghdr *cm; |
| 106 | STRLEN len; |
| 107 | PPCODE: |
| 108 | dummy.msg_control = (struct cmsghdr *) SvPV(cmsv, len); |
| 109 | dummy.msg_controllen = len; |
| 110 | |
| 111 | if (!len) |
| 112 | XSRETURN_EMPTY; |
| 113 | |
| 114 | cm = CMSG_FIRSTHDR(&dummy); |
| 115 | for (; cm; cm = CMSG_NXTHDR(&dummy, cm)) { |
| 116 | XPUSHs(sv_2mortal(newSViv(cm->cmsg_level))); |
| 117 | XPUSHs(sv_2mortal(newSViv(cm->cmsg_type))); |
| 118 | XPUSHs(sv_2mortal(newSVpvn(CMSG_DATA(cm), |
| 119 | (cm->cmsg_len - aligned_cmsghdr_sz)))); |
| 120 | } |
| 121 | |
| 122 | SysRet |
| 123 | smh_sendmsg(s, msg_hdr, flags = 0) |
| 124 | InOutStream s; |
| 125 | SV * msg_hdr; |
| 126 | int flags; |
| 127 | |
| 128 | PROTOTYPE: $$;$ |
| 129 | PREINIT: |
| 130 | struct Socket__MsgHdr mh; |
| 131 | CODE: |
| 132 | smhobj_2msghdr(msg_hdr, &mh); |
| 133 | RETVAL = sendmsg(PerlIO_fileno(s), &mh.m, flags); |
| 134 | OUTPUT: |
| 135 | RETVAL |
| 136 | |
| 137 | SysRet |
| 138 | smh_recvmsg(s, msg_hdr, flags = 0) |
| 139 | InOutStream s; |
| 140 | SV * msg_hdr; |
| 141 | int flags; |
| 142 | |
| 143 | PROTOTYPE: $$;$ |
| 144 | PREINIT: |
| 145 | struct Socket__MsgHdr mh; |
| 146 | |
| 147 | CODE: |
| 148 | smhobj_2msghdr(msg_hdr, &mh); |
| 149 | if ((RETVAL = recvmsg(PerlIO_fileno(s), &mh.m, flags)) >= 0) { |
| 150 | SV** svp; |
| 151 | HV* hsh; |
| 152 | |
| 153 | hsh = (HV*) SvRV(msg_hdr); |
| 154 | |
| 155 | if ((svp = hv_fetch(hsh, "name", 4, FALSE))) |
| 156 | SvCUR_set(*svp, mh.m.msg_namelen); |
| 157 | if ((svp = hv_fetch(hsh, "buf", 3, FALSE))) |
| 158 | SvCUR_set(*svp, RETVAL); |
| 159 | if ((svp = hv_fetch(hsh, "control", 7, FALSE))) |
| 160 | SvCUR_set(*svp, mh.m.msg_controllen); |
| 161 | if ((svp = hv_fetch(hsh, "flags", 5, FALSE))) { |
| 162 | SvUPGRADE(*svp, SVt_IV); |
| 163 | SvIV_set(*svp, mh.m.msg_flags); |
| 164 | } |
| 165 | } |
| 166 | OUTPUT: |
| 167 | RETVAL |
| 168 | |
| 169 | BOOT: |
| 170 | aligned_cmsghdr_sz = CMSG_LEN(0); |