| 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 |
21 |
| smhobj_2msghdr(SV *obj, struct Socket__MsgHdr *mh) |
| 26 |
|
| { |
| 27 |
|
| HV* hash; |
| 28 |
|
| SV ** svp; |
| 29 |
|
| STRLEN dlen; |
| 30 |
|
| |
| 31 |
21 |
▶ | if (!obj || !sv_isa(obj, "Socket::MsgHdr")) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!obj || !sv_isa(obj, "Socket::MsgHdr")) |
21 |
0 |
21 |
| if (!obj || !sv_isa(obj, "Socket::MsgHdr")) |
0 |
21 |
21 |
|
| 32 |
0 |
| croak("parameter not of type Socket::MsgHdr"); |
| 33 |
|
| |
| 34 |
21 |
| hash = (HV*) SvRV(obj); |
| 35 |
|
| |
| 36 |
21 |
| Zero(mh, 1, struct Socket__MsgHdr); |
| 37 |
|
| |
| 38 |
21 |
| mh->m.msg_iov = &mh->io; |
| 39 |
21 |
| 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 |
21 |
▶ | if ((svp = hv_fetch(hash, "name", 4, FALSE)) && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hash, "name", 4, FALSE)) && SvOK(*svp)) { |
21 |
0 |
21 |
| if ((svp = hv_fetch(hash, "name", 4, FALSE)) && SvOK(*svp)) { |
21 |
0 |
21 |
|
| 45 |
21 |
| mh->m.msg_name = SvPV_force(*svp, dlen); |
| 46 |
21 |
| mh->m.msg_namelen = dlen; |
| 47 |
21 |
▶ | if (0 == dlen) mh->m.msg_name = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == dlen) mh->m.msg_name = NULL; |
12 |
9 |
21 |
|
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
21 |
▶ | if ((svp = hv_fetch(hash, "buf", 3, FALSE)) && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hash, "buf", 3, FALSE)) && SvOK(*svp)) { |
21 |
0 |
21 |
| if ((svp = hv_fetch(hash, "buf", 3, FALSE)) && SvOK(*svp)) { |
21 |
0 |
21 |
|
| 51 |
21 |
| mh->io.iov_base = SvPV_force(*svp, dlen); |
| 52 |
21 |
| mh->io.iov_len = dlen; |
| 53 |
21 |
▶ | if (0 == dlen) mh->io.iov_base = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == dlen) mh->io.iov_base = NULL; |
0 |
21 |
21 |
|
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
21 |
▶ | if ((svp = hv_fetch(hash, "control", 7, FALSE)) && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hash, "control", 7, FALSE)) && SvOK(*svp)) { |
21 |
0 |
21 |
| if ((svp = hv_fetch(hash, "control", 7, FALSE)) && SvOK(*svp)) { |
21 |
0 |
21 |
|
| 57 |
21 |
| mh->m.msg_control = SvPV_force(*svp, dlen); |
| 58 |
21 |
| mh->m.msg_controllen = dlen; |
| 59 |
21 |
▶ | if (0 == dlen) mh->m.msg_control = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == dlen) mh->m.msg_control = NULL; |
16 |
5 |
21 |
|
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
21 |
▶ | if ((svp = hv_fetch(hash, "flags", 5, FALSE)) && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((svp = hv_fetch(hash, "flags", 5, FALSE)) && SvOK(*svp)) { |
21 |
0 |
21 |
| if ((svp = hv_fetch(hash, "flags", 5, FALSE)) && SvOK(*svp)) { |
21 |
0 |
21 |
|
| 63 |
21 |
| mh->m.msg_flags = SvIV(*svp); |
| 64 |
|
| } |
| 65 |
21 |
| } |
| 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 |
5 |
| space = 0; |
| 79 |
11 |
▶ | for (i=0; i<items; i+=3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<items; i+=3) { |
6 |
5 |
11 |
|
| 80 |
6 |
| len = sv_len(ST(i+2)); |
| 81 |
6 |
| space += CMSG_SPACE(len); |
| 82 |
|
| } |
| 83 |
5 |
| RETVAL = newSV( space ); |
| 84 |
5 |
| SvPOK_on(RETVAL); |
| 85 |
5 |
| SvCUR_set(RETVAL, space); |
| 86 |
|
| |
| 87 |
5 |
| cm = (struct cmsghdr *)SvPVX(RETVAL); |
| 88 |
|
| |
| 89 |
11 |
▶ | for (i=0; i<items; i+=3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<items; i+=3) { |
6 |
5 |
11 |
|
| 90 |
6 |
| len = sv_len(ST(i+2)); |
| 91 |
6 |
| cm->cmsg_len = CMSG_LEN(len); |
| 92 |
6 |
| cm->cmsg_level = SvIV( ST(i) ); |
| 93 |
6 |
| cm->cmsg_type = SvIV( ST(i+1) ); |
| 94 |
6 |
| Copy(SvPVX(ST(i+2)), CMSG_DATA(cm), len, U8); |
| 95 |
6 |
| 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 |
2 |
| dummy.msg_control = (struct cmsghdr *) SvPV(cmsv, len); |
| 109 |
2 |
| dummy.msg_controllen = len; |
| 110 |
|
| |
| 111 |
2 |
▶ | if (!len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!len) |
0 |
2 |
2 |
|
| 112 |
0 |
| XSRETURN_EMPTY; |
| 113 |
|
| |
| 114 |
2 |
▶ | cm = CMSG_FIRSTHDR(&dummy); |
Branch: 1 branch
| Branch | True | False | Total |
| cm = CMSG_FIRSTHDR(&dummy); |
2 |
0 |
2 |
|
| 115 |
5 |
▶ | for (; cm; cm = CMSG_NXTHDR(&dummy, cm)) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; cm; cm = CMSG_NXTHDR(&dummy, cm)) { |
3 |
2 |
5 |
|
| 116 |
3 |
▶ | XPUSHs(sv_2mortal(newSViv(cm->cmsg_level))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(cm->cmsg_level))); |
0 |
3 |
3 |
|
| 117 |
3 |
▶ | XPUSHs(sv_2mortal(newSViv(cm->cmsg_type))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(cm->cmsg_type))); |
0 |
3 |
3 |
|
| 118 |
3 |
▶ | XPUSHs(sv_2mortal(newSVpvn(CMSG_DATA(cm), |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn(CMSG_DATA(cm), |
0 |
3 |
3 |
|
| 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 |
11 |
| smhobj_2msghdr(msg_hdr, &mh); |
| 133 |
11 |
| 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 |
10 |
| smhobj_2msghdr(msg_hdr, &mh); |
| 149 |
10 |
▶ | if ((RETVAL = recvmsg(PerlIO_fileno(s), &mh.m, flags)) >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((RETVAL = recvmsg(PerlIO_fileno(s), &mh.m, flags)) >= 0) { |
9 |
1 |
10 |
|
| 150 |
|
| SV** svp; |
| 151 |
|
| HV* hsh; |
| 152 |
|
| |
| 153 |
9 |
| hsh = (HV*) SvRV(msg_hdr); |
| 154 |
|
| |
| 155 |
9 |
▶ | if ((svp = hv_fetch(hsh, "name", 4, FALSE))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((svp = hv_fetch(hsh, "name", 4, FALSE))) |
9 |
0 |
9 |
|
| 156 |
9 |
| SvCUR_set(*svp, mh.m.msg_namelen); |
| 157 |
9 |
▶ | if ((svp = hv_fetch(hsh, "buf", 3, FALSE))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((svp = hv_fetch(hsh, "buf", 3, FALSE))) |
9 |
0 |
9 |
|
| 158 |
9 |
| SvCUR_set(*svp, RETVAL); |
| 159 |
9 |
▶ | if ((svp = hv_fetch(hsh, "control", 7, FALSE))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((svp = hv_fetch(hsh, "control", 7, FALSE))) |
9 |
0 |
9 |
|
| 160 |
9 |
| SvCUR_set(*svp, mh.m.msg_controllen); |
| 161 |
9 |
▶ | if ((svp = hv_fetch(hsh, "flags", 5, FALSE))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((svp = hv_fetch(hsh, "flags", 5, FALSE))) { |
9 |
0 |
9 |
|
| 162 |
9 |
▶ | SvUPGRADE(*svp, SVt_IV); |
Branch: 1 branch
| Branch | True | False | Total |
| SvUPGRADE(*svp, SVt_IV); |
0 |
9 |
9 |
|
| 163 |
9 |
| SvIV_set(*svp, mh.m.msg_flags); |
| 164 |
|
| } |
| 165 |
|
| } |
| 166 |
|
| OUTPUT: |
| 167 |
|
| RETVAL |
| 168 |
|
| |
| 169 |
|
| BOOT: |
| 170 |
5 |
| aligned_cmsghdr_sz = CMSG_LEN(0); |